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:
parent
e17c407af4
commit
4ebd954176
@ -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 }
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -40,7 +40,8 @@ class LocationsListViewModel: ObservableObject {
|
||||
extension LocationsListViewModel: LocationsListViewModeling {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
|
||||
var locationsDidChangePublisher: PassthroughSubject<[Persistence.Change], Never> { locationProvider.didChangePublisher }
|
||||
var numberOfSectionsInData: Int { locationProvider.numberOfSections }
|
||||
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user