diff --git a/MarvelUI/App/MarvelUIApp.swift b/MarvelUI/App/MarvelUIApp.swift index f7c2e76..47b5b3c 100644 --- a/MarvelUI/App/MarvelUIApp.swift +++ b/MarvelUI/App/MarvelUIApp.swift @@ -9,11 +9,12 @@ import SwiftUI @main struct MarvelUIApp: App { - let persistenceController = PersistenceController.shared + @StateObject var locationViewModel = LocationSearchViewModel() var body: some Scene { WindowGroup { HomeView() + .environmentObject(locationViewModel) } } } diff --git a/MarvelUI/Core/Home/View/HomeView.swift b/MarvelUI/Core/Home/View/HomeView.swift index 447fd01..e32feb1 100644 --- a/MarvelUI/Core/Home/View/HomeView.swift +++ b/MarvelUI/Core/Home/View/HomeView.swift @@ -9,13 +9,14 @@ import SwiftUI struct HomeView: View { @State private var showLocationSearchView = false + var body: some View { ZStack(alignment: .top) { UberMapViewRepresentable() .ignoresSafeArea() if showLocationSearchView { - LocationSeachView() + LocationSeachView(showLocationSearchView: $showLocationSearchView) } else { LocationSearchActivationView() .padding(.vertical, 72) diff --git a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift index 5fa2110..df6425e 100644 --- a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift +++ b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift @@ -11,6 +11,7 @@ import MapKit struct UberMapViewRepresentable: UIViewRepresentable { let mapView = MKMapView() let locationManager = LocationManager() + @EnvironmentObject var locationViewModel: LocationSearchViewModel func makeUIView(context: Context) -> some UIView { mapView.delegate = context.coordinator @@ -22,7 +23,9 @@ struct UberMapViewRepresentable: UIViewRepresentable { } func updateUIView(_ uiView: UIViewType, context: Context) { - + if let selectedLocation = locationViewModel.selectedLocation { + print("DEBUG: Selected location in map view \(selectedLocation)") + } } func makeCoordinator() -> MapCoordinator { diff --git a/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift b/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift index 433ee28..6152674 100644 --- a/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift +++ b/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift @@ -9,8 +9,8 @@ import SwiftUI struct LocationSeachView: View { @State private var startLocationText = "" - @State private var destinationLocationText = "" - @StateObject var viewModel = LocationServiceViewModel() + @Binding var showLocationSearchView: Bool + @EnvironmentObject var viewModel: LocationSearchViewModel var body: some View { VStack { @@ -55,6 +55,10 @@ struct LocationSeachView: View { VStack(alignment: .leading) { ForEach(viewModel.resuls, id: \.self) { result in LocationSearchResultCell(title: result.title, subtitle: result.subtitle) + .onTapGesture { + showLocationSearchView.toggle() + viewModel.selectLocation(result.title) + } } } } @@ -66,6 +70,6 @@ struct LocationSeachView: View { struct LocationSeachView_Previews: PreviewProvider { static var previews: some View { - LocationSeachView() + LocationSeachView(showLocationSearchView: .constant(true)) } } diff --git a/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift b/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift index c084b4c..5753ae0 100644 --- a/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift +++ b/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift @@ -8,9 +8,10 @@ import Foundation import MapKit -class LocationServiceViewModel: NSObject, ObservableObject { +class LocationSearchViewModel: NSObject, ObservableObject { @Published var resuls = [MKLocalSearchCompletion]() + @Published var selectedLocation: String? private let searchCompleter = MKLocalSearchCompleter() var queryFragment: String = "" { didSet { @@ -23,11 +24,15 @@ class LocationServiceViewModel: NSObject, ObservableObject { searchCompleter.delegate = self searchCompleter.queryFragment = queryFragment } + + func selectLocation(_ location: String) { + self.selectedLocation = location + } } // MARK: - MKLocalSearchCompleterDelegate -extension LocationServiceViewModel: MKLocalSearchCompleterDelegate { +extension LocationSearchViewModel: MKLocalSearchCompleterDelegate { func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) { self.resuls = completer.results }