Skip to content

Commit

Permalink
Add destination search functionality to
Browse files Browse the repository at this point in the history
ExploreView
  • Loading branch information
sebastian-nunez committed Dec 10, 2023
1 parent 8d9a14f commit f72d7c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
19 changes: 15 additions & 4 deletions Airbnb Clone/Core/Explore/View/DestinationSearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum DestinationSearchOptions {
struct DestinationSearchView: View {
// bindings
@Binding var showView: Bool
@ObservedObject var viewModel: ExploreViewModel

// state
@State private var selectedOption: DestinationSearchOptions = .location
Expand All @@ -31,7 +32,10 @@ struct DestinationSearchView: View {
destinationText: $destinationText)

// location search
LocationSearchView(text: $destinationText, selectedOption: selectedOption)
LocationSearchView(destination: $destinationText,
selectedOption: selectedOption,
viewModel: viewModel,
showView: $showView)
.onTapGesture {
withAnimation(.snappy) { selectedOption = .location }
}
Expand All @@ -55,7 +59,8 @@ struct DestinationSearchView: View {
}

#Preview {
DestinationSearchView(showView: .constant(false))
DestinationSearchView(showView: .constant(false),
viewModel: ExploreViewModel(with: MockExploreServiceImpl()))
}

private struct NavigationControlsView: View {
Expand Down Expand Up @@ -98,8 +103,10 @@ private struct NavigationControlsView: View {
}

private struct LocationSearchView: View {
@Binding var text: String
@Binding var destination: String
var selectedOption: DestinationSearchOptions
@ObservedObject var viewModel: ExploreViewModel
@Binding var showView: Bool

var body: some View {
if selectedOption == .location {
Expand All @@ -112,8 +119,12 @@ private struct LocationSearchView: View {
Image(systemName: "magnifyingglass")
.imageScale(.medium)

TextField("Search destinations...", text: $text)
TextField("Search destinations...", text: $destination)
.font(.subheadline)
.onSubmit {
viewModel.filterListingByLocation(for: destination)
showView.toggle() // close overlay
}
}
.padding()
.overlay {
Expand Down
11 changes: 6 additions & 5 deletions Airbnb Clone/Core/Explore/View/ExploreView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import SwiftUI

struct ExploreView: View {
@State private var showDestinationSearchView = false

// inject view model
@State private var viewModel = ExploreViewModel(with: MockExploreServiceImpl())
@StateObject var viewModel = ExploreViewModel(with: MockExploreServiceImpl())

@State private var showDestinationSearchView = false

var body: some View {
NavigationStack {
if showDestinationSearchView {
DestinationSearchView(showView: $showDestinationSearchView)
DestinationSearchView(showView: $showDestinationSearchView,
viewModel: viewModel)
} else {
ScrollView(.vertical) {
// search bar
Expand Down Expand Up @@ -46,5 +47,5 @@ struct ExploreView: View {
}

#Preview {
ExploreView()
ExploreView(viewModel: ExploreViewModel(with: MockExploreServiceImpl()))
}
9 changes: 3 additions & 6 deletions Airbnb Clone/Core/Explore/ViewModel/ExploreViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ class ExploreViewModel: ObservableObject {
func filterListingByLocation(for location: String) {
let normalizedLocation = location.lowercased()

let filteredListings = listings.filter { listing in
let normalizedCity = listing.city.lowercased()
let normalizedState = listing.state.lowercased()

return normalizedCity.starts(with: normalizedLocation) ||
normalizedState.starts(with: normalizedLocation)
let filteredListings = listings.filter {
$0.city.lowercased().starts(with: normalizedLocation) ||
$0.state.lowercased().starts(with: normalizedLocation)
}

listings = filteredListings.isEmpty ? listings : filteredListings
Expand Down

0 comments on commit f72d7c1

Please sign in to comment.