-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainView.swift
159 lines (136 loc) · 4.28 KB
/
MainView.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
//
// MainView.swift
// Uplift
//
// Created by Vin Bui on 12/25/23.
// Copyright © 2023 Cornell AppDev. All rights reserved.
//
import SwiftUI
/// The app's entry point view.
struct MainView: View {
// MARK: - Properties
@State private var selectedTab: Screen = .home
@StateObject var tabBarProp = TabBarProperty()
@StateObject private var viewModel = ViewModel()
// MARK: - UI
var body: some View {
ZStack {
VStack(spacing: 0) {
switch selectedTab {
case .home:
HomeView(popUpGiveaway: $viewModel.popUpGiveaway)
case .classes:
ClassesView()
.environmentObject(tabBarProp)
case .profile:
ProfileView()
.environmentObject(tabBarProp)
}
}
.overlay(alignment: .bottom) {
!tabBarProp.hidden ? tabBar.transition(.move(edge: .bottom)) : nil
}
if viewModel.popUpGiveaway {
Constants.Colors.gray04
.opacity(0.4)
.ignoresSafeArea(.all)
GiveawayPopup(
didClickSubmit: $viewModel.didClickSubmit,
instagram: $viewModel.instagram,
netID: $viewModel.netID,
popUpGiveaway: $viewModel.popUpGiveaway,
submitSuccessful: $viewModel.submitSuccessful
)
.padding(.horizontal, 20)
.transition(.scale(scale: 0.5, anchor: .bottom))
.transition(.opacity)
.alert(isPresented: $viewModel.showGiveawayErrorAlert) {
Alert(
title: Text("Unable to enter giveaway"),
message: Text("Something went wrong.")
)
}
}
}
.background(Color.white)
.onChange(of: viewModel.didClickSubmit) { didClickSubmit in
if didClickSubmit {
viewModel.enterGiveaway()
}
}
}
private var tabBar: some View {
HStack {
Spacer()
tabItem(for: .home)
Spacer()
tabItem(for: .classes)
Spacer()
// TODO: Uncomment when profile released
// tabItem(for: .profile)
//
// Spacer()
}
.frame(height: Constants.Padding.tabBarHeight)
.background(Constants.Colors.yellow)
.ignoresSafeArea(.all)
}
@ViewBuilder
private func tabItem(for screen: Screen) -> some View {
switch screen {
case .home:
Button {
selectedTab = .home
} label: {
tabItemView(
icon: selectedTab == .home ? Constants.Images.dumbbellSolid : Constants.Images.dumbbellOutline,
name: "Home"
)
}
.buttonStyle(.plain)
case .classes:
Button {
selectedTab = .classes
} label: {
tabItemView(
icon: selectedTab == .classes ? Constants.Images.whistleSolid : Constants.Images.whistleOutline,
name: "Classes"
)
}
case .profile:
Button {
selectedTab = .profile
} label: {
tabItemView(
icon: selectedTab == .profile ? Constants.Images.profileSolid : Constants.Images.profileOutline,
name: "Profile"
)
}
}
}
private func tabItemView(icon: Image, name: String) -> some View {
VStack {
icon
.resizable()
.scaledToFit()
.frame(width: 24, height: 24)
Text(name)
.font(Constants.Fonts.h3)
}
.foregroundStyle(Constants.Colors.black)
}
}
extension MainView {
/// An enumeration to keep track of which tab the user is currently on.
private enum Screen {
case home
case classes
case profile
}
}
final class TabBarProperty: ObservableObject {
@Published var hidden: Bool = false
}
#Preview {
MainView()
}