SPB Git

Repository Public License

RoutineEditorView.swift 4.9 KB 98 lines swift

1// ORIX Alarm2// File: App/Features/Sleep/RoutineEditorView.swift3// Description: Editor of the bedtime routine checklist: add, rename, pick a symbol, delete and reorder items.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910struct RoutineEditorView: View {11    @Environment(AppModel.self) private var model12    @Environment(\.dismiss) private var dismiss13    @State private var items: [SleepPlan.RoutineItem] = []14    @State private var loaded = false15    @FocusState private var focused: UUID?1617    static let maxItems = 1218    /// Symbols offered for routine items, each with a spoken name.19    static let symbols: [(symbol: String, name: String)] = [20        ("lightbulb.min", "Dim light"), ("battery.100.bolt", "Charger"), ("tshirt", "Clothes"), ("drop", "Water"),21        ("book", "Book"), ("iphone.slash", "No phone"), ("cup.and.saucer", "Warm drink"), ("shower", "Shower"),22        ("mouth", "Teeth"), ("figure.mind.and.body", "Stretch"), ("pencil.and.list.clipboard", "Journal"), ("alarm", "Alarm"),23        ("thermometer.snowflake", "Cool room"), ("window.shade.closed", "Blinds"), ("pills", "Medication"), ("lock", "Lock up"),24        ("pawprint", "Pet"), ("bag", "Bag"), ("headphones", "Audio"), ("moon.stars", "Moon"),25    ]2627    var body: some View {28        NavigationStack {29            List {30                Section {31                    ForEach($items) { $item in32                        HStack(spacing: 12) {33                            symbolMenu($item)34                            TextField("Routine item", text: $item.title)35                                .font(.orixBody).submitLabel(.done).focused($focused, equals: item.id)36                                .accessibilityIdentifier("sleep.routine.title")37                        }38                        .frame(minHeight: 44)39                        .listRowBackground(Theme.surface)40                    }41                    .onDelete { items.remove(atOffsets: $0) }42                    .onMove { items.move(fromOffsets: $0, toOffset: $1) }43                } footer: {44                    Text("Drag the handles to reorder, swipe or use the red button to delete. Tonight's checks are kept for the items that remain.")45                        .font(.orixCaption).foregroundStyle(Theme.textSecondary)46                }47                Section {48                    Button {49                        let item = SleepPlan.RoutineItem(title: "", symbol: "moon.stars")50                        items.append(item); focused = item.id51                        Haptics.play(.selection)52                    } label: { Label("Add an item", systemImage: "plus.circle.fill").font(.orixHeadline).frame(minHeight: 44) }53                    .disabled(items.count >= Self.maxItems)54                    .listRowBackground(Theme.surface)55                    .accessibilityIdentifier("sleep.routine.add")56                    Button("Restore the suggested routine") { items = SleepPlan.defaultRoutine; Haptics.play(.selection) }57                        .font(.orixBody).frame(minHeight: 44).listRowBackground(Theme.surface)58                }59            }60            .environment(\.editMode, .constant(.active))61            .tint(Theme.sunrise)62            .navigationTitle("Bedtime routine")63            .navigationBarTitleDisplayMode(.inline)64            .toolbar {65                ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }66                ToolbarItem(placement: .confirmationAction) {67                    Button("Done") { save(); dismiss() }.accessibilityIdentifier("sleep.routine.done")68                }69            }70            .orixScreen()71        }72        .onAppear { if !loaded { items = model.sleepPlan.routine; loaded = true } }73    }7475    private func symbolMenu(_ item: Binding<SleepPlan.RoutineItem>) -> some View {76        Menu {77            ForEach(Self.symbols, id: \.symbol) { option in78                Button { item.wrappedValue.symbol = option.symbol } label: { Label(option.name, systemImage: option.symbol) }79            }80        } label: {81            Image(systemName: item.wrappedValue.symbol).font(.body.weight(.semibold)).foregroundStyle(Theme.gold)82                .frame(width: 44, height: 44).background(Theme.surfaceRaised, in: RoundedRectangle(cornerRadius: 12, style: .continuous))83        }84        .accessibilityLabel("Symbol")85        .accessibilityValue(Self.symbols.first { $0.symbol == item.wrappedValue.symbol }?.name ?? item.wrappedValue.symbol)86    }8788    /// Items left without a title are dropped rather than saved blank.89    private func save() {90        let cleaned = items.compactMap { item -> SleepPlan.RoutineItem? in91            let title = item.title.trimmingCharacters(in: .whitespacesAndNewlines)92            return title.isEmpty ? nil : SleepPlan.RoutineItem(id: item.id, title: String(title.prefix(60)), symbol: item.symbol)93        }94        guard cleaned != model.sleepPlan.routine else { return }95        model.updateSleepPlan { $0.routine = cleaned }96    }97}98
Files main
Menu

Theme