Skip to content

Commit e4f939a

Browse files
committed
feat(keyboard): add haptic feedback toggle in keyboard sub-settings #638
- Add hapticFeedback case to UserInteractiveState enum - Add 'Haptic feedback' toggle to Functionality section in languageSettingsData (keyboard sub-settings page, not main settings) - Handle save/load of langCode+HapticFeedback UserDefaults key in InfoChildTableViewCell (switchDidChange + fetchSwitchStateForCell) - Add hapticFeedbackIsEnabled() in KeyboardViewController reading from shared UserDefaults container - Guard haptic block behind hapticFeedbackIsEnabled() — off by default - Strings hard-coded for now pending Scribe-i18n entry
1 parent a22444f commit e4f939a

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

Keyboards/KeyboardsBase/KeyboardViewController.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,15 @@ class KeyboardViewController: UIInputViewController {
19821982
}
19831983
}
19841984

1985+
func hapticFeedbackIsEnabled() -> Bool {
1986+
let langCode = languagesAbbrDict[controllerLanguage] ?? "unknown"
1987+
if let userDefaults = UserDefaults(suiteName: "group.be.scri.userDefaultsContainer") {
1988+
let dictionaryKey = langCode + "HapticFeedback"
1989+
return userDefaults.bool(forKey: dictionaryKey)
1990+
}
1991+
return false // default off
1992+
}
1993+
19851994
// MARK: Button Actions
19861995

19871996
/// Triggers actions based on the press of a key.
@@ -2542,6 +2551,32 @@ class KeyboardViewController: UIInputViewController {
25422551
} else {
25432552
sender.backgroundColor = keyPressedColor
25442553
}
2554+
2555+
// Haptic feedback on key press.
2556+
// Heavy: destructive/dismiss actions.
2557+
// Medium: command, navigation, and action keys.
2558+
// Light: all regular character keys.
2559+
guard hapticFeedbackIsEnabled() else { return }
2560+
let heavyKeys = ["delete", "hideKeyboard"]
2561+
let mediumKeys = [
2562+
"shift", "return", "Translate", "Conjugate", "Plural",
2563+
"AutoAction0", "AutoAction1", "AutoAction2",
2564+
"EmojiKey0", "EmojiKey1", "EmojiKey2",
2565+
"GetAnnotationInfo", "ScribeAnnotation",
2566+
"shiftFormsDisplayLeft", "shiftFormsDisplayRight",
2567+
"ABC", "АБВ", "123", "#+=", ".?123", "selectKeyboard",
2568+
SpecialKeys.indent, SpecialKeys.capsLock,
2569+
spaceBar, languageTextForSpaceBar
2570+
]
2571+
let style: UIImpactFeedbackGenerator.FeedbackStyle
2572+
if heavyKeys.contains(originalKey) {
2573+
style = .heavy
2574+
} else if mediumKeys.contains(originalKey) {
2575+
style = .medium
2576+
} else {
2577+
style = .light
2578+
}
2579+
UIImpactFeedbackGenerator(style: style).impactOccurred()
25452580
}
25462581

25472582
/// Shows the conjugation view for verbs.

Scribe/ParentTableCellModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ enum UserInteractiveState {
7171
case toggleAccentCharacters
7272
case toggleWordForWordDeletion
7373
case increaseTextSize
74+
case hapticFeedback
7475
case none
7576
}
7677

Scribe/SettingsTab/SettingsTableData.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ enum SettingsTableData {
153153
value: "Delete text word by word when the delete key is pressed and held.",
154154
comment: ""
155155
)
156+
),
157+
Section(
158+
sectionTitle: "Haptic feedback",
159+
hasToggle: true,
160+
sectionState: .none(.hapticFeedback),
161+
shortDescription: "Enable haptic feedback on key press."
156162
)
157163
],
158164
hasDynamicData: nil

Scribe/Views/Cells/InfoChildTableViewCell/InfoChildTableViewCell.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ final class InfoChildTableViewCell: UITableViewCell {
158158
let dictionaryKey = languageCode + "WordForWordDeletion"
159159
userDefaults.setValue(toggleSwitch.isOn, forKey: dictionaryKey)
160160

161+
case .hapticFeedback:
162+
let dictionaryKey = languageCode + "HapticFeedback"
163+
userDefaults.setValue(toggleSwitch.isOn, forKey: dictionaryKey)
164+
161165
case .increaseTextSize:
162166
userDefaults.setValue(toggleSwitch.isOn, forKey: "increaseTextSize")
163167
initializeFontSize()
@@ -211,6 +215,14 @@ final class InfoChildTableViewCell: UITableViewCell {
211215
toggleSwitch.isOn = false // default off
212216
}
213217

218+
case .hapticFeedback:
219+
let dictionaryKey = languageCode + "HapticFeedback"
220+
if let toggleValue = userDefaults.object(forKey: dictionaryKey) as? Bool {
221+
toggleSwitch.isOn = toggleValue
222+
} else {
223+
toggleSwitch.isOn = false // default off
224+
}
225+
214226
case .increaseTextSize:
215227
if let toggleValue = userDefaults.object(forKey: "increaseTextSize") as? Bool {
216228
toggleSwitch.isOn = toggleValue

0 commit comments

Comments
 (0)