Skip to content
Merged
Changes from 1 commit
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
44 changes: 17 additions & 27 deletions Stepic/StreakLocalNotificationContentProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут кстати вроде как есть bridge в TimeZone

var localStartHour = self.UTCStartHour + timeZoneDiff

if localStartHour < 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не по теме ПРа, но каждый раз смотрю на этот кусок кода и не понимаю, почему вообще в пикере происходит какое-то шаманство с датой. Ведь можно же оттуда отдавать DateComponents с выбранным часом сразу

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я не разбирался, возможно @Ostrenkiy помнит, почему пришлось шаманить.
Кстати, в момент выбора даты уже выполняется перевод в UTC https://github.com/StepicOrg/stepik-ios/blob/dev/Stepic/NotificationTimePickerViewController.swift#L37
а потом еще раз точно такой же код выполняется тут - в StreakLocalNotificationContentProvider

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, там шаманство из-за того, что в пикере дата должна быть в локальном часовом поясе, а сохраняем ее мы в UTC формате.
А почему это шаманство именно такое я без понятия, если можно сделать такие вещи по-другому и хорошо - это круто и надо бы сделать

localStartHour = 24 + localStartHour
}
if localStartHour > 23 {
localStartHour = localStartHour - 24
}

return DateComponents(hour: localStartHour)
}

var title = ""

var body: String {
Expand Down Expand Up @@ -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
Expand Down