hide String serialization of lastItemSyncTime

This commit is contained in:
Sixten Otto
2023-02-21 09:02:40 -07:00
parent a2f812376a
commit f013408cf7
3 changed files with 20 additions and 9 deletions

View File

@ -6,8 +6,6 @@ import Utils
import Views
@MainActor final class HomeFeedViewModel: NSObject, ObservableObject {
let dateFormatter = DateFormatter.formatterISO8601
var currentDetailViewModel: LinkItemDetailViewModel?
private var fetchedResultsController: NSFetchedResultsController<LinkedItem>?
@ -110,7 +108,7 @@ import Views
func syncItems(dataService: DataService) async {
let syncStart = Date.now
let lastSyncDate = dateFormatter.date(from: dataService.lastItemSyncTime) ?? Date(timeIntervalSinceReferenceDate: 0)
let lastSyncDate = dataService.lastItemSyncTime
try? await dataService.syncOfflineItemsWithServerIfNeeded()
@ -124,7 +122,7 @@ import Views
self.isLoading = false
}
} else {
dataService.lastItemSyncTime = DateFormatter.formatterISO8601.string(from: syncStart)
dataService.lastItemSyncTime = syncStart
}
// If possible start prefetching new pages in the background

View File

@ -29,9 +29,22 @@ public final class DataService: ObservableObject {
persistentContainer.viewContext
}
@AppStorage(UserDefaultKey.lastItemSyncTime.rawValue) public var lastItemSyncTime: String = {
DateFormatter.formatterISO8601.string(from: Date(timeIntervalSinceReferenceDate: 0))
}()
public var lastItemSyncTime: Date {
get {
guard
let str = UserDefaults.standard.string(forKey: UserDefaultKey.lastItemSyncTime.rawValue),
let date = DateFormatter.formatterISO8601.date(from: str)
else {
return Date(timeIntervalSinceReferenceDate: 0)
}
return date
}
set {
logger.trace("last item sync updated to \(newValue)")
let str = DateFormatter.formatterISO8601.string(from: newValue)
UserDefaults.standard.set(str, forKey: UserDefaultKey.lastItemSyncTime.rawValue)
}
}
public init(appEnvironment: AppEnvironment, networker: Networker) {
self.appEnvironment = appEnvironment
@ -150,7 +163,7 @@ public final class DataService: ObservableObject {
}
public func resetLocalStorage() {
lastItemSyncTime = DateFormatter.formatterISO8601.string(from: Date(timeIntervalSinceReferenceDate: 0))
lastItemSyncTime = Date(timeIntervalSinceReferenceDate: 0)
clearCoreData()
clearDownloadedFiles()

View File

@ -93,7 +93,7 @@ public extension DataService {
}
}
DispatchQueue.main.sync {
self.lastItemSyncTime = DateFormatter.formatterISO8601.string(from: Date.now)
self.lastItemSyncTime = Date.now
onComplete()
}
}