Implemented the LocationsAddViewController view controller.
This commit is contained in:
parent
5f0c6640cc
commit
1683347c1a
@ -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<AnyCancellable> = []
|
||||
|
||||
// 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)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user