mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-26 18:21:00 +00:00
Quote statuses: Added cache + faster
This commit is contained in:
parent
c7bb84eb9c
commit
f93f0f0974
2 changed files with 48 additions and 5 deletions
20
Packages/Status/Sources/Status/Embed/StatusEmbedCache.swift
Normal file
20
Packages/Status/Sources/Status/Embed/StatusEmbedCache.swift
Normal 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]
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue