Cotton code
119 lines · 5.9 KB · SwiftRaw
// Will this phone ring? The note that stands above the alarm list.//// The list STATES WHAT THE DEVICE WILL ACTUALLY DO, a three-way answer: it// rings loudly (say nothing — a thing that works needs no caption), it rings// quietly (one typed line about the silent switch), or it does not ring at all// (a pink note with the ONE control that fixes it). The third case is the bug// the note exists for: an alarm scheduled into a queue iOS would never present// looked exactly like one that was going to go off.//// It reads `AlarmSyncModel.gate` from the environment and asks for a pass with// `reconcile()` once the permission has been asked. The list itself is the// alarm table's (`TableListRoute(table: "alarm")`); this stands above it in// `AlarmListHead`, the view the alarm engine registers (`EngineListHead`).import CottonKitimport SwiftUIstruct AlarmDeliveryNotice: View {    /// The environment's, when the app mounted one. Optional on purpose: the    /// list still draws without the sync model (a gallery, a test), and the    /// note simply says nothing about ringing.    @Environment(AlarmSyncModel.self) private var alarms: AlarmSyncModel?    @Environment(\.cottonHaptics) private var haptics    @Environment(\.openURL) private var openURL    /// Nothing at all while the gate is `nil`: "we have not asked yet" and    /// "nothing will ring" are different, and warning about the second before    /// the first is a warning about a state nobody is in.    var body: some View {        if let gate = alarms?.gate {            if let silence = gate.silence {                Paper(dye: .pink, edge: .cut, seed: "alarms-silence", tilt: false, inset: 16) {                    VStack(alignment: .leading, spacing: 0) {                        // The headline states the CONSEQUENCE, not the cause:                        // "notifications are off" is a fact about iOS, and this                        // is a fact about tomorrow morning.                        Text(ShellLines.line("reminders", "alarms.these_alarms_will_not")).paperBody(15)                        Text(Self.sentence(silence))                            .paperBody(12.5, ink: .pencil)                            .lineSpacing(3)                            .fixedSize(horizontal: false, vertical: true)                            .padding(.top, 6)                        Button {                            haptics.select()                            act(on: silence)                        } label: {                            Text(Self.action(silence))                                .underline()                                .paperBody(14)                                .frame(minHeight: 44)                                .contentShape(Rectangle())                        }                        .buttonStyle(OpacityButtonStyle())                        .accessibilityIdentifier("alarms-silence-action")                        .padding(.top, 2)                    }                    .frame(maxWidth: .infinity, alignment: .leading)                }            } else if gate.backend == .notification {                // It works. The only thing left worth saying is how loud it is,                // and only when the answer is "less than the word alarm                // implies".                Text(ShellLines.line("reminders", "alarms.reminder_style_alarm_it"))                    .font(CottonFonts.type(12.5))                    .foregroundStyle(CottonColors.inkTertiary)            }        }    }    private static func sentence(_ silence: AlarmSilence) -> String {        switch silence {        case .switchedOff: return ShellLines.line("reminders", "alarms.you_turned_alarms_off")        case .permissionDenied: return ShellLines.line("reminders", "alarms.cotton_cannot_send_notifications")        case .permissionUndetermined: return ShellLines.line("reminders", "alarms.cotton_delivers_these_alarms")        }    }    private static func action(_ silence: AlarmSilence) -> String {        switch silence {        case .switchedOff: return ShellLines.line("reminders", "alarms.open_reminders")        case .permissionDenied: return ShellLines.line("reminders", "alarms.open_settings")        case .permissionUndetermined: return ShellLines.line("reminders", "alarms.allow_notifications")        }    }    /// The ONE control that fixes this particular silence. Offering the wrong    /// one is worse than offering none — a button that cannot work teaches    /// people the app is broken.    private func act(on silence: AlarmSilence) {        switch silence {        case .switchedOff:            // The person's own Alarms switch is the device page's            // Notifications row; the route is the handoff.            openURL(URL(string: Route.device.path)!)        case .permissionDenied:            if let url = URL(string: UIApplication.openSettingsURLString) { openURL(url) }        case .permissionUndetermined:            askForNotifications()        }    }    /// A moment where the question answers itself — somebody being told their    /// alarms will not ring knows exactly what they are being asked and why.    /// It is NEVER asked at mount: an alarm list somebody opened to look at is    /// not consent to spend the one prompt iOS ever grants.    private func askForNotifications() {        Task {            // THROUGH THE ONE AUTHORITY. The OS is asked in            // `SystemCapabilityAuthority` and nowhere else            // (`CapabilityGateSweepTests`), and this goes through            // `SystemNotificationAuthority` because that wrapper BROADCASTS:            // the reminder pass and the push-token hand-off both listen.            _ = await SystemNotificationAuthority().ask()            // The gate is re-read by the pass, so the note stops apologising            // the moment it is no longer true.            alarms?.reconcile()        }    }}