From e17c407af4296b95db90abf9de0b78302647b70d Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 23:15:47 +0200 Subject: [PATCH] Integrated the LocationProvider provider in the LocationsListViewModel view model. --- .../LocationsListViewModel.swift | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index ebf3e9d..9bde5b2 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -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.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.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) } }