Skip to content

Commit

Permalink
feature: ask for notification permission on news page (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
WezSieTato committed Mar 16, 2023
1 parent 3711254 commit e6c493b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
9 changes: 5 additions & 4 deletions BuyPolish/Pola/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
analyticsProvider.configure()
applyAppearance()

let notificationProvider = DI.container.resolve(NotificationProvider.self)!
notificationProvider.register(in: application)
self.notificationProvider = notificationProvider

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = RootViewController(analyticsProvider: analyticsProvider)
window?.rootViewController = RootViewController(analyticsProvider: analyticsProvider, notificationProvider: notificationProvider)
window?.backgroundColor = R.color.backgroundWindowColor()
window?.makeKeyAndVisible()

Expand All @@ -32,9 +36,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ = handleShortcutItem(shortcutItem)
}

notificationProvider = DI.container.resolve(NotificationProvider.self)
notificationProvider?.register(in: application)

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ import Foundation
import UIKit

final class NotificationManager: NSObject, NotificationProvider {
weak var application: UIApplication?
func register(in application: UIApplication) {
UNUserNotificationCenter.current().delegate = self

self.application = application
}

func requestAuthorization() {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
options: authOptions) { [weak self] success, _ in
if success {
DispatchQueue.main.async {
self?.authorizationGranted()
}
}
}
}

application.registerForRemoteNotifications()
private func authorizationGranted() {
application?.registerForRemoteNotifications()

Messaging.messaging().token { token, error in
if let error = error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
protocol NotificationProvider {
func register(in application: UIApplication)
func requestAuthorization()
}
17 changes: 16 additions & 1 deletion BuyPolish/Pola/UI/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import UIKit
final class RootViewController: UITabBarController {
private let scanCodeViewController: ScanCodeViewController
private let analytics: AnalyticsHelper
private let notificationProvider: NotificationProvider

private enum TabOrder: Int, CaseIterable {
case scan = 0
case search
case news
}

init(analyticsProvider: AnalyticsProvider) {
init(analyticsProvider: AnalyticsProvider,
notificationProvider: NotificationProvider) {
analytics = AnalyticsHelper(provider: analyticsProvider)
scanCodeViewController = DI.container.resolve(ScanCodeViewController.self)!
self.notificationProvider = notificationProvider
super.init(nibName: nil, bundle: nil)

let strings = R.string.localizable.self
Expand Down Expand Up @@ -71,6 +74,18 @@ final class RootViewController: UITabBarController {
}

override func tabBar(_: UITabBar, didSelect item: UITabBarItem) {
requestNotifictionAuthorizationIfNeeded(selectedTab: item)
trackTabChanged(item: item)
}

private func requestNotifictionAuthorizationIfNeeded(selectedTab: UITabBarItem) {
guard TabOrder(rawValue: selectedTab.tag) == .news else {
return
}
notificationProvider.requestAuthorization()
}

private func trackTabChanged(item: UITabBarItem) {
let tab = TabOrder(rawValue: item.tag).map { order -> AnalyticsMainTab in
switch order {
case .scan:
Expand Down

0 comments on commit e6c493b

Please sign in to comment.