diff --git a/OTPKit/Features/OriginDestination/FavoriteView.swift b/OTPKit/Features/OriginDestination/FavoriteView.swift index c8d47d6..ce4d3a4 100644 --- a/OTPKit/Features/OriginDestination/FavoriteView.swift +++ b/OTPKit/Features/OriginDestination/FavoriteView.swift @@ -11,18 +11,18 @@ import SwiftUI struct FavoriteView: View { private let title: String private let imageName: String - private let action: VoidBlock? + private let tapAction: VoidBlock? + private let longTapAction: VoidBlock? - init(title: String, imageName: String, action: VoidBlock? = nil) { + init(title: String, imageName: String, tapAction: VoidBlock? = nil, longTapAction: VoidBlock? = nil) { self.title = title self.imageName = imageName - self.action = action + self.tapAction = tapAction + self.longTapAction = longTapAction } var body: some View { - Button(action: { - action?() - }, label: { + Button(action: {}, label: { VStack(alignment: .center) { Image(systemName: imageName) .frame(width: 48, height: 48) @@ -38,6 +38,12 @@ struct FavoriteView: View { .padding(.all, 4) .foregroundStyle(.foreground) }) + .simultaneousGesture(LongPressGesture().onEnded { _ in + longTapAction?() + }) + .simultaneousGesture(TapGesture().onEnded { + tapAction?() + }) } } diff --git a/OTPKit/Features/OriginDestination/Sheets/FavoriteLocationsSheet.swift b/OTPKit/Features/OriginDestination/Sheets/FavoriteLocationsSheet.swift index 8f0b146..b3c3a74 100644 --- a/OTPKit/Features/OriginDestination/Sheets/FavoriteLocationsSheet.swift +++ b/OTPKit/Features/OriginDestination/Sheets/FavoriteLocationsSheet.swift @@ -12,6 +12,7 @@ public struct FavoriteLocationsSheet: View { @Environment(\.dismiss) private var dismiss @EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment + @ObservedObject private var locationManagerService = LocationManagerService.shared @State private var isDetailSheetOpened = false @@ -24,10 +25,7 @@ public struct FavoriteLocationsSheet: View { List { ForEach(sheetEnvironment.favoriteLocations) { location in - Button(action: { - sheetEnvironment.selectedDetailFavoriteLocation = location - isDetailSheetOpened.toggle() - }, label: { + Button(action: {}, label: { VStack(alignment: .leading) { Text(location.title) .font(.headline) diff --git a/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift b/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift index 4ee00fe..86ede29 100644 --- a/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift +++ b/OTPKit/Features/OriginDestination/Sheets/OriginDestinationSheetView.swift @@ -20,6 +20,12 @@ public struct OriginDestinationSheetView: View { @State private var isFavoriteLocationSheetOpen = false @State private var isRecentLocationSheetOpen = false @State private var isFavoriteLocationDetailSheetOpen = false + @State private var isShowFavoriteConfirmationDialog = false + + // Alert States + @State private var isShowErrorAlert = false + @State private var errorTitle = "" + @State private var errorMessage = "" @FocusState private var isSearchFocused: Bool @@ -39,20 +45,51 @@ public struct OriginDestinationSheetView: View { .clipShape(RoundedRectangle(cornerRadius: 12)) } + private func favoriteSectionConfirmationDialog() -> some View { + Group { + Button(action: { + isFavoriteLocationDetailSheetOpen.toggle() + }, label: { + Text("Show Details") + }) + + Button(role: .destructive, action: { + guard let uid = sheetEnvironment.selectedDetailFavoriteLocation?.id else { + return + } + switch UserDefaultsServices.shared.deleteFavoriteLocationData(with: uid) { + case .success: + sheetEnvironment.selectedDetailFavoriteLocation = nil + sheetEnvironment.refreshFavoriteLocations() + case let .failure(failure): + errorTitle = "Failed to Delete Favorite Location" + errorMessage = failure.localizedDescription + isShowErrorAlert.toggle() + } + }, label: { + Text("Delete") + }) + } + } + private func favoritesSection() -> some View { Section(content: { ScrollView(.horizontal) { HStack { ForEach(sheetEnvironment.favoriteLocations, content: { location in - FavoriteView(title: location.title, imageName: "mappin") { + FavoriteView(title: location.title, imageName: "mappin", tapAction: { + locationManagerService.appendMarker(location: location) + locationManagerService.addOriginDestinationData() + dismiss() + }, longTapAction: { + isShowFavoriteConfirmationDialog = true sheetEnvironment.selectedDetailFavoriteLocation = location - isFavoriteLocationDetailSheetOpen.toggle() - } + }) }) - FavoriteView(title: "Add", imageName: "plus") { + FavoriteView(title: "Add", imageName: "plus", tapAction: { isAddSavedLocationsSheetOpen.toggle() - } + }) } } }, header: { @@ -72,6 +109,9 @@ public struct OriginDestinationSheetView: View { FavoriteLocationDetailSheet() .environmentObject(sheetEnvironment) }) + .confirmationDialog("", isPresented: $isShowFavoriteConfirmationDialog, actions: { + favoriteSectionConfirmationDialog() + }) } private func recentsSection() -> some View { @@ -199,6 +239,11 @@ public struct OriginDestinationSheetView: View { Spacer() } + .alert(isPresented: $isShowErrorAlert) { + Alert(title: Text(errorTitle), + message: Text(errorMessage), + dismissButton: .cancel(Text("Ok"))) + } .onAppear { sheetEnvironment.refreshFavoriteLocations() sheetEnvironment.refreshRecentLocations()