Skip to content

Commit bc5643b

Browse files
noahweidigykisana
andcommitted
feat: add haptic feedback when blocking and unblocking
Play a medium impact haptic after a block or unblock goes through: Home, group cards, links and widgets, Shortcuts and emergency unblock. It plays from BlockActions after the change succeeds, so a failed unblock or a cancelled key check plays nothing. Home's Block All Now now goes through BlockActions too. Settings > Haptic Feedback turns it off; it's on by default. Co-authored-by: ykisana <ykisana@protonmail.com>
1 parent 150a21f commit bc5643b

9 files changed

Lines changed: 53 additions & 11 deletions

File tree

‎Normal/Models/Settings.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class Settings {
1414
var blockAllPreventsAppDelete: Bool = true
1515
var defaultTab: AppTab?
1616
var hideDonateButton: Bool = false
17+
var hapticFeedback: Bool = true
1718
var showTimedUnblockLiveActivity: Bool = false
1819
var enableCustomDomains: Bool = false
1920
var skipBlockWithoutKeyConfirmation: Bool = false

‎Normal/Resources/Localizable.xcstrings‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,10 @@
836836
"comment" : "A bullet point in the \"Pick one option for the passcode and Apple ID\" section of the SettingsBypassGuide view.",
837837
"isCommentAutoGenerated" : true
838838
},
839+
"Haptic Feedback" : {
840+
"comment" : "A toggle that turns the haptic played after blocking or unblocking on or off.",
841+
"isCommentAutoGenerated" : true
842+
},
839843
"Head to our GitHub repository to get started, check out open issues, or submit a pull request." : {
840844

841845
},

‎Normal/Services/BlockActions.swift‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import FamilyControls
12
import Foundation
3+
import UIKit
24

