SPB Git

Repository Public

WidgetSnapshot.swift 5.3 KB 102 lines swift

1// SP Drive iOS2// File: Core/WidgetSnapshot.swift3// Description: What the widgets show, written by the app into the group container and read by the widget extension. The widgets never4//              touch the network, the keychain or the database: they render this small file. Self-contained (Foundation only).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public struct WidgetSnapshot: Codable, Sendable, Equatable {11    public struct File: Codable, Sendable, Equatable, Identifiable {12        public var id: String13        public var name: String14        /// SF Symbol of the file's family.15        public var symbol: String16        public var size: Int6417        public var modifiedAt: Date18        /// File name of a small JPEG next to the snapshot, when a thumbnail was available.19        public var thumbnail: String?20        public init(id: String, name: String, symbol: String, size: Int64, modifiedAt: Date, thumbnail: String?) { self.id = id; self.name = name; self.symbol = symbol; self.size = size; self.modifiedAt = modifiedAt; self.thumbnail = thumbnail }21    }22    public struct Folder: Codable, Sendable, Equatable, Identifiable {23        public var id: String24        public var name: String25        public var itemCount: Int26        public init(id: String, name: String, itemCount: Int) { self.id = id; self.name = name; self.itemCount = itemCount }27    }28    public struct Storage: Codable, Sendable, Equatable {29        public var used: Int6430        public var limit: Int6431        public var offlineBytes: Int6432        public init(used: Int64, limit: Int64, offlineBytes: Int64) { self.used = used; self.limit = limit; self.offlineBytes = offlineBytes }33        public var fraction: Double { limit > 0 ? min(1, Double(used) / Double(limit)) : 0 }34    }35    public struct Transfers: Codable, Sendable, Equatable {36        public var uploads: Int37        public var downloads: Int38        public var failed: Int39        public var doneBytes: Int6440        public var totalBytes: Int6441        /// Raw value of `TransferPolicy.Hold` when the queue is waiting for something (Wi-Fi, network, power).42        public var hold: String?43        public init(uploads: Int, downloads: Int, failed: Int, doneBytes: Int64, totalBytes: Int64, hold: String?) { self.uploads = uploads; self.downloads = downloads; self.failed = failed; self.doneBytes = doneBytes; self.totalBytes = totalBytes; self.hold = hold }44        public var active: Int { uploads + downloads }45        public var fraction: Double { totalBytes > 0 ? min(1, Double(doneBytes) / Double(totalBytes)) : 0 }46    }4748    public var signedIn: Bool49    /// The app lock is on: names stay out of the widgets.50    public var redacted: Bool51    public var generatedAt: Date52    public var recent: [File]53    public var favoriteFolders: [Folder]54    public var storage: Storage?55    public var transfers: Transfers56    public var conflicts: Int5758    public init(signedIn: Bool, redacted: Bool = false, generatedAt: Date = .now, recent: [File] = [], favoriteFolders: [Folder] = [], storage: Storage? = nil,59                transfers: Transfers = .init(uploads: 0, downloads: 0, failed: 0, doneBytes: 0, totalBytes: 0, hold: nil), conflicts: Int = 0) {60        self.signedIn = signedIn; self.redacted = redacted; self.generatedAt = generatedAt; self.recent = recent; self.favoriteFolders = favoriteFolders61        self.storage = storage; self.transfers = transfers; self.conflicts = conflicts62    }6364    public static let signedOut = WidgetSnapshot(signedIn: false)6566    // MARK: Storage in the group container6768    public static let groupIdentifier = "group.ai.spboucher.spdrive.ios"69    public static func directory(group: String = groupIdentifier) -> URL? {70        guard let base = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group) else { return nil }71        let dir = base.appendingPathComponent("Widgets", isDirectory: true)72        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)73        return dir74    }7576    public func write(to directory: URL) throws {77        let e = JSONEncoder()78        e.dateEncodingStrategy = .secondsSince197079        try e.encode(self).write(to: directory.appendingPathComponent("snapshot.json"), options: .atomic)80    }8182    public static func read(from directory: URL?) -> WidgetSnapshot? {83        guard let directory, let data = try? Data(contentsOf: directory.appendingPathComponent("snapshot.json")) else { return nil }84        let d = JSONDecoder()85        d.dateDecodingStrategy = .secondsSince197086        return try? d.decode(WidgetSnapshot.self, from: data)87    }88}8990/// Deep links shared by the widgets and the app (`spdrive://…`).91public enum SPDriveLink {92    public static let scheme = "spdrive"93    public static let upload = URL(string: "spdrive://upload")!94    public static let scan = URL(string: "spdrive://scan")!95    public static let transfers = URL(string: "spdrive://transfers")!96    public static let recent = URL(string: "spdrive://recent")!97    public static let storage = URL(string: "spdrive://storage")!98    public static let search = URL(string: "spdrive://search")!99    public static func folder(_ id: String) -> URL { URL(string: "spdrive://folder/\(id)")! }100    public static func file(_ id: String) -> URL { URL(string: "spdrive://file/\(id)")! }101}102
Files main
Menu

Theme