Prepare disk cache for Timeline

This commit is contained in:
Thomas Ricouard 2023-02-04 12:17:16 +01:00
parent 86937e65fe
commit 3c0ffdb1ae
5 changed files with 17 additions and 12 deletions

View file

@ -1,11 +1,11 @@
import Foundation import Foundation
public struct Account: Decodable, Identifiable, Equatable, Hashable { public struct Account: Codable, Identifiable, Equatable, Hashable {
public func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {
hasher.combine(id) hasher.combine(id)
} }
public struct Field: Decodable, Equatable, Identifiable { public struct Field: Codable, Equatable, Identifiable {
public var id: String { public var id: String {
value.asRawText + name value.asRawText + name
} }
@ -15,7 +15,7 @@ public struct Account: Decodable, Identifiable, Equatable, Hashable {
public let verifiedAt: String? public let verifiedAt: String?
} }
public struct Source: Decodable, Equatable { public struct Source: Codable, Equatable {
public let privacy: Visibility public let privacy: Visibility
public let sensitive: Bool public let sensitive: Bool
public let language: String? public let language: String?

View file

@ -2,7 +2,7 @@ import Foundation
import SwiftSoup import SwiftSoup
import SwiftUI import SwiftUI
public struct HTMLString: Decodable, Equatable, Hashable { public struct HTMLString: Codable, Equatable, Hashable {
public var htmlValue: String = "" public var htmlValue: String = ""
public var asMarkdown: String = "" public var asMarkdown: String = ""
public var asRawText: String = "" public var asRawText: String = ""
@ -61,6 +61,11 @@ public struct HTMLString: Decodable, Equatable, Hashable {
asSafeMarkdownAttributedString = AttributedString(stringLiteral: htmlValue) 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 ) { private mutating func handleNode(node: SwiftSoup.Node ) {
@ -146,6 +151,4 @@ public struct HTMLString: Decodable, Equatable, Hashable {
} }
} }
} }

View file

@ -58,7 +58,7 @@ protocol StatusUI {
var uiShouldHighlight: Bool? { get set } 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 { public var viewId: String {
id + createdAt + (editedAt ?? "") 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 { public var viewId: String {
id + createdAt + (editedAt ?? "") id + createdAt + (editedAt ?? "")
} }

View file

@ -5,8 +5,8 @@ import SwiftUI
public class Client: ObservableObject, Equatable, Identifiable, Hashable { public class Client: ObservableObject, Equatable, Identifiable, Hashable {
public static func == (lhs: Client, rhs: Client) -> Bool { public static func == (lhs: Client, rhs: Client) -> Bool {
lhs.isAuth == rhs.isAuth && lhs.isAuth == rhs.isAuth &&
lhs.server == rhs.server && lhs.server == rhs.server &&
lhs.oauthToken?.accessToken == rhs.oauthToken?.accessToken lhs.oauthToken?.accessToken == rhs.oauthToken?.accessToken
} }
public enum Version: String { public enum Version: String {
@ -19,7 +19,7 @@ public class Client: ObservableObject, Equatable, Identifiable, Hashable {
} }
public var id: String { public var id: String {
"\(isAuth)\(server)\(oauthToken?.accessToken ?? "")" "\(isAuth)\(server)\(oauthToken?.createdAt ?? 0)"
} }
public func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {

View file

@ -156,7 +156,9 @@ extension TimelineViewModel: StatusesFetcher {
// If we get statuses from the cache for the home timeline, we displays those. // If we get statuses from the cache for the home timeline, we displays those.
// Else we fetch top most page from the API. // 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 statuses = cachedStatuses
withAnimation { withAnimation {
statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage)