SPB Git

Repository Public License

LibraryModel.swift 8.2 KB 223 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Camera9// File             : App/Sources/Features/Library/LibraryModel.swift10// Description      : State of the library screen: the filtered item list,11//                    selection, albums and the actions on them.12// Module           : Features13// Repository       : https://git.spboucher.ai/orix-camera14// Project Website  : https://www.orixlab.dev15// Created          : 2026-11-2116// Last Modified    : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : Proprietary, all rights reserved (see license file)83// License File     : LICENSE84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation92import Observation93import ORIXImaging9495@Observable96final class LibraryModel {97    let library: MediaLibrary98    private(set) var items: [MediaItem] = []99    private(set) var albums: [MediaAlbum] = []100    private(set) var missing: [MediaItem] = []101    private(set) var totalBytes: Int64 = 0102    private(set) var totalCount = 0103    /// Bumped whenever content changes, so tiles refresh their thumbnails.104    private(set) var revision = 0105106    var filter: MediaFilter = .all { didSet { if filter != oldValue { reload() } } }107    var search = "" { didSet { if search != oldValue { reload() } } }108    var sort: MediaSort = .newest { didSet { if sort != oldValue { reload() } } }109    var album: MediaAlbum? { didSet { reload() } }110    var isSelecting = false { didSet { if !isSelecting { selection.removeAll() } } }111    var selection: Set<UUID> = []112113    init(library: MediaLibrary) {114        self.library = library115        reload()116    }117118    func reload() {119        items = library.items(filter: filter, search: search, sort: sort, album: album)120        albums = library.albums()121        missing = library.itemsMissingFiles()122        let all = library.allItems()123        totalBytes = all.reduce(0) { $0 + $1.byteSize }124        totalCount = all.count125        selection.formIntersection(Set(items.map(\.id)))126        revision += 1127    }128129    func snapshot(_ item: MediaItem) -> ItemSnapshot { item.snapshot(storage: library.storage) }130131    var selectedItems: [MediaItem] { items.filter { selection.contains($0.id) } }132133    func toggleSelection(_ item: MediaItem) {134        if selection.contains(item.id) { selection.remove(item.id) } else { selection.insert(item.id) }135    }136137    func selectAll() { selection = Set(items.map(\.id)) }138139    func toggleFavorite(_ item: MediaItem) { library.toggleFavorite(item); reload() }140141    /// Favourites every selected item, or clears the flag when all of them already have it.142    func toggleFavoriteForSelection() {143        let chosen = selectedItems144        let target = !chosen.allSatisfy(\.isFavorite)145        for item in chosen where item.isFavorite != target { library.toggleFavorite(item) }146        reload()147    }148149    /// Removes items and their files from the device. Callers confirm with the user first.150    func delete(_ doomed: [MediaItem]) {151        for item in doomed { library.delete(item) }152        selection.subtract(doomed.map(\.id))153        reload()154    }155156    @discardableResult157    func createAlbum(named name: String) -> MediaAlbum? {158        let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)159        guard !trimmed.isEmpty else { return nil }160        let created = library.createAlbum(named: trimmed)161        reload()162        return created163    }164165    func add(_ chosen: [MediaItem], to album: MediaAlbum) {166        for item in chosen { library.add(item, to: album) }167        reload()168    }169170    func remove(_ chosen: [MediaItem], from album: MediaAlbum) {171        for item in chosen { library.remove(item, from: album) }172        reload()173    }174175    /// Deletes the album only; its items stay in the library.176    func delete(_ doomed: MediaAlbum) {177        if album?.id == doomed.id { album = nil }178        library.delete(doomed)179        reload()180    }181182    /// Forgets rows whose file has disappeared (the user asked for it from the notice).183    func forgetMissing() {184        for item in missing { library.delete(item) }185        reload()186    }187188    func duplicateGroups() -> [[MediaItem]] { library.duplicateGroups() }189190    static func title(_ filter: MediaFilter) -> LocalizedStringResource {191        switch filter {192        case .all: "All"193        case .photos: "Photos"194        case .videos: "Videos"195        case .raw: "RAW"196        case .favorites: "Favorites"197        case .edited: "Edited"198        case .computational: "Computational"199        }200    }201202    static func title(_ sort: MediaSort) -> LocalizedStringResource {203        switch sort {204        case .newest: "Newest first"205        case .oldest: "Oldest first"206        case .largest: "Largest first"207        case .name: "File name"208        }209    }210}211212/// Launch arguments used for screenshots of deep screens (Debug builds only).213///   -ORIXLibraryShow viewer|metadata|export|albums|duplicates      -ORIXEditorTab <tab>214enum LibraryDebug {215    #if DEBUG216    static var show: String? { UserDefaults.standard.string(forKey: "ORIXLibraryShow") }217    static var editorTab: String? { UserDefaults.standard.string(forKey: "ORIXEditorTab") }218    #else219    static var show: String? { nil }220    static var editorTab: String? { nil }221    #endif222}223
Files main
Menu

Theme