Refactoring

This commit is contained in:
Justin Mazzocchi 2020-10-06 16:26:11 -07:00
parent c589c74052
commit dc7d0df55b
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -12,7 +12,7 @@ final public class CollectionItemsViewModel: ObservableObject {
private let items = CurrentValueSubject<[[CollectionItem]], Never>([]) private let items = CurrentValueSubject<[[CollectionItem]], Never>([])
private let collectionService: CollectionService private let collectionService: CollectionService
private var viewModelCache = [CollectionItem: (CollectionItemViewModel, AnyCancellable)]() private var viewModelCache = [CollectionItem: (viewModel: CollectionItemViewModel, events: AnyCancellable)]()
private let navigationEventsSubject = PassthroughSubject<NavigationEvent, Never>() private let navigationEventsSubject = PassthroughSubject<NavigationEvent, Never>()
private let loadingSubject = PassthroughSubject<Bool, Never>() private let loadingSubject = PassthroughSubject<Bool, Never>()
private var topVisibleIndexPath = IndexPath(item: 0, section: 0) private var topVisibleIndexPath = IndexPath(item: 0, section: 0)
@ -98,12 +98,13 @@ extension CollectionItemsViewModel: CollectionViewModel {
public func viewModel(indexPath: IndexPath) -> CollectionItemViewModel { public func viewModel(indexPath: IndexPath) -> CollectionItemViewModel {
let item = items.value[indexPath.section][indexPath.item] let item = items.value[indexPath.section][indexPath.item]
let cachedViewModel = viewModelCache[item]?.viewModel
switch item { switch item {
case let .status(status, configuration): case let .status(status, configuration):
var viewModel: StatusViewModel var viewModel: StatusViewModel
if let cachedViewModel = viewModelCache[item]?.0 as? StatusViewModel { if let cachedViewModel = cachedViewModel as? StatusViewModel {
viewModel = cachedViewModel viewModel = cachedViewModel
} else { } else {
viewModel = .init(statusService: collectionService.navigationService.statusService(status: status)) viewModel = .init(statusService: collectionService.navigationService.statusService(status: status))
@ -114,7 +115,7 @@ extension CollectionItemsViewModel: CollectionViewModel {
return viewModel return viewModel
case let .loadMore(loadMore): case let .loadMore(loadMore):
if let cachedViewModel = viewModelCache[item]?.0 as? LoadMoreViewModel { if let cachedViewModel = cachedViewModel {
return cachedViewModel return cachedViewModel
} }
@ -125,7 +126,7 @@ extension CollectionItemsViewModel: CollectionViewModel {
return viewModel return viewModel
case let .account(account): case let .account(account):
if let cachedViewModel = viewModelCache[item]?.0 as? AccountViewModel { if let cachedViewModel = cachedViewModel {
return cachedViewModel return cachedViewModel
} }
@ -174,7 +175,7 @@ private extension CollectionItemsViewModel {
if case let .remove(_, item, _) = removal, if case let .remove(_, item, _) = removal,
case let .loadMore(loadMore) = item, case let .loadMore(loadMore) = item,
loadMore == lastSelectedLoadMore, loadMore == lastSelectedLoadMore,
let direction = (viewModelCache[item]?.0 as? LoadMoreViewModel)?.direction, let direction = (viewModelCache[item]?.viewModel as? LoadMoreViewModel)?.direction,
direction == .up, direction == .up,
let statusAfterLoadMore = flatItems.first(where: { let statusAfterLoadMore = flatItems.first(where: {
guard case let .status(status, _) = $0 else { return false } guard case let .status(status, _) = $0 else { return false }