Mercurial > public > geoquiz
annotate GeoQuiz/ProfileModalView.swift @ 16:1011e56b7832
implement user profile
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 20 Oct 2022 13:49:42 +0200 |
parents | f1967f8cc67b |
children | 8dac58bb4569 |
rev | line source |
---|---|
6 | 1 // |
2 // ProfileModalView.swift | |
3 // GeoQuiz | |
4 // | |
5 // Created by Dennis Concepción Martín on 25/9/22. | |
6 // | |
7 | |
8 import SwiftUI | |
14 | 9 import PhotosUI |
6 | 10 |
11 struct ProfileModalView: View { | |
14 | 12 @ObservedObject var user: User |
13 @ObservedObject var storeKitRC: StoreKitRC | |
14 | |
15 @Environment(\.dismiss) var dismiss | |
15
f1967f8cc67b
first iteration of core data
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
16 @Environment(\.managedObjectContext) var moc |
f1967f8cc67b
first iteration of core data
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
17 |
f1967f8cc67b
first iteration of core data
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
18 @FetchRequest(sortDescriptors: [ |
16 | 19 SortDescriptor(\.date, order: .reverse), |
15
f1967f8cc67b
first iteration of core data
Dennis C. M. <dennis@denniscm.com>
parents:
14
diff
changeset
|
20 ]) var playedGames: FetchedResults<PlayedGame> |
14 | 21 |
22 @State private var showingEditModalView = false | |
23 | |
6 | 24 var body: some View { |
16 | 25 NavigationView { |
26 ScrollView { | |
27 VStack(alignment: .leading, spacing: 15) { | |
28 UserProfile(user: user, storeKitRC: storeKitRC) | |
29 | |
30 UserProgress(playedGames: playedGames) | |
14 | 31 |
16 | 32 ForEach(playedGames) { playedGame in |
33 RecentGame(game: playedGame) | |
34 } | |
35 .onDelete(perform: deleteGame) | |
36 } | |
37 .padding() | |
38 } | |
39 .background(.customBackground) | |
40 .navigationTitle("Profile") | |
41 .navigationBarTitleDisplayMode(.inline) | |
42 .toolbar { | |
43 ToolbarItem(placement: .cancellationAction) { | |
44 Button { | |
45 dismiss() | |
46 } label: { | |
47 Label("Exit", systemImage: "multiply") | |
48 } | |
49 } | |
50 | |
51 ToolbarItem(placement: .navigationBarTrailing) { | |
52 Button("Edit") { | |
53 showingEditModalView = true | |
14 | 54 } |
55 } | |
56 } | |
57 | |
16 | 58 .sheet(isPresented: $showingEditModalView) { |
59 ProfileEditModalView(user: user) | |
14 | 60 } |
61 } | |
16 | 62 |
63 } | |
64 | |
65 private func deleteGame(at offsets: IndexSet) { | |
66 for offset in offsets { | |
67 let game = playedGames[offset] | |
68 moc.delete(game) | |
14 | 69 } |
16 | 70 |
71 try? moc.save() | |
6 | 72 } |
73 } | |
74 | |
75 struct ProfileView_Previews: PreviewProvider { | |
76 static var previews: some View { | |
14 | 77 ProfileModalView(user: User(), storeKitRC: StoreKitRC()) |
16 | 78 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) |
6 | 79 } |
80 } |