71 lines
3.1 KiB
Swift
71 lines
3.1 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Foundation
|
|
import Combine
|
|
|
|
enum MediaDestination: Codable, Hashable, Sendable {
|
|
case systemROM, diskROM, binary, cassette, floppy(Int), hardDisk(Int), cartridge(Int)
|
|
var label: String {
|
|
switch self {
|
|
case .systemROM: return "CoCo 3 system ROM"
|
|
case .diskROM: return "Floppy controller ROM"
|
|
case .binary: return "Run binary program"
|
|
case .cassette: return "Cassette"
|
|
case .floppy(let n): return "Floppy FD\(n)"
|
|
case .hardDisk(let n): return "Hard disk HD\(n)"
|
|
case .cartridge(let n): return n == 0 ? "Direct cartridge" : "Multi-Pak slot \(n)"
|
|
}
|
|
}
|
|
var valid: Bool {
|
|
switch self {
|
|
case .floppy(let n): return (0..<4).contains(n)
|
|
case .hardDisk(let n): return (0..<2).contains(n)
|
|
case .cartridge(let n): return (0..<5).contains(n)
|
|
default: return true
|
|
}
|
|
}
|
|
static func choices(for url: URL) -> [Self] {
|
|
switch url.pathExtension.lowercased() {
|
|
case "vhd", "img": return (0..<2).map(Self.hardDisk)
|
|
case "os9": return (0..<4).map(Self.floppy) + (0..<2).map(Self.hardDisk)
|
|
case let ext where DiskFiles.extensions.contains(ext): return (0..<4).map(Self.floppy)
|
|
case "cas", "wav": return [.cassette]
|
|
case "ccc", "pak": return (0..<5).map(Self.cartridge)
|
|
case "rom": return [.systemROM, .diskROM] + (0..<5).map(Self.cartridge)
|
|
case "bin": return [.binary] + (0..<5).map(Self.cartridge)
|
|
default: return []
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
final class RecentMedia: ObservableObject {
|
|
struct Entry: Codable, Equatable, Identifiable {
|
|
var url: URL
|
|
var destination: MediaDestination
|
|
var id: String { url.absoluteString + "|" + destination.label }
|
|
var label: String { url.lastPathComponent + " — " + destination.label }
|
|
}
|
|
static func isLocalFile(_ url: URL) -> Bool {
|
|
url.isFileURL && (url.host == nil || url.host == "" || url.host == "localhost") && url.path.hasPrefix("/")
|
|
}
|
|
static let key = "RecentMedia.v1"
|
|
@Published private(set) var entries: [Entry] = []
|
|
private let defaults: UserDefaults
|
|
init(defaults: UserDefaults) {
|
|
self.defaults = defaults
|
|
if let data = defaults.data(forKey: Self.key), data.count < 262144,
|
|
let saved = try? JSONDecoder().decode([Entry].self, from: data) {
|
|
var seen = Set<String>()
|
|
entries = saved.filter { Self.isLocalFile($0.url) && $0.destination.valid && seen.insert($0.id).inserted }.prefix(12).map { $0 }
|
|
}
|
|
}
|
|
func record(_ url: URL, as destination: MediaDestination) {
|
|
guard Self.isLocalFile(url), destination.valid else { return }
|
|
let entry = Entry(url: url.standardizedFileURL, destination: destination)
|
|
entries.removeAll { $0.id == entry.id }; entries.insert(entry, at: 0)
|
|
entries = Array(entries.prefix(12)); save()
|
|
}
|
|
func clear() { entries = []; defaults.removeObject(forKey: Self.key) }
|
|
private func save() { if let data = try? JSONEncoder().encode(entries) { defaults.set(data, forKey: Self.key) } }
|
|
}
|