-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathAppView.swift
347 lines (304 loc) · 10.7 KB
/
AppView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import ClientModels
import ComposableArchitecture
import ComposableStoreKit
import CubeCore
import GameCore
import HomeFeature
import NotificationHelpers
import OnboardingFeature
import SharedModels
import Styleguide
import SwiftUI
@Reducer
public struct AppReducer {
@Reducer(state: .equatable)
public enum Destination {
case game(Game)
case onboarding(Onboarding)
}
@ObservableState
public struct State: Equatable {
public var appDelegate: AppDelegateReducer.State
@Presents public var destination: Destination.State?
public var home: Home.State
public init(
appDelegate: AppDelegateReducer.State = AppDelegateReducer.State(),
destination: Destination.State? = nil,
home: Home.State = Home.State()
) {
self.appDelegate = appDelegate
self.destination = destination
self.home = home
}
}
public enum Action {
case appDelegate(AppDelegateReducer.Action)
case destination(PresentationAction<Destination.Action>)
case didChangeScenePhase(ScenePhase)
case gameCenter(GameCenterAction)
case home(Home.Action)
case paymentTransaction(StoreKitClient.PaymentTransactionObserverEvent)
case savedGamesLoaded(Result<SavedGamesState, Error>)
case verifyReceiptResponse(Result<ReceiptFinalizationEnvelope, Error>)
}
@Dependency(\.fileClient) var fileClient
@Dependency(\.gameCenter.turnBasedMatch.load) var loadTurnBasedMatch
@Dependency(\.database.migrate) var migrate
@Dependency(\.mainRunLoop.now.date) var now
@Dependency(\.dictionary.randomCubes) var randomCubes
@Dependency(\.remoteNotifications) var remoteNotifications
@Dependency(\.serverConfig.refresh) var refreshServerConfig
@Dependency(\.userDefaults) var userDefaults
@Dependency(\.userNotifications) var userNotifications
public init() {}
public var body: some ReducerOf<Self> {
self.core
.onChange(of: \.destination?.game?.moves) { _, moves in
Reduce { state, _ in
guard let game = state.destination?.game, game.isSavable
else { return .none }
switch (game.gameContext, game.gameMode) {
case (.dailyChallenge, .unlimited):
state.home.savedGames.dailyChallengeUnlimited = InProgressGame(gameState: game)
case (.shared, .unlimited), (.solo, .unlimited):
state.home.savedGames.unlimited = InProgressGame(gameState: game)
case (.turnBased, _), (_, .timed):
return .none
}
return .none
}
}
.onChange(of: \.home.savedGames) { _, savedGames in
Reduce { _, action in
if case .savedGamesLoaded(.success) = action { return .none }
return .run { _ in
try await self.fileClient.save(games: savedGames)
}
}
}
GameCenterLogic()
StoreKitLogic()
}
@ReducerBuilder<State, Action>
var core: some ReducerOf<Self> {
Scope(state: \.appDelegate, action: \.appDelegate) {
AppDelegateReducer()
}
Scope(state: \.home, action: \.home) {
Home()
}
Reduce { state, action in
switch action {
case .appDelegate(.didFinishLaunching):
if !self.userDefaults.hasShownFirstLaunchOnboarding {
state.destination = .onboarding(Onboarding.State(presentationStyle: .firstLaunch))
}
return .run { send in
async let migrate: Void = self.migrate()
if self.userDefaults.installationTime <= 0 {
await self.userDefaults.setInstallationTime(
self.now.timeIntervalSinceReferenceDate
)
}
await send(
.savedGamesLoaded(
Result { try await self.fileClient.loadSavedGames() }
)
)
_ = try await migrate
}
case let .appDelegate(.userNotifications(.didReceiveResponse(response, completionHandler))):
if let data =
try? JSONSerialization
.data(withJSONObject: response.notification.request.content.userInfo),
let pushNotificationContent = try? JSONDecoder()
.decode(PushNotificationContent.self, from: data)
{
switch pushNotificationContent {
case .dailyChallengeEndsSoon:
if let inProgressGame = state.home.savedGames.dailyChallengeUnlimited {
state.destination = .game(Game.State(inProgressGame: inProgressGame))
} else {
// TODO: load/retry
}
case .dailyChallengeReport:
state.destination = nil
state.home.destination = .dailyChallenge(.init())
}
}
return .run { _ in completionHandler() }
case .appDelegate:
return .none
case .destination(
.presented(.game(.destination(.presented(.bottomMenu(.endGameButtonTapped)))))
),
.destination(.presented(.game(.destination(.presented(.gameOver(.task)))))):
guard let game = state.destination?.game else { return .none }
switch (game.gameContext, game.gameMode) {
case (.dailyChallenge, .unlimited):
state.home.savedGames.dailyChallengeUnlimited = nil
case (.solo, .unlimited):
state.home.savedGames.unlimited = nil
default:
break
}
return .none
case .destination(.presented(.game(.activeGames(.dailyChallengeTapped)))),
.home(.activeGames(.dailyChallengeTapped)):
guard let inProgressGame = state.home.savedGames.dailyChallengeUnlimited
else { return .none }
state.destination = .game(Game.State(inProgressGame: inProgressGame))
return .none
case .destination(.presented(.game(.activeGames(.soloTapped)))),
.home(.activeGames(.soloTapped)):
guard let inProgressGame = state.home.savedGames.unlimited
else { return .none }
state.destination = .game(Game.State(inProgressGame: inProgressGame))
return .none
case let .destination(.presented(.game(.activeGames(.turnBasedGameTapped(matchId))))),
let .home(.activeGames(.turnBasedGameTapped(matchId))):
return .run { send in
do {
let match = try await self.loadTurnBasedMatch(matchId)
await send(
.gameCenter(
.listener(.turnBased(.receivedTurnEventForMatch(match, didBecomeActive: true)))
),
animation: .default
)
} catch {}
}
case .destination(
.presented(.game(.destination(.presented(.gameOver(.delegate(.startSoloGame(.timed)))))))
),
.home(.destination(.presented(.solo(.gameButtonTapped(.timed))))):
state.destination = .game(
Game.State(
cubes: self.randomCubes(.en),
gameContext: .solo,
gameCurrentTime: self.now,
gameMode: .timed,
gameStartTime: self.now,
isGameLoaded: state.destination?.game?.isGameLoaded == .some(true)
)
)
return .none
case .destination(
.presented(
.game(.destination(.presented(.gameOver(.delegate(.startSoloGame(.unlimited))))))
)
),
.home(.destination(.presented(.solo(.gameButtonTapped(.unlimited))))):
state.destination = .game(
state.home.savedGames.unlimited
.map { Game.State(inProgressGame: $0) }
?? Game.State(
cubes: self.randomCubes(.en),
gameContext: .solo,
gameCurrentTime: self.now,
gameMode: .unlimited,
gameStartTime: self.now,
isGameLoaded: state.destination?.game?.isGameLoaded == .some(true)
)
)
return .none
case let .destination(.presented(.onboarding(.delegate(action)))):
switch action {
case .getStarted:
state.destination = nil
return .none
}
case .destination:
return .none
case let .home(
.destination(.presented(.dailyChallenge(.delegate(.startGame(inProgressGame)))))
):
state.destination = .game(Game.State(inProgressGame: inProgressGame))
return .none
case let .home(.dailyChallengeResponse(.success(dailyChallenges))):
if dailyChallenges.unlimited?.dailyChallenge.id
!= state.home.savedGames.dailyChallengeUnlimited?.gameContext.dailyChallenge
{
state.home.savedGames.dailyChallengeUnlimited = nil
return .run { [savedGames = state.home.savedGames] _ in
try await self.fileClient.save(games: savedGames)
}
}
return .none
case .home(.howToPlayButtonTapped):
state.destination = .onboarding(Onboarding.State(presentationStyle: .help))
return .none
case .didChangeScenePhase(.active):
return .run { _ in
async let register: Void = registerForRemoteNotificationsAsync(
remoteNotifications: self.remoteNotifications,
userNotifications: self.userNotifications
)
async let refresh = self.refreshServerConfig()
_ = try await (register, refresh)
} catch: { _, _ in
}
case .didChangeScenePhase:
return .none
case .gameCenter:
return .none
case .home:
return .none
case .paymentTransaction:
return .none
case .savedGamesLoaded(.failure):
return .none
case let .savedGamesLoaded(.success(savedGames)):
state.home.savedGames = savedGames
return .none
case .verifyReceiptResponse:
return .none
}
}
.ifLet(\.$destination, action: \.destination) {
Destination.body
}
}
}
public struct AppView: View {
let store: StoreOf<AppReducer>
@Environment(\.deviceState) var deviceState
public init(store: StoreOf<AppReducer>) {
self.store = store
}
public var body: some View {
Group {
switch store.destination {
case .none:
NavigationStack {
HomeView(store: store.scope(state: \.home, action: \.home))
}
.zIndex(0)
case .some(.game):
if let store = store.scope(state: \.destination?.game, action: \.destination.game) {
GameView(
content: CubeView(store: store.scope(state: \.cubeScene, action: \.cubeScene)),
store: store
)
.transition(.game)
.zIndex(1)
}
case .some(.onboarding):
if let store = store.scope(
state: \.destination?.onboarding, action: \.destination.onboarding
) {
OnboardingView(store: store)
.zIndex(2)
}
}
}
.modifier(DeviceStateModifier())
}
}
#Preview {
AppView(
store: Store(initialState: AppReducer.State()) {
AppReducer()
}
)
}