diff --git a/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewModel.swift b/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewModel.swift index 84e9678..def74b0 100644 --- a/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsAdd/LocationsAddViewModel.swift @@ -10,19 +10,80 @@ import Combine import Core class LocationsAddViewModel: ObservableObject { - + // MARK: Properties weak var coordinator: LocationsAddCoordination? + @Published private var location: Location? + @Published private var locationExists: Bool = false + + private let saveLocalLocation = SaveLocalLocationUseCase() + // MARK: Initialisers init(coordinator: LocationsAddCoordination) { self.coordinator = coordinator + + setupBindings() } } // MARK: - LocationsAddViewModeling -extension LocationsAddViewModel: LocationsAddViewModeling {} +extension LocationsAddViewModel: LocationsAddViewModeling { + + // MARK: Properties + + var locationExistsPublisher: Published.Publisher { $locationExists } + + // MARK: Functions + + func cleanLocation() { + location = nil + } + + func saveLocation() { + guard let location else { + return + } + + saveLocalLocation( + latitude: location.latitude, + longitude: location.longitude + ) + } + + 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 + } +}