Files
Kisani/Services/ThumbnailCache.swift
Robin Kutesa 025326f2b2 feat: redesign Gallery as photo timeline with sync state, deduplication, and detail view
- GalleryItem: unified model with phone/nas/synced source, sync state enum
- GalleryViewModel: off-actor PHFetchResult + manifest merge, Phone/NAS/All tab filtering, date section grouping, Combine-driven reactivity
- GalleryView: sticky date section headers, 3-col grid, shimmer placeholders, sync dots (green/orange/blue), video duration badge, search bar, context menu (Share, Save to Photos, Delete from Device)
- GalleryDetailView: full-screen swipeable photo viewer (TabView page mode), pinch-to-zoom, double-tap zoom toggle, bottom metadata sheet with file info and actions
- BackupStatusService: expose lastManifest for gallery deduplication without extra NAS connection
- ThumbnailCache: add decodeOffThread static helper for off-actor JPEG decode
- BackupManifest: Sendable conformance for safe Task.detached capture
- ThumbnailCache: synced items load from PHImageManager (instant) instead of NAS

Co-Authored-By: Kutesir <kutesir@provoc.ug>
Co-Authored-By: Sentry <sentry@provoc.ug>
2026-05-17 21:30:31 +03:00

59 lines
2.1 KiB
Swift

import UIKit
final class ThumbnailCache {
static let shared = ThumbnailCache()
private let memory = NSCache<NSString, UIImage>()
private let diskDir: URL
private init() {
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
diskDir = caches.appendingPathComponent("NASThumb", isDirectory: true)
try? FileManager.default.createDirectory(at: diskDir, withIntermediateDirectories: true)
memory.countLimit = 300
memory.totalCostLimit = 80 * 1024 * 1024 // 80 MB
}
func get(for path: String) -> UIImage? {
let key = cacheKey(path)
if let img = memory.object(forKey: key as NSString) { return img }
let url = diskDir.appendingPathComponent(key)
guard let data = try? Data(contentsOf: url),
let img = UIImage(data: data) else { return nil }
memory.setObject(img, forKey: key as NSString, cost: data.count)
return img
}
func set(_ image: UIImage, for path: String) {
let key = cacheKey(path)
let data = image.jpegData(compressionQuality: 0.7) ?? Data()
memory.setObject(image, forKey: key as NSString, cost: data.count)
let url = diskDir.appendingPathComponent(key)
try? data.write(to: url, options: .atomic)
}
private func cacheKey(_ path: String) -> String {
path
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: ":", with: "-")
}
}
extension ThumbnailCache {
static func decodeOffThread(data: Data, size: CGFloat) async -> UIImage? {
await Task.detached(priority: .userInitiated) {
UIImage(data: data)?.thumbnailScaled(to: size)
}.value
}
}
extension UIImage {
func thumbnailScaled(to size: CGFloat) -> UIImage {
let scale = min(size / self.size.width, size / self.size.height)
guard scale < 1 else { return self }
let newSize = CGSize(width: self.size.width * scale, height: self.size.height * scale)
let renderer = UIGraphicsImageRenderer(size: newSize)
return renderer.image { _ in self.draw(in: CGRect(origin: .zero, size: newSize)) }
}
}