comparison GeoQuiz/ContentView.swift @ 19:f140bb277c96

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Sun, 23 Oct 2022 00:11:38 +0100
parents 1011e56b7832
children b145c408f791
comparison
equal deleted inserted replaced
18:d20cf93c9812 19:f140bb277c96
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct ContentView: View { 10 struct ContentView: View {
11 @State private var path: [GameType] = [] 11 @State private var path: [GameType] = []
12
13 @State private var showingBuyPremiumModalView = false 12 @State private var showingBuyPremiumModalView = false
14 @State private var showingSettingsModalView = false 13 @State private var showingSettingsModalView = false
15 @State private var showingProfileModalView = false 14 @State private var showingProfileModalView = false
16 15
17 @StateObject var storeKitRC = StoreKitRC() 16 @StateObject var storeKitController = StoreKitController()
18 @StateObject var user = User() 17 @StateObject var userController = UserController()
18
19 let premiumGames: [GameType] = [.guessTheCapital, .guessTheCountry, .guessThePopulation]
19 20
20 var body: some View { 21 var body: some View {
21 NavigationStack(path: $path) { 22 NavigationStack(path: $path) {
22 ScrollView(showsIndicators: false) { 23 ScrollView(showsIndicators: false) {
23 24 ForEach(GameType.allCases, id: \.rawValue) { gameType in
24 NavigationLink(value: GameType.guessTheFlag) { EmptyView() } 25 NavigationLink(value: gameType) { EmptyView() }
25 NavigationLink(value: GameType.guessTheCapital) { EmptyView() } 26 }
26 NavigationLink(value: GameType.guessTheCountry) { EmptyView() }
27 NavigationLink(value: GameType.guessThePopulation) { EmptyView() }
28 27
29 VStack(alignment: .leading, spacing: 30) { 28 VStack(alignment: .leading, spacing: 30) {
30 Text("Select a game 🎮") 29 Text("Select a game 🎮")
31 .font(.largeTitle.bold()) 30 .font(.largeTitle.bold())
32 .padding(.bottom) 31 .padding(.bottom)
33 32
34 Button { 33 Button {
35 path.append(.guessTheFlag) 34 path.append(.guessTheFlag)
36 } label: { 35 } label: {
37 GameButton( 36 GameButton(gameType: .guessTheFlag, isActive: true)
38 gradient: GuessTheFlagInfo.gradient,
39 level: GuessTheFlagInfo.level,
40 symbol: GuessTheFlagInfo.symbol,
41 name: GuessTheFlagInfo.name
42 )
43 } 37 }
44 38
45 Button { 39 ForEach(premiumGames, id: \.rawValue) { gameType in
46 if storeKitRC.isActive { 40 Button {
47 path.append(.guessTheCapital) 41 if storeKitController.premiumIsActive {
48 } else { 42 path.append(gameType)
49 showingBuyPremiumModalView = true 43 } else {
44 showingBuyPremiumModalView = true
45 }
46 } label: {
47 GameButton(gameType: gameType, isActive: storeKitController.premiumIsActive)
48 }
50 } 49 }
51 } label: {
52 GameButton(
53 gradient: GuessTheCapitalInfo.gradient,
54 level: GuessTheCapitalInfo.level,
55 symbol: storeKitRC.isActive ? GuessTheCapitalInfo.symbol: "lock.fill",
56 name: GuessTheCapitalInfo.name
57 )
58 } 50 }
59 51 .padding()
60 Button {
61 if storeKitRC.isActive {
62 path.append(.guessTheCountry)
63 } else {
64 showingBuyPremiumModalView = true
65 }
66 } label: {
67 GameButton(
68 gradient: GuessTheCountryInfo.gradient,
69 level: GuessTheCountryInfo.level,
70 symbol: storeKitRC.isActive ? GuessTheCountryInfo.symbol: "lock.fill",
71 name: GuessTheCountryInfo.name
72 )
73 }
74
75 Button {
76 if storeKitRC.isActive {
77 path.append(.guessThePopulation)
78 } else {
79 showingBuyPremiumModalView = true
80 }
81 } label: {
82 GameButton(
83 gradient: GuessThePopulationInfo.gradient,
84 level: GuessThePopulationInfo.level,
85 symbol: storeKitRC.isActive ? GuessThePopulationInfo.symbol: "lock.fill",
86 name: GuessThePopulationInfo.name
87 )
88 }
89
90 } 52 }
91 .padding() 53 .navigationTitle("GeoQuiz")
92 } 54 .navigationBarTitleDisplayMode(.inline)
93 .navigationTitle("GeoQuiz") 55
94 .navigationBarTitleDisplayMode(.inline) 56 .navigationDestination(for: GameType.self) { gameMode in
95 57 switch gameMode {
96 .navigationDestination(for: GameType.self) { gameMode in 58 case .guessTheFlag:
97 switch gameMode { 59 GuessTheFlagView()
98 case .guessTheFlag: 60 case .guessTheCapital:
99 GuessTheFlagView() 61 GuessTheFlagView()
100 case .guessTheCapital: 62 case .guessTheCountry:
101 GuessTheFlagView() 63 GuessTheCountryView()
102 case .guessTheCountry: 64 case .guessThePopulation:
103 GuessTheCountryView() 65 GuessThePopulationView()
104 case .guessThePopulation:
105 GuessThePopulationView()
106 }
107 }
108
109 .toolbar {
110 ToolbarItem(placement: .navigationBarLeading) {
111 Button {
112 showingSettingsModalView = true
113 } label: {
114 Label("Settings", systemImage: "gear")
115 } 66 }
116 } 67 }
117 68
118 ToolbarItemGroup { 69 .toolbar {
119 if !storeKitRC.isActive { 70 ToolbarItem(placement: .navigationBarLeading) {
120 Button { 71 Button {
121 showingBuyPremiumModalView = true 72 showingSettingsModalView = true
122 } label: { 73 } label: {
123 Label("Buy premium", systemImage: "star") 74 Label("Settings", systemImage: "gear")
124 } 75 }
125 } 76 }
126 77
127 Button { 78 ToolbarItemGroup {
128 showingProfileModalView = true 79 if !storeKitController.premiumIsActive {
129 } label: { 80 Button {
130 Label("Profile", systemImage: "person") 81 showingBuyPremiumModalView = true
82 } label: {
83 Label("Buy premium", systemImage: "star")
84 }
85 }
86
87 Button {
88 showingProfileModalView = true
89 } label: {
90 Label("Profile", systemImage: "person")
91 }
131 } 92 }
132 } 93 }
94 .sheet(isPresented: $showingBuyPremiumModalView) {
95 BuyPremiumModalView(storeKitController: storeKitController)
96 }
97
98 .sheet(isPresented: $showingSettingsModalView) {
99 SettingsModalView(user: userController)
100 }
101
102 .sheet(isPresented: $showingProfileModalView) {
103 ProfileModalView(userController: userController, storeKitController: storeKitController)
104 }
133 } 105 }
134 .sheet(isPresented: $showingBuyPremiumModalView) { 106 .navigationViewStyle(StackNavigationViewStyle())
135 BuyPremiumModalView(storeKitRC: storeKitRC)
136 }
137
138 .sheet(isPresented: $showingSettingsModalView) {
139 SettingsModalView(user: user)
140 }
141
142 .sheet(isPresented: $showingProfileModalView) {
143 ProfileModalView(user: user, storeKitRC: storeKitRC)
144 }
145 } 107 }
146 .navigationViewStyle(StackNavigationViewStyle())
147 } 108 }
148 } 109
149 110 struct ContentView_Previews: PreviewProvider {
150 struct ContentView_Previews: PreviewProvider { 111 static var previews: some View {
151 static var previews: some View { 112 ContentView()
152 ContentView() 113 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
153 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) 114 }
154 } 115 }
155 }