Implemented the handling of Locations entity changes in the managed context in the LocationsListViewController view controller and the LocationsListViewModel view model.

This commit is contained in:
Javier Cicchelli 2023-04-12 23:17:30 +02:00
parent e17c407af4
commit 4ebd954176
3 changed files with 38 additions and 1 deletions

View File

@ -16,6 +16,7 @@ protocol LocationsListViewModeling: AnyObject {
var coordinator: LocationsListCoordination? { get set }
var locationsDidChangePublisher: PassthroughSubject<[Change], Never> { get }
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { get }
var numberOfSectionsInData: Int { get }

View File

@ -161,6 +161,41 @@ private extension LocationsListViewController {
}
}
.store(in: &cancellables)
viewModel
.controllerDidChangePublisher
.sink(receiveValue: { [weak self] updates in
var movedToIndexPaths = [IndexPath]()
self?.table.performBatchUpdates({
for update in updates {
switch update {
case let .section(sectionUpdate):
switch sectionUpdate {
case let .inserted(index):
self?.table.insertSections([index], with: .automatic)
case let .deleted(index):
self?.table.deleteSections([index], with: .automatic)
}
case let .object(objectUpdate):
switch objectUpdate {
case let .inserted(at: indexPath):
self?.table.insertRows(at: [indexPath], with: .automatic)
case let .deleted(from: indexPath):
self?.table.deleteRows(at: [indexPath], with: .automatic)
case let .updated(at: indexPath):
self?.table.reloadRows(at: [indexPath], with: .automatic)
case let .moved(from: source, to: target):
self?.table.moveRow(at: source, to: target)
movedToIndexPaths.append(target)
}
}
}
}, completion: { done in
self?.table.reloadRows(at: movedToIndexPaths, with: .automatic)
})
})
.store(in: &cancellables)
}
@objc func addLocationPressed() {

View File

@ -41,6 +41,7 @@ extension LocationsListViewModel: LocationsListViewModeling {
// MARK: Properties
var locationsDidChangePublisher: PassthroughSubject<[Persistence.Change], Never> { locationProvider.didChangePublisher }
var numberOfSectionsInData: Int { locationProvider.numberOfSections }
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }