From 3c0ffdb1aeb6fab2e1c2d7ec22fcd5f4f6f42c0d Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sat, 4 Feb 2023 12:17:16 +0100 Subject: [PATCH] Prepare disk cache for Timeline --- Packages/Models/Sources/Models/Account.swift | 6 +++--- Packages/Models/Sources/Models/Alias/HTMLString.swift | 9 ++++++--- Packages/Models/Sources/Models/Status.swift | 4 ++-- Packages/Network/Sources/Network/Client.swift | 6 +++--- .../Timeline/Sources/Timeline/TimelineViewModel.swift | 4 +++- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Packages/Models/Sources/Models/Account.swift b/Packages/Models/Sources/Models/Account.swift index 8915687b..f89d10e4 100644 --- a/Packages/Models/Sources/Models/Account.swift +++ b/Packages/Models/Sources/Models/Account.swift @@ -1,11 +1,11 @@ import Foundation -public struct Account: Decodable, Identifiable, Equatable, Hashable { +public struct Account: Codable, Identifiable, Equatable, Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(id) } - public struct Field: Decodable, Equatable, Identifiable { + public struct Field: Codable, Equatable, Identifiable { public var id: String { value.asRawText + name } @@ -15,7 +15,7 @@ public struct Account: Decodable, Identifiable, Equatable, Hashable { public let verifiedAt: String? } - public struct Source: Decodable, Equatable { + public struct Source: Codable, Equatable { public let privacy: Visibility public let sensitive: Bool public let language: String? diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index 9dda2ef3..a78734e7 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -2,7 +2,7 @@ import Foundation import SwiftSoup import SwiftUI -public struct HTMLString: Decodable, Equatable, Hashable { +public struct HTMLString: Codable, Equatable, Hashable { public var htmlValue: String = "" public var asMarkdown: String = "" public var asRawText: String = "" @@ -61,6 +61,11 @@ public struct HTMLString: Decodable, Equatable, Hashable { asSafeMarkdownAttributedString = AttributedString(stringLiteral: htmlValue) } + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(htmlValue) + } + private mutating func handleNode(node: SwiftSoup.Node ) { @@ -146,6 +151,4 @@ public struct HTMLString: Decodable, Equatable, Hashable { } } - - } diff --git a/Packages/Models/Sources/Models/Status.swift b/Packages/Models/Sources/Models/Status.swift index ee19d0d2..f121a2c3 100644 --- a/Packages/Models/Sources/Models/Status.swift +++ b/Packages/Models/Sources/Models/Status.swift @@ -58,7 +58,7 @@ protocol StatusUI { var uiShouldHighlight: Bool? { get set } } -public struct Status: AnyStatus, Decodable, Identifiable, Equatable, Hashable, StatusUI { +public struct Status: AnyStatus, Codable, Identifiable, Equatable, Hashable, StatusUI { public var viewId: String { id + createdAt + (editedAt ?? "") } @@ -134,7 +134,7 @@ public struct Status: AnyStatus, Decodable, Identifiable, Equatable, Hashable, S } } -public struct ReblogStatus: AnyStatus, Decodable, Identifiable, Equatable, Hashable { +public struct ReblogStatus: AnyStatus, Codable, Identifiable, Equatable, Hashable { public var viewId: String { id + createdAt + (editedAt ?? "") } diff --git a/Packages/Network/Sources/Network/Client.swift b/Packages/Network/Sources/Network/Client.swift index d8276a07..a5707983 100644 --- a/Packages/Network/Sources/Network/Client.swift +++ b/Packages/Network/Sources/Network/Client.swift @@ -5,8 +5,8 @@ import SwiftUI public class Client: ObservableObject, Equatable, Identifiable, Hashable { public static func == (lhs: Client, rhs: Client) -> Bool { lhs.isAuth == rhs.isAuth && - lhs.server == rhs.server && - lhs.oauthToken?.accessToken == rhs.oauthToken?.accessToken + lhs.server == rhs.server && + lhs.oauthToken?.accessToken == rhs.oauthToken?.accessToken } public enum Version: String { @@ -19,7 +19,7 @@ public class Client: ObservableObject, Equatable, Identifiable, Hashable { } public var id: String { - "\(isAuth)\(server)\(oauthToken?.accessToken ?? "")" + "\(isAuth)\(server)\(oauthToken?.createdAt ?? 0)" } public func hash(into hasher: inout Hasher) { diff --git a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift index 1fda44e7..50e2d4cb 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift @@ -156,7 +156,9 @@ extension TimelineViewModel: StatusesFetcher { // If we get statuses from the cache for the home timeline, we displays those. // Else we fetch top most page from the API. - if let cachedStatuses = await getCachedStatuses(), timeline == .home { + if let cachedStatuses = await getCachedStatuses(), + !cachedStatuses.isEmpty, + timeline == .home { statuses = cachedStatuses withAnimation { statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage)