Cotton code
54 lines · 2.3 KB · SwiftRaw
// `/bring-in` — Settings › Bring in: the share sheet's door, opened from inside// the app. The files chosen in Files are staged into the app group exactly as// the share extension stages a share, and handed to the SAME drain// (`ShareIntake.intake`), so a photo, a clip, a recording and any other file// land as they would have through the share sheet: the row is written with its// bytes in the store, and the app opens what was made.import CottonKitimport CottonValuesimport SwiftUIstruct BringInRoute: View {    let shares: ShareIntake    @Environment(\.theme) private var theme    @Environment(ToastCenter.self) private var toasts    @State private var picking = false    @State private var adding = false    /// The words the screen says, one place.    enum Words {        static var title: String { ShellLines.line("databases", "bring_in.title") }        static var lead: String { ShellLines.line("databases", "bring_in.lead") }        static var choose: String { ShellLines.line("databases", "bring_in.choose") }        static var adding: String { ShellLines.line("databases", "bring_in.adding") }        static var unreadable: String { ShellLines.line("databases", "bring_in.unreadable") }    }    var body: some View {        PaperScreen(title: Words.title) {            VStack(alignment: .leading, spacing: RecordListSurfaceKnobs.paperGap) {                Text(adding ? Words.adding : Words.lead)                    .paperBody(15, ink: .pencil)                    .frame(maxWidth: .infinity, alignment: .leading)                PaperTypedButton(Words.choose) { picking = true }                    .accessibilityIdentifier("bring-in-choose")            }            .disabled(adding)        }        .fileImporter(isPresented: $picking, allowedContentTypes: [.item], allowsMultipleSelection: true) { result in            guard case .success(let urls) = result else { return }            Task { await bring(urls) }        }    }    /// Stage each chosen file and hand them all to the drain, which files each    /// one and opens what it made.    private func bring(_ urls: [URL]) async {        adding = true        defer { adding = false }        let staged = stageForIntake(urls, at: CottonDate.format(Date()))        if staged.count < urls.count { toasts.show(Words.unreadable, tone: .error) }        await shares.intake(staged)    }}