From de6fb064124c9932114d62f94b899b8ccefec314 Mon Sep 17 00:00:00 2001 From: Kiryl Kaveryn Date: Fri, 1 Nov 2024 19:41:55 +0400 Subject: [PATCH] [ios] replace all the date formatters instances with the one static The date formatter initialization is resource-intensive and all unnecessary instantiation are replaced with the call to one default static formatter. Signed-off-by: Kiryl Kaveryn --- .../Common/MWMOpeningHoursCommon.mm | 9 +- .../RecentlyDeletedTableViewCell.swift | 11 +-- .../DateComponentsFormatter+ETA.swift | 10 --- .../MWMNavigationDashboardManager+Entity.mm | 9 +- .../Views/NavigationControlView.swift | 2 +- iphone/Maps/Maps.xcodeproj/project.pbxproj | 4 - .../MWMOpeningHoursClosedSpanTableViewCell.mm | 15 ++-- ...WMOpeningHoursTimeSelectorTableViewCell.mm | 5 -- .../MWMOpeningHoursTimeSpanTableViewCell.mm | 13 +-- .../ElevationProfilePresenter.swift | 3 +- .../PlacePagePreviewViewController.swift | 86 +++++++++---------- 11 files changed, 68 insertions(+), 99 deletions(-) delete mode 100644 iphone/Maps/Categories/DateComponentsFormatter+ETA.swift diff --git a/iphone/CoreApi/CoreApi/PlacePageData/Common/MWMOpeningHoursCommon.mm b/iphone/CoreApi/CoreApi/PlacePageData/Common/MWMOpeningHoursCommon.mm index d0020e5aec..50e3cbb660 100644 --- a/iphone/CoreApi/CoreApi/PlacePageData/Common/MWMOpeningHoursCommon.mm +++ b/iphone/CoreApi/CoreApi/PlacePageData/Common/MWMOpeningHoursCommon.mm @@ -1,4 +1,5 @@ #import "MWMOpeningHoursCommon.h" +#import "CoreApi/CoreApi-Swift.h" NSDateComponents * dateComponentsFromTime(osmoh::Time const & time) { @@ -17,11 +18,9 @@ NSDate * dateFromTime(osmoh::Time const & time) NSString * stringFromTime(osmoh::Time const & time) { - NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; - fmt.locale = NSLocale.currentLocale; - fmt.timeStyle = NSDateFormatterShortStyle; - fmt.dateStyle = NSDateFormatterNoStyle; - return [fmt stringFromDate:dateFromTime(time)]; + return [DateTimeFormatter dateStringFrom:dateFromTime(time) + dateStyle:NSDateFormatterNoStyle + timeStyle:NSDateFormatterShortStyle]; } NSString * stringFromOpeningDays(editor::ui::OpeningDays const & openingDays) diff --git a/iphone/Maps/Bookmarks/Categories/RecentlyDeleted/RecentlyDeletedTableViewCell.swift b/iphone/Maps/Bookmarks/Categories/RecentlyDeleted/RecentlyDeletedTableViewCell.swift index d9cb6d7c98..e5916b5cb8 100644 --- a/iphone/Maps/Bookmarks/Categories/RecentlyDeleted/RecentlyDeletedTableViewCell.swift +++ b/iphone/Maps/Bookmarks/Categories/RecentlyDeleted/RecentlyDeletedTableViewCell.swift @@ -6,13 +6,6 @@ final class RecentlyDeletedTableViewCell: UITableViewCell { let deletionDate: Date } - private static let dateFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateStyle = .medium - formatter.timeStyle = .medium - return formatter - }() - override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) } @@ -24,7 +17,9 @@ final class RecentlyDeletedTableViewCell: UITableViewCell { func configureWith(_ viewModel: ViewModel) { textLabel?.text = viewModel.fileName - detailTextLabel?.text = Self.dateFormatter.string(from: viewModel.deletionDate) + detailTextLabel?.text = DateTimeFormatter.dateString(from: viewModel.deletionDate, + dateStyle: .medium, + timeStyle: .medium) } } diff --git a/iphone/Maps/Categories/DateComponentsFormatter+ETA.swift b/iphone/Maps/Categories/DateComponentsFormatter+ETA.swift deleted file mode 100644 index c45705ed37..0000000000 --- a/iphone/Maps/Categories/DateComponentsFormatter+ETA.swift +++ /dev/null @@ -1,10 +0,0 @@ -extension DateComponentsFormatter { - @objc static func etaString(from ti: TimeInterval) -> String? { - let formatter = DateComponentsFormatter() - formatter.allowedUnits = [.minute, .hour, .day] - formatter.maximumUnitCount = 2 - formatter.unitsStyle = .short - formatter.zeroFormattingBehavior = .dropAll - return formatter.string(from: ti) - } -} diff --git a/iphone/Maps/Classes/CustomViews/NavigationDashboard/MWMNavigationDashboardManager+Entity.mm b/iphone/Maps/Classes/CustomViews/NavigationDashboard/MWMNavigationDashboardManager+Entity.mm index 51e5abad00..8a61cf33db 100644 --- a/iphone/Maps/Classes/CustomViews/NavigationDashboard/MWMNavigationDashboardManager+Entity.mm +++ b/iphone/Maps/Classes/CustomViews/NavigationDashboard/MWMNavigationDashboardManager+Entity.mm @@ -7,6 +7,7 @@ #import #import +#import #include "routing/following_info.hpp" #include "routing/turns.hpp" @@ -121,9 +122,9 @@ NSArray *buildRouteTransitSteps(NSArray *buildRouteTransitSteps(NSArray String { + DateTimeFormatter.dateString(from: Date(timeIntervalSince1970: TimeInterval(time)), + dateStyle: .none, + timeStyle: .short) + } + switch placePagePreviewData.schedule.state { case .allDay: setScheduleLabel(state: L("twentyfour_seven"), stateColor: UIColor.systemGreen, - details: nil); + details: nil) case .open: - let nextTimeClosed = placePagePreviewData.schedule.nextTimeClosed; - let minutesUntilClosed = (nextTimeClosed - now) / 60; - let stringTimeInterval = getTimeIntervalString(minutes: minutesUntilClosed); - let stringTime = hourFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(nextTimeClosed))); - - let details: String?; + let nextTimeClosed = placePagePreviewData.schedule.nextTimeClosed + let minutesUntilClosed = (nextTimeClosed - now) / 60 + let stringTimeInterval = getTimeIntervalString(minutes: minutesUntilClosed) + let stringTime = stringFromTime(nextTimeClosed) + + let details: String? if (minutesUntilClosed < 3 * 60) // Less than 3 hours { - details = String(format: L("closes_in"), stringTimeInterval) + " • " + stringTime; + details = String(format: L("closes_in"), stringTimeInterval) + " • " + stringTime } else if (minutesUntilClosed < 24 * 60) // Less than 24 hours { - details = String(format: L("closes_at"), stringTime); + details = String(format: L("closes_at"), stringTime) } else { - details = nil; + details = nil } setScheduleLabel(state: L("editor_time_open"), stateColor: UIColor.systemGreen, - details: details); + details: details) case .closed: - let nextTimeOpen = placePagePreviewData.schedule.nextTimeOpen; - let nextTimeOpenDate = Date(timeIntervalSince1970: TimeInterval(nextTimeOpen)); + let nextTimeOpen = placePagePreviewData.schedule.nextTimeOpen + let nextTimeOpenDate = Date(timeIntervalSince1970: TimeInterval(nextTimeOpen)) - let minutesUntilOpen = (nextTimeOpen - now) / 60; - let stringTimeInterval = getTimeIntervalString(minutes: minutesUntilOpen); - let stringTime = hourFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(nextTimeOpen))); - - let details: String?; + let minutesUntilOpen = (nextTimeOpen - now) / 60 + let stringTimeInterval = getTimeIntervalString(minutes: minutesUntilOpen) + let stringTime = stringFromTime(nextTimeOpen) + + let details: String? if (minutesUntilOpen < 3 * 60) // Less than 3 hours { - details = String(format: L("opens_in"), stringTimeInterval) + " • " + stringTime; + details = String(format: L("opens_in"), stringTimeInterval) + " • " + stringTime } else if (Calendar.current.isDateInToday(nextTimeOpenDate)) // Today { - details = String(format: L("opens_at"), stringTime); + details = String(format: L("opens_at"), stringTime) } else if (minutesUntilOpen < 24 * 60) // Less than 24 hours { - details = String(format: L("opens_tomorrow_at"), stringTime); + details = String(format: L("opens_tomorrow_at"), stringTime) } else if (minutesUntilOpen < 7 * 24 * 60) // Less than 1 week { - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "EEEE"; - let dayOfWeek = dateFormatter.string(from: nextTimeOpenDate); - details = String(format: L("opens_dayoftheweek_at"), dayOfWeek, stringTime); + let dayOfWeek = DateTimeFormatter.dateString(from: nextTimeOpenDate, format: "EEEE") + details = String(format: L("opens_dayoftheweek_at"), dayOfWeek, stringTime) } else { - details = nil; + details = nil } setScheduleLabel(state: L("closed_now"), stateColor: UIColor.systemRed, - details: details); + details: details) case .unknown: scheduleContainerView.isHidden = true @@ -222,28 +222,28 @@ final class PlacePagePreviewViewController: UIViewController { } private func getTimeIntervalString(minutes: Int) -> String { - var str = ""; + var str = "" if (minutes >= 60) { - str = String(minutes / 60) + " " + L("hour") + " "; + str = String(minutes / 60) + " " + L("hour") + " " } - str += String(minutes % 60) + " " + L("minute"); - return str; + str += String(minutes % 60) + " " + L("minute") + return str } private func setScheduleLabel(state: String, stateColor: UIColor, details: String?) { - let attributedString = NSMutableAttributedString(); + let attributedString = NSMutableAttributedString() let stateString = NSAttributedString(string: state, attributes: [NSAttributedString.Key.font: UIFont.regular14(), - NSAttributedString.Key.foregroundColor: stateColor]); - attributedString.append(stateString); + NSAttributedString.Key.foregroundColor: stateColor]) + attributedString.append(stateString) if (details != nil) { let detailsString = NSAttributedString(string: " • " + details!, attributes: [NSAttributedString.Key.font: UIFont.regular14(), - NSAttributedString.Key.foregroundColor: UIColor.blackSecondaryText()]); - attributedString.append(detailsString); + NSAttributedString.Key.foregroundColor: UIColor.blackSecondaryText()]) + attributedString.append(detailsString) } - scheduleLabel.attributedText = attributedString; + scheduleLabel.attributedText = attributedString } }