Cotton code
81 lines · 3.6 KB · SwiftRaw
import CottonKitimport CottonValuesimport SwiftUI/// The build line ([`BuildLine`]) as it is drawn: faint, one line, at the/// bottom of the Databases sheet and of This phone / This Mac; red while the/// two schemas differ. A tap copies it. Debug information the owner allowed/// in the app until the 1 Nov packaging (12 Oct 2026).struct BuildLineView: View {    @Environment(\.cottonCore) private var core    @Environment(\.theme) private var theme    @State private var schema: Int?    @State private var peer: BuildLine.Peer?    @State private var copied = false    private let build = BuildFacts.fromBundle()    /// The databases the sheet lists / the databases the Mac answered, said    /// for the device's reported state where the line stands on the sheet.    var databasesListed: String? = nil    private struct Facts: Decodable { let schemaVersion: Int }    private var line: BuildLine {        BuildLine(version: build.version, build: build.build, commit: build.commit, schema: schema, peer: peer)    }    var body: some View {        let line = line        Text(copied ? ShellLines.line("diagnostics", "copied") : line.text)            .font(.system(size: 10, design: .monospaced))            .foregroundStyle(line.mismatched ? theme.colors.error.color : theme.colors.inkTertiary.color)            // Wraps whole, never cut by an ellipsis: the commit at its end is            // the part a report needs (walker 291).            .multilineTextAlignment(.center)            .fixedSize(horizontal: false, vertical: true)            .frame(maxWidth: .infinity)            .padding(.vertical, 6)            .contentShape(Rectangle())            .onTapGesture {                UIPasteboard.general.string = line.text                copied = true            }            .accessibilityIdentifier("build-line")            .task { await listen() }            .task(id: databasesListed) {                if let databasesListed { Diagnostics.say(core, ["databasesListed": databasesListed]) }            }            .task(id: copied) {                guard copied else { return }                try? await Task.sleep(for: .seconds(1))                copied = false            }    }    /// The serving Mac as this device's own copy names it (the `copy` row    /// whose role is serving): a phone that holds a copy but has not heard    /// the Mac this run names that Mac, never "no Mac heard yet" (walker's    /// offline pass). Version and schema stay unsaid until the loop hears it.    private func servingCopy() async -> BuildLine.Peer? {        guard let core else { return nil }        let request: [String: RecordValue] = ["table": .text(HomeSync.table)]        guard let rows = try? await core.call(CottonMethod.recordsList, request, as: Rows.self) else { return nil }        let serving = rows.records.map(\.record).first { $0["role"].text == "serving" }        guard let name = serving?["name"].text else { return nil }        return BuildLine.Peer(name: name)    }    private struct Rows: Decodable { let records: [CoreRecords.WireRecord] }    /// The constants once, then what the loop last heard every few seconds    /// while the line is on screen.    private func listen() async {        guard let core else { return }        schema = (try? await core.call("build.facts", as: Facts.self))?.schemaVersion        while !Task.isCancelled {            let status = try? await core.callJSON(CottonMethod.readSyncStatus)            peer = BuildLine.Peer.of(syncStatus: status) ?? peer            if peer == nil { peer = await servingCopy() }            try? await Task.sleep(for: .seconds(5))        }    }}