comparison 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
comparison
equal deleted inserted replaced
15:f1967f8cc67b 16:1011e56b7832
14 14
15 @Environment(\.dismiss) var dismiss 15 @Environment(\.dismiss) var dismiss
16 @Environment(\.managedObjectContext) var moc 16 @Environment(\.managedObjectContext) var moc
17 17
18 @FetchRequest(sortDescriptors: [ 18 @FetchRequest(sortDescriptors: [
19 SortDescriptor(\.date), 19 SortDescriptor(\.date, order: .reverse),
20 ]) var playedGames: FetchedResults<PlayedGame> 20 ]) var playedGames: FetchedResults<PlayedGame>
21 21
22 @State private var showingEditModalView = false 22 @State private var showingEditModalView = false
23 23
24 var body: some View { 24 var body: some View {
25 NavigationView { 25 NavigationView {
26 Form { 26 ScrollView {
27 Section { 27 VStack(alignment: .leading, spacing: 15) {
28 HStack(spacing: 20) { 28 UserProfile(user: user, storeKitRC: storeKitRC)
29 UserImage(uiImage: user.data.uiImage)
30 29
31 VStack(alignment: .leading, spacing: 8) { 30 UserProgress(playedGames: playedGames)
32 Text(user.data.username) 31
33 .font(.title) 32 ForEach(playedGames) { playedGame in
34 .fontWeight(.semibold) 33 RecentGame(game: playedGame)
35 34 }
36 if storeKitRC.isActive { 35 .onDelete(perform: deleteGame)
37 Text("Premium user ⭐️") 36 }
38 .foregroundColor(.secondary) 37 .padding()
39 } 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
40 } 54 }
41 } 55 }
42 } 56 }
43 57
44 Section { 58 .sheet(isPresented: $showingEditModalView) {
45 VStack(alignment: .leading) { 59 ProfileEditModalView(user: user)
46 Text("Game 1")
47 Capsule()
48 .frame(height: 6)
49 }
50
51 VStack(alignment: .leading) {
52 Text("Game 1")
53 Capsule()
54 .frame(height: 6)
55 }
56 VStack(alignment: .leading) {
57 Text("Game 1")
58 Capsule()
59 .frame(height: 6)
60 }
61 VStack(alignment: .leading) {
62 Text("Game 1")
63 Capsule()
64 .frame(height: 6)
65 }
66 } header: {
67 Text("Progress")
68 }
69
70 Section {
71 ForEach(playedGames) { playedGame in
72 HStack {
73 Text("\(playedGame.id)")
74 Text("\(playedGame.date)")
75 }
76 }
77 } header: {
78 Text("Recent games")
79 } 60 }
80 } 61 }
81 .navigationTitle("Profile") 62
82 .navigationBarTitleDisplayMode(.inline) 63 }
83 .toolbar { 64
84 ToolbarItem(placement: .cancellationAction) { 65 private func deleteGame(at offsets: IndexSet) {
85 Button { 66 for offset in offsets {
86 dismiss() 67 let game = playedGames[offset]
87 } label: { 68 moc.delete(game)
88 Label("Exit", systemImage: "multiply")
89 }
90 }
91
92 ToolbarItem(placement: .navigationBarTrailing) {
93 Button("Edit") {
94 showingEditModalView = true
95 }
96 }
97 }
98
99 .sheet(isPresented: $showingEditModalView) {
100 ProfileEditModalView(user: user)
101 }
102 } 69 }
70
71 try? moc.save()
103 } 72 }
104 } 73 }
105 74
106 struct ProfileView_Previews: PreviewProvider { 75 struct ProfileView_Previews: PreviewProvider {
107 static var previews: some View { 76 static var previews: some View {
108 ProfileModalView(user: User(), storeKitRC: StoreKitRC()) 77 ProfileModalView(user: User(), storeKitRC: StoreKitRC())
78 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
109 } 79 }
110 } 80 }