mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Step 5 of the phone side (design §7), behind Settings › Experiments › Typing configs. - Counts (HEXShared/TypingLog/TypingConfigHealth.swift): per config and for Hex defaults, words finished, autocorrections, undone, kept as typed, fixed by hand, suggestions picked, from two hooks in Learn how I type's journal (TypingJournal.didFinish and record); no words kept. - Comparison: Apply records the config in use before it. "Seems worse" only with 400 words on each side, twice the trouble rate and at least 8 troubles. Best so far marks the lowest rate once two configs have 400 words. - Telling the person, at most once per config and never after Go back, Keep this one or Turn typing configs off: the keyboard's bar notice (Open Hex, ✕ for good; FullKeyboardView+TypingConfigNotice, a few one-line hooks in FullKeyboardView), then a quiet notification from the keyboard a day later if the notice wasn't acted on, and a Home card. Settings › Keyboard › Typing configs gains Alerts (off: no bar notice, no notification) and Compare with the one before. - The comparison sheet (hex-dictation://typing-configs/compare, from the notice, the notification or the card): each side's rate, words and counts; Go back, Keep this one, Turn typing configs off. - Notification permission asked on the applied sheet, the first time a config is applied, with the reason and Hex's own buttons before the system's prompt. - Start-up fallback (TypingConfigStartupGuard): the keyboard marks a start in viewDidLoad and clears it in viewDidAppear; after two starts that never came up it starts without the config, and the app goes back to the one before and says so on Home. - Review wording no longer promises a tap: zones "count for the key more often", the next-letter guess "can still pick its neighbour"; each zone says how far its touch area reaches. - Strings in English and Simplified Chinese; docs/typing-configs.md and the feature map. Verified on the Studio (iOS 27.2): TypingLogTests 17 of 17 (the comparison rule, the start-up guard); every state on iPhone 17 Pro and iPhone SE in English and 简体中文 with seeded counts (420 words, 30 troubles against 460 and 12), a backdated bar notice and a seeded start mark; the notification shown as a banner over Files and on the lock screen; code-shape.py --check.
169 lines
7 KiB
Swift
169 lines
7 KiB
Swift
import SwiftUI
|
||
|
||
/// A typing config's review in the sheet a file opens: its parts (`TypingConfigParts`), Needs
|
||
/// Full Access before Apply, which kept config a new one replaces at the limit, then Apply or
|
||
/// Not now. A rendering of `TypingConfigReview`, which decides everything shown here.
|
||
struct TypingConfigReviewView: View {
|
||
let arrival: TypingConfigArrival
|
||
let data: Data
|
||
/// Done with this file: the sheet shows the next one, or closes.
|
||
let done: () -> Void
|
||
@State private var review: TypingConfigReview
|
||
@State private var partsOff: Set<String>
|
||
@State private var appliedNow = false
|
||
@State private var failure: String?
|
||
@State private var asksNotifications = false
|
||
private let existing: TypingConfigEntry?
|
||
private let isActive: Bool
|
||
/// The kept config keeping this one would remove (`TypingConfigHistory.limit`).
|
||
private let replaces: TypingConfigEntry?
|
||
private let fullAccessOn: Bool
|
||
|
||
/// `keyboards`: the keyboards Hex's keys have run on (`TypingConfigSettings.keyboards`).
|
||
init(arrival: TypingConfigArrival, data: Data, keyboards: [TypingConfigKeyboard], fullAccessOn: Bool, done: @escaping () -> Void) {
|
||
self.arrival = arrival
|
||
self.data = data
|
||
self.done = done
|
||
self.fullAccessOn = fullAccessOn
|
||
let review = TypingConfigReview(data: data, keyboards: keyboards)
|
||
let history = TypingConfigHistory()
|
||
existing = history.entry(review.contentHash)
|
||
isActive = history.activeID == review.contentHash
|
||
replaces = history.removedByAdding(review.contentHash)
|
||
_review = State(initialValue: review)
|
||
_partsOff = State(initialValue: Set(history.entry(review.contentHash)?.partsOff ?? []))
|
||
}
|
||
|
||
private var name: String { existing?.name ?? arrival.name }
|
||
|
||
var body: some View {
|
||
if let refused = review.refused {
|
||
refusedView(refused)
|
||
} else if appliedNow {
|
||
appliedView
|
||
} else {
|
||
reviewView
|
||
}
|
||
}
|
||
|
||
private var canApply: Bool {
|
||
fullAccessOn && TypingConfigParts.switchedOn(review, partsOff: partsOff) > 0
|
||
}
|
||
|
||
private var reviewView: some View {
|
||
VStack(alignment: .leading, spacing: 12) {
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
TypingConfigSheetHeader(title: name, close: notNow)
|
||
Text(subtitle)
|
||
.font(.system(size: 13))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
|
||
if !fullAccessOn {
|
||
TypingConfigFullAccessCard()
|
||
}
|
||
|
||
TypingConfigParts(review: review, partsOff: $partsOff)
|
||
|
||
TypingConfigParts.plainCard(String(localized: "Hex can't check this config on your typing yet. Once it's on, Hex watches how your typing goes and tells you if it seems worse. You can go back in Settings › Keyboard › Typing configs."))
|
||
.padding(.top, 4)
|
||
|
||
if let replaces {
|
||
Text("Hex keeps \(TypingConfigHistory.limit) typing configs. Keeping this one removes “\(replaces.name)”, the oldest one you're not using.")
|
||
.font(.system(size: 13))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
if let failure {
|
||
Text(failure)
|
||
.font(.system(size: 13))
|
||
.foregroundStyle(HexTheme.danger)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
|
||
VStack(spacing: 4) {
|
||
OnboardingPrimaryButton(title: isActive ? "Apply again" : "Apply", enabled: canApply, action: apply)
|
||
OnboardingQuietButton(title: "Not now", action: notNow)
|
||
}
|
||
.padding(.top, 8)
|
||
}
|
||
}
|
||
|
||
private var subtitle: String {
|
||
var parts: [String] = [String(localized: "Typing config")]
|
||
if let days = review.file?.madeFrom?.logDays, !days.isEmpty {
|
||
parts.append(days.count == 1
|
||
? String(localized: "made from 1 day of typing")
|
||
: String(localized: "made from \(days.count) days of typing"))
|
||
}
|
||
if isActive {
|
||
parts.append(String(localized: "the one you use now"))
|
||
} else if existing != nil {
|
||
parts.append(String(localized: "opened before"))
|
||
}
|
||
return parts.joined(separator: " · ")
|
||
}
|
||
|
||
// MARK: - Choices
|
||
|
||
/// Keeps the file in the history, unapplied (or as it was, if it was kept before).
|
||
private func notNow() {
|
||
if review.refused == nil {
|
||
let history = TypingConfigHistory()
|
||
if case .added(let entry) = history.add(data, hash: review.contentHash, name: arrival.name), !partsOff.isEmpty {
|
||
history.setPartsOff(entry.id, partsOff.sorted())
|
||
}
|
||
}
|
||
done()
|
||
}
|
||
|
||
private func apply() {
|
||
let history = TypingConfigHistory()
|
||
let entry: TypingConfigEntry
|
||
switch history.add(data, hash: review.contentHash, name: arrival.name) {
|
||
case .added(let added): entry = added
|
||
case .existing(let kept): entry = kept
|
||
case .failed:
|
||
failure = String(localized: "Hex couldn't save this config. Nothing has changed.")
|
||
return
|
||
}
|
||
do {
|
||
try TypingConfigActivation.apply(entry, review: review, partsOff: partsOff, history: history)
|
||
appliedNow = true
|
||
} catch {
|
||
failure = String(localized: "Hex couldn't save this config. Nothing has changed.")
|
||
}
|
||
}
|
||
|
||
// MARK: - After
|
||
|
||
private var appliedView: some View {
|
||
VStack(alignment: .leading, spacing: 16) {
|
||
TypingConfigSheetHeader(title: String(localized: "Typing config applied"), close: done)
|
||
Text("“\(name)” takes effect the next time the keyboard opens. To go back, open Settings › Keyboard › Typing configs.")
|
||
.font(.system(size: 15))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
// Asked here, the first time a config is applied, never at first launch.
|
||
if asksNotifications {
|
||
TypingConfigNotificationAsk { asksNotifications = false }
|
||
}
|
||
OnboardingPrimaryButton(title: "Done", action: done)
|
||
.padding(.top, 4)
|
||
}
|
||
.task { asksNotifications = await TypingConfigWatch.shouldAsk() }
|
||
}
|
||
|
||
private func refusedView(_ reason: TypingConfigReview.Reason) -> some View {
|
||
VStack(alignment: .leading, spacing: 16) {
|
||
TypingConfigSheetHeader(title: String(localized: "This isn't a typing config"), close: done)
|
||
Text("\(reason.sentence) “\(arrival.name)” wasn't opened, and nothing has changed.")
|
||
.font(.system(size: 15))
|
||
.foregroundStyle(HexTheme.textMuted)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
OnboardingPrimaryButton(title: "Close", action: done)
|
||
.padding(.top, 4)
|
||
}
|
||
}
|
||
}
|