Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix streak notifications manager not checking notification permission status #1015

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -21,7 +21,7 @@ final class AuthNavigationViewController: UINavigationController {

weak var source: UIViewController? {
didSet {
streaksAlertPresentationManager.controller = source
Self.streaksAlertPresentationManager.controller = source
}
}
var success: (() -> Void)?
Expand All @@ -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)
}
Expand All @@ -47,7 +47,7 @@ final class AuthNavigationViewController: UINavigationController {
after: .login
)
if canShowAlert && userActivity.didSolveThisWeek {
self.streaksAlertPresentationManager.suggestStreak(
Self.streaksAlertPresentationManager.suggestStreak(
streak: userActivity.currentStreak
)
}
Expand Down
21 changes: 17 additions & 4 deletions Stepic/Legacy/Model/Managers/StreaksAlertPresentationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -144,7 +153,6 @@ final class StreaksAlertPresentationManager {
case .denied:
self?.showSettingsAlert()
}
return
}
}

Expand Down Expand Up @@ -205,3 +213,8 @@ final class StreaksAlertPresentationManager {
}
}
}

extension Foundation.Notification.Name {
static let streaksAlertPresentationManagerDidChangeStreakNotifications = Foundation.Notification
.Name("streaksAlertPresentationManagerDidChangeStreakNotifications")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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<NotificationPermissionStatus> 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) {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
}
}

Expand Down