Repository Public
1// SP Drive iOS2// File: App/AppRoute.swift3// Description: Places and actions that can be reached from outside a screen: deep links (`spdrive://…` from the widgets), keyboard4// shortcuts, and « open in a new window ». Also publishes the widgets' snapshot into the group container.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import UIKit10import WidgetKit1112enum AppRoute: Hashable, Sendable {13 case upload, scan, transfers, recent, storage, search14 case folder(String)15 case file(String)1617 init?(url: URL) {18 guard url.scheme == SPDriveLink.scheme, let host = url.host() else { return nil }19 // identifiers are UUIDs (or `local-` + UUID): anything else is refused rather than looked up20 let id = url.pathComponents.dropFirst().first.flatMap { $0.range(of: #"^(local-)?[0-9a-fA-F-]{36}$"#, options: .regularExpression) != nil ? $0 : nil }21 switch host {22 case "upload": self = .upload23 case "scan": self = .scan24 case "transfers": self = .transfers25 case "recent": self = .recent26 case "storage": self = .storage27 case "search": self = .search28 case "folder": guard let id else { return nil }; self = .folder(id)29 case "file": guard let id else { return nil }; self = .file(id)30 default: return nil31 }32 }33}3435/// Writes what the widgets show. Called by the app model when the Drive, the transfers or the account change, and when the app goes to the background.36enum WidgetBridge {37 @MainActor static func publish(store: DriveStore, files: LocalFiles, quota: (used: Int64, limit: Int64)?, holds: [String: TransferPolicy.Hold]) {38 guard let dir = WidgetSnapshot.directory(group: AppGroup.identifier) else { return }39 let redacted = UserDefaults.standard.bool(forKey: "lock.enabled")40 let transfers = store.transfers(states: [.queued, .running, .failed])41 let active = transfers.filter(\.isActive)42 let usage = store.localUsage()43 let hold = TransferPolicy.Hold.allCases.first { h in holds.values.contains(h) }44 var recent: [WidgetSnapshot.File] = []45 if !redacted {46 let thumbs = dir.appendingPathComponent("thumbs", isDirectory: true)47 try? FileManager.default.removeItem(at: thumbs)48 try? FileManager.default.createDirectory(at: thumbs, withIntermediateDirectories: true)49 for item in store.recent(limit: 8) {50 var thumbName: String?51 // the explorer already cached a thumbnail for files it displayed: reuse it, never fetch here52 let cached = files.thumbnail(itemID: item.id, sha: item.sha256)53 if FileManager.default.fileExists(atPath: cached.path) {54 let name = "\(item.id).jpg"55 if (try? FileManager.default.copyItem(at: cached, to: thumbs.appendingPathComponent(name))) != nil { thumbName = name }56 }57 recent.append(.init(id: item.id, name: item.name, symbol: item.kind.symbol, size: item.size, modifiedAt: item.updatedAt, thumbnail: thumbName))58 }59 }60 let folders = redacted ? [] : store.starred().filter(\.isFolder).prefix(6).map { WidgetSnapshot.Folder(id: $0.id, name: $0.name, itemCount: store.childCount(of: $0.id)) }61 let snapshot = WidgetSnapshot(signedIn: true, redacted: redacted, recent: recent, favoriteFolders: Array(folders),62 storage: quota.map { .init(used: $0.used, limit: $0.limit, offlineBytes: usage.offlineBytes) },63 transfers: .init(uploads: active.filter { $0.kind == .upload }.count, downloads: active.filter { $0.kind == .download }.count,64 failed: transfers.filter { $0.state == .failed }.count, doneBytes: active.reduce(0) { $0 + $1.done }, totalBytes: active.reduce(0) { $0 + $1.size }, hold: hold?.rawValue),65 conflicts: store.conflicts().count)66 guard WidgetSnapshot.read(from: dir).map({ var a = $0; a.generatedAt = snapshot.generatedAt; return a != snapshot }) ?? true else { return }67 try? snapshot.write(to: dir)68 WidgetCenter.shared.reloadAllTimelines()69 }7071 static func clear() {72 guard let dir = WidgetSnapshot.directory(group: AppGroup.identifier) else { return }73 try? FileManager.default.removeItem(at: dir.appendingPathComponent("thumbs"))74 try? WidgetSnapshot.signedOut.write(to: dir)75 WidgetCenter.shared.reloadAllTimelines()76 }77}78