From 25bacca43f2ef50004ea9431b5af6b24acc24a6a Mon Sep 17 00:00:00 2001 From: KhubaibKhan4 <18.bscs.803@gmail.com> Date: Mon, 25 Nov 2024 22:36:17 +0500 Subject: [PATCH] -Add Notes Enhancement --- Notes/AddNotes.swift | 158 +++++++++++++-------------- Notes/HomeScreen.swift | 210 ++++++++++++++++++------------------ Notes/todo/TodoScreen.swift | 10 +- 3 files changed, 189 insertions(+), 189 deletions(-) diff --git a/Notes/AddNotes.swift b/Notes/AddNotes.swift index 094a909..2f91a42 100644 --- a/Notes/AddNotes.swift +++ b/Notes/AddNotes.swift @@ -30,84 +30,84 @@ struct AddNotes: View { var onSave: () -> Void var body: some View { - VStack{ - Form { - Section("Note") { - TextField("Title", text: $title) - TextField("Description", text: $desc) + VStack{ + Form { + Section("Note") { + TextField("Title", text: $title) + TextField("Description", text: $desc) + } + Section("Map") { + Button("Add Map", systemImage: "map.circle") { + isMapSheet = !isMapSheet } - Section("Map") { - Button("Add Map", systemImage: "map.circle") { - isMapSheet = !isMapSheet + .foregroundColor(.white) + .buttonStyle(.borderedProminent) + + Map(position: $position) { + if let location = selectedLocation { + Marker(selectedLocationName, coordinate: location) } - .foregroundColor(.white) - .buttonStyle(.borderedProminent) - - Map(position: $position) { - if let location = selectedLocation { - Marker(selectedLocationName, coordinate: location) - } - } - .frame(height: 250) - .clipShape(RoundedRectangle(cornerRadius: 12)) - .overlay( - selectedLocation == nil ? - Text("No location selected") - .foregroundColor(.gray) - .padding() - .background(Color.white.opacity(0.8)) - .cornerRadius(10) - : nil - ) } + .frame(height: 250) + .clipShape(RoundedRectangle(cornerRadius: 12)) + .overlay( + selectedLocation == nil ? + Text("No location selected") + .foregroundColor(.gray) + .padding() + .background(Color.white.opacity(0.8)) + .cornerRadius(10) + : nil + ) } - }.navigationTitle("Add Notes") + } + }.navigationTitle("Add Notes") .navigationBarBackButtonHidden(true) .sheet(isPresented: $isMapSheet, content: { NavigationStack { ZStack { - MapReader { proxy in - Map(position: $position) { - Marker(coordinate: selectedLocation ?? CLLocationCoordinate2D(latitude: 48.8566, longitude: 2.3522)) { - Label(selectedLocationName, image: "mappin") - } - }.mapControls({ - /// Shows up when you pitch to zoom - MapScaleView() - /// Shows up when you rotate the map - MapCompass() - /// 3D and 2D button on the top right - MapPitchToggle() - }) - .frame(width: .infinity, height: .infinity) - .onTapGesture {position in - if let mapLocation = proxy.convert(position, from: .local) { - print("Location: \(mapLocation)") - selectedLocation = mapLocation - locationList.append(mapLocation) - - self.position = .region( - MKCoordinateRegion( - center: mapLocation, - span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) - ) - ) - fetchLocationName(for: mapLocation) { name in - self.selectedLocationName = name ?? "Unknown" - } - } + MapReader { proxy in + Map(position: $position) { + Marker(coordinate: selectedLocation ?? CLLocationCoordinate2D(latitude: 48.8566, longitude: 2.3522)) { + Label(selectedLocationName, image: "mappin") + } + }.mapControls({ + /// Shows up when you pitch to zoom + MapScaleView() + /// Shows up when you rotate the map + MapCompass() + /// 3D and 2D button on the top right + MapPitchToggle() + }) + .frame(width: .infinity, height: .infinity) + .onTapGesture {position in + if let mapLocation = proxy.convert(position, from: .local) { + print("Location: \(mapLocation)") + selectedLocation = mapLocation + locationList.append(mapLocation) + + self.position = .region( + MKCoordinateRegion( + center: mapLocation, + span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) + ) + ) + fetchLocationName(for: mapLocation) { name in + self.selectedLocationName = name ?? "Unknown" } + } } + } }.navigationTitle("Add Map") .toolbarTitleDisplayMode(.inline) .toolbarBackground(.hidden, for: ToolbarPlacement.navigationBar) - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Button("Add Location") { - isMapSheet = !isMapSheet + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + Button("Add Location") { + isMapSheet = !isMapSheet + } } } - } } }) .toolbar { @@ -137,22 +137,22 @@ struct AddNotes: View { } private func fetchLocationName(for coordinate: CLLocationCoordinate2D, completion: @escaping (String?) -> Void) { - let geocoder = CLGeocoder() - let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude) - geocoder.reverseGeocodeLocation(location) { placemarks, error in - if let error = error { - print("Reverse geocoding error: \(error.localizedDescription)") - completion(nil) - return - } - if let placemark = placemarks?.first { - let name = [placemark.name, placemark.locality, placemark.country] - .compactMap { $0 } - .joined(separator: ", ") - completion(name) - } else { - completion(nil) - } + let geocoder = CLGeocoder() + let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude) + geocoder.reverseGeocodeLocation(location) { placemarks, error in + if let error = error { + print("Reverse geocoding error: \(error.localizedDescription)") + completion(nil) + return + } + if let placemark = placemarks?.first { + let name = [placemark.name, placemark.locality, placemark.country] + .compactMap { $0 } + .joined(separator: ", ") + completion(name) + } else { + completion(nil) } } + } } diff --git a/Notes/HomeScreen.swift b/Notes/HomeScreen.swift index d5d5292..4d43375 100644 --- a/Notes/HomeScreen.swift +++ b/Notes/HomeScreen.swift @@ -34,7 +34,7 @@ struct HomeScreen: View{ if searchText.isEmpty { return notestList } else { - return notestList.filter { $0.title.localizedCaseInsensitiveContains(searchText) } + return notestList.filter { $0.title.localizedCaseInsensitiveContains(searchText) } } } @State var isSearchPresented: Bool = false @@ -45,27 +45,27 @@ struct HomeScreen: View{ if searchResults.isEmpty { ContentUnavailableView("No Notes Found", systemImage: "text.document.fill") - }else{ - List{ - if(searchResults.isEmpty){ - - }else { - if(!searchResults.filter{$0.isPinned}.isEmpty){ - Section("Pinned") { - ForEach(searchResults.filter { $0.isPinned } ) { item in - pinnedNotesView(for: item) - } + }else{ + List{ + if(searchResults.isEmpty){ + + }else { + if(!searchResults.filter{$0.isPinned}.isEmpty){ + Section("Pinned") { + ForEach(searchResults.filter { $0.isPinned } ) { item in + pinnedNotesView(for: item) } } - } - ForEach(searchResults.filter {!$0.isPinned}) { item in - notesView(for: item) - } - }.refreshable { - print("Refresh Notes") + } + ForEach(searchResults.filter {!$0.isPinned}) { item in + notesView(for: item) + } + }.refreshable { + print("Refresh Notes") } + } } .navigationTitle("Notes") .searchable(text: $searchText, isPresented: $isSearchPresented) @@ -77,12 +77,12 @@ struct HomeScreen: View{ desc: $notesDesc, navTitle: $navTitle, onSave: { -// if let selectedItem = selectedItem { -// selectedItem.title = notesTitle -// selectedItem.desc = notesDesc -// } else { -// -// } + // if let selectedItem = selectedItem { + // selectedItem.title = notesTitle + // selectedItem.desc = notesDesc + // } else { + // + // } selectedItem = nil notesTitle = "" notesDesc = "" @@ -107,27 +107,27 @@ struct HomeScreen: View{ isSheetExpanded = false }) { UpdateNotes( - title: $notesTitle, - desc: $notesDesc, - navTitle: $navTitle, - selectedLocation: $selectedLocation, - selectedLocationName: $selectedLocationName, - onSave: { - if let selectedItem = selectedItem { - selectedItem.title = notesTitle - selectedItem.desc = notesDesc - selectedItem.location = selectedLocation - } else { - let newItem = NotesItem(title: notesTitle, desc: notesDesc, isPinned: false,location: selectedLocation) - context.insert(newItem) - } - try? context.save() - isSheetExpanded = false - selectedItem = nil - notesTitle = "" - notesDesc = "" - navTitle = "Add Note" - } + title: $notesTitle, + desc: $notesDesc, + navTitle: $navTitle, + selectedLocation: $selectedLocation, + selectedLocationName: $selectedLocationName, + onSave: { + if let selectedItem = selectedItem { + selectedItem.title = notesTitle + selectedItem.desc = notesDesc + selectedItem.location = selectedLocation + } else { + let newItem = NotesItem(title: notesTitle, desc: notesDesc, isPinned: false,location: selectedLocation) + context.insert(newItem) + } + try? context.save() + isSheetExpanded = false + selectedItem = nil + notesTitle = "" + notesDesc = "" + navTitle = "Add Note" + } ) } } @@ -137,28 +137,28 @@ struct HomeScreen: View{ func pinnedNotesView(for item: NotesItem) -> some View { NavigationLink(destination: NotesDetail(notesItem: item)) { VStack(alignment: .leading){ - Text(item.title) - .font(.headline) - Text(item.desc) - .font(.subheadline) - .lineLimit(2) - } - }.contextMenu { - Button(role:.destructive) { - context.delete(item) - try? context.save() - } label: { - Label("Delete", systemImage: "trash.circle.fill") - } - - }.swipeActions(edge:.leading ,content: { - Button("UnPin", systemImage: "pin.slash.fill") { - selectedItem = item - item.isPinned = false - try? context.save() - }.tint(.yellow) - }) - .swipeActions{ + Text(item.title) + .font(.headline) + Text(item.desc) + .font(.subheadline) + .lineLimit(2) + } + }.contextMenu { + Button(role:.destructive) { + context.delete(item) + try? context.save() + } label: { + Label("Delete", systemImage: "trash.circle.fill") + } + + }.swipeActions(edge:.leading ,content: { + Button("UnPin", systemImage: "pin.slash.fill") { + selectedItem = item + item.isPinned = false + try? context.save() + }.tint(.yellow) + }) + .swipeActions{ Button("Edit", systemImage: "pencil") { selectedItem = item notesTitle = item.title @@ -178,33 +178,33 @@ struct HomeScreen: View{ } } } - + func notesView(for item: NotesItem) -> some View { NavigationLink(destination: NotesDetail(notesItem: item)) { VStack(alignment: .leading){ - Text(item.title) - .font(.headline) - Text(item.desc) - .font(.subheadline) - .lineLimit(2) - } - }.contextMenu { - Button(role:.destructive) { - context.delete(item) - try? context.save() - } label: { - Label("Delete", systemImage: "trash.circle.fill") - } - - }.swipeActions(edge:.leading ,content: { - Button("Pin", systemImage: "pin") { - selectedItem = item - item.isPinned = true - try? context.save() - }.tint(.yellow) - }) - .swipeActions{ + Text(item.title) + .font(.headline) + Text(item.desc) + .font(.subheadline) + .lineLimit(2) + } + }.contextMenu { + Button(role:.destructive) { + context.delete(item) + try? context.save() + } label: { + Label("Delete", systemImage: "trash.circle.fill") + } + + }.swipeActions(edge:.leading ,content: { + Button("Pin", systemImage: "pin") { + selectedItem = item + item.isPinned = true + try? context.save() + }.tint(.yellow) + }) + .swipeActions{ Button("Edit", systemImage: "pencil") { selectedItem = item notesTitle = selectedItem?.title ?? "" @@ -225,24 +225,24 @@ struct HomeScreen: View{ } } private func fetchLocationName(for coordinate: CLLocationCoordinate2D, completion: @escaping (String?) -> Void) { - let geocoder = CLGeocoder() - let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude) - geocoder.reverseGeocodeLocation(location) { placemarks, error in - if let error = error { - print("Reverse geocoding error: \(error.localizedDescription)") - completion(nil) - return - } - if let placemark = placemarks?.first { - let name = [placemark.name, placemark.locality, placemark.country] - .compactMap { $0 } - .joined(separator: ", ") - completion(name) - } else { - completion(nil) - } + let geocoder = CLGeocoder() + let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude) + geocoder.reverseGeocodeLocation(location) { placemarks, error in + if let error = error { + print("Reverse geocoding error: \(error.localizedDescription)") + completion(nil) + return + } + if let placemark = placemarks?.first { + let name = [placemark.name, placemark.locality, placemark.country] + .compactMap { $0 } + .joined(separator: ", ") + completion(name) + } else { + completion(nil) } } + } } #Preview { diff --git a/Notes/todo/TodoScreen.swift b/Notes/todo/TodoScreen.swift index 393088d..a845f33 100644 --- a/Notes/todo/TodoScreen.swift +++ b/Notes/todo/TodoScreen.swift @@ -16,13 +16,13 @@ struct TodoScreen: View { @Query(filter: .true, sort: \TodoItem.id, order: .forward, animation: .smooth) private var todoList: [TodoItem] @Query(filter: #Predicate { $0.isCompleted == false }) private var inCompleteList: [TodoItem] @Query(filter: #Predicate { $0.isCompleted == true }, animation: .smooth) private var completedList: [TodoItem] - + @State private var title: String = "" @State private var isCompleted: Bool = false @State private var isSheet: Bool = false @State private var navTitle: String = "Add Task" @State private var selectedItem: TodoItem? - + var body: some View { NavigationView { VStack { @@ -42,7 +42,7 @@ struct TodoScreen: View { } } } - + if !inCompleteList.isEmpty { Section("Incomplete Tasks") { ForEach(inCompleteList) { item in @@ -117,13 +117,13 @@ struct TodoScreen: View { Button("Edit") { editItem(item) }.tint(.blue) - + Button { toggleCompletion(for: item) } label: { Label("Completed", systemImage: item.isCompleted ? "xmark.square" : "checkmark.square") }.tint(.green) - + Button("Delete", role: .destructive) { deleteItem(item) }