diff --git a/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewController.swift b/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewController.swift index 86f4421..6a7ced6 100644 --- a/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewController.swift @@ -6,14 +6,30 @@ // Copyright © 2023 Röck+Cöde. All rights reserved. // +import Combine import Core import UIKit +import MapKit class LocationsAddViewController: BaseViewController { // MARK: Properties - var viewModel: LocationsAddViewModeling + private let viewModel: LocationsAddViewModeling + + private var cancellables: Set = [] + + // MARK: Outlets + + private lazy var map = { + let map = MKMapView() + + map.translatesAutoresizingMaskIntoConstraints = false + + map.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapOnMap))) + + return map + }() // MARK: Initialisers @@ -32,7 +48,91 @@ class LocationsAddViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() - title = "Location Add" + setupBar() + setupView() + bindViewModel() + } + +} + +// MARK: - Helpers + +private extension LocationsAddViewController { + + // MARK: Functions + + func bindViewModel() { + viewModel + .locationExistsPublisher + .receive(on: RunLoop.main) + .sink { locationExists in + self.navigationItem + .rightBarButtonItems? + .forEach { $0.isEnabled = locationExists } + } + .store(in: &cancellables) + } + + func setupBar() { + title = "Add a location" + navigationController?.navigationBar.prefersLargeTitles = false + navigationController?.navigationBar.backgroundColor = .systemBackground + navigationController?.navigationBar.isTranslucent = true + navigationController?.navigationBar.tintColor = .red + navigationItem.rightBarButtonItems = [ + .init( + title: "Save", + style: .plain, + target: self, + action: #selector(saveButtonPressed) + ), + .init( + title: "Clean", + style: .plain, + target: self, + action: #selector(cleanButtonPressed) + ), + ] + } + + func setupView() { + view.addSubview(map) + + NSLayoutConstraint.activate([ + view.bottomAnchor.constraint(equalTo: map.bottomAnchor), + view.leadingAnchor.constraint(equalTo: map.leadingAnchor), + view.topAnchor.constraint(equalTo: map.topAnchor), + view.trailingAnchor.constraint(equalTo: map.trailingAnchor), + ]) + } + + // MARK: Actions + + @objc func cleanButtonPressed() { + map.removeAnnotations(map.annotations) + + viewModel.cleanLocation() + } + + @objc func saveButtonPressed() { + viewModel.saveLocation() + } + + @objc func tapOnMap(recognizer: UITapGestureRecognizer) { + let tapOnView = recognizer.location(in: map) + let mapCoordinates = map.convert(tapOnView, toCoordinateFrom: map) + let annotation = MKPointAnnotation() + + annotation.coordinate = mapCoordinates + + map.removeAnnotations(map.annotations) + map.addAnnotation(annotation) + map.setCenter(mapCoordinates, animated: true) + + viewModel.setLocation( + latitude: Float(mapCoordinates.latitude), + longitude: Float(mapCoordinates.longitude) + ) } }