Skip to content

Commit 87778b7

Browse files
committed
Modernize case study using Notification Center (#1090)
* Use NotificationCenter directly in case study * wip * wip
1 parent 632ad0f commit 87778b7

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

Examples/CaseStudies/SwiftUICaseStudies/00-Core.swift

Lines changed: 3 additions & 7 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: DateScheduler
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: QueueScheduler.main,
96-
userDidTakeScreenshot: liveUserDidTakeScreenshot.producer,
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(
@@ -311,7 +311,3 @@ private func liveFetchNumber() -> Effect<Int, Never> {
311311
Effect.deferred { Effect(value: Int.random(in: 1...1_000)) }
312312
.delay(1, on: QueueScheduler.main)
313313
}
314-
315-
private let liveUserDidTakeScreenshot = NotificationCenter.default
316-
.reactive.notifications(forName: UIApplication.userDidTakeScreenshotNotification)
317-
.map { _ in () }

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

Lines changed: 7 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.reactive.notifications(forName:)` directly
32-
// in the reducer so that we can test it
33-
var userDidTakeScreenshot: Effect<Void, Never>
30+
var notificationCenter: NotificationCenter
3431
}
3532

3633
// MARK: - Business logic
@@ -48,10 +45,13 @@ 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+
.reactive.notifications(forName: UIApplication.userDidTakeScreenshotNotification)
50+
.producer
51+
.map { _ in LongLivingEffectsAction.userDidTakeScreenshotNotification }
5352
.cancellable(id: UserDidTakeScreenshotNotificationId.self)
5453

54+
5555
case .onDisappear:
5656
// When view disappears, stop the effect.
5757
return .cancel(id: UserDidTakeScreenshotNotificationId.self)
@@ -103,7 +103,7 @@ struct EffectsLongLiving_Previews: PreviewProvider {
103103
initialState: LongLivingEffectsState(),
104104
reducer: longLivingEffectsReducer,
105105
environment: LongLivingEffectsEnvironment(
106-
userDidTakeScreenshot: .none
106+
notificationCenter: .default
107107
)
108108
)
109109
)

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 = Signal<Void, Never>.pipe()
9+
let notificationCenter = NotificationCenter()
1110

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

2019
store.send(.onAppear)
2120

2221
// Simulate a screenshot being taken
23-
screenshotTaken.input.send(value: ())
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.input.send(value: ())
31+
notificationCenter.post(name: UIApplication.userDidTakeScreenshotNotification, object: nil)
3332
}
3433
}

0 commit comments

Comments
 (0)