Cotton code
119 lines · 5.3 KB · SwiftRaw
// Where and when (wireframes 4): a scene's heading, from the bottom over the// scene. Where are we? with the places this script already uses offered, so// one place is never spelled two ways; inside or outside; the time of day;// one optional line that tells the scene apart on the home. Done keeps it.// A time chip is on when the stored time names its band (the core reads// `Dawn`, `dawn` or `EARLY DAWN` as the dawn band, `picker.band`).#if os(iOS)import SwiftUIstruct InkWhereWhen: View {    let model: InkSceneModel    let picker: StorySceneAnswer.Picker    @State private var place: String    @State private var setting: String?    @State private var band: String?    /// Whether a time chip was tapped: one not touched leaves the stored time as it is.    @State private var timeTouched = false    @State private var summary: String    @FocusState private var placeFocused: Bool    @FocusState private var summaryFocused: Bool    @Environment(\.closeCard) private var closeCard    /// Closes the panel this stands in.    private func dismiss() { closeCard?() }    init(model: InkSceneModel, answer: StorySceneAnswer) {        self.model = model        picker = answer.picker        _place = State(initialValue: answer.picker.place)        _setting = State(initialValue: answer.picker.setting)        _band = State(initialValue: answer.picker.band)        _summary = State(initialValue: answer.picker.summary ?? "")    }    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 22) {                InkSheetTop(title: picker.title, close: InkWords.line("home.close")) { dismiss() }                InkField(hint: picker.hint, text: $place, focused: $placeFocused, font: InkType.title(22, relativeTo: .title2)) {                    placeFocused = false                }                if !offered.isEmpty {                    VStack(alignment: .leading, spacing: 10) {                        if let used = picker.usedLabel {                            Text(used)                                .font(InkType.caption)                                .foregroundStyle(Ink.quiet)                        }                        InkFlow(spacing: 8) {                            ForEach(offered, id: \.self) { name in                                choiceChip(name, on: same(name, place)) { place = name }                            }                        }                    }                }                if let settings = picker.settings, !settings.isEmpty {                    InkFlow(spacing: 8) {                        ForEach(settings) { one in                            choiceChip(one.word, on: setting == one.setting) { setting = setting == one.setting ? nil : one.setting }                        }                    }                }                InkFlow(spacing: 8) {                    ForEach(picker.times) { time in                        choiceChip(time.word, on: band == time.band) {                            timeTouched = true                            band = band == time.band ? nil : time.band                        }                    }                }                InkField(hint: picker.summaryHint ?? "", text: $summary, focused: $summaryFocused, font: InkType.line) {                    summaryFocused = false                }                HStack {                    Spacer()                    InkButton(words: picker.done) { done() }                        .disabled(place.trimmingCharacters(in: .whitespaces).isEmpty)                        .opacity(place.trimmingCharacters(in: .whitespaces).isEmpty ? 0.4 : 1)                }            }            .padding(.horizontal, Ink.gutter)            .padding(.top, 22)            .padding(.bottom, 26)        }        .scrollIndicators(.hidden)        .onAppear { if place.isEmpty { placeFocused = true } }    }    /// The places offered: those used, the typed letters narrowing them.    private var offered: [String] {        let all = picker.used ?? []        let typed = place.trimmingCharacters(in: .whitespaces)        guard !typed.isEmpty, !all.contains(where: { same($0, typed) }) else { return all }        return all.filter { $0.localizedCaseInsensitiveContains(typed) }    }    private func same(_ one: String, _ other: String) -> Bool {        one.trimmingCharacters(in: .whitespaces).localizedCaseInsensitiveCompare(other.trimmingCharacters(in: .whitespaces)) == .orderedSame    }    private func choiceChip(_ words: String, on: Bool, tap: @escaping () -> Void) -> some View {        Button(action: tap) {            InkChip(on: on) { Text(words) }                .font(InkType.pill)        }        .buttonStyle(InkPress())        .accessibilityAddTraits(on ? .isSelected : [])    }    private func done() {        let named = place.trimmingCharacters(in: .whitespacesAndNewlines)        guard !named.isEmpty else { return }        // The time is written as the word shown (`Dawn`), as Android writes it; an        // untouched choice leaves the stored time as it was (`CONTINUOUS` stays).        let time: String? = timeTouched ? (picker.times.first { $0.band == band }?.word ?? "") : nil        Task { await model.setPlace(setting: setting, place: named, time: time, summary: summary.trimmingCharacters(in: .whitespacesAndNewlines)) }    }}#endif