Skip to content

Commit 65401c3

Browse files
authored
Modernize case study using Notification Center (#1090)
* Use NotificationCenter directly in case study * wip * wip
1 parent c232a9f commit 65401c3

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

Examples/CaseStudies/SwiftUICaseStudies/00-Core.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct RootEnvironment {
8282
var favorite: (UUID, Bool) -> Effect<Bool, Error>
8383
var fetchNumber: () -> Effect<Int, Never>
8484
var mainQueue: AnySchedulerOf<DispatchQueue>
85-
var userDidTakeScreenshot: Effect<Void, Never>
85+
var notificationCenter: NotificationCenter
8686
var uuid: () -> UUID
8787
var webSocket: WebSocketClient
8888

@@ -93,7 +93,7 @@ struct RootEnvironment {
9393
favorite: favorite(id:isFavorite:),
9494
fetchNumber: liveFetchNumber,
9595
mainQueue: .main,
96-
userDidTakeScreenshot: liveUserDidTakeScreenshot,
96+
notificationCenter: .default,
9797
uuid: UUID.init,
9898
webSocket: .live
9999
)
@@ -220,7 +220,7 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
220220
.pullback(
221221
state: \.longLivingEffects,
222222
action: /RootAction.longLivingEffects,
223-
environment: { .init(userDidTakeScreenshot: $0.userDidTakeScreenshot) }
223+
environment: { .init(notificationCenter: $0.notificationCenter) }
224224
),
225225
mapAppReducer
226226
.pullback(
@@ -312,8 +312,3 @@ private func liveFetchNumber() -> Effect<Int, Never> {
312312
.delay(for: 1, scheduler: DispatchQueue.main)
313313
.eraseToEffect()
314314
}
315-
316-
private let liveUserDidTakeScreenshot = NotificationCenter.default
317-
.publisher(for: UIApplication.userDidTakeScreenshotNotification)
318-
.map { _ in () }
319-
.eraseToEffect()

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-LongLiving.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ enum LongLivingEffectsAction {
2727
}
2828

2929
struct LongLivingEffectsEnvironment {
30-
// An effect that emits Void whenever the user takes a screenshot of the device. We use this
31-
// instead of `NotificationCenter.default.publisher` directly in the reducer so that we can test
32-
// it.
33-
var userDidTakeScreenshot: Effect<Void, Never>
30+
var notificationCenter: NotificationCenter
3431
}
3532

3633
// MARK: - Business logic
@@ -48,8 +45,10 @@ let longLivingEffectsReducer = Reducer<
4845

4946
case .onAppear:
5047
// When the view appears, start the effect that emits when screenshots are taken.
51-
return environment.userDidTakeScreenshot
52-
.map { LongLivingEffectsAction.userDidTakeScreenshotNotification }
48+
return environment.notificationCenter
49+
.publisher(for: UIApplication.userDidTakeScreenshotNotification)
50+
.map { _ in LongLivingEffectsAction.userDidTakeScreenshotNotification }
51+
.eraseToEffect()
5352
.cancellable(id: UserDidTakeScreenshotNotificationId.self)
5453

5554
case .onDisappear:
@@ -103,7 +102,7 @@ struct EffectsLongLiving_Previews: PreviewProvider {
103102
initialState: LongLivingEffectsState(),
104103
reducer: longLivingEffectsReducer,
105104
environment: LongLivingEffectsEnvironment(
106-
userDidTakeScreenshot: .none
105+
notificationCenter: .default
107106
)
108107
)
109108
)

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-LongLivingTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ import XCTest
66

77
class LongLivingEffectsTests: XCTestCase {
88
func testReducer() {
9-
// A passthrough subject to simulate the screenshot notification
10-
let screenshotTaken = PassthroughSubject<Void, Never>()
9+
let notificationCenter = NotificationCenter()
1110

1211
let store = TestStore(
1312
initialState: .init(),
1413
reducer: longLivingEffectsReducer,
1514
environment: .init(
16-
userDidTakeScreenshot: Effect(screenshotTaken)
15+
notificationCenter: notificationCenter
1716
)
1817
)
1918

2019
store.send(.onAppear)
2120

2221
// Simulate a screenshot being taken
23-
screenshotTaken.send()
22+
notificationCenter.post(name: UIApplication.userDidTakeScreenshotNotification, object: nil)
2423
store.receive(.userDidTakeScreenshotNotification) {
2524
$0.screenshotCount = 1
2625
}
@@ -29,6 +28,6 @@ class LongLivingEffectsTests: XCTestCase {
2928

3029
// Simulate a screenshot being taken to show no effects
3130
// are executed.
32-
screenshotTaken.send()
31+
notificationCenter.post(name: UIApplication.userDidTakeScreenshotNotification, object: nil)
3332
}
3433
}

0 commit comments

Comments
 (0)