Mercurial > public > geoquiz
comparison GeoQuiz/ProfileModalView.swift @ 19:f140bb277c96
refactor code
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 23 Oct 2022 00:11:38 +0100 |
parents | 8dac58bb4569 |
children | e281791e0494 |
comparison
equal
deleted
inserted
replaced
18:d20cf93c9812 | 19:f140bb277c96 |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 25/9/22. | 5 // Created by Dennis Concepción Martín on 25/9/22. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import PhotosUI | |
10 | 9 |
11 struct ProfileModalView: View { | 10 struct ProfileModalView: View { |
12 @ObservedObject var user: User | 11 @ObservedObject var userController: UserController |
13 @ObservedObject var storeKitRC: StoreKitRC | 12 @ObservedObject var storeKitController: StoreKitController |
14 | 13 |
15 @Environment(\.dismiss) var dismiss | 14 @State var showingEditModalView = false |
16 @Environment(\.managedObjectContext) var moc | |
17 | 15 |
18 @FetchRequest(sortDescriptors: [ | 16 @FetchRequest(sortDescriptors: [ |
19 SortDescriptor(\.date, order: .reverse), | 17 SortDescriptor(\.date, order: .reverse), |
20 ]) var playedGames: FetchedResults<PlayedGame> | 18 ]) var playedGames: FetchedResults<PlayedGame> |
21 | 19 |
22 @State private var showingEditModalView = false | 20 @Environment(\.dismiss) var dismiss |
21 @Environment(\.managedObjectContext) var moc | |
23 | 22 |
24 var body: some View { | 23 var body: some View { |
25 NavigationView { | 24 NavigationStack { |
26 List { | 25 ScrollView { |
27 Section { | 26 VStack(spacing: 30) { |
28 UserProfile(user: user, storeKitRC: storeKitRC) | 27 UserProfile( |
28 userController: userController, | |
29 storeKitController: storeKitController, | |
30 isShowing: $showingEditModalView | |
31 ) | |
32 | |
33 VStack(spacing: 20) { | |
34 ForEach(playedGames.prefix(8)) { playedGame in | |
35 RecentGame(game: playedGame) | |
36 } | |
37 } | |
29 } | 38 } |
30 | 39 .padding() |
31 Section { | |
32 UserProgress(playedGames: playedGames, gameType: .guessTheFlag) | |
33 UserProgress(playedGames: playedGames, gameType: .guessTheCapital) | |
34 UserProgress(playedGames: playedGames, gameType: .guessTheCountry) | |
35 UserProgress(playedGames: playedGames, gameType: .guessThePopulation) | |
36 } header: { | |
37 Text("Progress") | |
38 } | |
39 | |
40 Section { | |
41 ForEach(playedGames) { playedGame in | |
42 RecentGame(game: playedGame) | |
43 } | |
44 .onDelete(perform: deleteGame) | |
45 } header: { | |
46 Text("Recent games") | |
47 } | |
48 } | 40 } |
49 .background(.customBackground) | |
50 .navigationTitle("Profile") | 41 .navigationTitle("Profile") |
51 .navigationBarTitleDisplayMode(.inline) | 42 .navigationBarTitleDisplayMode(.inline) |
43 .background(Color(.systemGroupedBackground)) | |
52 .toolbar { | 44 .toolbar { |
53 ToolbarItem(placement: .cancellationAction) { | 45 ToolbarItem(placement: .cancellationAction) { |
54 Button { | 46 Button { |
55 dismiss() | 47 dismiss() |
56 } label: { | 48 } label: { |
57 Label("Exit", systemImage: "multiply") | 49 Label("Exit", systemImage: "multiply") |
58 } | 50 } |
59 } | 51 } |
60 | |
61 ToolbarItem(placement: .navigationBarTrailing) { | |
62 Button("Edit") { | |
63 showingEditModalView = true | |
64 } | |
65 } | |
66 } | 52 } |
67 | 53 |
68 .sheet(isPresented: $showingEditModalView) { | 54 .sheet(isPresented: $showingEditModalView) { |
69 ProfileEditModalView(user: user) | 55 ProfileEditModalView(user: userController) |
70 } | 56 } |
71 } | 57 } |
72 | |
73 } | |
74 | |
75 private func deleteGame(at offsets: IndexSet) { | |
76 for offset in offsets { | |
77 let game = playedGames[offset] | |
78 moc.delete(game) | |
79 } | |
80 | |
81 try? moc.save() | |
82 } | 58 } |
83 } | 59 } |
84 | 60 |
85 struct ProfileView_Previews: PreviewProvider { | 61 struct ProfileView_Previews: PreviewProvider { |
86 static var previews: some View { | 62 static var previews: some View { |
87 ProfileModalView(user: User(), storeKitRC: StoreKitRC()) | 63 ProfileModalView(userController: UserController(), storeKitController: StoreKitController()) |
88 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | 64 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) |
89 } | 65 } |
90 } | 66 } |