This PR contains the work done to fetch a set of locations from a remote server to then persist them into the persistence stack and finally to display these data into the locations list screen. To give further details about the work done: - [x] implemented the `LoadingSpinnerView` and `ErrorMessageView` custom views; - [x] implemented the outlets of the `LocationsListViewController` view controller; - [x] add properties and functions to the `LocationsListViewModeling` protocol to support reactive updates, load data, and table data source conformances; - [x] deactivated the Location entity code generation from the Core Data model in the `Persistence` library; - [x] add fetch requests builder functions to the `NSFetchRequest+Location` extension in the `Persistence` library; - [x] implemented the `LoadRemoteLocationUseCase` use case; - [x] implemented the loading of locations in the LocationsListViewModel view model; - [x] implemented properties and functions in the LocationsListViewModel view model to support the table data source conformance of the `LocationsListViewController` view controller; - [x] implemented the `LocationViewCell` custom cell; - [x] registered the `LocationViewCell` with the table of the `LocationsListViewController` view controller and implemented its update with real data from Location entities. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#10
100 lines
2.3 KiB
Swift
100 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 CoreData
|
|
import Combine
|
|
import Dependency
|
|
import Foundation
|
|
import Persistence
|
|
|
|
class LocationsListViewModel: ObservableObject {
|
|
|
|
// MARK: Dependencies
|
|
|
|
@Dependency(\.persistence) private var persistence
|
|
|
|
// MARK: Properties
|
|
|
|
weak var coordinator: LocationsListCoordination?
|
|
|
|
@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()
|
|
|
|
// MARK: Initialisers
|
|
|
|
init(coordinator: LocationsListCoordination) {
|
|
self.coordinator = coordinator
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - LocationsListViewModeling
|
|
|
|
extension LocationsListViewModel: LocationsListViewModeling {
|
|
|
|
// MARK: Properties
|
|
|
|
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
|
var numberOfSectionsInData: Int { fetchedResultsController.sections?.count ?? 0 }
|
|
|
|
// MARK: Functions
|
|
|
|
func openAddLocation() {
|
|
coordinator?.openAddLocation()
|
|
}
|
|
|
|
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
|
|
}
|