Mercurial > public > geoquiz
comparison GeoQuiz/GuessTheCapitalView.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 GuessTheCapitalView: View { | 10 struct GuessTheCapitalView: View { |
11 @StateObject var game = CountryGameController() | 11 @StateObject var gameController = CountryGameController() |
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: .secondary, startPoint: .top, endPoint: .bottom) | 17 LinearGradient(gradient: .secondary, 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: .atomicTangerine) | 22 GameToolbar(gameController: gameController, color: .atomicTangerine) |
23 .padding(.bottom) | 23 .padding(.bottom) |
24 | 24 |
25 Spacer() | 25 Spacer() |
26 | 26 |
27 Image(game.correctAnswer.value.flag) | 27 /* |
28 THE PROBLEM: | |
29 SwiftUI caches the image when it's shown using the `Image(string)` API. | |
30 Once the image is not showed anymore, SwiftUI doesn't release memory, | |
31 so it keeps caching new images until the app crashes | |
32 UIImage(contentsOfFile: path) doesn't cache the image | |
33 | |
34 THE SOLUTION: | |
35 Using `UIImage(contentsOfFile: path)` images aren't cached. | |
36 */ | |
37 | |
38 let flag = gameController.correctAnswer.value.flag | |
39 let flagPath = Bundle.main.path(forResource: flag, ofType: "png")! | |
40 Image(uiImage: UIImage(contentsOfFile: flagPath)!) | |
28 .renderingMode(.original) | 41 .renderingMode(.original) |
29 .resizable() | 42 .resizable() |
30 .scaledToFit() | 43 .scaledToFit() |
31 .clipShape(RoundedRectangle(cornerRadius: 20)) | 44 .clipShape(RoundedRectangle(cornerRadius: 20)) |
32 .shadow(radius: 10) | 45 .shadow(radius: 10) |
34 | 47 |
35 Spacer() | 48 Spacer() |
36 | 49 |
37 VStack(alignment: .leading) { | 50 VStack(alignment: .leading) { |
38 VStack(alignment: .leading, spacing: 10) { | 51 VStack(alignment: .leading, spacing: 10) { |
39 Text("Question \(game.questionCounter) of \(game.data.count)") | 52 Text("Question \(gameController.questionCounter) of \(gameController.data.count)") |
40 .font(.title3) | 53 .font(.title3) |
41 .foregroundColor(.white.opacity(0.7)) | 54 .foregroundColor(.white.opacity(0.7)) |
42 | 55 |
43 Text("What is the capital of \(game.correctAnswer.key)?") | 56 Text("What is the capital of \(gameController.correctAnswer.key)?") |
44 .font(.title) | 57 .font(.title) |
45 .fontWeight(.semibold) | 58 .fontWeight(.semibold) |
46 .foregroundColor(.white) | 59 .foregroundColor(.white) |
47 } | 60 } |
48 | 61 |
49 VStack(spacing: 15) { | 62 VStack(spacing: 15) { |
50 ForEach(Array(game.userChoices.keys), id: \.self) { countryName in | 63 ForEach(Array(gameController.userChoices.keys), id: \.self) { countryName in |
51 Button { | 64 Button { |
52 game.answer((key: countryName, value: game.data[countryName]!)) { | 65 gameController.answer( |
53 game.selector() | 66 choice: (key: countryName, value: gameController.data[countryName]!), |
67 wrongMessage: "That's the capital of \(countryName)" | |
68 ) { | |
69 gameController.selector() | |
54 } | 70 } |
55 } label: { | 71 } label: { |
56 AnswerButton( | 72 AnswerButton( |
57 name: game.data[countryName]!.capital, | 73 name: gameController.data[countryName]!.capital, |
58 color: .chinaPink | 74 color: .chinaPink |
59 ) | 75 ) |
60 .frame(height: geo.size.height * 0.08) | 76 .frame(height: geo.size.height * 0.08) |
61 } | 77 } |
62 } | 78 } |
66 } | 82 } |
67 .padding() | 83 .padding() |
68 } | 84 } |
69 } | 85 } |
70 .navigationBarHidden(true) | 86 .navigationBarHidden(true) |
71 .modifier(GameAlertsModifier(game: game, gameType: .guessTheCapital, moc: moc)) | 87 .modifier(GameAlertsModifier(gameController: gameController, gameType: .guessTheCapital, moc: moc)) |
72 } | 88 } |
73 } | 89 } |
74 | 90 |
75 struct GuessTheCapitalView_Previews: PreviewProvider { | 91 struct GuessTheCapitalView_Previews: PreviewProvider { |
76 static var previews: some View { | 92 static var previews: some View { |
77 GuessTheCapitalView() | 93 GuessTheCapitalView() |
78 .previewDevice(PreviewDevice(rawValue: "iPhone 14 Pro Max")) | |
79 .previewDisplayName("iPhone 14 Pro Max") | |
80 | |
81 GuessTheCapitalView() | |
82 .previewDevice(PreviewDevice(rawValue: "iPad Pro (12.9-inch) (5th generation)")) | |
83 .previewDisplayName("iPad Pro (12.9-inch)") | |
84 | |
85 GuessTheCapitalView() | |
86 .previewDevice(PreviewDevice(rawValue: "iPhone 8")) | |
87 .previewDisplayName("iPhone 8") | |
88 } | 94 } |
89 } | 95 } |