Integrated the LocationProvider provider in the LocationsListViewModel view model.
This commit is contained in:
parent
2d91edd38b
commit
e17c407af4
@ -6,7 +6,6 @@
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Combine
|
||||
import Dependency
|
||||
import Foundation
|
||||
@ -21,16 +20,11 @@ class LocationsListViewModel: ObservableObject {
|
||||
// MARK: Properties
|
||||
|
||||
weak var coordinator: LocationsListCoordination?
|
||||
|
||||
private lazy var locationProvider = LocationProvider(managedContext: persistence.container.viewContext)
|
||||
|
||||
@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
|
||||
@ -44,18 +38,18 @@ class LocationsListViewModel: ObservableObject {
|
||||
// MARK: - LocationsListViewModeling
|
||||
|
||||
extension LocationsListViewModel: LocationsListViewModeling {
|
||||
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
var numberOfSectionsInData: Int { locationProvider.numberOfSections }
|
||||
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
||||
var numberOfSectionsInData: Int { fetchedResultsController.sections?.count ?? 0 }
|
||||
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
|
||||
func openAddLocation() {
|
||||
coordinator?.openAddLocation()
|
||||
}
|
||||
|
||||
|
||||
func loadLocations() {
|
||||
Task {
|
||||
do {
|
||||
@ -63,7 +57,7 @@ extension LocationsListViewModel: LocationsListViewModeling {
|
||||
|
||||
try await loadRemoteLocations()
|
||||
|
||||
try fetchedResultsController.performFetch()
|
||||
try locationProvider.fetch()
|
||||
|
||||
viewStatus = .loaded
|
||||
} catch {
|
||||
@ -73,18 +67,11 @@ extension LocationsListViewModel: LocationsListViewModeling {
|
||||
}
|
||||
|
||||
func numberOfDataItems(in section: Int) -> Int {
|
||||
guard
|
||||
let sections = fetchedResultsController.sections,
|
||||
sections.endIndex > section
|
||||
else {
|
||||
return 0
|
||||
}
|
||||
|
||||
return sections[section].numberOfObjects
|
||||
locationProvider.numberOfLocationsInSection(section)
|
||||
}
|
||||
|
||||
func dataItem(at indexPath: IndexPath) -> Location {
|
||||
fetchedResultsController.object(at: indexPath)
|
||||
locationProvider.location(at: indexPath)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user