14
|
1 //
|
|
2 // ProfileEditModalView.swift
|
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 19/10/22.
|
|
6 //
|
|
7
|
|
8 import SwiftUI
|
|
9 import PhotosUI
|
|
10
|
|
11 struct ProfileEditModalView: View {
|
|
12 @ObservedObject var user: User
|
|
13 @Environment(\.dismiss) var dismiss
|
|
14
|
|
15 @State private var selectedItem: PhotosPickerItem? = nil
|
|
16
|
|
17 var body: some View {
|
|
18 NavigationStack {
|
|
19 Form {
|
|
20 Section {
|
|
21 HStack {
|
|
22 Spacer()
|
|
23 ZStack {
|
|
24 UserImage(uiImage: user.data.uiImage)
|
|
25 .onChange(of: selectedItem) { newItem in
|
|
26 Task {
|
|
27 if let data = try? await newItem?.loadTransferable(type: Data.self) {
|
|
28 user.data.imageData = data
|
|
29 }
|
|
30 }
|
|
31 }
|
|
32
|
|
33 PhotosPicker(
|
|
34 selection: $selectedItem,
|
|
35 matching: .images,
|
|
36 photoLibrary: .shared()) {
|
|
37 EmptyView()
|
|
38 }
|
|
39 }
|
|
40
|
|
41 Spacer()
|
|
42 }
|
|
43 } header: {
|
|
44 Text("Profile image")
|
|
45 }
|
|
46
|
|
47 Section {
|
|
48 TextField("Enter a username", text: $user.data.username)
|
|
49 } header: {
|
|
50 Text("Username")
|
|
51 }
|
|
52 }
|
|
53 .navigationTitle("Edit profile")
|
|
54 .navigationBarTitleDisplayMode(.inline)
|
|
55 .toolbar {
|
|
56 ToolbarItem(placement: .navigationBarTrailing) {
|
|
57 Button("Done") {
|
|
58 dismiss()
|
|
59 }
|
|
60 }
|
|
61 }
|
|
62 }
|
|
63 }
|
|
64 }
|
|
65
|
|
66 struct ProfileEditModalView_Previews: PreviewProvider {
|
|
67 static var previews: some View {
|
|
68 ProfileEditModalView(user: User())
|
|
69 }
|
|
70 }
|