diff --git a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift index aa3a71f..385b599 100644 --- a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift +++ b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift @@ -16,6 +16,7 @@ protocol LocationsListViewModeling: AnyObject { var coordinator: LocationsListCoordination? { get set } + var locationsDidChangePublisher: PassthroughSubject<[Change], Never> { get } var viewStatusPublisher: Published.Publisher { get } var numberOfSectionsInData: Int { get } diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index 5daf098..e20b00e 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -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() { diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index 9bde5b2..b5e2384 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -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.Publisher { $viewStatus }