17
|
1 //
|
|
2 // UserProgressHelper.swift
|
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 20/10/22.
|
|
6 //
|
|
7
|
|
8 import SwiftUI
|
|
9
|
|
10 struct ViewTest: View {
|
|
11 var body: some View {
|
|
12 GeometryReader { geo in
|
|
13 ZStack {
|
|
14 Capsule()
|
|
15 .frame(height: 6)
|
|
16 }
|
|
17 }
|
|
18 }
|
|
19 }
|
|
20
|
|
21 struct UserProgress: View {
|
|
22 let name: String
|
|
23 let gradient: Gradient
|
|
24 let score: Int
|
|
25 let maxScore: Int
|
|
26 let pctScore: Double
|
|
27
|
|
28 var body: some View {
|
|
29 VStack {
|
|
30 Spacer()
|
|
31 HStack {
|
|
32 Text(name)
|
|
33 .font(.headline)
|
|
34
|
|
35 Spacer()
|
|
36
|
|
37 Text("\(score) of \(maxScore)")
|
|
38 .font(.callout)
|
|
39 .foregroundColor(.secondary)
|
|
40 }
|
|
41
|
|
42 GeometryReader { geo in
|
|
43 ZStack(alignment: .leading) {
|
|
44 Capsule()
|
|
45 .foregroundColor(.customBackground)
|
|
46 .frame(height: 6)
|
|
47
|
|
48 Capsule()
|
|
49 .fill(
|
|
50 LinearGradient(
|
|
51 gradient: gradient,
|
|
52 startPoint: .trailing, endPoint: .leading
|
|
53 )
|
|
54 )
|
|
55 .frame(width: geo.size.width * pctScore, height: 6)
|
|
56 }
|
|
57 .frame(height: geo.size.height)
|
|
58 }
|
|
59 }
|
|
60 }
|
|
61
|
|
62 init(playedGames: FetchedResults<PlayedGame>, gameType: GameType) {
|
|
63 switch(gameType) {
|
|
64 case . guessTheFlag:
|
|
65 self.name = GuessTheFlagInfo.name
|
|
66 self.gradient = GuessTheFlagInfo.gradient
|
|
67 self.maxScore = GuessTheFlagInfo.numberOfQuestions
|
|
68 case .guessTheCapital:
|
|
69 self.name = GuessTheCapitalInfo.name
|
|
70 self.gradient = GuessTheCapitalInfo.gradient
|
|
71 self.maxScore = GuessTheCapitalInfo.numberOfQuestions
|
|
72 case .guessTheCountry:
|
|
73 self.name = GuessTheCountryInfo.name
|
|
74 self.gradient = GuessTheCountryInfo.gradient
|
|
75 self.maxScore = GuessTheCountryInfo.numberOfQuestions
|
|
76 case .guessThePopulation:
|
|
77 self.name = GuessThePopulationInfo.name
|
|
78 self.gradient = GuessThePopulationInfo.gradient
|
|
79 self.maxScore = GuessThePopulationInfo.numberOfQuestions
|
|
80 }
|
|
81
|
|
82 let games = playedGames.filter { $0.type == gameType }
|
|
83 self.score = Int(games.max { $0.score < $1.score }?.score ?? 0)
|
|
84 self.pctScore = Double(score) / Double(maxScore)
|
|
85 }
|
|
86 }
|