Implemented the "loadLocations()" function in the LocationsListViewModel view model.
This commit is contained in:
parent
94d905ffc3
commit
985e8ffe8e
@ -6,10 +6,17 @@
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Combine
|
||||
import Core
|
||||
import Dependency
|
||||
import Foundation
|
||||
import Persistence
|
||||
|
||||
class LocationsListViewModel: ObservableObject {
|
||||
|
||||
// MARK: Dependencies
|
||||
|
||||
@Dependency(\.persistence) private var persistence
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@ -17,6 +24,15 @@ class LocationsListViewModel: ObservableObject {
|
||||
|
||||
@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) {
|
||||
@ -39,6 +55,22 @@ extension LocationsListViewModel: LocationsListViewModeling {
|
||||
coordinator?.openAddLocation()
|
||||
}
|
||||
|
||||
func loadLocations() {
|
||||
Task {
|
||||
do {
|
||||
viewStatus = .loading
|
||||
|
||||
try await loadRemoteLocations()
|
||||
|
||||
try fetchedResultsController.performFetch()
|
||||
|
||||
viewStatus = .loaded
|
||||
} catch {
|
||||
viewStatus = .error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Enumerations
|
||||
|
Loading…
x
Reference in New Issue
Block a user