Cotton code
46 lines · 2.0 KB · SwiftRaw
// How far a films' intake has got, for the deck's top line ("Reading 12// files", then "41 of 212"): a folder of a series brings hundreds of files in// at once, and the deck says so while they come, then goes back to its own// count. As Music's (`MusicIntakeProgress`).import CottonValuesimport Foundationimport Observation@MainActor @Observablepublic final class FilmIntakeProgress {    public nonisolated static let shared = FilmIntakeProgress()    /// Made off the main actor: the share drain says how far it has got from    /// wherever it runs.    nonisolated init() {}    /// Files brought in, of all of them, and whether the first of them are    /// still being read; nil when nothing is coming in.    public private(set) var counted: (done: Int, total: Int, reading: Bool)?    /// The tables whose decks say it: the films and what they gather into.    public static let tables: Set<String> = ["film", "film_season", "film_episode"]    /// Said from any thread: the deck reads it on the main one.    public nonisolated func say(_ done: Int, of total: Int, reading: Bool) {        Task { @MainActor in self.counted = done >= total ? nil : (done, total, reading) }    }    /// Nothing is coming in any more.    public nonisolated func done() {        Task { @MainActor in self.counted = nil }    }    /// The deck's top line while films come in; nil when none do.    public func line(table: String) -> String? {        guard let counted, Self.tables.contains(table) else { return nil }        return Self.line(counted.done, of: counted.total, reading: counted.reading)    }    /// "Reading 12 files" before the first is in, then "41 of 212".    public nonisolated static func line(_ done: Int, of total: Int, reading: Bool) -> String {        if reading, done == 0 { return ShellLines.line("cinemas", "intake.reading", ["count": String(total)]) }        return ShellLines.line("cinemas", "intake.progress", ["done": String(done), "total": String(total)])    }}