comparison GeoQuiz/Components/GameToolbarHelper.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 GeoQuiz/Helpers/GameToolbar.swift@e09959b4e4a8
children f140bb277c96
comparison
equal deleted inserted replaced
9:3540c7efc216 10:a793f33f05fb
1 //
2 // GameToolbarHelper.swift
3 // GeoQuiz
4 //
5 // Created by Dennis Concepción Martín on 18/9/22.
6 //
7
8 import SwiftUI
9
10 struct GameToolbar<T: Game>: View {
11 @ObservedObject var game: T
12
13 var color: Color
14
15 var body: some View {
16 HStack(spacing: 0) {
17 Group {
18 Button {
19 game.showingExitGameAlert = true
20 } label: {
21 Image(systemName: "multiply")
22 .font(.headline)
23 .foregroundColor(color)
24 .padding(10)
25 .background(
26 Circle()
27 .foregroundColor(.white)
28 )
29 }
30 }
31 .font(.headline)
32 .frame(maxWidth: .infinity, alignment: .leading)
33
34 Group {
35 Text("\(game.userScore)")
36 .font(.title.bold())
37 .foregroundColor(color)
38 .padding()
39 .background(
40 Group {
41 if game.userScore < 1000 {
42 Circle()
43 } else {
44 Capsule()
45 }
46 }
47 .foregroundColor(.white)
48 )
49 }
50 .font(.title2)
51 .scaleEffect(game.scoreScaleAmount)
52 .frame(maxWidth: .infinity, alignment: .center)
53
54 Group {
55 HStack {
56 Image(systemName: "heart.fill")
57 Text("\(game.userLives)")
58 }
59 .font(.headline)
60 .foregroundColor(color)
61 .padding(10)
62 .background(
63 Capsule()
64 .foregroundColor(.white)
65 )
66 .scaleEffect(game.livesScaleAmount)
67 }
68 .font(.headline)
69 .frame(maxWidth: .infinity, alignment: .trailing)
70 }
71 }
72 }
73
74 struct GameToolbar_Previews: PreviewProvider {
75 static var previews: some View {
76 ZStack {
77 LinearGradient(gradient: .main, startPoint: .top, endPoint: .bottom)
78 .ignoresSafeArea()
79
80 GeometryReader { geo in
81 VStack {
82 GameToolbar(game: CountryGame(), color: .mayaBlue)
83
84 Spacer()
85 }
86 .padding()
87 }
88 }
89 }
90 }