diff --git a/View Controllers/MainNavigationViewController.swift b/View Controllers/MainNavigationViewController.swift index 0c9b28d..05bf7b1 100644 --- a/View Controllers/MainNavigationViewController.swift +++ b/View Controllers/MainNavigationViewController.swift @@ -25,12 +25,6 @@ final class MainNavigationViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() - setupViewControllers() - - if viewModel.identityContext.identity.authenticated { - setupNewStatusButton() - } - viewModel.$presentingSecondaryNavigation.sink { [weak self] in if $0 { self?.presentSecondaryNavigation() @@ -40,10 +34,20 @@ final class MainNavigationViewController: UITabBarController { } .store(in: &cancellables) + viewModel.identityContext.$identity.map(\.pending) + .removeDuplicates() + .print() + .sink { [weak self] in self?.setupViewControllers(pending: $0) } + .store(in: &cancellables) + viewModel.timelineNavigations.map { _ in } .merge(with: viewModel.followRequestNavigations.map { _ in }) .sink { [weak self] in self?.selectedIndex = 0 } .store(in: &cancellables) + + NotificationCenter.default.publisher(for: UIScene.willEnterForegroundNotification) + .sink { [weak self] _ in self?.viewModel.refreshIdentity() } + .store(in: &cancellables) } override func viewWillAppear(_ animated: Bool) { @@ -54,29 +58,29 @@ final class MainNavigationViewController: UITabBarController { } private extension MainNavigationViewController { - func setupViewControllers() { + func setupViewControllers(pending: Bool) { var controllers: [UIViewController] = [ TimelinesViewController( viewModel: viewModel, rootViewModel: rootViewModel), ExploreViewController( - viewModel: viewModel.exploreViewModel, + viewModel: viewModel.exploreViewModel(), rootViewModel: rootViewModel) ] - if viewModel.identityContext.identity.authenticated { + if viewModel.identityContext.identity.authenticated && !pending { controllers.append(NotificationsViewController(viewModel: viewModel, rootViewModel: rootViewModel)) - } - if let conversationsViewModel = viewModel.conversationsViewModel { let conversationsViewController = TableViewController( - viewModel: conversationsViewModel, + viewModel: viewModel.conversationsViewModel(), rootViewModel: rootViewModel) conversationsViewController.tabBarItem = NavigationViewModel.Tab.messages.tabBarItem conversationsViewController.navigationItem.title = NavigationViewModel.Tab.messages.title controllers.append(conversationsViewController) + + setupNewStatusButton() } let secondaryNavigationButton = SecondaryNavigationButton(viewModel: viewModel, rootViewModel: rootViewModel) diff --git a/ViewModels/Sources/ViewModels/View Models/NavigationViewModel.swift b/ViewModels/Sources/ViewModels/View Models/NavigationViewModel.swift index d75f481..9461bc0 100644 --- a/ViewModels/Sources/ViewModels/View Models/NavigationViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/NavigationViewModel.swift @@ -14,30 +14,6 @@ public final class NavigationViewModel: ObservableObject { @Published public var presentingSecondaryNavigation = false @Published public var alertItem: AlertItem? - public lazy var exploreViewModel: ExploreViewModel = { - let exploreViewModel = ExploreViewModel( - service: identityContext.service.exploreService(), - identityContext: identityContext) - - // TODO: initial request - - return exploreViewModel - }() - - public lazy var conversationsViewModel: CollectionViewModel? = { - if identityContext.identity.authenticated { - let conversationsViewModel = CollectionItemsViewModel( - collectionService: identityContext.service.conversationsService(), - identityContext: identityContext) - - conversationsViewModel.request(maxId: nil, minId: nil, search: nil) - - return conversationsViewModel - } else { - return nil - } - }() - private let timelineNavigationsSubject = PassthroughSubject() private let followRequestNavigationsSubject = PassthroughSubject() private var cancellables = Set() @@ -141,6 +117,16 @@ public extension NavigationViewModel { identityContext: identityContext) } + func exploreViewModel() -> ExploreViewModel { + let exploreViewModel = ExploreViewModel( + service: identityContext.service.exploreService(), + identityContext: identityContext) + + // TODO: initial request + + return exploreViewModel + } + func notificationsViewModel(excludeTypes: Set) -> CollectionItemsViewModel { let viewModel = CollectionItemsViewModel( collectionService: identityContext.service.notificationsService(excludeTypes: excludeTypes), @@ -150,4 +136,14 @@ public extension NavigationViewModel { return viewModel } + + func conversationsViewModel() -> CollectionViewModel { + let conversationsViewModel = CollectionItemsViewModel( + collectionService: identityContext.service.conversationsService(), + identityContext: identityContext) + + conversationsViewModel.request(maxId: nil, minId: nil, search: nil) + + return conversationsViewModel + } }