This PR contains the work done to add a location at a time and updates the locations in the list of locations screen right after. To give further details about the work done: - [x] implemented the `LocationProvider` provider in the **Persistence** library; - [x] implemented the `SaveLocalLocationUseCase` use case; - [x] defined the properties and functions of the `LocationsAddViewModeling` protocol to support the clean, updating and saving of locations; - [x] implemented the `LocationsAddViewModel` view model; - [x] implemented the `LocationsAddViewController` view controller; - [x] implemented the dismissal of the `LocationsAddCoordinator` coordinator. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#11
88 lines
2.0 KiB
Swift
88 lines
2.0 KiB
Swift
//
|
|
// LocationsListViewModel.swift
|
|
// Locations
|
|
//
|
|
// Created by Javier Cicchelli on 11/04/2023.
|
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
|
//
|
|
|
|
import Combine
|
|
import Dependency
|
|
import Foundation
|
|
import Persistence
|
|
|
|
class LocationsListViewModel: ObservableObject {
|
|
|
|
// MARK: Dependencies
|
|
|
|
@Dependency(\.persistence) private var persistence
|
|
|
|
// MARK: Properties
|
|
|
|
weak var coordinator: LocationsListCoordination?
|
|
|
|
private lazy var locationProvider = LocationProvider(managedContext: persistence.container.viewContext)
|
|
|
|
@Published private var viewStatus: LocationsListViewStatus = .initialised
|
|
|
|
private let loadRemoteLocations = LoadRemoteLocationsUseCase()
|
|
|
|
// MARK: Initialisers
|
|
|
|
init(coordinator: LocationsListCoordination) {
|
|
self.coordinator = coordinator
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - LocationsListViewModeling
|
|
|
|
extension LocationsListViewModel: LocationsListViewModeling {
|
|
|
|
// MARK: Properties
|
|
|
|
var locationsDidChangePublisher: PassthroughSubject<[Persistence.Change], Never> { locationProvider.didChangePublisher }
|
|
var numberOfSectionsInData: Int { locationProvider.numberOfSections }
|
|
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
|
|
|
// MARK: Functions
|
|
|
|
func openAddLocation() {
|
|
coordinator?.openAddLocation()
|
|
}
|
|
|
|
func loadLocations() {
|
|
Task {
|
|
do {
|
|
viewStatus = .loading
|
|
|
|
try await loadRemoteLocations()
|
|
|
|
try locationProvider.fetch()
|
|
|
|
viewStatus = .loaded
|
|
} catch {
|
|
viewStatus = .error
|
|
}
|
|
}
|
|
}
|
|
|
|
func numberOfDataItems(in section: Int) -> Int {
|
|
locationProvider.numberOfLocationsInSection(section)
|
|
}
|
|
|
|
func dataItem(at indexPath: IndexPath) -> Location {
|
|
locationProvider.location(at: indexPath)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Enumerations
|
|
|
|
enum LocationsListViewStatus {
|
|
case initialised
|
|
case loading
|
|
case loaded
|
|
case error
|
|
}
|