Skip to content

Commit

Permalink
Merge pull request #3 from LucasJorgeHubert/features/selecting-location
Browse files Browse the repository at this point in the history
Features - selecting location and getting coordinates
  • Loading branch information
LucasJorgeHubert authored May 17, 2023
2 parents fdd019a + ebb3a19 commit 42f0d55
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
8 changes: 4 additions & 4 deletions MarvelUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
C9CFFE812A151F7400708A15 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE802A151F7400708A15 /* LocationManager.swift */; };
C9CFFE872A153D7500708A15 /* LocationSearchActivationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */; };
C9CFFE892A153FA800708A15 /* MapViewActionButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE882A153FA800708A15 /* MapViewActionButton.swift */; };
C9CFFE8B2A15410900708A15 /* LocationSeachView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */; };
C9CFFE8B2A15410900708A15 /* LocationSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */; };
C9CFFE8D2A15447800708A15 /* LocationSearchResultCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */; };
C9CFFE8F2A154F1900708A15 /* LocationSearchViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8E2A154F1900708A15 /* LocationSearchViewModel.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -60,7 +60,7 @@
C9CFFE802A151F7400708A15 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchActivationView.swift; sourceTree = "<group>"; };
C9CFFE882A153FA800708A15 /* MapViewActionButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewActionButton.swift; sourceTree = "<group>"; };
C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSeachView.swift; sourceTree = "<group>"; };
C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchView.swift; sourceTree = "<group>"; };
C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchResultCell.swift; sourceTree = "<group>"; };
C9CFFE8E2A154F1900708A15 /* LocationSearchViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -230,7 +230,7 @@
isa = PBXGroup;
children = (
C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */,
C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */,
C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */,
C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */,
);
path = View;
Expand Down Expand Up @@ -374,7 +374,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C9CFFE8B2A15410900708A15 /* LocationSeachView.swift in Sources */,
C9CFFE8B2A15410900708A15 /* LocationSearchView.swift in Sources */,
C9CFFE4D2A1517A200708A15 /* Persistence.swift in Sources */,
C9CFFE7D2A151D2400708A15 /* HomeView.swift in Sources */,
C9CFFE812A151F7400708A15 /* LocationManager.swift in Sources */,
Expand Down
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()
LocationSearchView(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 coordinate = locationViewModel.selectedLocationCoordinate {
print("DEBUG: Selected coordinate in map view \(coordinate)")
}
}

func makeCoordinator() -> MapCoordinator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import SwiftUI

struct LocationSeachView: View {
struct LocationSearchView: 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)
}
}
}
}
Expand All @@ -66,6 +70,6 @@ struct LocationSeachView: View {

struct LocationSeachView_Previews: PreviewProvider {
static var previews: some View {
LocationSeachView()
LocationSearchView(showLocationSearchView: .constant(true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import Foundation
import MapKit

class LocationServiceViewModel: NSObject, ObservableObject {
class LocationSearchViewModel: NSObject, ObservableObject {

@Published var resuls = [MKLocalSearchCompletion]()
@Published var selectedLocationCoordinate: CLLocationCoordinate2D?

private let searchCompleter = MKLocalSearchCompleter()

var queryFragment: String = "" {
didSet {
searchCompleter.queryFragment = queryFragment
Expand All @@ -23,11 +26,36 @@ class LocationServiceViewModel: NSObject, ObservableObject {
searchCompleter.delegate = self
searchCompleter.queryFragment = queryFragment
}


// MARK: - Helpers
func selectLocation(_ localSearch: MKLocalSearchCompletion) {
locationSearch(forLocalSearchCompletion: localSearch) { response, error in
if let error = error {
print("DEBUG: Location search error: \(error.localizedDescription)")
return
}
guard let item = response?.mapItems.first else { return }
let coordinate = item.placemark.coordinate
self.selectedLocationCoordinate = coordinate

print("DEBUG: Location coordinates \(coordinate)")
}
}

func locationSearch(forLocalSearchCompletion localSearch: MKLocalSearchCompletion,
completion: @escaping MKLocalSearch.CompletionHandler) {
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = localSearch.title.appending(localSearch.subtitle)
let search = MKLocalSearch(request: searchRequest)
search.start(completionHandler: completion)
}

}

// MARK: - MKLocalSearchCompleterDelegate

extension LocationServiceViewModel: MKLocalSearchCompleterDelegate {
extension LocationSearchViewModel: MKLocalSearchCompleterDelegate {
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
self.resuls = completer.results
}
Expand Down

0 comments on commit 42f0d55

Please sign in to comment.