-
Notifications
You must be signed in to change notification settings - Fork 210
/
OnboardingView.swift
472 lines (416 loc) · 13.5 KB
/
OnboardingView.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import AudioPlayerClient
import Combine
import ComposableArchitecture
import CubeCore
import DictionaryClient
import FeedbackGeneratorClient
import GameCore
import PuzzleGen
import SharedModels
import Styleguide
import SwiftUI
import UIApplicationClient
import UserDefaultsClient
@Reducer
public struct Onboarding {
@ObservableState
public struct State: Equatable {
@Presents public var alert: AlertState<Action.Alert>?
public var game: Game.State
public var presentationStyle: PresentationStyle
public var step: Step
public init(
alert: AlertState<Action.Alert>? = nil,
game: Game.State = .onboarding,
presentationStyle: PresentationStyle,
step: Step = Step.allCases.first!
) {
self.alert = alert
self.game = game
self.presentationStyle = presentationStyle
self.step = step
}
fileprivate var isSkipButtonVisible: Bool {
self.step != Onboarding.State.Step.allCases.last
}
fileprivate var cubeScene: CubeSceneView.ViewState {
var viewState = CubeSceneView.ViewState(game: self.game, nub: nil)
LatticePoint.cubeIndices.forEach { index in
CubeFace.Side.allCases.forEach { side in
if !isVisible(step: self.step, index: index, side: side) {
viewState.cubes[index][side].letterIsHidden = true
viewState.cubes[index][side].status = .deselected
}
}
}
return viewState
}
public enum PresentationStyle {
case demo
case firstLaunch
case help
}
public enum Step: Int, CaseIterable, Comparable, Equatable {
case step1_Welcome
case step2_FindWordsOnCube
case step3_ConnectLettersTouching
case step4_FindGame
case step5_SubmitGame
case step6_Congrats
case step7_BiggerCube
case step8_FindCubes
case step9_Congrats
case step10_CubeDisappear
case step11_FindRemove
case step12_CubeIsShaking
case step13_Congrats
case step14_LettersRevealed
case step15_FullCube
case step16_FindAnyWord
case step17_Congrats
case step18_OneLastThing
case step19_DoubleTapToRemove
case step20_Congrats
case step21_PlayAGameYourself
mutating func next() {
self = Self(rawValue: self.rawValue + 1) ?? Self.allCases.last!
}
mutating func previous() {
self = Self(rawValue: self.rawValue - 1) ?? Self.allCases.first!
}
var isFullscreen: Bool {
switch self {
case .step1_Welcome,
.step2_FindWordsOnCube,
.step3_ConnectLettersTouching,
.step7_BiggerCube,
.step10_CubeDisappear,
.step14_LettersRevealed,
.step15_FullCube,
.step18_OneLastThing,
.step21_PlayAGameYourself:
return true
case .step4_FindGame,
.step5_SubmitGame,
.step6_Congrats,
.step8_FindCubes,
.step9_Congrats,
.step11_FindRemove,
.step12_CubeIsShaking,
.step13_Congrats,
.step16_FindAnyWord,
.step17_Congrats,
.step19_DoubleTapToRemove,
.step20_Congrats:
return false
}
}
var isCongratsStep: Bool {
switch self {
case .step6_Congrats,
.step9_Congrats,
.step13_Congrats,
.step17_Congrats,
.step20_Congrats:
return true
case .step1_Welcome,
.step2_FindWordsOnCube,
.step3_ConnectLettersTouching,
.step4_FindGame,
.step5_SubmitGame,
.step7_BiggerCube,
.step8_FindCubes,
.step10_CubeDisappear,
.step11_FindRemove,
.step12_CubeIsShaking,
.step14_LettersRevealed,
.step15_FullCube,
.step16_FindAnyWord,
.step18_OneLastThing,
.step19_DoubleTapToRemove,
.step21_PlayAGameYourself:
return false
}
}
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
}
}
}
public enum Action {
case alert(PresentationAction<Alert>)
case delayedNextStep
case delegate(Delegate)
case game(Game.Action)
case getStartedButtonTapped
case nextButtonTapped
case skipButtonTapped
case task
@CasePathable
public enum Alert {
case skipButtonTapped
}
@CasePathable
public enum Delegate {
case getStarted
}
}
@Dependency(\.audioPlayer) var _audioPlayer
var audioPlayer: AudioPlayerClient {
self._audioPlayer.filteredSounds(doNotInclude: AudioPlayerClient.Sound.allValidWords)
}
@Dependency(\.dictionary) var dictionary
@Dependency(\.feedbackGenerator) var feedbackGenerator
@Dependency(\.mainQueue) var mainQueue
@Dependency(\.userDefaults) var userDefaults
@Dependency(\.userSettings) var userSettings
public init() {}
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .alert(.dismiss):
return .none
case .alert(.presented(.skipButtonTapped)):
state.step = State.Step.allCases.last!
return .run { _ in
await self.audioPlayer.play(.uiSfxTap)
Task.cancel(id: CancelID.delayedNextStep)
}
case .delayedNextStep:
state.step.next()
return .none
case .delegate(.getStarted):
return .run { _ in
await self.userDefaults.setHasShownFirstLaunchOnboarding(true)
await self.audioPlayer.stop(.onboardingBgMusic)
Task.cancel(id: CancelID.delayedNextStep)
}
case .game where state.step.isCongratsStep:
return .none
case .game(.submitButtonTapped):
switch state.step {
case .step5_SubmitGame where state.game.selectedWordString == "GAME",
.step8_FindCubes where state.game.selectedWordString == "CUBES",
.step12_CubeIsShaking where state.game.selectedWordString.isRemove,
.step16_FindAnyWord where self.dictionary.contains(state.game.selectedWordString, .en):
state.step.next()
return self.gameReducer.reduce(
into: &state,
action: .game(.submitButtonTapped(reaction: nil))
)
default:
state.game.selectedWord = []
return .none
}
case let .game(.confirmRemoveCube(index)):
state.step.next()
return self.gameReducer.reduce(into: &state, action: .game(.confirmRemoveCube(index)))
case let .game(.doubleTap(index: index)):
guard state.step == .some(.step19_DoubleTapToRemove)
else { return .none }
return .send(.game(.confirmRemoveCube(index)))
case let .game(.tap(gestureState, .some(indexedCubeFace))):
let index =
isVisible(step: state.step, index: indexedCubeFace.index, side: indexedCubeFace.side)
? indexedCubeFace
: nil
return self.gameReducer.reduce(into: &state, action: .game(.tap(gestureState, index)))
case let .game(.pan(recognizerState, panData)):
if let indexedCubeFace = panData?.cubeFaceState,
!isVisible(step: state.step, index: indexedCubeFace.index, side: indexedCubeFace.side)
{
return .none
}
return self.gameReducer.reduce(into: &state, action: .game(.pan(recognizerState, panData)))
case .game:
return self.gameReducer.reduce(into: &state, action: action)
case .getStartedButtonTapped:
return .send(.delegate(.getStarted))
case .nextButtonTapped:
state.step.next()
return .run { _ in await self.audioPlayer.play(.uiSfxTap) }
case .skipButtonTapped:
guard !self.userDefaults.hasShownFirstLaunchOnboarding else {
return .run { send in
await send(.delegate(.getStarted), animation: .default)
await self.audioPlayer.play(.uiSfxTap)
}
}
state.alert = AlertState {
TextState("Skip tutorial?")
} actions: {
ButtonState(action: .send(.skipButtonTapped, animation: .default)) {
TextState("Yes, skip")
}
ButtonState(role: .cancel) {
TextState("No, resume")
}
} message: {
TextState(
"""
Are you sure you want to skip the tutorial? It only takes about a minute to complete.
You can always view it again later in settings.
"""
)
}
return .run { _ in await self.audioPlayer.play(.uiSfxTap) }
case .task:
let firstStepDelay: Int = {
switch state.presentationStyle {
case .demo, .firstLaunch:
return 4
case .help:
return 2
}
}()
return .run { [step = state.step, presentationStyle = state.presentationStyle] send in
await self.audioPlayer.load(AudioPlayerClient.Sound.allCases)
_ = try self.dictionary.load(.en)
await self.audioPlayer.play(
presentationStyle == .demo ? .timedGameBgLoop1 : .onboardingBgMusic
)
if step == State.Step.allCases[0] {
try await self.mainQueue.sleep(for: .seconds(firstStepDelay))
await send(.delayedNextStep, animation: .default)
}
}
.cancellable(id: CancelID.delayedNextStep)
}
}
.ifLet(\.$alert, action: \.alert)
.onChange(of: \.game.selectedWordString) { _, selectedWord in
Reduce { state, _ in
switch state.step {
case .step4_FindGame where selectedWord == "GAME",
.step11_FindRemove where selectedWord.isRemove:
state.step.next()
return .none
case .step5_SubmitGame where selectedWord != "GAME",
.step12_CubeIsShaking where !selectedWord.isRemove:
state.step.previous()
return .none
default:
return .none
}
}
}
.onChange(of: \.step) { _, step in
Reduce { _, _ in
switch step {
case .step1_Welcome,
.step2_FindWordsOnCube,
.step3_ConnectLettersTouching,
.step4_FindGame,
.step5_SubmitGame,
.step7_BiggerCube,
.step8_FindCubes,
.step10_CubeDisappear,
.step11_FindRemove,
.step12_CubeIsShaking,
.step14_LettersRevealed,
.step15_FullCube,
.step16_FindAnyWord,
.step18_OneLastThing,
.step19_DoubleTapToRemove,
.step21_PlayAGameYourself:
return .none
case .step13_Congrats:
return .run { send in
try await self.mainQueue.sleep(for: .seconds(3))
await send(.delayedNextStep, animation: .default)
}
case .step6_Congrats,
.step9_Congrats,
.step17_Congrats,
.step20_Congrats:
return .run { send in
try await self.mainQueue.sleep(for: .seconds(2))
await send(.delayedNextStep, animation: .default)
}
}
}
}
}
var gameReducer: some ReducerOf<Self> {
Scope(state: \.game, action: \.game) {
Game()
.haptics(
isEnabled: { _ in self.userSettings.enableHaptics },
triggerOnChangeOf: \.selectedWord
)
}
.transformDependency(\.self) {
$0.gameOnboarding()
}
}
}
public struct OnboardingView: View {
@Environment(\.colorScheme) var colorScheme
let store: StoreOf<Onboarding>
public init(store: StoreOf<Onboarding>) {
self.store = store
}
public var body: some View {
ZStack(alignment: .topTrailing) {
CubeView(store: store.scope(state: \.cubeScene, action: \.game.cubeScene))
.opacity(store.step.isFullscreen ? 0 : 1)
OnboardingStepView(store: store)
if store.isSkipButtonVisible {
Button("Skip") { store.send(.skipButtonTapped, animation: .default) }
.adaptiveFont(.matterMedium, size: 18)
.buttonStyle(PlainButtonStyle())
.padding(.horizontal)
.foregroundColor(
self.colorScheme == .dark
? store.step.color
: Color.isowordsBlack
)
}
}
.background(
(self.colorScheme == .dark ? Color.isowordsBlack : store.step.color)
.ignoresSafeArea()
)
}
}
private func isVisible(
step: Onboarding.State.Step,
index: LatticePoint,
side: CubeFace.Side
) -> Bool {
if step < .step8_FindCubes {
return index == .init(x: .one, y: .two, z: .two) && side == .left
|| index == .init(x: .two, y: .two, z: .two) && side == .left
|| index == .init(x: .two, y: .two, z: .two) && side == .right
|| index == .init(x: .two, y: .two, z: .one) && side == .right
} else if step < .step11_FindRemove {
return index == .init(x: .one, y: .two, z: .two)
|| index == .init(x: .two, y: .two, z: .two)
|| index == .init(x: .two, y: .two, z: .one)
|| index == .init(x: .one, y: .two, z: .one)
} else if step < .step13_Congrats {
return index.x >= .one && index.y >= .one && index.z >= .one
} else if step < .step16_FindAnyWord {
return index.x >= .one && index.y >= .one && index.z >= .one
|| index == .init(x: .two, y: .two, z: .zero) && side == .left
}
return true
}
extension String {
var isRemove: Bool {
self == "REMOVE" || self == "REMOVES"
}
}
private enum CancelID {
case delayedNextStep
}
#if DEBUG
struct OnboardingView_Previews: PreviewProvider {
static var previews: some View {
OnboardingView(
store: Store(initialState: .init(presentationStyle: .firstLaunch)) {
}
)
}
}
#endif