Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Favorite click with Long and Tap Gesture #46

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions OTPKit/Features/OriginDestination/FavoriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,6 +38,12 @@ struct FavoriteView: View {
.padding(.all, 4)
.foregroundStyle(.foreground)
})
.simultaneousGesture(LongPressGesture().onEnded { _ in
longTapAction?()
})
.simultaneousGesture(TapGesture().onEnded {
tapAction?()
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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: {
Expand All @@ -72,6 +109,9 @@ public struct OriginDestinationSheetView: View {
FavoriteLocationDetailSheet()
.environmentObject(sheetEnvironment)
})
.confirmationDialog("", isPresented: $isShowFavoriteConfirmationDialog, actions: {
favoriteSectionConfirmationDialog()
})
}

private func recentsSection() -> some View {
Expand Down Expand Up @@ -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()
Expand Down
Loading