-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonboarding.swift
111 lines (98 loc) · 3.71 KB
/
onboarding.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
import SwiftUI
struct OnboardingView: View {
@State private var currentPage = 0
let pages = [
OnboardingPage(title: "Welcome", description: "Welcome to our Mindfulness app. Let's begin your journey to a peaceful mind."),
OnboardingPage(title: "Track Your Mood", description: "Log your mood every day to track your mental well-being."),
OnboardingPage(title: "Practice Mindfulness", description: "Learn and practice mindfulness to manage stress and anxiety."),
OnboardingPage(title: "Get Started", description: "Ready to start? Let's dive in!")
]
var body: some View {
VStack {
TabView(selection: $currentPage) {
ForEach(pages.indices, id: \.self) { index in
OnboardingPageView(page: pages[index], isLastPage: index == pages.count - 1)
.tag(index)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
HStack {
Button(action: {
if currentPage > 0 {
currentPage -= 1
}
}) {
Image(systemName: "arrow.left")
.padding()
.background(Color.gray.opacity(0.2))
.clipShape(Circle())
.foregroundColor(Color.blue)
}
.disabled(currentPage == 0)
Spacer()
Button(action: {
// Skip the onboarding and navigate to the home page
UserDefaults.standard.set(true, forKey: "hasSeenOnboarding")
NotificationCenter.default.post(name: .onboardingCompleted, object: nil)
}) {
Text("Skip")
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
.foregroundColor(Color.blue)
}
Spacer()
Button(action: {
if currentPage < pages.count - 1 {
currentPage += 1
}
}) {
Image(systemName: "arrow.right")
.padding()
.background(Color.gray.opacity(0.2))
.clipShape(Circle())
.foregroundColor(Color.blue)
}
.disabled(currentPage == pages.count - 1)
}
.padding()
}
}
}
struct OnboardingPageView: View {
let page: OnboardingPage
let isLastPage: Bool
var body: some View {
VStack {
Text(page.title)
.font(.title)
.bold()
.padding()
Text(page.description)
.font(.subheadline)
.multilineTextAlignment(.center)
.padding()
if isLastPage {
Button(action: {
// navigates to the home page
UserDefaults.standard.set(true, forKey: "hasSeenOnboarding")
NotificationCenter.default.post(name: .onboardingCompleted, object: nil)
}) {
Text("Get Started")
.font(.headline)
.padding()
.frame(maxWidth: .infinity)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
}
}
.padding()
}
}
struct OnboardingPage {
let title: String
let description: String
}