Quote statuses: Added cache + faster

This commit is contained in:
Thomas Ricouard 2023-02-23 07:23:18 +01:00
parent c7bb84eb9c
commit f93f0f0974
2 changed files with 48 additions and 5 deletions

View File

@ -0,0 +1,20 @@
import Foundation
import SwiftUI
import Models
@MainActor
class StatusEmbedCache {
static let shared = StatusEmbedCache()
private var cache: [URL: Status] = [:]
private init() { }
func set(url: URL, status: Status) {
cache[url] = status
}
func get(url: URL) -> Status? {
cache[url]
}
}

View File

@ -96,8 +96,14 @@ public class StatusRowViewModel: ObservableObject {
}
isFiltered = filter != nil
if let url = embededStatusURL(),
let embed = StatusEmbedCache.shared.get(url: url) {
isEmbedLoading = false
embeddedStatus = embed
}
}
func markSeen() {
// called in on appear so we can cache that the status has been seen.
if UserPreferences.shared.suppressDupeReblogs && !seen {
@ -147,18 +153,32 @@ public class StatusRowViewModel: ObservableObject {
routerPath.navigate(to: .accountDetail(id: mention.id))
}
}
private func embededStatusURL() -> URL? {
let content = status.reblog?.content ?? status.content
if !content.statusesURLs.isEmpty,
let url = content.statusesURLs.first,
client.hasConnection(with: url) {
return url
}
return nil
}
func loadEmbeddedStatus() async {
guard embeddedStatus == nil,
!status.content.statusesURLs.isEmpty,
let url = status.content.statusesURLs.first,
client.hasConnection(with: url)
else {
let url = embededStatusURL() else {
if isEmbedLoading {
isEmbedLoading = false
}
return
}
if let embed = StatusEmbedCache.shared.get(url: url) {
isEmbedLoading = false
embeddedStatus = embed
return
}
do {
isEmbedLoading = true
var embed: Status?
@ -172,6 +192,9 @@ public class StatusRowViewModel: ObservableObject {
forceVersion: .v2)
embed = results.statuses.first
}
if let embed {
StatusEmbedCache.shared.set(url: url, status: embed)
}
withAnimation {
embeddedStatus = embed
isEmbedLoading = false