From 1a7e001ce339e8315f7fe1a045e95f061c9839dd Mon Sep 17 00:00:00 2001 From: Ivan Magda Date: Wed, 21 Jul 2021 17:58:23 +0300 Subject: [PATCH 1/2] Fix streaks alert presentation after login --- .../AuthNavigationViewController.swift | 8 ++-- .../StreaksAlertPresentationManager.swift | 23 ++++++++--- ...ProfileStreakNotificationsInteractor.swift | 40 ++++++++++++++----- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/Stepic/Legacy/Controllers/RegistrationSignIn/AuthNavigationViewController.swift b/Stepic/Legacy/Controllers/RegistrationSignIn/AuthNavigationViewController.swift index 011a6c19ba..a790e6ea5e 100644 --- a/Stepic/Legacy/Controllers/RegistrationSignIn/AuthNavigationViewController.swift +++ b/Stepic/Legacy/Controllers/RegistrationSignIn/AuthNavigationViewController.swift @@ -9,7 +9,7 @@ import UIKit final class AuthNavigationViewController: UINavigationController { - private let streaksAlertPresentationManager = StreaksAlertPresentationManager(source: .login) + private static let streaksAlertPresentationManager = StreaksAlertPresentationManager(source: .login) private let notificationSuggestionManager = NotificationSuggestionManager() private let userActivitiesAPI = UserActivitiesAPI() @@ -21,7 +21,7 @@ final class AuthNavigationViewController: UINavigationController { weak var source: UIViewController? { didSet { - streaksAlertPresentationManager.controller = source + Self.streaksAlertPresentationManager.controller = source } } var success: (() -> Void)? @@ -31,7 +31,7 @@ final class AuthNavigationViewController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() - streaksAlertPresentationManager.controller = source + Self.streaksAlertPresentationManager.controller = source navigationBar.shadowImage = UIImage() navigationBar.setBackgroundImage(UIImage(), for: .default) } @@ -47,7 +47,7 @@ final class AuthNavigationViewController: UINavigationController { after: .login ) if canShowAlert && userActivity.didSolveThisWeek { - self.streaksAlertPresentationManager.suggestStreak( + Self.streaksAlertPresentationManager.suggestStreak( streak: userActivity.currentStreak ) } diff --git a/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift b/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift index b67b8a10e4..613db04f40 100644 --- a/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift +++ b/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift @@ -22,7 +22,7 @@ final class StreaksAlertPresentationManager { }() private let streakTimePickerPresenter: Presentr = { - let streakTimePickerPresenter = Presentr(presentationType: .popup) + let streakTimePickerPresenter = Presentr(presentationType: .bottomHalf) return streakTimePickerPresenter }() @@ -61,14 +61,18 @@ final class StreaksAlertPresentationManager { let source = self.source.analyticsSource presenter.onPositiveCallback = { [weak self] in PreferencesContainer.notifications.allowStreaksNotifications = true + NotificationCenter.default.post( + name: .streaksAlertPresentationManagerDidChangeStreakNotifications, + object: nil + ) StepikAnalytics.shared.send( .streaksSuggestionSucceeded(index: NotificationSuggestionManager().streakAlertShownCnt) ) NotificationAlertsAnalytics(source: source).reportCustomAlertInteractionResult(.yes) - // When we are suggesting streak with the `Source` of .login type - `self` will be deallocated at this point. - // In this case we need to register for remote notifications. + assert(self != nil) + if let strongSelf = self { strongSelf.notifyPressed() } else { @@ -101,7 +105,12 @@ final class StreaksAlertPresentationManager { } } - private func didChooseTime() {} + private func didChooseTime() { + NotificationCenter.default.post( + name: .streaksAlertPresentationManagerDidChangeStreakNotifications, + object: PreferencesContainer.notifications.streaksNotificationStartHourUTC + ) + } private func selectStreakNotificationTime() { guard let controller = controller else { @@ -144,7 +153,6 @@ final class StreaksAlertPresentationManager { case .denied: self?.showSettingsAlert() } - return } } @@ -205,3 +213,8 @@ final class StreaksAlertPresentationManager { } } } + +extension Foundation.Notification.Name { + static let streaksAlertPresentationManagerDidChangeStreakNotifications = Foundation.Notification + .Name("streaksAlertPresentationManagerDidChangeStreakNotifications") +} diff --git a/Stepic/Sources/Modules/NewProfileSubmodules/StreakNotifications/NewProfileStreakNotificationsInteractor.swift b/Stepic/Sources/Modules/NewProfileSubmodules/StreakNotifications/NewProfileStreakNotificationsInteractor.swift index 1969a90f75..54f206f2cb 100644 --- a/Stepic/Sources/Modules/NewProfileSubmodules/StreakNotifications/NewProfileStreakNotificationsInteractor.swift +++ b/Stepic/Sources/Modules/NewProfileSubmodules/StreakNotifications/NewProfileStreakNotificationsInteractor.swift @@ -47,6 +47,12 @@ final class NewProfileStreakNotificationsInteractor: NewProfileStreakNotificatio name: .notificationsRegistrationServiceDidUpdatePermissionStatus, object: nil ) + NotificationCenter.default.addObserver( + self, + selector: #selector(self.onStreaksAlertPresentationManagerDidChangeStreakNotifications), + name: .streaksAlertPresentationManagerDidChangeStreakNotifications, + object: nil + ) self.checkPermissionStatus() } @@ -86,14 +92,24 @@ final class NewProfileStreakNotificationsInteractor: NewProfileStreakNotificatio // MARK: Private API - private func presentStreakNotifications(isStreakNotificationsEnabled: Bool? = nil) { - let isOn = isStreakNotificationsEnabled ?? self.streakNotificationsStorageManager.isStreakNotificationsEnabled - self.presenter.presentStreakNotifications( - response: .init( - isStreakNotificationsEnabled: isOn, - streaksNotificationsStartHour: self.streakNotificationsStorageManager.streakNotificationsStartHourUTC + private func presentStreakNotifications(permissionStatus: NotificationPermissionStatus? = nil) { + firstly { () -> Guarantee in + if let permissionStatus = permissionStatus { + return .value(permissionStatus) + } + return NotificationPermissionStatus.current + }.done { permissionStatus in + let isOn = permissionStatus.isRegistered + && self.streakNotificationsStorageManager.isStreakNotificationsEnabled + let startHour = self.streakNotificationsStorageManager.streakNotificationsStartHourUTC + + self.presenter.presentStreakNotifications( + response: .init( + isStreakNotificationsEnabled: isOn, + streaksNotificationsStartHour: startHour + ) ) - ) + } } private func setStreakNotificationsEnabled(_ enabled: Bool) { @@ -121,12 +137,14 @@ final class NewProfileStreakNotificationsInteractor: NewProfileStreakNotificatio @objc private func onPermissionStatusUpdate(_ notification: Foundation.Notification) { - guard let permissionStatus = notification.object as? NotificationPermissionStatus else { - return + if let permissionStatus = notification.object as? NotificationPermissionStatus { + self.presentStreakNotifications(permissionStatus: permissionStatus) } + } - let isOn = self.streakNotificationsStorageManager.isStreakNotificationsEnabled && permissionStatus.isRegistered - self.presentStreakNotifications(isStreakNotificationsEnabled: isOn) + @objc + private func onStreaksAlertPresentationManagerDidChangeStreakNotifications() { + self.doStreakNotificationsLoad(request: .init()) } private func checkPermissionStatus() { From eb7a4ef3d3321705fe59e836eb131a0da34a4b09 Mon Sep 17 00:00:00 2001 From: Ivan Magda Date: Wed, 21 Jul 2021 18:24:04 +0300 Subject: [PATCH 2/2] Fix streaks alert presentation after submission --- .../Model/Managers/StreaksAlertPresentationManager.swift | 2 +- .../Modules/Quizzes/BaseQuiz/BaseQuizViewController.swift | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift b/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift index 613db04f40..0102d59e15 100644 --- a/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift +++ b/Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift @@ -22,7 +22,7 @@ final class StreaksAlertPresentationManager { }() private let streakTimePickerPresenter: Presentr = { - let streakTimePickerPresenter = Presentr(presentationType: .bottomHalf) + let streakTimePickerPresenter = Presentr(presentationType: .popup) return streakTimePickerPresenter }() diff --git a/Stepic/Sources/Modules/Quizzes/BaseQuiz/BaseQuizViewController.swift b/Stepic/Sources/Modules/Quizzes/BaseQuiz/BaseQuizViewController.swift index ed4407a5c2..1dfb986b21 100644 --- a/Stepic/Sources/Modules/Quizzes/BaseQuiz/BaseQuizViewController.swift +++ b/Stepic/Sources/Modules/Quizzes/BaseQuiz/BaseQuizViewController.swift @@ -13,6 +13,8 @@ final class BaseQuizViewController: UIViewController, ControllerWithStepikPlaceh lazy var baseQuizView = self.view as? BaseQuizView + private lazy var streaksAlertPresentationManager = StreaksAlertPresentationManager(source: .submission) + private var quizAssembly: QuizAssembly private var childQuizModuleInput: QuizInputProtocol? { self.quizAssembly.moduleInput } @@ -151,9 +153,8 @@ extension BaseQuizViewController: BaseQuizViewControllerProtocol { } func displayStreakAlert(viewModel: BaseQuiz.StreakAlertPresentation.ViewModel) { - let streaksAlertPresentationManager = StreaksAlertPresentationManager(source: .submission) - streaksAlertPresentationManager.controller = self - streaksAlertPresentationManager.suggestStreak(streak: viewModel.streak) + self.streaksAlertPresentationManager.controller = self + self.streaksAlertPresentationManager.suggestStreak(streak: viewModel.streak) } }