2023-04-11 22:14:40 +00:00
|
|
|
//
|
|
|
|
// LocationsListViewModel.swift
|
|
|
|
// Locations
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 11/04/2023.
|
|
|
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2023-04-12 16:58:27 +00:00
|
|
|
import Dependency
|
|
|
|
import Foundation
|
|
|
|
import Persistence
|
2023-04-11 22:14:40 +00:00
|
|
|
|
|
|
|
class LocationsListViewModel: ObservableObject {
|
2023-04-12 16:58:27 +00:00
|
|
|
|
|
|
|
// MARK: Dependencies
|
|
|
|
|
|
|
|
@Dependency(\.persistence) private var persistence
|
2023-04-11 22:14:40 +00:00
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
weak var coordinator: LocationsListCoordination?
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-13 13:34:06 +00:00
|
|
|
private lazy var locationProvider = LocationProvider(managedContext: persistence.viewContext)
|
2023-04-11 22:14:40 +00:00
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
@Published private var viewStatus: LocationsListViewStatus = .initialised
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
private let loadRemoteLocations = LoadRemoteLocationsUseCase()
|
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
init(coordinator: LocationsListCoordination) {
|
|
|
|
self.coordinator = coordinator
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - LocationsListViewModeling
|
|
|
|
|
|
|
|
extension LocationsListViewModel: LocationsListViewModeling {
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
// MARK: Properties
|
2023-04-12 21:25:08 +00:00
|
|
|
|
|
|
|
var locationsDidChangePublisher: PassthroughSubject<[Persistence.Change], Never> { locationProvider.didChangePublisher }
|
2023-04-12 23:07:42 +00:00
|
|
|
var numberOfLocationSections: Int { locationProvider.numberOfSections }
|
2023-04-12 16:58:27 +00:00
|
|
|
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: Functions
|
2023-04-12 21:25:08 +00:00
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
func loadLocations() {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
viewStatus = .loading
|
|
|
|
|
|
|
|
try await loadRemoteLocations()
|
|
|
|
|
2023-04-12 21:25:08 +00:00
|
|
|
try locationProvider.fetch()
|
2023-04-12 16:58:27 +00:00
|
|
|
|
|
|
|
viewStatus = .loaded
|
|
|
|
} catch {
|
|
|
|
viewStatus = .error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 23:07:42 +00:00
|
|
|
|
|
|
|
func location(at indexPath: IndexPath) -> Location {
|
|
|
|
locationProvider.location(at: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfLocations(in section: Int) -> Int {
|
2023-04-12 21:25:08 +00:00
|
|
|
locationProvider.numberOfLocationsInSection(section)
|
2023-04-12 16:58:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 23:07:42 +00:00
|
|
|
func openLocationsAdd() {
|
|
|
|
coordinator?.openLocationsAddScreen()
|
|
|
|
}
|
|
|
|
|
|
|
|
func openWikipedia(at indexPath: IndexPath) {
|
|
|
|
guard let url = locationProvider.location(at: indexPath).wikipediaPlacesURL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinator?.openWikipediaApp(with: url)
|
2023-04-12 16:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Enumerations
|
|
|
|
|
|
|
|
enum LocationsListViewStatus {
|
|
|
|
case initialised
|
|
|
|
case loading
|
|
|
|
case loaded
|
|
|
|
case error
|
2023-04-11 22:14:40 +00:00
|
|
|
}
|