diff --git a/Wikipedia/Code/ArticleFetchedResultsViewController.swift b/Wikipedia/Code/ArticleFetchedResultsViewController.swift index 3ffe801b8a..147c8362e2 100644 --- a/Wikipedia/Code/ArticleFetchedResultsViewController.swift +++ b/Wikipedia/Code/ArticleFetchedResultsViewController.swift @@ -48,26 +48,12 @@ class ArticleFetchedResultsViewController: ArticleCollectionViewController, Coll var deleteAllConfirmationText: String? = nil var deleteAllCancelText: String? = nil var deleteAllText: String? = nil - var isDeleteAllVisible: Bool = false open func deleteAll() { } - fileprivate final func updateDeleteButton() { - guard isDeleteAllVisible else { - navigationItem.rightBarButtonItem = nil - return - } - - if navigationItem.rightBarButtonItem == nil { - navigationItem.rightBarButtonItem = UIBarButtonItem(title: deleteAllButtonText, style: .plain, target: self, action: #selector(deleteButtonPressed(_:))) - } - - navigationItem.rightBarButtonItem?.isEnabled = !isEmpty - } - - @objc fileprivate final func deleteButtonPressed(_ sender: UIBarButtonItem) { + @objc final func deleteButtonPressed(_ sender: UIBarButtonItem) { let alertController = UIAlertController(title: deleteAllConfirmationText, message: nil, preferredStyle: .actionSheet) alertController.addAction(UIAlertAction(title: deleteAllText, style: .destructive, handler: { (action) in self.deleteAll() @@ -91,11 +77,6 @@ class ArticleFetchedResultsViewController: ArticleCollectionViewController, Coll func collectionViewUpdater(_ updater: CollectionViewUpdater, updateItemAtIndexPath indexPath: IndexPath, in collectionView: UICollectionView) { } - - override func isEmptyDidChange() { - super.isEmptyDidChange() - updateDeleteButton() - } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) diff --git a/Wikipedia/Code/HistoryViewController.swift b/Wikipedia/Code/HistoryViewController.swift index 1dd20569f6..dcdfec6f97 100644 --- a/Wikipedia/Code/HistoryViewController.swift +++ b/Wikipedia/Code/HistoryViewController.swift @@ -9,6 +9,48 @@ class HistoryViewController: ArticleFetchedResultsViewController, WMFNavigationB var topSafeAreaOverlayHeightConstraint: NSLayoutConstraint? var topSafeAreaOverlayView: UIView? + + // Properties needed for Profile Button + + private var _yirCoordinator: YearInReviewCoordinator? + var yirCoordinator: YearInReviewCoordinator? { + + guard let navigationController, + let yirDataController, + let dataStore else { + return nil + } + + guard let existingYirCoordinator = _yirCoordinator else { + _yirCoordinator = YearInReviewCoordinator(navigationController: navigationController, theme: theme, dataStore: dataStore, dataController: yirDataController) + _yirCoordinator?.badgeDelegate = self + return _yirCoordinator + } + + return existingYirCoordinator + } + + private var _profileCoordinator: ProfileCoordinator? + private var profileCoordinator: ProfileCoordinator? { + + guard let navigationController, + let yirCoordinator = self.yirCoordinator, + let dataStore else { + return nil + } + + guard let existingProfileCoordinator = _profileCoordinator else { + _profileCoordinator = ProfileCoordinator(navigationController: navigationController, theme: theme, dataStore: dataStore, donateSouce: .savedProfile, logoutDelegate: self, sourcePage: ProfileCoordinatorSource.saved, yirCoordinator: yirCoordinator) + _profileCoordinator?.badgeDelegate = self + return _profileCoordinator + } + + return existingProfileCoordinator + } + + private var yirDataController: WMFYearInReviewDataController? { + return try? WMFYearInReviewDataController() + } override var headerStyle: ColumnarCollectionViewController.HeaderStyle { return .sections @@ -32,7 +74,6 @@ class HistoryViewController: ArticleFetchedResultsViewController, WMFNavigationB deleteAllConfirmationText = WMFLocalizedString("history-clear-confirmation-heading", value: "Are you sure you want to delete all your recent items?", comment: "Heading text of delete all confirmation dialog") deleteAllCancelText = WMFLocalizedString("history-clear-cancel", value: "Cancel", comment: "Button text for cancelling delete all action {{Identical|Cancel}}") deleteAllText = WMFLocalizedString("history-clear-delete-all", value: "Yes, delete all", comment: "Button text for confirming delete all action") - isDeleteAllVisible = true setupTopSafeAreaOverlay(scrollView: collectionView) } @@ -137,8 +178,45 @@ class HistoryViewController: ArticleFetchedResultsViewController, WMFNavigationB } let hideNavigationBarOnScroll = !isEmpty + + let deleteButton = UIBarButtonItem(title: deleteAllButtonText, style: .plain, target: self, action: #selector(deleteButtonPressed(_:))) + deleteButton.isEnabled = !isEmpty + + let profileButtonConfig: WMFNavigationBarProfileButtonConfig? + if let dataStore { + profileButtonConfig = self.profileButtonConfig(target: self, action: #selector(userDidTapProfile), dataStore: dataStore, yirDataController: yirDataController, leadingBarButtonItem: deleteButton, trailingBarButtonItem: nil) + } else { + profileButtonConfig = nil + } + + configureNavigationBar(titleConfig: titleConfig, closeButtonConfig: nil, profileButtonConfig: profileButtonConfig, searchBarConfig: nil, hideNavigationBarOnScroll: hideNavigationBarOnScroll) + } + + private func updateProfileButton() { - configureNavigationBar(titleConfig: titleConfig, closeButtonConfig: nil, profileButtonConfig: nil, searchBarConfig: nil, hideNavigationBarOnScroll: hideNavigationBarOnScroll) + guard let dataStore else { + return + } + + let config = self.profileButtonConfig(target: self, action: #selector(userDidTapProfile), dataStore: dataStore, yirDataController: yirDataController, leadingBarButtonItem: nil, trailingBarButtonItem: nil) + updateNavigationBarProfileButton(needsBadge: config.needsBadge, needsBadgeLabel: CommonStrings.profileButtonBadgeTitle, noBadgeLabel: CommonStrings.profileButtonTitle) + } + + @objc func userDidTapProfile() { + + guard let dataStore else { + return + } + + guard let languageCode = dataStore.languageLinkController.appLanguage?.languageCode, + let metricsID = DonateCoordinator.metricsID(for: .savedProfile, languageCode: languageCode) else { + return + } + + // TODO: Do we need logging like this? + // DonateFunnel.shared.logExploreProfile(metricsID: metricsID) + + profileCoordinator?.start() } func titleForHeaderInSection(_ section: Int) -> String? { @@ -184,6 +262,28 @@ class HistoryViewController: ArticleFetchedResultsViewController, WMFNavigationB override func apply(theme: Theme) { super.apply(theme: theme) + updateProfileButton() + profileCoordinator?.theme = theme + themeTopSafeAreaOverlay() } } + +extension HistoryViewController: LogoutCoordinatorDelegate { + func didTapLogout() { + + guard let dataStore else { + return + } + + wmf_showKeepSavedArticlesOnDevicePanelIfNeeded(triggeredBy: .logout, theme: theme) { + dataStore.authenticationManager.logout(initiatedBy: .user) + } + } +} + +extension HistoryViewController: YearInReviewBadgeDelegate { + func updateYIRBadgeVisibility() { + updateProfileButton() + } +} diff --git a/Wikipedia/Code/SearchViewController.swift b/Wikipedia/Code/SearchViewController.swift index 7dadd76a59..53d32a46f9 100644 --- a/Wikipedia/Code/SearchViewController.swift +++ b/Wikipedia/Code/SearchViewController.swift @@ -1,5 +1,6 @@ import UIKit import WMFComponents +import WMFData class SearchViewController: ArticleCollectionViewController, WMFNavigationBarConfiguring, WMFNavigationBarHiding {