35
enum BlockActions {
46
static func blockAll(
@@ -13,6 +15,23 @@ enum BlockActions {
1315
customDomains: settings.enableCustomDomains ? mainSelection.customDomains : [],
1416
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
1517
)
18+
playHaptic(settings)
19+
}
20+
21+
static func blockAllNow(
22+
selection: FamilyActivitySelection,
23+
customDomains: [String],
24+
settings: Settings,
25+
screenTimeService: any ScreenTimeProviding,
26+
timedUnblockService: TimedUnblockService
27+
) {
28+
timedUnblockService.cancelMain(
29+
selection: selection,
30+
customDomains: settings.enableCustomDomains ? customDomains : [],
31+
screenTimeService: screenTimeService,
32+
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
33+
)
34+
playHaptic(settings)
1635
}
1736

1837
static func blockGroup(
@@ -26,6 +45,7 @@ enum BlockActions {
2645
timedUnblockService.cancelGroup(groupId: group.id)
2746
}
2847
screenTimeService.block(selection: group.selection, customDomains: customDomains)
48+
playHaptic(settings)
2949
}
3050

3151
@discardableResult
@@ -36,22 +56,26 @@ enum BlockActions {
3656
screenTimeService: any ScreenTimeProviding,
3757
timedUnblockService: TimedUnblockService
3858
) throws -> UnblockTiming {
59+
let timing: UnblockTiming
3960
switch length {
4061
case let .timed(duration):
41-
return try timedUnblockService.start(
62+
timing = try timedUnblockService.start(
4263
grant,
4364
duration: duration,
4465
settings: settings,
4566
screenTimeService: screenTimeService
4667
)
4768
case .permanent:
4869
screenTimeService.unblockPermanently(grant, settings: settings)
49-
return .exact
70+
timing = .exact
5071
}
72+
playHaptic(settings)
73+
return timing
5174
}
5275

5376
static func emergencyUnblock(
5477
schedules: [BlockSchedule],
78+
settings: Settings,
5579
screenTimeService: any ScreenTimeProviding,
5680
timedUnblockService: TimedUnblockService,
5781
scheduleService: ScheduleService,
@@ -61,6 +85,12 @@ enum BlockActions {
6185
scheduleService.disableAll(schedules, screenTimeService: screenTimeService)
6286
usageLimitService.overrideToday()
6387
screenTimeService.removeAllRestrictions()
88+
playHaptic(settings)
89+
}
90+
91+
private static func playHaptic(_ settings: Settings) {
92+
guard settings.hapticFeedback else { return }
93+
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
6494
}
6595

6696
static func validate(isAuthorized: Bool, hasCompletedOnboarding: Bool, keys: [Key]) throws {

‎Normal/Views/BlockUnblockViews/TimedUnlock/TimedUnlockBannerView.swift‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ struct TimedUnblockBannerView: View {
1515
allSettings.unwrapped
1616
}
1717

18-
private var effectiveCustomDomains: [String] {
19-
settings.enableCustomDomains ? customDomains : []
20-
}
21-
2218
var body: some View {
2319
if let endDate = timedUnblockService.mainUnblockEndDate, endDate > .now {
2420
Section {
@@ -42,11 +38,12 @@ struct TimedUnblockBannerView: View {
4238
tint: .orange
4339
) {
4440
authAction = .tighten {
45-
timedUnblockService.cancelMain(
41+
BlockActions.blockAllNow(
4642
selection: selection,
47-
customDomains: effectiveCustomDomains,
43+
customDomains: customDomains,
44+
settings: settings,
4845
screenTimeService: screenTimeService,
49-
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
46+
timedUnblockService: timedUnblockService
5047
)
5148
}
5249
}

‎Normal/Views/Settings/GeneralSettingsView.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ struct GeneralSettingsView: View {
134134
"Hide Donate Button",
135135
isOn: Bindable(settings).hideDonateButton
136136
)
137+
138+
Toggle(
139+
"Haptic Feedback",
140+
isOn: Bindable(settings).hapticFeedback
141+
)
137142
} header: {
138143
Text("Appearance")
139144
}

‎Normal/Views/Settings/SettingsView.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct SettingsView: View {
117117
emergencyUnblockService.record(into: settings)
118118
BlockActions.emergencyUnblock(
119119
schedules: schedules,
120+
settings: settings,
120121
screenTimeService: screenTimeService,
121122
timedUnblockService: timedUnblockService,
122123
scheduleService: scheduleService,

‎NormalTests/Models/StoreUpgradeTests.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ struct StoreUpgradeTests {
392392
#expect(!settings.skipBlockWithoutKeyConfirmation)
393393
#expect(settings.dailyLimitResetMinutes == 0)
394394
#expect(settings.unblockDurations == TimedUnblockDuration.presets)
395+
#expect(settings.hapticFeedback)
395396
}
396397

397398
@Test func version15StoreKeepsDurationsResetTimeAndDailyLimits() throws {

‎NormalTests/Services/BlockActionsTests.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct BlockActionsTests {
169169
) {
170170
BlockActions.emergencyUnblock(
171171
schedules: schedules,
172+
settings: Settings(),
172173
screenTimeService: screenTime,
173174
timedUnblockService: timedUnblock ?? makeTimedUnblock(),
174175
scheduleService: makeSchedule(),

‎NormalTests/TestSupport/BlockingHarness.swift‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,19 @@ final class BlockingHarness {
150150
}
151151

152152
func blockAllNow() {
153-
timedUnblock.cancelMain(
153+
BlockActions.blockAllNow(
154154
selection: main.selection,
155155
customDomains: main.customDomains,
156+
settings: settings,
156157
screenTimeService: screenTime,
157-
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
158+
timedUnblockService: timedUnblock
158159
)
159160
}
160161

161162
func emergencyUnblock() {
162163
BlockActions.emergencyUnblock(
163164
schedules: schedules,
165+
settings: settings,
164166
screenTimeService: screenTime,
165167
timedUnblockService: timedUnblock,
166168
scheduleService: scheduleService,

0 commit comments

Comments
 (0)