2023-04-11 22:14:40 +00:00
|
|
|
//
|
|
|
|
// LocationsAddViewModel.swift
|
|
|
|
// Locations
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 11/04/2023.
|
|
|
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Core
|
|
|
|
|
|
|
|
class LocationsAddViewModel: ObservableObject {
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
weak var coordinator: LocationsAddCoordination?
|
|
|
|
|
2023-04-12 21:25:08 +00:00
|
|
|
@Published private var location: Location?
|
|
|
|
@Published private var locationExists: Bool = false
|
|
|
|
|
|
|
|
private let saveLocalLocation = SaveLocalLocationUseCase()
|
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
init(coordinator: LocationsAddCoordination) {
|
|
|
|
self.coordinator = coordinator
|
2023-04-12 21:25:08 +00:00
|
|
|
|
|
|
|
setupBindings()
|
2023-04-11 22:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - LocationsAddViewModeling
|
|
|
|
|
2023-04-12 21:25:08 +00:00
|
|
|
extension LocationsAddViewModel: LocationsAddViewModeling {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
var locationExistsPublisher: Published<Bool>.Publisher { $locationExists }
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func cleanLocation() {
|
|
|
|
location = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveLocation() {
|
|
|
|
guard let location else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
saveLocalLocation(
|
|
|
|
latitude: location.latitude,
|
|
|
|
longitude: location.longitude
|
|
|
|
)
|
|
|
|
|
2023-04-12 23:07:42 +00:00
|
|
|
coordinator?.closeLocationsAddScreen()
|
2023-04-12 21:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setLocation(latitude: Float, longitude: Float) {
|
|
|
|
if location == nil {
|
|
|
|
location = .init(latitude: latitude, longitude: longitude)
|
|
|
|
} else {
|
|
|
|
location?.latitude = latitude
|
|
|
|
location?.longitude = longitude
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension LocationsAddViewModel {
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func setupBindings() {
|
|
|
|
$location
|
|
|
|
.map { $0 != nil }
|
|
|
|
.assign(to: &$locationExists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Structs
|
|
|
|
|
|
|
|
private extension LocationsAddViewModel {
|
|
|
|
struct Location {
|
|
|
|
var latitude: Float
|
|
|
|
var longitude: Float
|
|
|
|
}
|
|
|
|
}
|