comparison GeoQuiz/GuessThePopulationView.swift @ 10:a793f33f05fb

refactor code and fix layout
author Dennis C. M. <dennis@denniscm.com>
date Sat, 08 Oct 2022 21:36:40 +0200
parents e09959b4e4a8
children f1967f8cc67b
comparison
equal deleted inserted replaced
9:3540c7efc216 10:a793f33f05fb
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct GuessThePopulationView: View { 10 struct GuessThePopulationView: View {
11 @StateObject var game = CountryGame()
12
11 var body: some View { 13 var body: some View {
12 Text("Hello, World!") 14 ZStack {
15 LinearGradient(gradient: .quaternary, startPoint: .top, endPoint: .bottom)
16 .ignoresSafeArea()
17
18 GeometryReader { geo in
19 VStack {
20 GameToolbar(game: game, color: .maizeCrayola)
21
22 Spacer()
23
24 FlagImage(flagSymbol: game.correctAnswer.value.flag, cornerRadius: 20)
25 .clipShape(RoundedRectangle(cornerRadius: 20))
26 .shadow(radius: 10)
27 .frame(height: geo.size.height * 0.15)
28
29 Spacer()
30
31 VStack(alignment: .leading) {
32 VStack(alignment: .leading, spacing: 10) {
33 Text("Question \(game.questionCounter) of \(game.data.count)")
34 .font(.title3)
35 .foregroundColor(.white.opacity(0.7))
36
37 Text("What is the population of \(game.correctAnswer.key)?")
38 .font(.title)
39 .fontWeight(.semibold)
40 .foregroundColor(.white)
41 }
42
43 VStack(spacing: 15) {
44 ForEach(Array(game.userChoices.keys), id: \.self) { countryName in
45 Button {
46 game.answer((key: countryName, value: game.data[countryName]!)) {
47 game.selector()
48 }
49 } label: {
50 let population = game.data[countryName]!.population
51 AnswerButton(
52 optionName: population.formattedWithSeparator,
53 color: .middleRed
54 )
55 .frame(height: geo.size.height * 0.08)
56 }
57 }
58 }
59 }
60 .frame(maxWidth: 500)
61 }
62 .padding()
63 }
64 }
65 .navigationBarHidden(true)
66 .modifier(GameAlertsModifier(game: game))
13 } 67 }
14 } 68 }
15 69
16 struct GuessThePopulation_Previews: PreviewProvider { 70 struct GuessThePopulationView_Previews: PreviewProvider {
17 static var previews: some View { 71 static var previews: some View {
18 GuessThePopulationView() 72 GuessThePopulationView()
73 .previewDevice(PreviewDevice(rawValue: "iPhone 14 Pro Max"))
74 .previewDisplayName("iPhone 14 Pro Max")
75
76 GuessThePopulationView()
77 .previewDevice(PreviewDevice(rawValue: "iPad Pro (12.9-inch) (5th generation)"))
78 .previewDisplayName("iPad Pro (12.9-inch) (5th generation)")
19 } 79 }
20 } 80 }