mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-04-26 18:04:44 +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
|
isFiltered = filter != nil
|
||||||
|
|
||||||
|
if let url = embededStatusURL(),
|
||||||
|
let embed = StatusEmbedCache.shared.get(url: url) {
|
||||||
|
isEmbedLoading = false
|
||||||
|
embeddedStatus = embed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func markSeen() {
|
func markSeen() {
|
||||||
// called in on appear so we can cache that the status has been seen.
|
// called in on appear so we can cache that the status has been seen.
|
||||||
if UserPreferences.shared.suppressDupeReblogs && !seen {
|
if UserPreferences.shared.suppressDupeReblogs && !seen {
|
||||||
|
@ -147,18 +153,32 @@ public class StatusRowViewModel: ObservableObject {
|
||||||
routerPath.navigate(to: .accountDetail(id: mention.id))
|
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 {
|
func loadEmbeddedStatus() async {
|
||||||
guard embeddedStatus == nil,
|
guard embeddedStatus == nil,
|
||||||
!status.content.statusesURLs.isEmpty,
|
let url = embededStatusURL() else {
|
||||||
let url = status.content.statusesURLs.first,
|
|
||||||
client.hasConnection(with: url)
|
|
||||||
else {
|
|
||||||
if isEmbedLoading {
|
if isEmbedLoading {
|
||||||
isEmbedLoading = false
|
isEmbedLoading = false
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let embed = StatusEmbedCache.shared.get(url: url) {
|
||||||
|
isEmbedLoading = false
|
||||||
|
embeddedStatus = embed
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
isEmbedLoading = true
|
isEmbedLoading = true
|
||||||
var embed: Status?
|
var embed: Status?
|
||||||
|
@ -172,6 +192,9 @@ public class StatusRowViewModel: ObservableObject {
|
||||||
forceVersion: .v2)
|
forceVersion: .v2)
|
||||||
embed = results.statuses.first
|
embed = results.statuses.first
|
||||||
}
|
}
|
||||||
|
if let embed {
|
||||||
|
StatusEmbedCache.shared.set(url: url, status: embed)
|
||||||
|
}
|
||||||
withAnimation {
|
withAnimation {
|
||||||
embeddedStatus = embed
|
embeddedStatus = embed
|
||||||
isEmbedLoading = false
|
isEmbedLoading = false
|
||||||
|
|
Loading…
Reference in a new issue