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

Issue 15 - saved frequent user locations #20

Merged
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// AddFavoriteLocationsSheet.swift
// OTPKitDemo
//
// Created by Hilmy Veradin on 03/07/24.
//

import SwiftUI

/// This sheet responsible to add a new favorite location.
/// Users can search and add their favorite locations
public struct AddFavoriteLocationsSheet: View {
@Environment(\.dismiss) var dismiss

@EnvironmentObject private var locationService: LocationService
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment

@State private var search = ""

private var filteredCompletions: [Location] {
let favorites = sheetEnvironment.favoriteLocations
return locationService.completions.filter { completion in
!favorites.contains { favorite in
favorite.title == completion.title &&
favorite.subTitle == completion.subTitle
}
}
}

public var body: some View {
VStack {
HStack {
Text("Add favorite location")
.font(.title2)
.fontWeight(.bold)
Spacer()
Button(action: {
dismiss()
}, label: {
Image(systemName: "xmark.circle.fill")
.font(.title2)
.foregroundColor(.gray)
})
}
.padding()

HStack {
Image(systemName: "magnifyingglass")
TextField("Search for a place", text: $search)
.autocorrectionDisabled()
}
.padding(.vertical, 8)
.padding(.horizontal, 12)
.background(Color.gray.opacity(0.2))
.clipShape(.rect(cornerRadius: 12))
.padding(.horizontal, 16)

List {
ForEach(filteredCompletions) { location in
Button(action: {
switch UserDefaultsServices.shared.saveFavoriteLocationData(data: location) {
case .success:
sheetEnvironment.refreshFavoriteLocations()
dismiss()
case let .failure(error):
print(error)
}
}, label: {
HStack {
VStack(alignment: .leading) {
Text(location.title)
.font(.headline)
Text(location.subTitle)
}.foregroundStyle(Color.black)

Spacer()

Image(systemName: "plus")
}

})
}
}
.onChange(of: search) { searchValue in
locationService.update(queryFragment: searchValue)
}
}
}
}

#Preview {
AddFavoriteLocationsSheet()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// FavoriteLocationDetailSheet.swift
// OTPKitDemo
//
// Created by Hilmy Veradin on 04/07/24.
//

import SwiftUI

/// This responsible for showing the details of favorite locations
/// Users can see the details and delete the location sheet
public struct FavoriteLocationDetailSheet: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment

@State private var isShowErrorAlert = false
@State private var errorMessage = ""

private func headerView() -> some View {
HStack {
Text("Favorite location detail")
.font(.title2)
.fontWeight(.bold)
Spacer()
Button(action: {
sheetEnvironment.selectedDetailFavoriteLocation = nil
dismiss()
}, label: {
Image(systemName: "xmark.circle.fill")
.font(.title2)
.foregroundColor(.gray)
})
}
}

public var body: some View {
VStack {
headerView()
.padding(.vertical)

Text("\(sheetEnvironment.selectedDetailFavoriteLocation?.title ?? "")")
.font(.headline)
Text("\(sheetEnvironment.selectedDetailFavoriteLocation?.subTitle ?? "")")

Button(action: {
guard let uid = sheetEnvironment.selectedDetailFavoriteLocation?.id else {
return
}
switch UserDefaultsServices.shared.deleteFavoriteLocationData(with: uid) {
case .success:
sheetEnvironment.selectedDetailFavoriteLocation = nil
sheetEnvironment.refreshFavoriteLocations()
dismiss()
case let .failure(failure):
errorMessage = failure.localizedDescription
isShowErrorAlert.toggle()
}
}, label: {
Text("Delete Location")
})
.padding()

Spacer()
}
.padding()
.alert(isPresented: $isShowErrorAlert) {
Alert(title: Text("Error Delete Favorite Location"),
message: Text(errorMessage),
dismissButton: .cancel(Text("Ok")))
}
}
}

#Preview {
FavoriteLocationDetailSheet()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// FavoriteLocationsSheet.swift
// OTPKitDemo
//
// Created by Hilmy Veradin on 03/07/24.
//

import SwiftUI

/// Show all the lists of favorite locations
public struct FavoriteLocationsSheet: View {
@Environment(\.dismiss) private var dismiss

@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment

@State private var isDetailSheetOpened = false

public var body: some View {
VStack {
HStack {
Text("Favorites")
.font(.title2)
.fontWeight(.bold)
Spacer()
Button(action: {
dismiss()
}, label: {
Image(systemName: "xmark.circle.fill")
.font(.title2)
.foregroundColor(.gray)
})
}
.padding()

List {
ForEach(sheetEnvironment.favoriteLocations) { location in
Button(action: {
sheetEnvironment.selectedDetailFavoriteLocation = location
isDetailSheetOpened.toggle()
}, label: {
VStack(alignment: .leading) {
Text(location.title)
.font(.headline)
Text(location.subTitle)
}
.foregroundStyle(Color.black)
})
}
}
.sheet(isPresented: $isDetailSheetOpened, content: {
FavoriteLocationDetailSheet()
.environmentObject(sheetEnvironment)
})
}
}
}

#Preview {
FavoriteLocationsSheet()
}
Loading
Loading