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.
|
|
|
|
//
|
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
import CoreData
|
2023-04-11 22:14:40 +00:00
|
|
|
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 16:58:27 +00:00
|
|
|
@Published private var viewStatus: LocationsListViewStatus = .initialised
|
|
|
|
|
|
|
|
private lazy var fetchedResultsController = NSFetchedResultsController(
|
|
|
|
fetchRequest: NSFetchRequest<Location>.allLocations(),
|
|
|
|
managedObjectContext: persistence.container.viewContext,
|
|
|
|
sectionNameKeyPath: nil,
|
|
|
|
cacheName: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
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 16:58:27 +00:00
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
|
|
|
var numberOfSectionsInData: Int { fetchedResultsController.sections?.count ?? 0 }
|
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func openAddLocation() {
|
|
|
|
coordinator?.openAddLocation()
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:58:27 +00:00
|
|
|
func loadLocations() {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
viewStatus = .loading
|
|
|
|
|
|
|
|
try await loadRemoteLocations()
|
|
|
|
|
|
|
|
try fetchedResultsController.performFetch()
|
|
|
|
|
|
|
|
viewStatus = .loaded
|
|
|
|
} catch {
|
|
|
|
viewStatus = .error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfDataItems(in section: Int) -> Int {
|
|
|
|
guard
|
|
|
|
let sections = fetchedResultsController.sections,
|
|
|
|
sections.endIndex > section
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return sections[section].numberOfObjects
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataItem(at indexPath: IndexPath) -> Location {
|
|
|
|
fetchedResultsController.object(at: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Enumerations
|
|
|
|
|
|
|
|
enum LocationsListViewStatus {
|
|
|
|
case initialised
|
|
|
|
case loading
|
|
|
|
case loaded
|
|
|
|
case error
|
2023-04-11 22:14:40 +00:00
|
|
|
}
|