Mercurial > public > geoquiz
changeset 21:b145c408f791
refactor code
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 25 Oct 2022 15:30:01 +0200 |
parents | e281791e0494 |
children | 56add5561261 |
files | GeoQuiz.xcodeproj/project.pbxproj GeoQuiz/Components/FlagImage.swift GeoQuiz/Components/PlayedGamesList.swift GeoQuiz/Components/RecentGame.swift GeoQuiz/ContentView.swift GeoQuiz/GuessTheCapitalView.swift GeoQuiz/GuessTheFlagView.swift GeoQuiz/GuessThePopulationView.swift GeoQuiz/Logic/PersistenceController.swift GeoQuiz/Logic/StoreKitController.swift GeoQuiz/ProfileEditModalView.swift GeoQuiz/ProfileModalView.swift |
diffstat | 12 files changed, 90 insertions(+), 87 deletions(-) [+] |
line wrap: on
line diff
--- a/GeoQuiz.xcodeproj/project.pbxproj Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz.xcodeproj/project.pbxproj Tue Oct 25 15:30:01 2022 +0200 @@ -33,7 +33,6 @@ 955A658128D703EB00CEEC6D /* GameToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955A658028D703EB00CEEC6D /* GameToolbar.swift */; }; 955A658328D733E400CEEC6D /* GameProtocol+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955A658228D733E400CEEC6D /* GameProtocol+Extension.swift */; }; 955A65A928D7815E00CEEC6D /* HapticsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955A65A828D7815E00CEEC6D /* HapticsController.swift */; }; - 956273EA28CB2E98008DC094 /* FlagImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 956273E928CB2E98008DC094 /* FlagImage.swift */; }; 9590359528E098FF00B24560 /* ProfileModalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9590359428E098FF00B24560 /* ProfileModalView.swift */; }; 95919DB628F076BF00F21F8F /* UserController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95919DB528F076BF00F21F8F /* UserController.swift */; }; 95919DBC28F08D0600F21F8F /* FormLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95919DBB28F08D0600F21F8F /* FormLink.swift */; }; @@ -89,7 +88,6 @@ 955A658028D703EB00CEEC6D /* GameToolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameToolbar.swift; sourceTree = "<group>"; }; 955A658228D733E400CEEC6D /* GameProtocol+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GameProtocol+Extension.swift"; sourceTree = "<group>"; }; 955A65A828D7815E00CEEC6D /* HapticsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HapticsController.swift; sourceTree = "<group>"; }; - 956273E928CB2E98008DC094 /* FlagImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlagImage.swift; sourceTree = "<group>"; }; 9590359428E098FF00B24560 /* ProfileModalView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileModalView.swift; sourceTree = "<group>"; }; 95919DB528F076BF00F21F8F /* UserController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserController.swift; sourceTree = "<group>"; }; 95919DBB28F08D0600F21F8F /* FormLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormLink.swift; sourceTree = "<group>"; }; @@ -228,7 +226,6 @@ isa = PBXGroup; children = ( 95C4315828C6500000212131 /* GameButton.swift */, - 956273E928CB2E98008DC094 /* FlagImage.swift */, 95030CE928D1BA4D001AA3A1 /* AnswerButton.swift */, 955A658028D703EB00CEEC6D /* GameToolbar.swift */, 95BC392C28EC42570049AB49 /* CityMap.swift */, @@ -344,7 +341,6 @@ 95C645C229014442000CD570 /* GameInfoProtocol.swift in Sources */, 954AF4682905397A00180065 /* PlayedGamesList.swift in Sources */, 95C4315928C6500000212131 /* GameButton.swift in Sources */, - 956273EA28CB2E98008DC094 /* FlagImage.swift in Sources */, 951AFAED28E5657500A4A4BD /* CityModel.swift in Sources */, 950C535328F2FA3300179C78 /* BuyPremiumModalView.swift in Sources */, 954AF46A29053D4E00180065 /* LatestGamesPlaceholder.swift in Sources */,
--- a/GeoQuiz/Components/FlagImage.swift Sun Oct 23 11:48:39 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -// -// FlagImage.swift -// GeoQuiz -// -// Created by Dennis Concepción Martín on 9/9/22. -// - -import SwiftUI - -struct FlagImage: View { - var flagSymbol: String - - @Environment(\.colorScheme) var colorScheme - - var body: some View { - Image(flagSymbol) - .renderingMode(.original) - .resizable() - .scaledToFill() - } -} - -struct FlagImage_Previews: PreviewProvider { - static var previews: some View { - FlagImage(flagSymbol: "es") - .frame(height: 130) - } -}
--- a/GeoQuiz/Components/PlayedGamesList.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/Components/PlayedGamesList.swift Tue Oct 25 15:30:01 2022 +0200 @@ -8,7 +8,9 @@ import SwiftUI struct PlayedGamesList: View { - var playedGames: FetchedResults<PlayedGame> + @FetchRequest(sortDescriptors: [ + SortDescriptor(\.date, order: .reverse), + ]) var playedGames: FetchedResults<PlayedGame> @Environment(\.managedObjectContext) var moc @@ -17,7 +19,7 @@ ForEach(playedGames, id: \.id) { game in RecentGame(game: game) } - .onDelete { indexSet in + .onDelete{ indexSet in CoreDataController.deleteGame(at: indexSet, from: playedGames, with: moc) } } @@ -28,3 +30,12 @@ } } } + +struct PlayedGamesList_Previews: PreviewProvider { + static var previews: some View { + NavigationView { + PlayedGamesList() + .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + } + } +}
--- a/GeoQuiz/Components/RecentGame.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/Components/RecentGame.swift Tue Oct 25 15:30:01 2022 +0200 @@ -46,7 +46,6 @@ Text("\(game.score, format: .number) ⭐️") .font(.headline) - } } }
--- a/GeoQuiz/ContentView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/ContentView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -58,7 +58,7 @@ case .guessTheFlag: GuessTheFlagView() case .guessTheCapital: - GuessTheFlagView() + GuessTheCapitalView() case .guessTheCountry: GuessTheCountryView() case .guessThePopulation:
--- a/GeoQuiz/GuessTheCapitalView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/GuessTheCapitalView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -23,7 +23,10 @@ Spacer() - FlagImage(flagSymbol: game.correctAnswer.value.flag) + Image(game.correctAnswer.value.flag) + .renderingMode(.original) + .resizable() + .scaledToFit() .clipShape(RoundedRectangle(cornerRadius: 20)) .shadow(radius: 10) .frame(height: geo.size.height * 0.15)
--- a/GeoQuiz/GuessTheFlagView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/GuessTheFlagView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -51,7 +51,10 @@ .frame(height: geo.size.height * 0.15) .shadow(radius: 10) .overlay( - FlagImage(flagSymbol: game.data[countryName]!.flag) + Image(game.data[countryName]!.flag) + .renderingMode(.original) + .resizable() + .scaledToFill() .clipShape(Circle()) ) }
--- a/GeoQuiz/GuessThePopulationView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/GuessThePopulationView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -23,7 +23,10 @@ Spacer() - FlagImage(flagSymbol: game.correctAnswer.value.flag) + Image(game.correctAnswer.value.flag) + .renderingMode(.original) + .resizable() + .scaledToFit() .clipShape(RoundedRectangle(cornerRadius: 20)) .shadow(radius: 10) .frame(height: geo.size.height * 0.15)
--- a/GeoQuiz/Logic/PersistenceController.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/Logic/PersistenceController.swift Tue Oct 25 15:30:01 2022 +0200 @@ -6,40 +6,20 @@ // import CoreData +import SwiftUI class PersistenceController { static let shared = PersistenceController() - // Create mock data for previews + let container: NSPersistentCloudKitContainer + static var preview: PersistenceController = { let result = PersistenceController(inMemory: true) let viewContext = result.container.viewContext - - for _ in 0..<10 { - let playedGame = PlayedGame(context: viewContext) - playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! - playedGame.score = Int32.random(in: 0...50) - playedGame.date = Date() - - if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { - playedGame.correctAnswers = ["Bangladesh", "Belgium", "Burkina Faso", "Bermuda", "Jamaica"] - playedGame.wrongAnswers = ["Belarus", "Russia"] - } else { - playedGame.correctAnswers = ["Herat", "Lobito", "Darregueira", "San Juan"] - playedGame.wrongAnswers = ["San Luis", "Oranjestad"] - } - } - do { - try viewContext.save() - } catch { - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") - } - + createMockData(nil, viewContext) + return result }() - - let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: "GeoQuiz") @@ -56,4 +36,30 @@ container.viewContext.automaticallyMergesChangesFromParent = true } + + #if DEBUG + static func createMockData(_ playedGames: FetchedResults<PlayedGame>?, _ moc: NSManagedObjectContext) { + for _ in 0..<10 { + let playedGame = PlayedGame(context: moc) + + playedGame.type = GameType(rawValue: Int16.random(in: 0...3))! + playedGame.score = Int32.random(in: 0...50) + playedGame.date = Date() + + if playedGame.type == .guessTheFlag || playedGame.type == .guessTheCapital { + playedGame.correctAnswers = ["Bangladesh", "Belgium", "Burkina Faso", "Bermuda", "Jamaica"] + playedGame.wrongAnswers = ["Belarus", "Russia"] + } else { + playedGame.correctAnswers = ["Herat", "Lobito", "Darregueira", "San Juan"] + playedGame.wrongAnswers = ["San Luis", "Oranjestad"] + } + } + do { + try moc.save() + } catch { + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } + } + #endif }
--- a/GeoQuiz/Logic/StoreKitController.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/Logic/StoreKitController.swift Tue Oct 25 15:30:01 2022 +0200 @@ -26,9 +26,13 @@ @Published var premiumIsActive = false init() { + #if DEBUG + premiumIsActive = true + #else Purchases.shared.getCustomerInfo { (customerInfo, error) in self.customerInfo = customerInfo } + #endif } func buy(_ package: Package) {
--- a/GeoQuiz/ProfileEditModalView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/ProfileEditModalView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -16,9 +16,9 @@ @Environment(\.dismiss) var dismiss - init(user: UserController) { - self.userController = user - self._newUsername = State(initialValue: user.data.username) + init(userController: UserController) { + self.userController = userController + self._newUsername = State(initialValue: userController.data.username) } var body: some View { @@ -87,6 +87,6 @@ struct ProfileEditModalView_Previews: PreviewProvider { static var previews: some View { - ProfileEditModalView(user: UserController()) + ProfileEditModalView(userController: UserController()) } }
--- a/GeoQuiz/ProfileModalView.swift Sun Oct 23 11:48:39 2022 +0100 +++ b/GeoQuiz/ProfileModalView.swift Tue Oct 25 15:30:01 2022 +0200 @@ -30,25 +30,25 @@ isShowing: $showingEditModalView ) - VStack(spacing: 20) { - HStack { - Text("Latest games") - .foregroundColor(.secondary) - - Spacer() + if !playedGames.isEmpty { + VStack(spacing: 20) { + HStack { + Text("Latest games") + .foregroundColor(.secondary) + + Spacer() + + NavigationLink { + PlayedGamesList() + } label: { + HStack { + Text("Show all") + Image(systemName: "chevron.right") + } + } + .disabled(playedGames.isEmpty) + } - NavigationLink { - PlayedGamesList(playedGames: playedGames) - } label: { - Text("Show all") - } - .disabled(playedGames.isEmpty) - } - - if playedGames.isEmpty { - Spacer() - LatestGamesPlaceholder() - } else { ForEach(playedGames.prefix(8)) { playedGame in RecentGame(game: playedGame) .padding() @@ -75,8 +75,14 @@ } .sheet(isPresented: $showingEditModalView) { - ProfileEditModalView(user: userController) + ProfileEditModalView(userController: userController) } + + #if DEBUG + .onAppear { + + } + #endif } } }