Skip to content

Commit

Permalink
Refactor notifications (#952)
Browse files Browse the repository at this point in the history
* Remove CRToast & LegacyNotificationsPresenter

* Migrate handle types of local and remote notification settings

* Migrate NotificationsRegistrationService

* Migrate LocalNotificationsService

* Migrate LocalNotificationContentProvider -> LocalNotificationProtocol

* Move notifications to Sources

* Move PermissionStatus

* Clean NotificationTimePickerViewController
  • Loading branch information
ivan-magda authored Apr 13, 2021
1 parent ec70f39 commit 754064e
Show file tree
Hide file tree
Showing 27 changed files with 537 additions and 880 deletions.
2 changes: 0 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def all_pods
pod 'IQKeyboardManagerSwift', '6.5.6'

pod 'Kanna', '5.2.2'
# Remove after NotificationsService refactoring
pod 'CRToast', '0.0.9'
pod 'TUSafariActivity', '1.0.4'

# Social SDKs
Expand Down
6 changes: 1 addition & 5 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ PODS:
- CocoaLumberjack (3.7.0):
- CocoaLumberjack/Core (= 3.7.0)
- CocoaLumberjack/Core (3.7.0)
- CRToast (0.0.9)
- DeviceKit (4.3.0)
- DownloadButton (0.1.0)
- EasyTipView (2.1.0)
Expand Down Expand Up @@ -210,7 +209,6 @@ DEPENDENCIES:
- BEMCheckBox (= 1.4.1)
- Branch (= 1.39.2)
- Charts (= 3.6.0)
- CRToast (= 0.0.9)
- DeviceKit (= 4.3.0)
- DownloadButton (= 0.1.0)
- EasyTipView (= 2.1.0)
Expand Down Expand Up @@ -261,7 +259,6 @@ SPEC REPOS:
- Branch
- Charts
- CocoaLumberjack
- CRToast
- DeviceKit
- DownloadButton
- EasyTipView
Expand Down Expand Up @@ -346,7 +343,6 @@ SPEC CHECKSUMS:
Branch: 6a281514287f99d707615ac62c2cca69e0213df0
Charts: b1e3a1f5a1c9ba5394438ca3b91bd8c9076310af
CocoaLumberjack: e8955b9d337ac307103b0a34fd141c32f27e53c5
CRToast: 5a78c22b921c5ed3487488af605bc403a9c92fed
DeviceKit: 7decde449c308ac7e816e2184ded1c7c596fb885
DownloadButton: 49a21a89e0d7d1b42d9134f79aaa40e727cd57c3
EasyTipView: a92b6edc377b81c5ac18e9fd35d5ee78e9409488
Expand Down Expand Up @@ -402,6 +398,6 @@ SPEC CHECKSUMS:
VK-ios-sdk: 5bcf00a2014a7323f98db9328b603d4f96635caa
YandexMobileMetrica: bf9b2bceb40811ab29c3d0f660ef13d381d8dd93

PODFILE CHECKSUM: b69bd78fc9f62642c86138e82167d75e186a16b0
PODFILE CHECKSUM: 37e1045751e3a882272f0d15079ed89c9efe5930

COCOAPODS: 1.10.1
126 changes: 59 additions & 67 deletions Stepic.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions Stepic/Legacy/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

NotificationCenter.default.addObserver(
self,
selector: #selector(self.didReceiveRegistrationToken(_:)),
name: .InstanceIDTokenRefresh,
selector: #selector(self.messagingRegistrationTokenDidRefresh),
name: .MessagingRegistrationTokenRefreshed,
object: nil
)
NotificationCenter.default.addObserver(
Expand Down Expand Up @@ -155,7 +155,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

NotificationsBadgesManager.shared.set(number: application.applicationIconBadgeNumber)

self.notificationsService.removeRetentionNotifications()
self.notificationsService.removeRetentionLocalNotifications()
self.coursePurchaseReminder.updateAllPurchaseNotifications()

self.userCoursesObserver.startObserving()
Expand All @@ -179,7 +179,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationWillResignActive(_ application: UIApplication) {
self.notificationsService.scheduleRetentionNotifications()
self.notificationsService.scheduleRetentionLocalNotifications()
self.userCoursesObserver.stopObserving()
self.visitedCoursesCleaner.removeObservers()

Expand Down Expand Up @@ -226,31 +226,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.notificationsService.handleRemoteNotification(with: userInfo)
}

@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework")
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
self.notificationsService.handleLocalNotification(with: notification.userInfo)
}

func application(
_ application: UIApplication,
didRegister notificationSettings: UIUserNotificationSettings
) {
self.notificationsRegistrationService.handleRegisteredNotificationSettings(notificationSettings)
}

// MARK: Private Helpers

@objc
private func didReceiveRegistrationToken(_ notification: Foundation.Notification) {
private func messagingRegistrationTokenDidRefresh() {
guard AuthInfo.shared.isAuthorized else {
return
}

InstanceID.instanceID().instanceID { [weak self] (result, error) in
Messaging.messaging().token { [weak self] (token, error) in
if let error = error {
print("Error fetching Firebase remote instance ID: \(error)")
} else if let result = result {
self?.notificationsRegistrationService.registerDevice(result.token)
print("Error fetching FCM token: \(error)")
} else if let token = token {
self?.notificationsRegistrationService.registerDevice(token)
}
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class PersonalDeadlinesService: PersonalDeadlinesServiceProtocol {
return self.storageRecordsAPI.create(record: record)
}.done { createdRecord in
self.localStorageManager.set(storageRecord: createdRecord, for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
seal.fulfill(())
}.catch { error in
seal.reject(error)
Expand All @@ -68,15 +68,15 @@ final class PersonalDeadlinesService: PersonalDeadlinesServiceProtocol {
if storageRecords.isEmpty {
courses.forEach { course in
self.localStorageManager.deleteRecord(for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
}
seal.fulfill(())
} else {
storageRecords.forEach { storageRecord in
if case .deadline(let courseID)? = storageRecord.kind,
let course = courses.first(where: { $0.id == courseID }) {
self.localStorageManager.set(storageRecord: storageRecord, for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
}
}
seal.fulfill(())
Expand All @@ -95,12 +95,12 @@ final class PersonalDeadlinesService: PersonalDeadlinesServiceProtocol {
).done { storageRecords, _ in
guard let storageRecord = storageRecords.first else {
self.localStorageManager.deleteRecord(for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
seal.fulfill(())
return
}
self.localStorageManager.set(storageRecord: storageRecord, for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
seal.fulfill(())
}.catch { error in
seal.reject(error)
Expand All @@ -126,7 +126,7 @@ final class PersonalDeadlinesService: PersonalDeadlinesServiceProtocol {
record.data = dataToChange
storageRecordsAPI.update(record: record).done { updatedRecord in
self.localStorageManager.set(storageRecord: updatedRecord, for: course)
self.notificationsService.updatePersonalDeadlineNotifications(for: course)
self.notificationsService.updatePersonalDeadlineLocalNotifications(course: course)
seal.fulfill(())
}.catch { error in
seal.reject(error)
Expand All @@ -141,7 +141,7 @@ final class PersonalDeadlinesService: PersonalDeadlinesServiceProtocol {
return
}
storageRecordsAPI.delete(id: record.id).done { _ in
self.notificationsService.removePersonalDeadlineNotifications(for: course)
self.notificationsService.removePersonalDeadlineLocalNotifications(course: course)
self.localStorageManager.deleteRecord(for: course)
seal.fulfill(())
}.catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,58 @@
import UIKit

final class NotificationTimePickerViewController: PickerViewController {
var startHour: Int = 0
var startHour = 0

override func viewDidLoad() {
super.viewDidLoad()
self.setup()
}

private func setup() {
self.titleLabel.text = NSLocalizedString("SelectTimeTitle", comment: "")

self.initializeData()
self.initializeSelectedAction()
self.picker.reloadAllComponents()
self.picker.selectRow(startHour, inComponent: 0, animated: false)
}
self.data = (0..<24).map(self.makeFormattedStreakTimeInterval(startHour:))

func initializeData() {
data = []
for hour in 0..<24 {
data += [getDisplayingStreakTimeInterval(startHour: hour)]
}
}
self.selectedAction = { [weak self] in
guard let strongSelf = self else {
return
}

func initializeSelectedAction() {
selectedAction = {
[weak self] in
if let s = self {
let selectedLocalStartHour = s.picker.selectedRow(inComponent: 0)
let timeZoneDiff = NSTimeZone.system.secondsFromGMT() / 3600
var selectedUTCStartHour = selectedLocalStartHour - timeZoneDiff
let selectedLocalStartHour = strongSelf.picker.selectedRow(inComponent: 0)
let timeZoneDiff = NSTimeZone.system.secondsFromGMT() / 3600
var selectedUTCStartHour = selectedLocalStartHour - timeZoneDiff

if selectedUTCStartHour < 0 {
selectedUTCStartHour = 24 + selectedUTCStartHour
}
if selectedUTCStartHour < 0 {
selectedUTCStartHour += 24
}

if selectedUTCStartHour > 23 {
selectedUTCStartHour = selectedUTCStartHour - 24
}
if selectedUTCStartHour > 23 {
selectedUTCStartHour -= 24
}

print("selected UTC start hour -> \(selectedUTCStartHour)")
print("selected UTC start hour -> \(selectedUTCStartHour)")

PreferencesContainer.notifications.streaksNotificationStartHourUTC = selectedUTCStartHour
NotificationsService().scheduleStreakLocalNotification(UTCStartHour: selectedUTCStartHour)
}
PreferencesContainer.notifications.streaksNotificationStartHourUTC = selectedUTCStartHour
NotificationsService().scheduleStreakLocalNotification(utcStartHour: selectedUTCStartHour)
}

self.picker.reloadAllComponents()
self.picker.selectRow(self.startHour, inComponent: 0, animated: false)
}

func getDisplayingStreakTimeInterval(startHour: Int) -> String {
private func makeFormattedStreakTimeInterval(startHour: Int) -> String {
let timeZoneDiff = NSTimeZone.system.secondsFromGMT()

let startInterval = TimeInterval((startHour % 24) * 60 * 60 - timeZoneDiff)
let startDate = Date(timeIntervalSinceReferenceDate: startInterval)

let endInterval = TimeInterval((startHour + 1) % 24 * 60 * 60 - timeZoneDiff)
let endDate = Date(timeIntervalSinceReferenceDate: endInterval)

let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
dateFormatter.dateStyle = .none

return "\(dateFormatter.string(from: startDate)) - \(dateFormatter.string(from: endDate))"
}
}
Loading

0 comments on commit 754064e

Please sign in to comment.