From dc7d0df55b1b5037feaa66498218f5f4f3e7d3a6 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Tue, 6 Oct 2020 16:26:11 -0700 Subject: [PATCH] Refactoring --- .../Sources/ViewModels/CollectionItemsViewModel.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift b/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift index 6aeed0e..3fa48e7 100644 --- a/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift +++ b/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift @@ -12,7 +12,7 @@ final public class CollectionItemsViewModel: ObservableObject { private let items = CurrentValueSubject<[[CollectionItem]], Never>([]) private let collectionService: CollectionService - private var viewModelCache = [CollectionItem: (CollectionItemViewModel, AnyCancellable)]() + private var viewModelCache = [CollectionItem: (viewModel: CollectionItemViewModel, events: AnyCancellable)]() private let navigationEventsSubject = PassthroughSubject() private let loadingSubject = PassthroughSubject() private var topVisibleIndexPath = IndexPath(item: 0, section: 0) @@ -98,12 +98,13 @@ extension CollectionItemsViewModel: CollectionViewModel { public func viewModel(indexPath: IndexPath) -> CollectionItemViewModel { let item = items.value[indexPath.section][indexPath.item] + let cachedViewModel = viewModelCache[item]?.viewModel switch item { case let .status(status, configuration): var viewModel: StatusViewModel - if let cachedViewModel = viewModelCache[item]?.0 as? StatusViewModel { + if let cachedViewModel = cachedViewModel as? StatusViewModel { viewModel = cachedViewModel } else { viewModel = .init(statusService: collectionService.navigationService.statusService(status: status)) @@ -114,7 +115,7 @@ extension CollectionItemsViewModel: CollectionViewModel { return viewModel case let .loadMore(loadMore): - if let cachedViewModel = viewModelCache[item]?.0 as? LoadMoreViewModel { + if let cachedViewModel = cachedViewModel { return cachedViewModel } @@ -125,7 +126,7 @@ extension CollectionItemsViewModel: CollectionViewModel { return viewModel case let .account(account): - if let cachedViewModel = viewModelCache[item]?.0 as? AccountViewModel { + if let cachedViewModel = cachedViewModel { return cachedViewModel } @@ -174,7 +175,7 @@ private extension CollectionItemsViewModel { if case let .remove(_, item, _) = removal, case let .loadMore(loadMore) = item, loadMore == lastSelectedLoadMore, - let direction = (viewModelCache[item]?.0 as? LoadMoreViewModel)?.direction, + let direction = (viewModelCache[item]?.viewModel as? LoadMoreViewModel)?.direction, direction == .up, let statusAfterLoadMore = flatItems.first(where: { guard case let .status(status, _) = $0 else { return false }