// How far an intake has got, for the deck's top line ("41 of 212"): the share // drain and the pickers bring hundreds of files in at once, and the deck says // so while they come, then goes back to its own count. import CottonValues import Foundation import Observation @MainActor @Observable public final class MusicIntakeProgress { public nonisolated static let shared = MusicIntakeProgress() /// Made off the main actor: the share drain and the pickers say how far /// they have got from wherever they run. nonisolated init() {} /// Files brought in, of all of them; nil when nothing is coming in. public private(set) var counted: (done: Int, total: Int)? /// The tables whose decks say it: the songs and what they gather into. public static let tables: Set = ["song", "music_album", "artist"] /// Said from any thread: the deck reads it on the main one. public nonisolated func say(_ done: Int, of total: Int) { Task { @MainActor in self.counted = done >= total ? nil : (done, total) } } /// Nothing is coming in any more. public nonisolated func done() { Task { @MainActor in self.counted = nil } } /// The deck's top line while songs come in; nil when none do. public func line(table: String) -> String? { guard let counted, Self.tables.contains(table) else { return nil } return ShellLines.line("music", "intake.progress", ["done": String(counted.done), "total": String(counted.total)]) } }