Implemented the LocationsAddViewModel view model that conforms to the LocationsAddViewModeling protocol.
This commit is contained in:
parent
9b04d3601c
commit
5f0c6640cc
@ -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<Bool>.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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user