Repository Public License
1// ORIX Alarm2// File: App/Features/Insights/InsightsAdaptiveSection.swift3// Description: Plain-language explanation of the adaptive difficulty per mission, and optional suggestions the user may apply by editing an alarm.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910struct InsightsAdaptiveSection: View {11 @Environment(AppModel.self) private var model1213 var body: some View {14 let events = model.events15 let used = Set(events.filter { $0.kind == .missionStepCompleted }.compactMap(\.missionKind))16 let kinds = MissionKind.allCases.filter(used.contains)17 let suggestions = model.suggestions18 if !kinds.isEmpty {19 VStack(alignment: .leading, spacing: 14) {20 Overline("Adaptive difficulty")21 Text("For mission steps set to Adaptive, the level comes from your last five results of that mission. Steps with a fixed difficulty never change by themselves.")22 .font(.orixCaption).foregroundStyle(Theme.textSecondary)23 ForEach(kinds, id: \.self) { kind in24 let e = AdaptiveEngine.explain(kind: kind, history: events)25 HStack(alignment: .top, spacing: 12) {26 Image(systemName: kind.symbol).font(.callout.weight(.semibold)).foregroundStyle(kind.tint)27 .frame(width: 34, height: 34).background(Theme.surfaceRaised, in: RoundedRectangle(cornerRadius: 10, style: .continuous))28 .accessibilityHidden(true)29 VStack(alignment: .leading, spacing: 4) {30 HStack(spacing: 8) {31 Text(kind.title).font(.orixHeadline)32 Spacer(minLength: 0)33 LevelPips(level: e.level)34 }35 Text("Level \(e.level) of 4 (\(Difficulty.fixed(level: e.level).title)) · \(Self.trend(e.reason))")36 .font(.orixCaption).foregroundStyle(Theme.textPrimary)37 Text(Self.reason(e)).font(.orixCaption).foregroundStyle(Theme.textSecondary)38 }39 }40 .accessibilityElement(children: .combine)41 .accessibilityIdentifier("insights.adaptive.\(kind.rawValue)")42 }43 }44 .glassCard()45 }46 if !suggestions.isEmpty {47 VStack(alignment: .leading, spacing: 14) {48 Overline("Suggestions")49 Text("Optional ideas from the last two weeks. Nothing is changed unless you open the alarm and edit it yourself.")50 .font(.orixCaption).foregroundStyle(Theme.textSecondary)51 ForEach(suggestions, id: \.self) { suggestion in52 suggestionRow(suggestion)53 }54 }55 .glassCard(tint: Theme.violet)56 .accessibilityIdentifier("insights.suggestions")57 }58 }5960 // MARK: Explanation wording61 private static func trend(_ reason: AdaptiveEngine.Explanation.Reason) -> String {62 switch reason {63 case .notEnoughData: String(localized: "starting level")64 case .fastAndAccurate: String(localized: "one level up")65 case .slowOrInaccurate: String(localized: "one level down")66 case .steady: String(localized: "unchanged")67 }68 }6970 private static func reason(_ e: AdaptiveEngine.Explanation) -> String {71 switch e.reason {72 case .notEnoughData:73 String(localized: "\(e.sampleCount) of the 3 results needed are recorded. Until then, adaptive steps of this mission use Normal.")74 case .fastAndAccurate:75 String(localized: "Based on your last \(e.sampleCount) results: at least four were quick with at most one mistake, so the next adaptive step is one level harder than usual.")76 case .slowOrInaccurate:77 String(localized: "Based on your last \(e.sampleCount) results: at least three were slow or had three or more mistakes, so the next adaptive step is one level easier.")78 case .steady:79 String(localized: "Based on your last \(e.sampleCount) results: time and mistakes are in the comfortable range, so the level stays where you usually play.")80 }81 }8283 // MARK: Suggestions84 @ViewBuilder private func suggestionRow(_ suggestion: AdaptiveEngine.Suggestion) -> some View {85 switch suggestion {86 case .reduceSnoozes(let alarmID, let average):87 if let alarm = model.alarm(alarmID) {88 tip(symbol: "zzz", title: String(localized: "Fewer snoozes for \(name(alarm))?"),89 text: String(localized: "This alarm was snoozed \(String(format: "%.1f", average)) times per morning on average. You could lower its snooze limit or shorten the snooze."),90 alarm: alarm)91 }92 case .raiseDifficulty(let kind):93 tip(symbol: "arrow.up.right.circle.fill", title: String(localized: "\(kind.title) looks easy for you"),94 text: String(localized: "Your recent results were quick and accurate at its fixed difficulty. You could raise it, or set the step to Adaptive."),95 alarm: firstAlarm(withFixed: kind))96 case .lowerDifficulty(let kind):97 tip(symbol: "arrow.down.right.circle.fill", title: String(localized: "\(kind.title) looks hard right now"),98 text: String(localized: "Your recent results were slow or had several mistakes at its fixed difficulty. You could lower it, or set the step to Adaptive."),99 alarm: firstAlarm(withFixed: kind))100 }101 }102103 private func name(_ alarm: AlarmDefinition) -> String {104 alarm.name.isEmpty ? Fmt.timeString(alarm.time, use24h: model.settings.use24HourClock) : "“\(alarm.name)”"105 }106107 private func firstAlarm(withFixed kind: MissionKind) -> AlarmDefinition? {108 model.alarms.first { $0.scenario.missions.contains { $0.kind == kind && $0.difficulty != .adaptive } }109 }110111 private func tip(symbol: String, title: String, text: String, alarm: AlarmDefinition?) -> some View {112 VStack(alignment: .leading, spacing: 10) {113 HStack(alignment: .top, spacing: 12) {114 Image(systemName: symbol).font(.title3).foregroundStyle(Theme.gold).frame(width: 30).accessibilityHidden(true)115 VStack(alignment: .leading, spacing: 3) {116 Text(title).font(.orixHeadline)117 Text(text).font(.orixCaption).foregroundStyle(Theme.textSecondary)118 }119 }120 .accessibilityElement(children: .combine)121 if let alarm {122 Button {123 Haptics.play(.selection)124 model.editingAlarm = alarm125 } label: { Label("Open \(name(alarm))", systemImage: "slider.horizontal.3") }126 .buttonStyle(SecondaryButtonStyle())127 .accessibilityIdentifier("insights.suggestion.open")128 }129 }130 }131}132133/// Four pips showing a level from 1 to 4 (filled count, not colour, carries the value).134private struct LevelPips: View {135 let level: Int136 var body: some View {137 HStack(spacing: 4) {138 ForEach(1...4, id: \.self) { i in139 Capsule().fill(i <= level ? AnyShapeStyle(Theme.sunriseGradient) : AnyShapeStyle(Theme.surfaceRaised))140 .overlay { if i > level { Capsule().strokeBorder(Theme.hairline) } }141 .frame(width: 14, height: 7)142 }143 }144 .accessibilityHidden(true)145 }146}147