|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import SwiftUI |
| 9 | +import CoreLocation |
9 | 10 |
|
10 | 11 | struct CreateNoteView: View { |
11 | 12 |
|
| 13 | + @State var coordinates: CLLocationCoordinate2D |
12 | 14 | @State private var noteText = "" |
13 | 15 | @Binding var showNotesBox: Bool |
| 16 | + @State private var alertMessage = "" |
| 17 | + @StateObject private var noteViewModel = NotesViewModel() |
| 18 | + |
| 19 | + @State var dismissSheet: (String) -> () |
14 | 20 |
|
15 | 21 | var body: some View { |
16 | | - VStack(alignment: .leading, spacing: 10) { |
17 | | - TextEditor(text: $noteText) |
18 | | - .padding(10) |
19 | | - .background(Color(.systemGray6)) |
20 | | - .cornerRadius(8) |
21 | | - .padding(.horizontal, 20) |
22 | | - |
23 | | - HStack { |
24 | | - Button("Submit") { |
25 | | - submitNote() |
26 | | - showNotesBox = false |
27 | | - } |
28 | | - .padding() |
29 | | - .frame(maxWidth: .infinity) |
30 | | - .background(Color.blue) |
31 | | - .foregroundColor(.white) |
32 | | - .cornerRadius(8) |
33 | | - .padding(.horizontal, 20) |
| 22 | + ZStack { |
| 23 | + VStack(alignment: .leading, spacing: 10) { |
| 24 | + TextEditor(text: $noteText) |
| 25 | + .padding(10) |
| 26 | + .background(Color(.systemGray6)) |
| 27 | + .cornerRadius(8) |
| 28 | + .padding(.horizontal, 20) |
34 | 29 |
|
35 | | - Button("Cancel") { |
36 | | - showNotesBox = false |
| 30 | + HStack { |
| 31 | + Button(action: { |
| 32 | + Task { |
| 33 | + do { |
| 34 | + try await submitNote() |
| 35 | + } catch { |
| 36 | + print("Error adding feature: \(error)") |
| 37 | + } |
| 38 | + } |
| 39 | + }) { |
| 40 | + if noteViewModel.isLoading { |
| 41 | + ProgressView() |
| 42 | + } else { |
| 43 | + Text("Submit") |
| 44 | + .font(.custom("Lato-Bold", size: 16)) |
| 45 | + .foregroundColor(.white) |
| 46 | + .padding() |
| 47 | + .frame(maxWidth: .infinity) |
| 48 | + .background(noteText != "" ? Color(red: 135/255, green: 62/255, blue: 242/255) : Color.gray) |
| 49 | + .cornerRadius(9) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Button (action: { |
| 54 | + showNotesBox = false |
| 55 | + }) { |
| 56 | + Text("Cancel") |
| 57 | + .font(.custom("Lato-Bold", size: 16)) |
| 58 | + .foregroundColor(.white) |
| 59 | + .padding() |
| 60 | + .frame(maxWidth: .infinity) |
| 61 | + .background(Color.red) |
| 62 | + .cornerRadius(9) |
| 63 | + } |
37 | 64 | } |
38 | | - .padding() |
39 | | - .frame(maxWidth: .infinity) |
40 | | - .background(Color.red) |
41 | | - .foregroundColor(.white) |
42 | | - .cornerRadius(8) |
43 | 65 | .padding(.horizontal, 20) |
44 | 66 | } |
45 | | - .padding(.horizontal, 20) |
| 67 | + .padding(.top, 10) |
| 68 | + |
| 69 | + if noteViewModel.isLoading { |
| 70 | + Color.black.opacity(0.3) |
| 71 | + .edgesIgnoringSafeArea(.all) |
| 72 | + ActivityView(activityText: "Submitting Note") |
| 73 | + } |
46 | 74 | } |
47 | | - .padding(.top, 10) |
48 | 75 | } |
49 | 76 |
|
50 | | - func submitNote() { |
| 77 | + func submitNote() async throws { |
51 | 78 | print("Note to be submitted: \(noteText)") |
| 79 | + |
| 80 | + do { |
| 81 | + let notesResult = try await noteViewModel.createNote(note: noteText, lat: coordinates.latitude, long: coordinates.longitude) |
| 82 | + |
| 83 | + if notesResult { |
| 84 | + print("Notes composed successfully") |
| 85 | + alertMessage = "Note submitted successfully" |
| 86 | + dismissSheet(alertMessage) |
| 87 | + |
| 88 | + } |
| 89 | + } catch { |
| 90 | + alertMessage = "Error submitting note. Please try again later." |
| 91 | + print("Error creating note: \(error.localizedDescription)") |
| 92 | + dismissSheet(alertMessage) |
| 93 | + } |
| 94 | + |
| 95 | + await MainActor.run { |
| 96 | + noteViewModel.isLoading = false |
| 97 | + showNotesBox = false |
| 98 | + dismissSheet(alertMessage) |
| 99 | + } |
52 | 100 | } |
53 | 101 | } |
54 | 102 |
|
55 | 103 |
|
56 | 104 | #Preview { |
57 | | - CreateNoteView(showNotesBox: .constant(true)) |
| 105 | + CreateNoteView(coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), showNotesBox: .constant(true), dismissSheet: {_ in }) |
58 | 106 | } |
0 commit comments