Cotton code
84 lines · 3.4 KB · SwiftRaw
// The model the core's plan names for hearing a meeting, offered on Meetings'// first screen while this phone lacks it (voice-meetings-finish). Until it is// here a meeting is read by the phone's own recogniser (MeetingsPhoneStep);// one tap fetches every file `voice.plan` lists into CottonVoice's shared// store (the App Group: a model fetched here serves every Cotton app), and the// next transcription uses it. The words are the plan's own lines.#if os(iOS)import CottonKitimport CottonVoiceimport SwiftUIstruct MeetingsModelOffer: View {    let plan: VoicePlan    @State private var store = VoiceModelStore.current    private var models: [String] {        var seen: [String] = []        for file in plan.downloads where !seen.contains(file.model) { seen.append(file.model) }        return seen    }    private var total: Int64 { plan.downloads.reduce(0) { $0 + $1.bytes } }    private var done: Int64 {        models.reduce(0) { sum, model in            sum + (store.states[model] == .ready ? plan.downloads.filter { $0.model == model }.reduce(0) { $0 + $1.bytes } : store.bytesDone[model] ?? 0)        }    }    private var state: VoiceModelState {        let states = models.map { store.states[$0] ?? .absent }        if states.allSatisfy({ $0 == .ready }) { return .ready }        if states.contains(.downloading) { return .downloading }        if states.contains(.failed) { return .failed }        return .absent    }    var body: some View {        HStack(spacing: 8) {            Text(verbatim: plan.downloads.first?.why ?? "")                .font(.footnote)                .frame(maxWidth: .infinity, alignment: .leading)            switch state {            case .downloading:                ProgressView(value: total > 0 ? min(1, Double(done) / Double(total)) : nil)                    .frame(width: 80)                    .accessibilityIdentifier("meetings-model-downloading")            case .ready:                Image(systemName: "checkmark")                    .accessibilityIdentifier("meetings-model-ready")            case .absent, .failed:                Button {                    Task {                        for model in models {                            await store.download(model, files: plan.downloads, mobileData: plan.mobileData)                        }                    }                } label: {                    Text(verbatim: line(state == .failed ? "failed" : "download"))                        .font(.footnote.bold())                }                .buttonStyle(.bordered)                .accessibilityIdentifier("meetings-model-download")            }        }        .padding(12)        .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 10))        .accessibilityElement(children: .combine)        .accessibilityIdentifier("meetings-model-offer")    }    private func line(_ key: String) -> String { plan.lines[key] ?? VoiceLines.line(key) }    /// The meeting plan's downloads, or nil when this phone holds every model it names.    @MainActor    static func plan(core: any CottonCalling) async -> VoicePlan? {        guard let plan = try? await core.call(            CottonMethod.voicePlan,            VoicePlanRequest(surface: "meeting", table: MeetingsTable.meeting, mode: .dictate),            as: VoicePlan.self), !plan.downloads.isEmpty else { return nil }        await VoiceModelStore.current.attach(core)        return plan    }}#endif