This PR contains the work on implementing some public interfaces that were forgotten during the development of this app and, of course, improves the text of the README file a bit more. To give further details about the work done: - [x] implemented the `Service` protocol in the `Persistence` library and conformed the `PersistenceService` service to it; - [x] implemented the `Service` protocol in the `Remote` library and conformed the `RemoteService` service to it; - [x] implemented the `Application` protocol in the `Core` library and conformed the `UIApplication` class to it; - [x] improved the dependency keys used by the `DependencyService` service to use these protocols instead; - [x] tweaked the text of the README file. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#14
97 lines
2.3 KiB
Swift
97 lines
2.3 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.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 numberOfLocationSections: Int { locationProvider.numberOfSections }
|
|
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
|
|
|
// MARK: Functions
|
|
|
|
func loadLocations() {
|
|
Task {
|
|
do {
|
|
viewStatus = .loading
|
|
|
|
try await loadRemoteLocations()
|
|
|
|
try locationProvider.fetch()
|
|
|
|
viewStatus = .loaded
|
|
} catch {
|
|
viewStatus = .error
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func location(at indexPath: IndexPath) -> Location {
|
|
locationProvider.location(at: indexPath)
|
|
}
|
|
|
|
func numberOfLocations(in section: Int) -> Int {
|
|
locationProvider.numberOfLocationsInSection(section)
|
|
}
|
|
|
|
func openLocationsAdd() {
|
|
coordinator?.openLocationsAddScreen()
|
|
}
|
|
|
|
func openWikipedia(at indexPath: IndexPath) {
|
|
guard let url = locationProvider.location(at: indexPath).wikipediaPlacesURL else {
|
|
return
|
|
}
|
|
|
|
coordinator?.openWikipediaApp(with: url)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Enumerations
|
|
|
|
enum LocationsListViewStatus {
|
|
case initialised
|
|
case loading
|
|
case loaded
|
|
case error
|
|
}
|