From 7cfe19d61f06f7fc153ca902f48cdd4af28f4453 Mon Sep 17 00:00:00 2001 From: Ivan Magda Date: Tue, 11 Dec 2018 19:27:11 +0300 Subject: [PATCH] Fix streak notifications date componenets for iOS 10 and above --- ...reakLocalNotificationContentProvider.swift | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/Stepic/StreakLocalNotificationContentProvider.swift b/Stepic/StreakLocalNotificationContentProvider.swift index df67a46526..758730b2e1 100644 --- a/Stepic/StreakLocalNotificationContentProvider.swift +++ b/Stepic/StreakLocalNotificationContentProvider.swift @@ -10,6 +10,23 @@ import Foundation import UserNotifications final class StreakLocalNotificationContentProvider: LocalNotificationContentProvider { + private let UTCStartHour: Int + private let calendar: Calendar + + private var dateComponents: DateComponents { + let timeZoneDiff = NSTimeZone.system.secondsFromGMT() / 3600 + var localStartHour = self.UTCStartHour + timeZoneDiff + + if localStartHour < 0 { + localStartHour = 24 + localStartHour + } + if localStartHour > 23 { + localStartHour = localStartHour - 24 + } + + return DateComponents(hour: localStartHour) + } + var title = "" var body: String { @@ -52,33 +69,6 @@ final class StreakLocalNotificationContentProvider: LocalNotificationContentProv return UNCalendarNotificationTrigger(dateMatching: self.dateComponents, repeats: true) } - private let UTCStartHour: Int - private let calendar: Calendar - - private var dateComponents: DateComponents { - let timeZoneDiff = NSTimeZone.system.secondsFromGMT() / 3600 - var localStartHour = self.UTCStartHour + timeZoneDiff - - if localStartHour < 0 { - localStartHour = 24 + localStartHour - } - if localStartHour > 23 { - localStartHour = localStartHour - 24 - } - - let currentDate = Date() - - var components = DateComponents() - components.year = self.calendar.component(.year, from: currentDate) - components.month = self.calendar.component(.month, from: currentDate) - components.day = self.calendar.component(.day, from: currentDate) - components.hour = localStartHour - components.minute = 0 - components.second = 0 - - return components - } - init(UTCStartHour: Int, calendar: Calendar = Calendar(identifier: .gregorian)) { self.UTCStartHour = UTCStartHour self.calendar = calendar