Sync source edition with VCC Plus 3.7.0 build 63
This commit is contained in:
1 parent
c652a5276d
commit
b2ecec4d87
86 files changed
+10519
-472
No files matched your search
@@ -2,6 +2,7 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
import AppKit
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
/// Native review stays attached to its window and cannot strand shutdown on a
|
||||
@@ -49,9 +50,9 @@ struct LibraryCategory: Identifiable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
struct LibraryBrowseQuery: Equatable, Sendable {
|
||||
enum Scope: String, CaseIterable { case showcase, started, all, favorites, installed }
|
||||
enum Sort: String, CaseIterable { case title = "Title", modified = "Last Modified", size = "Download Size" }
|
||||
struct LibraryBrowseQuery: Equatable, Sendable, Codable {
|
||||
enum Scope: String, CaseIterable, Codable { case showcase, started, all, favorites, installed, personal, search }
|
||||
enum Sort: String, CaseIterable, Codable { case title = "Title", modified = "Last Modified", size = "Download Size" }
|
||||
var scope: Scope = .started
|
||||
var text = ""
|
||||
var category = ""
|
||||
@@ -129,6 +130,11 @@ struct LibraryBrowseQuery: Equatable, Sendable {
|
||||
}
|
||||
func apply(to titles: [SoftwareLibraryTitle], favorites: Set<String>, installed: Set<String>) -> [LibraryBrowseRow] {
|
||||
var rows = titles.compactMap { title -> LibraryBrowseRow? in
|
||||
let personal = title.id.hasPrefix("personal:")
|
||||
if scope == .personal && !personal { return nil }
|
||||
// Getting Started and Cover Collection remain curated catalog
|
||||
// views. General browsing/search can combine local and public data.
|
||||
if personal && (scope == .started || scope == .showcase || scope == .installed) { return nil }
|
||||
if scope == .favorites && !favorites.contains(title.id) { return nil }
|
||||
let matches = title.releases.filter {
|
||||
(scope != .started || Self.isGettingStarted($0)) &&
|
||||
@@ -139,11 +145,12 @@ struct LibraryBrowseQuery: Equatable, Sendable {
|
||||
return LibraryBrowseRow(title: title, matchingReleases: matches)
|
||||
}
|
||||
rows.sort { a, b in
|
||||
if a.isPersonal != b.isPersonal { return a.isPersonal }
|
||||
let featuredGrouping = !category.isEmpty || !softwareType.isEmpty
|
||||
let aFeatured = featuredGrouping && a.categoryFeatured
|
||||
let bFeatured = featuredGrouping && b.categoryFeatured
|
||||
if aFeatured != bFeatured { return aFeatured }
|
||||
switch aFeatured ? .title : sort {
|
||||
switch a.isPersonal || aFeatured ? .title : sort {
|
||||
case .modified:
|
||||
let av = a.matchingReleases.compactMap(\.modifiedAt).max() ?? ""
|
||||
let bv = b.matchingReleases.compactMap(\.modifiedAt).max() ?? ""
|
||||
@@ -164,6 +171,7 @@ struct LibraryBrowseRow: Identifiable, Sendable {
|
||||
let title: SoftwareLibraryTitle
|
||||
let matchingReleases: [SoftwareLibraryRelease]
|
||||
var id: String { title.id }
|
||||
var isPersonal: Bool { title.id.hasPrefix("personal:") && matchingReleases.contains(where: \.isPersonal) }
|
||||
var name: String { title.name }
|
||||
var categoryFeatured: Bool { matchingReleases.contains(where: \.categoryFeatured) }
|
||||
var formats: String { Set(matchingReleases.map(\.mediaLabel)).sorted().joined(separator: ", ") }
|
||||
@@ -197,14 +205,28 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
@Published private(set) var loading = false
|
||||
@Published private(set) var filtering = false
|
||||
@Published private(set) var error: String?
|
||||
@Published var query = LibraryBrowseQuery() { didSet { if query != oldValue { filter() } } }
|
||||
@Published var selection: String?
|
||||
@Published var layout = "Shelf"
|
||||
@Published var query = LibraryBrowseQuery() { didSet { if query != oldValue { saveBrowseContext(); filter() } } }
|
||||
@Published var selection: String? { didSet { if selection != oldValue { saveBrowseContext() } } }
|
||||
@Published var layout = "Shelf" { didSet { if layout != oldValue { saveBrowseContext() } } }
|
||||
private(set) var browseTopID: String?
|
||||
private(set) var browseOffset: Double = 0
|
||||
private var restoredBrowse = false
|
||||
private var restoringBrowse = false
|
||||
let companionAccess: (any CompanionLibraryAccess)?
|
||||
private let insertCompanionDisk: ((URL) async throws -> Void)?
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
let installer: SoftwareLibraryInstaller
|
||||
let machine: MachineModel?
|
||||
private let browseContinuity: LibraryContinuityStore
|
||||
lazy var personalStore = PersonalSoftwareLibraryStore(root: state.directory.appendingPathComponent("PersonalLibrary", isDirectory: true))
|
||||
@Published private(set) var personalEntries: [PersonalSoftwareEntry] = []
|
||||
@Published var personalEditor: PersonalSoftwareEntry?
|
||||
@Published private(set) var personalInspecting = false
|
||||
@Published private(set) var personalSaving = false
|
||||
@Published private(set) var personalError: String?
|
||||
@Published private(set) var personalDuplicateID: UUID?
|
||||
private var personalDraft: PersonalSoftwareDraft?
|
||||
private var personalTask: Task<Void, Never>?
|
||||
private let sourceImporter: SoftwareLibrarySourceImporter
|
||||
private let sourceWorkspaceIdentity: (() -> UUID)?
|
||||
private let openSourceProject: ((URL, UUID, String?) async throws -> Bool)?
|
||||
@@ -239,11 +261,13 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
private var generation = 0
|
||||
private var subscriptions: Set<AnyCancellable> = []
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
init(state: SoftwareLibraryState, artworkDirectory: URL? = nil, machine: MachineModel? = nil,
|
||||
init(state: SoftwareLibraryState, continuity: LibraryContinuityStore? = nil, artworkDirectory: URL? = nil, machine: MachineModel? = nil,
|
||||
sourceWorkspaceIdentity: (() -> UUID)? = nil,
|
||||
openSourceProject: ((URL, UUID, String?) async throws -> Bool)? = nil) {
|
||||
companionAccess = nil; insertCompanionDisk = nil
|
||||
self.state = state
|
||||
browseContinuity = continuity ?? LibraryContinuityStore(directory: state.directory)
|
||||
manual.continuity = browseContinuity
|
||||
self.machine = machine
|
||||
self.sourceWorkspaceIdentity = sourceWorkspaceIdentity; self.openSourceProject = openSourceProject
|
||||
#if VCC_UI_TESTING
|
||||
@@ -277,6 +301,7 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
state = SoftwareLibraryState(companion: companion)
|
||||
artworkDirectory = FileManager.default.temporaryDirectory.appendingPathComponent("VCCCompanionPresentation")
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
browseContinuity = LibraryContinuityStore(directory: state.directory)
|
||||
machine = nil; installer = SoftwareLibraryInstaller(root: state.directory)
|
||||
sourceWorkspaceIdentity = nil; openSourceProject = nil
|
||||
sourceImporter = SoftwareLibrarySourceImporter(cacheDirectory: state.directory.appendingPathComponent("SourcePackages"))
|
||||
@@ -373,7 +398,7 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
if isDiskCompanion { return installed ? "Insert Disk…" : "Download & Insert…" }
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
if release.ownedGame?.firstPlayColdDOS == true { return installed ? "Play" : "Download & Play" }
|
||||
return SoftwareLibraryLaunchPlan.hasAutomaticStartup(for: release)
|
||||
return SoftwareLibraryLaunchPlan.hasPlayAction(for: release)
|
||||
? (installed ? "Play…" : "Download & Play…") : (installed ? "Set Up…" : "Download & Set Up…")
|
||||
#else
|
||||
return "Insert Disk…"
|
||||
@@ -382,7 +407,7 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
func launchSymbol(_ release: SoftwareLibraryRelease) -> String {
|
||||
if isDiskCompanion { return "externaldrive" }
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
return SoftwareLibraryLaunchPlan.hasAutomaticStartup(for: release) ? "play.fill" : "desktopcomputer"
|
||||
return SoftwareLibraryLaunchPlan.hasPlayAction(for: release) ? "play.fill" : "desktopcomputer"
|
||||
#else
|
||||
return "externaldrive"
|
||||
#endif
|
||||
@@ -392,11 +417,17 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
machine?.focusDisplay()
|
||||
#endif
|
||||
}
|
||||
func load(from url: URL? = SoftwareLibraryCatalog.bundledURL()) async {
|
||||
func load(from url: URL? = SoftwareLibraryCatalog.bundledURL(),
|
||||
catalogLoader: @Sendable (URL) async throws -> SoftwareLibraryCatalog = { try await SoftwareLibraryCatalog.load(from: $0) }) async {
|
||||
guard catalog == nil, !loading else { return }
|
||||
// Restoration may schedule a filter before recovered installations,
|
||||
// private entries or the catalog arrive. Reconcile selection only once
|
||||
// that complete initial result is available, preserving newer user input.
|
||||
loading = true; error = nil
|
||||
restoreBrowseContext()
|
||||
defer { loading = false; filter() }
|
||||
if let companionAccess {
|
||||
defer { loading = false; recoveringInstallations = false }
|
||||
defer { recoveringInstallations = false }
|
||||
do {
|
||||
let response = try await companionAccess.libraryRequest(.init(operation: .snapshot))
|
||||
try acceptCompanion(response)
|
||||
@@ -406,21 +437,21 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
}
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
await refreshInstallations()
|
||||
await refreshPersonalEntries()
|
||||
#if VCC_UI_TESTING
|
||||
if SoftwareLibraryInstallFixture.enabled {
|
||||
catalog = SoftwareLibraryInstallFixture.catalog
|
||||
categories = LibraryCategory.tree(catalog!.categories)
|
||||
updateSoftwareTypes(catalog!)
|
||||
query.scope = .all
|
||||
filter(); loading = false; return
|
||||
filter(); return
|
||||
}
|
||||
#endif
|
||||
do {
|
||||
guard let url else { throw SoftwareLibraryCatalog.Failure.missing }
|
||||
let snapshot = try await SoftwareLibraryCatalog.load(from: url)
|
||||
let snapshot = try await catalogLoader(url)
|
||||
catalog = snapshot; categories = LibraryCategory.tree(snapshot.categories); updateSoftwareTypes(snapshot); filter()
|
||||
} catch { self.error = error.localizedDescription }
|
||||
loading = false
|
||||
#endif
|
||||
}
|
||||
private func acceptCompanion(_ response: CompanionLibraryResponse) throws {
|
||||
@@ -464,8 +495,25 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
next.scope = scope; next.category = category; next.softwareType = ""
|
||||
query = next
|
||||
}
|
||||
private func updateSoftwareTypes(_ catalog: SoftwareLibraryCatalog) {
|
||||
softwareTypes = Array(Set(catalog.titles.flatMap { title in title.releases.flatMap { LibraryBrowseQuery.types(of: $0, related: title.releases) } })).sorted()
|
||||
/// The header is a global search entry point, not an implicit filter of
|
||||
/// whichever narrow collection happened to be selected before typing.
|
||||
func setSearchText(_ text: String) {
|
||||
guard text != query.text else { return }
|
||||
if !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && query.scope != .search {
|
||||
query = LibraryBrowseQuery(scope: .search, text: text, sort: query.sort)
|
||||
} else {
|
||||
var next = query
|
||||
next.text = text
|
||||
if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, next.scope == .search { next.scope = .all }
|
||||
query = next
|
||||
}
|
||||
}
|
||||
private func updateSoftwareTypes(_ catalog: SoftwareLibraryCatalog?) {
|
||||
var types = Set(catalog?.titles.flatMap { title in title.releases.flatMap { LibraryBrowseQuery.types(of: $0, related: title.releases) } } ?? [])
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
if !isDiskCompanion { types.formUnion(personalEntries.map { PersonalSoftwareEntry.standardCategory($0.category) }) }
|
||||
#endif
|
||||
softwareTypes = types.sorted()
|
||||
}
|
||||
func browseType(_ type: String) {
|
||||
var next = query
|
||||
@@ -479,6 +527,13 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
query = next
|
||||
}
|
||||
func install(_ release: SoftwareLibraryRelease, playAfter: Bool = false) {
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
if release.isPersonal && !isDiskCompanion {
|
||||
guard playAfter, let entry = personalEntries.first(where: { $0.releaseID == release.id }), !launchBusy, !hasPendingLaunch else { return }
|
||||
personalTask = Task { await prepareLaunch(release, receipt: entry.receipt(in: await personalStore.directory(for: entry.id))) }
|
||||
return
|
||||
}
|
||||
#endif
|
||||
guard installingReleaseID == nil, !recoveringInstallations, !launchBusy, !hasPendingLaunch else { return }
|
||||
if companionAccess != nil { installCompanion(release, insertAfter: playAfter); return }
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
@@ -510,6 +565,10 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
func shutdown() async {
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
personalTask?.cancel(); await personalTask?.value
|
||||
if let draft = personalDraft { await personalStore.cancel(draft.id) }
|
||||
#endif
|
||||
sourceImportTask?.cancel()
|
||||
await sourceImportTask?.value
|
||||
launchTask?.cancel()
|
||||
@@ -517,8 +576,14 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
cancelInstallation()
|
||||
await installationTask?.value
|
||||
await state.flush()
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
await browseContinuity.flush()
|
||||
#endif
|
||||
}
|
||||
func removeInstallation(_ id: String) async {
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
if let entry = personalEntries.first(where: { $0.releaseID == id }) { removePersonal(entry); return }
|
||||
#endif
|
||||
guard installingReleaseID == nil, !launchBusy, !hasPendingLaunch else { return }
|
||||
if let companionAccess {
|
||||
do { try acceptCompanion(try await companionAccess.libraryRequest(.init(operation: .removeInstallation, releaseID: id))) }
|
||||
@@ -687,19 +752,212 @@ final class SoftwareLibraryModel: ObservableObject {
|
||||
launchMessage = "The disk is inserted. Follow the loading instructions on your CoCo."
|
||||
} catch { if !Task.isCancelled { launchError = error.localizedDescription } }
|
||||
}
|
||||
/// A presentation/permissioned-local-connection snapshot. The prepared
|
||||
/// public catalog remains unchanged; private entries keep separate IDs.
|
||||
var combinedCatalog: SoftwareLibraryCatalog {
|
||||
var titles = catalog?.titles ?? []
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
if !isDiskCompanion {
|
||||
titles += personalEntries.map { entry in
|
||||
let directory = personalDirectory(entry)
|
||||
return SoftwareLibraryTitle(id: entry.titleID, name: entry.title, identityStatus: "personal", releases: [entry.release(in: directory)])
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return SoftwareLibraryCatalog(titles: titles, metadata: catalog?.metadata ?? [:])
|
||||
}
|
||||
private func filter() {
|
||||
guard let catalog else { return }
|
||||
let titles = combinedCatalog.titles
|
||||
task?.cancel(); generation += 1; let version = generation
|
||||
let query = query, favorites = state.favoriteTitleIDs, installed = Set(state.installedReleases.keys)
|
||||
let sourceTitles = titles
|
||||
filtering = true
|
||||
task = Task {
|
||||
// Debounce typing without blocking the machine's UI or audio queues.
|
||||
do { try await Task.sleep(nanoseconds: 90_000_000) } catch { return }
|
||||
let worker = Task.detached(priority: .userInitiated) { query.apply(to: catalog.titles, favorites: favorites, installed: installed) }
|
||||
let worker = Task.detached(priority: .userInitiated) { query.apply(to: sourceTitles, favorites: favorites, installed: installed) }
|
||||
let result = await worker.value
|
||||
guard !Task.isCancelled, version == generation else { return }
|
||||
rows = result; filtering = false
|
||||
if !rows.contains(where: { $0.id == selection }) { selection = rows.first?.id }
|
||||
// A failed public-catalog read is not evidence that a saved public
|
||||
// title disappeared. Keep it pending for Retry; private-only browsing
|
||||
// can still reconcile against the successfully recovered local set.
|
||||
let selectionDomainAvailable = catalog != nil || query.scope == .personal ||
|
||||
selection?.hasPrefix("personal:") == true || (selection == nil && !rows.isEmpty)
|
||||
if !loading, selectionDomainAvailable, !rows.contains(where: { $0.id == selection }) { selection = rows.first?.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
extension SoftwareLibraryModel {
|
||||
var personalUnsupported: [String] { personalDraft?.unsupported ?? [] }
|
||||
var isPersonalImport: Bool { personalDraft != nil }
|
||||
var canEditPersonal: Bool { !isDiskCompanion && !personalInspecting && !personalSaving }
|
||||
var selectedPersonalEntry: PersonalSoftwareEntry? { personalEntries.first { $0.titleID == selection } }
|
||||
func choosePersonalSoftware() {
|
||||
guard canEditPersonal, personalEditor == nil else { return }
|
||||
let panel = NSOpenPanel()
|
||||
panel.title = "Add Personal Software"; panel.prompt = "Review Files"
|
||||
// The native panel sizes its message horizontally. Keep each line short
|
||||
// so guidance does not force an excessively wide file chooser.
|
||||
panel.message = "Choose a folder, ZIP, or CoCo media files.\nOriginals stay untouched. Nothing is uploaded or run."
|
||||
panel.canChooseFiles = true; panel.canChooseDirectories = true; panel.allowsMultipleSelection = true
|
||||
panel.begin { [weak self] response in
|
||||
guard response == .OK, !panel.urls.isEmpty, let self else { return }
|
||||
self.personalInspecting = true; self.personalError = nil
|
||||
self.personalTask = Task {
|
||||
defer { self.personalInspecting = false; self.personalTask = nil }
|
||||
do {
|
||||
let draft = try await self.personalStore.inspect(panel.urls)
|
||||
if Task.isCancelled { await self.personalStore.cancel(draft.id); throw CancellationError() }
|
||||
self.personalDraft = draft; self.personalEditor = draft.entry; self.personalDuplicateID = nil
|
||||
} catch is CancellationError { }
|
||||
catch { self.personalError = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
}
|
||||
func cancelPersonalInspection() { if personalInspecting { personalTask?.cancel() } }
|
||||
func editPersonal(_ entry: PersonalSoftwareEntry) {
|
||||
guard canEditPersonal, personalEditor == nil else { return }
|
||||
personalDraft = nil; personalError = nil; personalDuplicateID = nil; personalEditor = entry
|
||||
}
|
||||
func savePersonal(_ entry: PersonalSoftwareEntry, coverPNG: Data? = nil, allowDuplicate: Bool) {
|
||||
guard !personalSaving else { return }
|
||||
personalSaving = true; personalError = nil; personalDuplicateID = nil
|
||||
personalTask = Task {
|
||||
defer { personalSaving = false; personalTask = nil }
|
||||
do {
|
||||
if var draft = personalDraft {
|
||||
draft.entry = entry
|
||||
_ = try await personalStore.save(draft, allowDuplicate: allowDuplicate, coverPNG: coverPNG)
|
||||
} else { try await personalStore.update(entry, coverPNG: coverPNG) }
|
||||
personalDraft = nil; personalEditor = nil
|
||||
await refreshPersonalEntries()
|
||||
query = LibraryBrowseQuery(scope: .personal); selection = entry.titleID
|
||||
installationNotice = "Your personal software is saved privately on this Mac. The CoCo has not been changed."
|
||||
} catch PersonalSoftwareLibraryStore.Failure.duplicate(let id) {
|
||||
personalDuplicateID = id
|
||||
personalError = PersonalSoftwareLibraryStore.Failure.duplicate(id).localizedDescription
|
||||
} catch { personalError = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
func cancelPersonalEdit() {
|
||||
guard !personalSaving else { return }
|
||||
let draft = personalDraft; personalEditor = nil; personalDraft = nil; personalDuplicateID = nil; personalError = nil
|
||||
if let draft { Task { await personalStore.cancel(draft.id) } }
|
||||
}
|
||||
func openExistingPersonal(_ id: UUID) {
|
||||
cancelPersonalEdit(); query = LibraryBrowseQuery(scope: .personal); selection = "personal:" + id.uuidString.lowercased()
|
||||
}
|
||||
func removePersonal(_ entry: PersonalSoftwareEntry) {
|
||||
guard !personalSaving else { return }
|
||||
personalTask = Task {
|
||||
do {
|
||||
var hidden = entry; hidden.hidden = true
|
||||
try await personalStore.update(hidden); await refreshPersonalEntries()
|
||||
installationNotice = "Removed from Personal Library. Original and working files are retained on this Mac; no space was reclaimed."
|
||||
} catch { personalError = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
func personalDirectory(_ entry: PersonalSoftwareEntry) -> URL {
|
||||
state.directory.appendingPathComponent("PersonalLibrary").appendingPathComponent(entry.id.uuidString.lowercased())
|
||||
}
|
||||
func personalCoverPreview(_ entry: PersonalSoftwareEntry) async throws -> Data? {
|
||||
let draftID = personalDraft?.entry.id == entry.id ? personalDraft?.id : nil
|
||||
return try await personalStore.previewCover(entry, draftID: draftID)
|
||||
}
|
||||
func choosePersonalCover(for entry: PersonalSoftwareEntry) {
|
||||
guard canEditPersonal, personalEditor == nil else { return }
|
||||
let panel = NSOpenPanel()
|
||||
panel.title = "Choose Cover Image"; panel.prompt = "Use Image"
|
||||
panel.message = "Choose an image for this personal entry. Nothing is uploaded."
|
||||
panel.allowedContentTypes = [.image]
|
||||
panel.canChooseDirectories = false; panel.allowsMultipleSelection = false
|
||||
panel.begin { [weak self] response in
|
||||
guard response == .OK, let url = panel.url else { return }
|
||||
self?.replacePersonalCover(from: url, for: entry)
|
||||
}
|
||||
}
|
||||
func acceptPersonalCoverDrop(_ urls: [URL], for entry: PersonalSoftwareEntry) -> Bool {
|
||||
guard urls.count == 1, let url = urls.first, url.isFileURL,
|
||||
canEditPersonal, personalEditor == nil else { return false }
|
||||
replacePersonalCover(from: url, for: entry)
|
||||
return true
|
||||
}
|
||||
func selectPersonalCover(_ choice: PersonalSoftwareEntry.CoverChoice, for entry: PersonalSoftwareEntry) {
|
||||
changePersonalCover(entry, choice: choice, source: nil)
|
||||
}
|
||||
private func replacePersonalCover(from url: URL, for entry: PersonalSoftwareEntry) {
|
||||
changePersonalCover(entry, choice: nil, source: url)
|
||||
}
|
||||
private func changePersonalCover(_ entry: PersonalSoftwareEntry,
|
||||
choice: PersonalSoftwareEntry.CoverChoice?, source: URL?) {
|
||||
guard canEditPersonal, personalEditor == nil, selectedPersonalEntry?.id == entry.id else { return }
|
||||
personalSaving = true; personalError = nil
|
||||
personalTask = Task {
|
||||
defer { personalSaving = false; personalTask = nil }
|
||||
do {
|
||||
let png: Data?
|
||||
if let source {
|
||||
let worker = Task.detached(priority: .userInitiated) { try PersonalCoverImage.prepare(source) }
|
||||
png = try await withTaskCancellationHandler { try await worker.value } onCancel: { worker.cancel() }
|
||||
} else { png = nil }
|
||||
try Task.checkCancellation()
|
||||
guard selectedPersonalEntry?.id == entry.id,
|
||||
personalEntries.first(where: { $0.id == entry.id })?.updatedAt == entry.updatedAt else {
|
||||
throw PersonalSoftwareLibraryStore.Failure.invalid("The selected personal entry changed. Choose its cover again.")
|
||||
}
|
||||
var edited = entry
|
||||
if let choice { edited.cover = choice }
|
||||
try await personalStore.update(edited, coverPNG: png)
|
||||
await refreshPersonalEntries()
|
||||
installationNotice = "Cover image updated privately on this Mac. Your software and saved games are unchanged."
|
||||
} catch is CancellationError { }
|
||||
catch { personalError = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
private func refreshPersonalEntries() async {
|
||||
do {
|
||||
personalEntries = try await personalStore.entries()
|
||||
updateSoftwareTypes(catalog)
|
||||
let warnings = await personalStore.warnings
|
||||
personalError = warnings.isEmpty ? nil : warnings.joined(separator: "\n")
|
||||
filter()
|
||||
}
|
||||
catch { personalError = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
extension SoftwareLibraryModel {
|
||||
func restoreBrowseContext() {
|
||||
guard !restoredBrowse else { return }
|
||||
restoredBrowse = true
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
guard !isDiskCompanion, let saved = browseContinuity.browse(for: "software-main"),
|
||||
let encoded = saved.filters["browse-query"], let data = encoded.data(using: .utf8),
|
||||
let restored = try? JSONDecoder().decode(LibraryBrowseQuery.self, from: data) else { return }
|
||||
restoringBrowse = true
|
||||
query = restored; selection = saved.selectedID
|
||||
layout = saved.filters["layout"] == "Table" ? "Table" : "Shelf"
|
||||
browseTopID = saved.topVisibleID; browseOffset = saved.offset
|
||||
restoringBrowse = false
|
||||
#endif
|
||||
}
|
||||
func saveBrowseContext() {
|
||||
guard restoredBrowse, !restoringBrowse else { return }
|
||||
#if !VCC_COMPANION_CLIENT
|
||||
guard !isDiskCompanion, let encoded = try? JSONEncoder().encode(query), let json = String(data: encoded, encoding: .utf8) else { return }
|
||||
browseContinuity.setBrowse(.init(scope: query.scope.rawValue, query: query.text,
|
||||
filters: ["browse-query": json, "layout": layout], selectedID: selection,
|
||||
selectedEditionID: selectedRelease?.id, topVisibleID: browseTopID, offset: browseOffset), for: "software-main")
|
||||
#endif
|
||||
}
|
||||
func rememberBrowsePosition(id: String?, offset: Double) {
|
||||
guard browseTopID != id || abs(browseOffset - offset) > 0.01 else { return }
|
||||
browseTopID = id; browseOffset = max(0, min(offset.isFinite ? offset : 0, 1))
|
||||
saveBrowseContext()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user