Skip to content

Commit

Permalink
refactor and get selected Location information
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasJorgeHubert committed May 17, 2023
1 parent fdd019a commit ffaa2bd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion MarvelUI/App/MarvelUIApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
3 changes: 2 additions & 1 deletion MarvelUI/Core/Home/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion MarvelUI/Core/Home/View/UberMapViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions MarvelUI/Core/LocationSearch/View/LocationSeachView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand All @@ -66,6 +70,6 @@ struct LocationSeachView: View {

struct LocationSeachView_Previews: PreviewProvider {
static var previews: some View {
LocationSeachView()
LocationSeachView(showLocationSearchView: .constant(true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit ffaa2bd

Please sign in to comment.