-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorialView.swift
More file actions
161 lines (144 loc) · 4.53 KB
/
TutorialView.swift
File metadata and controls
161 lines (144 loc) · 4.53 KB
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
//
// TutorialView.swift
// CleanAI
//
// Created by KoichiroUeki on 2025/03/30.
//
import ComposableArchitecture
import SwiftUI
public struct TutorialViewConfiguration {
var backgroundColor: Color
var tintColor: Color
var textProvider: (LocalizedStringKey) -> Text
public init(
backgroundColor: Color = .white,
tintColor: Color = .blue,
textProvider: @escaping (LocalizedStringKey) -> Text = { Text($0) }
) {
self.backgroundColor = backgroundColor
self.tintColor = tintColor
self.textProvider = textProvider
}
}
public struct TutorialView: View {
public init(store: StoreOf<TutorialReducer>, configuration: TutorialViewConfiguration) {
self.store = store
self.configuration = configuration
}
var store: StoreOf<TutorialReducer>
var configuration: TutorialViewConfiguration
public var body: some View {
TutorialPageView(
store: store,
textViewProvider: configuration.textProvider,
tintColor: configuration.tintColor
)
.safeAreaInset(edge: .top, alignment: .trailing) {
TopButton(
onTap: {
store.send(.skipButtonTapped, animation: .easeInOut)
}, isSkipButtonEnabled: store.isSkipButtonEnabled, tintColor: configuration.tintColor
)
}
.safeAreaInset(edge: .bottom) {
BottomButtons(store: store, tintColor: configuration.tintColor)
}
.onAppear {
store.send(.appear)
}
.frame(maxWidth: .infinity)
.background(configuration.backgroundColor)
}
}
private struct TopButton: View {
var onTap: () -> Void
var isSkipButtonEnabled: Bool
var tintColor: Color
var body: some View {
Button(isSkipButtonEnabled ? "Skip": "Close") {
onTap()
}
.buttonStyle(.borderedProminent)
.tint(tintColor)
.padding(.trailing, 30)
}
}
private struct BottomButtons: View {
var store: StoreOf<TutorialReducer>
var tintColor: Color
var body: some View {
HStack(spacing: 30) {
Button {
store.send(.backButtonTapped, animation: .easeInOut)
} label: {
Image(systemName: "arrow.left")
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.white)
.padding(20)
.background(
Circle()
)
}
.tint(tintColor)
.disabled(!store.isBackButtonEnabled)
Button {
store.send(.nextButtonTapped, animation: .easeInOut)
} label: {
Image(systemName: "arrow.right")
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.white)
.padding(20)
.background(
Circle()
)
}
.tint(tintColor)
.disabled(!store.isNextButtonEnabled)
}
}
}
struct TutorialPageView: View {
@Bindable var store: StoreOf<TutorialReducer>
var textViewProvider: (LocalizedStringKey) -> Text
var tintColor: Color
var body: some View {
TabView(selection: $store.selection) {
ForEach(Array(store.tutorials.enumerated()), id: \.offset) { index, tutorial in
TutorialStepView(
item: tutorial,
textViewProvider: textViewProvider,
tintColor: tintColor
)
.tag(index)
}
}
.padding(.horizontal, 30)
.frame(maxWidth: .infinity)
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
private struct TutorialStepView: View {
let item: TutorialData
var textViewProvider: (LocalizedStringKey) -> Text
var tintColor: Color
var body: some View {
VStack(alignment: .center) {
Spacer()
textViewProvider(item.title)
.font(.title)
Spacer()
Text(item.description)
.font(.title2)
Spacer()
item
.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 100)
.foregroundStyle(tintColor)
Spacer()
}
}
}