Mercurial > public > geoquiz
comparison GeoQuiz/GuessTheCountryView.swift @ 26:425078c01194
refactor code
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Wed, 09 Nov 2022 10:30:01 +0100 |
parents | 02dcebb8cc4a |
children | 3f4b366d476d |
comparison
equal
deleted
inserted
replaced
25:b3df0f5dc750 | 26:425078c01194 |
---|---|
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct GuessTheCountryView: View { | 10 struct GuessTheCountryView: View { |
11 @StateObject var game = CityGameController() | 11 @StateObject var gameController = CityGameController() |
12 | 12 |
13 @Environment(\.managedObjectContext) var moc | 13 @Environment(\.managedObjectContext) var moc |
14 | 14 |
15 var body: some View { | 15 var body: some View { |
16 ZStack { | 16 ZStack { |
17 LinearGradient(gradient: .tertiary, startPoint: .top, endPoint: .bottom) | 17 LinearGradient(gradient: .tertiary, startPoint: .top, endPoint: .bottom) |
18 .ignoresSafeArea() | 18 .ignoresSafeArea() |
19 | 19 |
20 GeometryReader { geo in | 20 GeometryReader { geo in |
21 VStack { | 21 VStack { |
22 GameToolbar(game: game, color: .pinkLavender) | 22 GameToolbar(gameController: gameController, color: .pinkLavender) |
23 .padding(.bottom) | 23 .padding(.bottom) |
24 | 24 |
25 Spacer() | 25 Spacer() |
26 | 26 |
27 CityMap(game: game) | 27 CityMap(game: gameController) |
28 .frame(height: geo.size.height * 0.35) | 28 .frame(height: geo.size.height * 0.35) |
29 .padding(.bottom) | 29 .padding(.bottom) |
30 | 30 |
31 Spacer() | 31 Spacer() |
32 | 32 |
33 VStack(alignment: .leading) { | 33 VStack(alignment: .leading) { |
34 VStack(alignment: .leading, spacing: 10) { | 34 VStack(alignment: .leading, spacing: 10) { |
35 Text("Question \(game.questionCounter) of \(game.data.count)") | 35 Text("Question \(gameController.questionCounter) of \(gameController.data.count)") |
36 .font(.title3) | 36 .font(.title3) |
37 .foregroundColor(.white.opacity(0.7)) | 37 .foregroundColor(.white.opacity(0.7)) |
38 | 38 |
39 Text("In what country is \(game.correctAnswer.key)?") | 39 Text("In what country is \(gameController.correctAnswer.key)?") |
40 .font(.title) | 40 .font(.title) |
41 .fontWeight(.semibold) | 41 .fontWeight(.semibold) |
42 .foregroundColor(.white) | 42 .foregroundColor(.white) |
43 } | 43 } |
44 | 44 |
45 VStack(spacing: 15) { | 45 VStack(spacing: 15) { |
46 ForEach(Array(game.userChoices.keys), id: \.self) { cityName in | 46 ForEach(Array(gameController.userChoices.keys), id: \.self) { cityName in |
47 Button { | 47 Button { |
48 game.answer((key: cityName, value: game.data[cityName]!)) { | 48 gameController.answer( |
49 game.selector() | 49 choice: (key: cityName, value: gameController.data[cityName]!), |
50 wrongMessage: "\(gameController.correctAnswer.key) is located in \(gameController.correctAnswer.value.country)" | |
51 ) { | |
52 gameController.selector() | |
50 } | 53 } |
51 } label: { | 54 } label: { |
52 AnswerButton( | 55 AnswerButton( |
53 name: game.data[cityName]!.country, | 56 name: gameController.data[cityName]!.country, |
54 color: .blueBell | 57 color: .blueBell |
55 ) | 58 ) |
56 .frame(height: geo.size.height * 0.08) | 59 .frame(height: geo.size.height * 0.08) |
57 } | 60 } |
58 } | 61 } |
62 } | 65 } |
63 .padding() | 66 .padding() |
64 } | 67 } |
65 } | 68 } |
66 .navigationBarHidden(true) | 69 .navigationBarHidden(true) |
67 .modifier(GameAlertsModifier(game: game, gameType: .guessTheCountry, moc: moc)) | 70 .modifier(GameAlertsModifier(gameController: gameController, gameType: .guessTheCountry, moc: moc)) |
68 } | 71 } |
69 } | 72 } |
70 | 73 |
71 struct GuessTheCountryView_Previews: PreviewProvider { | 74 struct GuessTheCountryView_Previews: PreviewProvider { |
72 static var previews: some View { | 75 static var previews: some View { |
73 GuessTheCountryView() | 76 GuessTheCountryView() |
74 .previewDevice(PreviewDevice(rawValue: "iPhone 14 Pro Max")) | |
75 .previewDisplayName("iPhone 14 Pro Max") | |
76 | |
77 GuessTheCountryView() | |
78 .previewDevice(PreviewDevice(rawValue: "iPad Pro (12.9-inch) (5th generation)")) | |
79 .previewDisplayName("iPad Pro (12.9-inch)") | |
80 | |
81 GuessTheCountryView() | |
82 .previewDevice(PreviewDevice(rawValue: "iPhone 8")) | |
83 .previewDisplayName("iPhone 8") | |
84 } | 77 } |
85 } | 78 } |