Skip to content

Commit e1cbde5

Browse files
author
Achyut Kumar M
committed
modify view and add dismissSheet closure to CreateNoteView
1 parent 41ee24c commit e1cbde5

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

GoInfoGame/GoInfoGame/UI/Map/CreateNoteView.swift

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,101 @@
66
//
77

88
import SwiftUI
9+
import CoreLocation
910

1011
struct CreateNoteView: View {
1112

13+
@State var coordinates: CLLocationCoordinate2D
1214
@State private var noteText = ""
1315
@Binding var showNotesBox: Bool
16+
@State private var alertMessage = ""
17+
@StateObject private var noteViewModel = NotesViewModel()
18+
19+
@State var dismissSheet: (String) -> ()
1420

1521
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)
3429

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+
}
3764
}
38-
.padding()
39-
.frame(maxWidth: .infinity)
40-
.background(Color.red)
41-
.foregroundColor(.white)
42-
.cornerRadius(8)
4365
.padding(.horizontal, 20)
4466
}
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+
}
4674
}
47-
.padding(.top, 10)
4875
}
4976

50-
func submitNote() {
77+
func submitNote() async throws {
5178
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+
}
52100
}
53101
}
54102

55103

56104
#Preview {
57-
CreateNoteView(showNotesBox: .constant(true))
105+
CreateNoteView(coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), showNotesBox: .constant(true), dismissSheet: {_ in })
58106
}

GoInfoGame/GoInfoGame/UI/Map/MapView.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,16 @@ struct MapView: View {
186186
}
187187
}
188188
.sheet(isPresented: $showCreateNoteSheet, content: {
189-
CreateNoteView(showNotesBox: $showCreateNoteSheet)
189+
CreateNoteView(coordinates: tappedCoordinate ?? CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), showNotesBox: $showCreateNoteSheet, dismissSheet: { message in
190+
if message.contains("wrong") {
191+
alertIcon = "exclamationmark.triangle.fill"
192+
} else {
193+
alertIcon = "checkmark.circle.fill"
194+
}
195+
alertMessage = message
196+
showAlert = true
197+
198+
})
190199
.presentationDetents([.fraction(0.6)])
191200
.presentationDragIndicator(.visible)
192201

0 commit comments

Comments
 (0)