Sync item read state to server

This commit is contained in:
Shadowfacts 2022-01-11 11:54:57 -05:00
parent 3d07069ee5
commit 12e0e3cdfd
3 changed files with 28 additions and 0 deletions

View File

@ -119,6 +119,18 @@ public class FervorClient {
}
}
public func read(item id: FervorID) async throws -> Item {
var request = URLRequest(url: buildURL(path: "/api/v1/items/\(id)/read"))
request.httpMethod = "POST"
return try await performRequest(request)
}
public func unread(item id: FervorID) async throws -> Item {
var request = URLRequest(url: buildURL(path: "/api/v1/items/\(id)/unread"))
request.httpMethod = "POST"
return try await performRequest(request)
}
public struct Auth {
public let accessToken: String
public let refreshToken: String?

View File

@ -66,4 +66,16 @@ class FervorController {
try! await persistentContainer.updateLastSyncDate(update.syncTimestamp)
}
@MainActor
func syncReadToServer() async throws {
var count = 0
for case let item as Item in self.persistentContainer.viewContext.updatedObjects {
count += 1
let f = item.read ? client.read(item:) : client.unread(item:)
let _ = try await f(item.id!)
}
logger.info("Synced \(count, privacy: .public) read/unread to server")
try self.persistentContainer.viewContext.save()
}
}

View File

@ -49,6 +49,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
Task(priority: .userInitiated) {
try await self.fervorController.syncReadToServer()
}
}
func sceneWillEnterForeground(_ scene: UIScene) {