comparison GeoQuiz/GuessTheCapitalView.swift @ 0:413e2d21333e

first commit
author Dennis C. M. <dennis@denniscm.com>
date Tue, 20 Sep 2022 08:13:26 +0200
parents
children 4dbe0cd9dadc
comparison
equal deleted inserted replaced
-1:000000000000 0:413e2d21333e
1 //
2 // GuessTheCapitalView.swift
3 // GeoQuiz
4 //
5 // Created by Dennis Concepción Martín on 14/9/22.
6 //
7
8 import SwiftUI
9
10 struct GuessTheCapitalView: View {
11 @Binding var gameName: GameName?
12 @StateObject var game = GuessTheCapital()
13
14 var flagSymbol: String {
15 if let countryAsked = game.countries[game.countryNameAsked] {
16 return countryAsked.flagSymbol
17 } else {
18 fatalError("Couldn't find \(game.countryNameAsked) in countries")
19 }
20 }
21
22 var body: some View {
23 ZStack {
24 LinearGradient(gradient: .secondary, startPoint: .top, endPoint: .bottom)
25 .ignoresSafeArea()
26
27 GeometryReader { geo in
28 VStack(spacing: 20) {
29 GameToolbar(
30 userScore: $game.userScore,
31 userLives: $game.userLives,
32 gameName: $gameName,
33 showingBuyLivesView: $game.showingBuyLivesView
34 )
35
36 Spacer()
37
38 FlagImage(flagSymbol: flagSymbol, cornerRadius: 20)
39 .shadow(radius: 10)
40 .frame(height: geo.size.height * 0.15)
41
42 Spacer()
43
44 HStack {
45 VStack(alignment: .leading, spacing: 10) {
46 Text("Question \(game.questionCounter) of \(game.countries.count)")
47 .font(.title3)
48
49 Text("What is the capital of \(game.countryNameAsked)?")
50 .font(.title)
51 .fontWeight(.semibold)
52 }
53 .foregroundColor(.white)
54
55 Spacer()
56 }
57
58 VStack {
59 ForEach(Array(game.userChoices.values), id: \.self) { country in
60 Button {
61 game.answered(userChoice: country.capitalName)
62 } label: {
63 AnswerButton(optionName: country.capitalName, color: .secondary)
64 .frame(height: geo.size.height * 0.08)
65 }
66 }
67 }
68 }
69 .padding()
70 }
71 }
72 .navigationBarHidden(true)
73
74 .sheet(isPresented: $game.showingBuyLivesView) {
75 BuyLivesModal()
76 }
77
78 .alert(game.alertTitle, isPresented: $game.showingNoLivesAlert) {
79 Button("Buy lives") { game.showingBuyLivesView = true }
80 Button("Exit", role: .destructive) { gameName = nil }
81 Button("Cancel", role: .cancel) { }
82 } message: {
83 Text(game.alertMessage)
84 }
85
86 .alert(game.alertTitle, isPresented: $game.showingWrongAnswerAlert) {
87 Button("Continue", role: .cancel) { game.askQuestion() }
88 } message: {
89 Text(game.alertMessage)
90 }
91
92 .alert(game.alertTitle, isPresented: $game.showingEndGameAlert) {
93 Button("Exit", role: .cancel) { gameName = nil }
94 } message: {
95 Text(game.alertMessage)
96 }
97 }
98 }
99
100 struct GuessCapitalView_Previews: PreviewProvider {
101 static var previews: some View {
102 NavigationView {
103 GuessTheCapitalView(gameName: .constant(GameName.guessTheCapital))
104 }
105 }
106 }