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