Skip to content

Commit af6e032

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 uses the same path as Block All. Settings > Haptic Feedback turns it off; it's on by default. Co-authored-by: ykisana <ykisana@protonmail.com>
1 parent 150a21f commit af6e032

13 files changed

Lines changed: 35 additions & 75 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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import UIKit
23

34
enum BlockActions {
45
static func blockAll(
@@ -13,6 +14,7 @@ enum BlockActions {
1314
customDomains: settings.enableCustomDomains ? mainSelection.customDomains : [],
1415
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
1516
)
17+
playHaptic(settings)
1618
}
1719

1820
static func blockGroup(
@@ -26,6 +28,7 @@ enum BlockActions {
2628
timedUnblockService.cancelGroup(groupId: group.id)
2729
}
2830
screenTimeService.block(selection: group.selection, customDomains: customDomains)
31+
playHaptic(settings)
2932
}
3033

3134
@discardableResult
@@ -36,22 +39,26 @@ enum BlockActions {
3639
screenTimeService: any ScreenTimeProviding,
3740
timedUnblockService: TimedUnblockService
3841
) throws -> UnblockTiming {
42+
let timing: UnblockTiming
3943
switch length {
4044
case let .timed(duration):
41-
return try timedUnblockService.start(
45+
timing = try timedUnblockService.start(
4246
grant,
4347
duration: duration,
4448
settings: settings,
4549
screenTimeService: screenTimeService
4650
)
4751
case .permanent:
4852
screenTimeService.unblockPermanently(grant, settings: settings)
49-
return .exact
53+
timing = .exact
5054
}
55+
playHaptic(settings)
56+
return timing
5157
}
5258

5359
static func emergencyUnblock(
5460
schedules: [BlockSchedule],
61+
settings: Settings,
5562
screenTimeService: any ScreenTimeProviding,
5663
timedUnblockService: TimedUnblockService,
5764
scheduleService: ScheduleService,
@@ -61,6 +68,12 @@ enum BlockActions {
6168
scheduleService.disableAll(schedules, screenTimeService: screenTimeService)
6269
usageLimitService.overrideToday()
6370
screenTimeService.removeAllRestrictions()
71+
playHaptic(settings)
72+
}
73+
74+
private static func playHaptic(_ settings: Settings) {
75+
guard settings.hapticFeedback else { return }
76+
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
6477
}
6578

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

‎Normal/Services/TimedUnblockService.swift‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,6 @@ final class TimedUnblockService {
116116
return timing
117117
}
118118

119-
func cancelMain(
120-
selection: FamilyActivitySelection,
121-
customDomains: [String] = [],
122-
screenTimeService: any ScreenTimeProviding,
123-
blockAllPreventsAppDelete: Bool = false
124-
) {
125-
cancelMonitoring(activityName: SharedConstants.mainTimedUnblockActivityName)
126-
cancelAllGroupUnblocks()
127-
forget(id: Self.mainID)
128-
syncHeartbeat()
129-
screenTimeService.blockAll(
130-
selection: selection,
131-
customDomains: customDomains,
132-
blockAllPreventsAppDelete: blockAllPreventsAppDelete
133-
)
134-
}
135-
136119
func cancelGroup(groupId: UUID) {
137120
cancelMonitoring(activityName: SharedConstants.groupTimedUnblockActivityName(for: groupId))
138121
forget(id: groupId.uuidString)

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
import FamilyControls
2-
import SwiftData
31
import SwiftUI
42

53
struct TimedUnblockBannerView: View {
6-
@Environment(ScreenTimeService.self) private var screenTimeService
74
@Environment(TimedUnblockService.self) private var timedUnblockService
8-
@Query private var allSettings: [Settings]
95

10-
let selection: FamilyActivitySelection
11-
var customDomains: [String] = []
126
@Binding var authAction: ProtectedAction?
13-
14-
private var settings: Settings {
15-
allSettings.unwrapped
16-
}
17-
18-
private var effectiveCustomDomains: [String] {
19-
settings.enableCustomDomains ? customDomains : []
20-
}
7+
let onBlockAllNow: () -> Void
218

229
var body: some View {
2310
if let endDate = timedUnblockService.mainUnblockEndDate, endDate > .now {
@@ -41,14 +28,7 @@ struct TimedUnblockBannerView: View {
4128
prominent: false,
4229
tint: .orange
4330
) {
44-
authAction = .tighten {
45-
timedUnblockService.cancelMain(
46-
selection: selection,
47-
customDomains: effectiveCustomDomains,
48-
screenTimeService: screenTimeService,
49-
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
50-
)
51-
}
31+
authAction = .tighten { onBlockAllNow() }
5232
}
5333
.accessibilityIdentifier("home.blockAllNow")
5434
}

‎Normal/Views/Home/HomeView.swift‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ struct HomeView: View {
3333
List {
3434
if let mainSelection, mainSelection.hasItemsToBlock(customDomainsEnabled: settings.enableCustomDomains) {
3535
TimedUnblockBannerView(
36-
selection: mainSelection.selection,
37-
customDomains: mainSelection.customDomains,
38-
authAction: $authAction
36+
authAction: $authAction,
37+
onBlockAllNow: { blockMain(mainSelection) }
3938
)
4039
MainBlockButtonView(
4140
mainSelection: mainSelection,

‎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(),

0 commit comments

Comments
 (0)