Mercurial > public > geoquiz
comparison GeoQuiz/Components/UserProgressComponent.swift @ 16:1011e56b7832
implement user profile
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 20 Oct 2022 13:49:42 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
15:f1967f8cc67b | 16:1011e56b7832 |
---|---|
1 // | |
2 // GameProgressHelper.swift | |
3 // GeoQuiz | |
4 // | |
5 // Created by Dennis Concepción Martín on 20/10/22. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 struct UserProgress: View { | |
11 private let games: [(key: String, value: GameProgress)] | |
12 | |
13 var body: some View { | |
14 VStack(alignment: .leading) { | |
15 VStack(spacing: 10) { | |
16 ForEach(games, id: \.key) { game in | |
17 HStack { | |
18 Text(game.key) | |
19 .font(.headline) | |
20 | |
21 Spacer() | |
22 | |
23 Text("\(game.value.highestScore) of \(game.value.numberOfQuestions)") | |
24 .font(.caption) | |
25 .foregroundColor(.secondary) | |
26 } | |
27 | |
28 ProgressBar(pctScore: game.value.pctScore, gradient: game.value.gradient) | |
29 | |
30 if game.key != games.last!.key { | |
31 Divider() | |
32 } | |
33 } | |
34 } | |
35 } | |
36 .padding() | |
37 .background( | |
38 RoundedRectangle(cornerRadius: 20) | |
39 .foregroundColor(.white) | |
40 ) | |
41 } | |
42 | |
43 init(playedGames: FetchedResults<PlayedGame>) { | |
44 let flagGames = playedGames.filter { $0.type == .guessTheFlag } | |
45 let capitalGames = playedGames.filter { $0.type == .guessTheCapital } | |
46 let countryGames = playedGames.filter { $0.type == .guessTheCountry } | |
47 let populationGames = playedGames.filter { $0.type == .guessThePopulation } | |
48 | |
49 self.games = [ | |
50 GuessTheFlagInfo.name: GameProgress( | |
51 numberOfQuestions: GuessTheFlagInfo.numberOfQuestions, | |
52 highestScore: Int(flagGames.max { $0.score < $1.score }?.score ?? 0), | |
53 gradient: GuessTheFlagInfo.gradient | |
54 ), | |
55 GuessTheCapitalInfo.name: GameProgress( | |
56 numberOfQuestions: GuessTheCapitalInfo.numberOfQuestions, | |
57 highestScore: Int(capitalGames.max { $0.score < $1.score }?.score ?? 0), | |
58 gradient: GuessTheCapitalInfo.gradient | |
59 ), | |
60 GuessTheCountryInfo.name: GameProgress( | |
61 numberOfQuestions: GuessTheCountryInfo.numberOfQuestions, | |
62 highestScore: Int(countryGames.max { $0.score < $1.score }?.score ?? 0), | |
63 gradient: GuessTheCountryInfo.gradient | |
64 ), | |
65 GuessThePopulationInfo.name: GameProgress( | |
66 numberOfQuestions: GuessThePopulationInfo.numberOfQuestions, | |
67 highestScore: Int(populationGames.max { $0.score < $1.score }?.score ?? 0), | |
68 gradient: GuessThePopulationInfo.gradient | |
69 ) | |
70 ].sorted { $0.value.pctScore > $1.value.pctScore } | |
71 } | |
72 | |
73 private struct GameProgress { | |
74 let numberOfQuestions: Int | |
75 let highestScore: Int | |
76 var pctScore: Double { | |
77 Double(highestScore) / Double(numberOfQuestions) | |
78 } | |
79 | |
80 let gradient: Gradient | |
81 } | |
82 } |