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
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?

View file

@ -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 {
}
}
}

View file

@ -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 ?? "")
}

View file

@ -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) {

View file

@ -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)