From b2519dcd2de8871eece4c013509fc9b015dbbbb0 Mon Sep 17 00:00:00 2001 From: Trey t Date: Tue, 8 Mar 2022 23:13:36 -0600 Subject: [PATCH] cache entries to help with header stutter --- Shared/Random.swift | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Shared/Random.swift b/Shared/Random.swift index 3052150..85fe0ea 100644 --- a/Shared/Random.swift +++ b/Shared/Random.swift @@ -34,11 +34,17 @@ class Random { return updateTime } + static var existingWeekdayName = [Int: String]() static func weekdayName(fromDate date: Date) -> String { let weekday = Calendar.current.component(.weekday, from: date) let calendar = Calendar.current let dayIndex = ((weekday - 1) + (calendar.firstWeekday - 1)) % 7 - return calendar.weekdaySymbols[dayIndex] + if let value = Random.existingWeekdayName[dayIndex] { + return value + } + let newValue = calendar.weekdaySymbols[dayIndex] + Random.existingWeekdayName[dayIndex] = newValue + return newValue } static func monthName(fromMonthInt: Int) -> String { @@ -46,13 +52,20 @@ class Random { return monthName } + static var existingDayFormat = [NSNumber: String]() static func dayFormat(fromDate date: Date) -> String { let components = Calendar.current.dateComponents([.day], from: date) let day = components.day! let formatter = NumberFormatter() formatter.numberStyle = .ordinal - return formatter.string(from: NSNumber(integerLiteral: day)) ?? "" + let num = NSNumber(integerLiteral: day) + if let value = existingDayFormat[num] { + return value + } + let newValue = formatter.string(from: num) ?? "" + existingDayFormat[num] = newValue + return newValue } static func createTotalPerc(fromEntries entries: [MoodEntry]) -> [MoodMetrics] {