Skip to content

Commit

Permalink
Fixing suggested date for reminder - Closes #157
Browse files Browse the repository at this point in the history
  • Loading branch information
DamascenoRafael committed Aug 15, 2023
1 parent dc1c3d1 commit 3d6182c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
15 changes: 10 additions & 5 deletions reminders-menubar/Extensions/Date+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ extension Date {
return Date().timeIntervalSince(self)
}

static func nextHour(of date: Date = Date()) -> Date {
let now = Date()
let dateComponentsWithoutTime = Calendar.current.dateComponents([.year, .month, .day], from: date)
let dateWithoutTime = Calendar.current.date(from: dateComponentsWithoutTime)!
var hourComponent = Calendar.current.dateComponents([.hour], from: now)
static func nextExactHour(of date: Date = Date(), allowDayChange: Bool = false) -> Date {
let today = Date()
let todayNextHour = Calendar.current.date(byAdding: .hour, value: 1, to: today)!
let isNextHourChangingDay = !todayNextHour.isToday

var hourComponent = Calendar.current.dateComponents([.hour], from: today)
if allowDayChange || !isNextHourChangingDay {
hourComponent.hour! += 1
}

let dateWithoutTime = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: date)!
return Calendar.current.date(byAdding: hourComponent, to: dateWithoutTime)!
}

Expand Down
14 changes: 7 additions & 7 deletions reminders-menubar/Models/RmbReminder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct RmbReminder {
var hasTime: Bool {
didSet {
// NOTE: When enabling the option to add a time the suggestion will be the next hour of the current moment
date = .nextHour(of: date)
date = .nextExactHour(of: date)
}
}
var priority: EKReminderPriority
Expand All @@ -61,23 +61,23 @@ struct RmbReminder {

init() {
title = ""
date = .nextHour()
date = .nextExactHour()
hasDueDate = false
hasTime = false
priority = .none
}

init(hasDueDate: Bool, isParsingEnabled: Bool) {
init(isAutoSuggestingTodayForCreation: Bool) {
self.init()
self.hasDueDate = hasDueDate
self.isParsingEnabled = isParsingEnabled
self.hasDueDate = isAutoSuggestingTodayForCreation
self.isParsingEnabled = true
}

init(reminder: EKReminder) {
originalReminder = reminder
title = reminder.title
notes = reminder.notes
date = reminder.dueDateComponents?.date ?? .nextHour()
date = reminder.dueDateComponents?.date ?? .nextExactHour()
hasDueDate = reminder.hasDueDate
hasTime = reminder.hasTime
priority = reminder.ekPriority
Expand All @@ -96,7 +96,7 @@ struct RmbReminder {
guard let dateResult = DateParser.shared.getDate(from: newTitle) else {
hasDueDate = false
hasTime = false
date = .nextHour()
date = .nextExactHour()
textDateResult = DateParser.TextDateResult()
return
}
Expand Down
2 changes: 1 addition & 1 deletion reminders-menubar/Views/FormNewReminderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct FormNewReminderView: View {
}

private func newRmbReminder(withTitle title: String = "") -> RmbReminder {
var rmbReminder = RmbReminder(hasDueDate: userPreferences.autoSuggestToday, isParsingEnabled: true)
var rmbReminder = RmbReminder(isAutoSuggestingTodayForCreation: userPreferences.autoSuggestToday)
rmbReminder.title = title
return rmbReminder
}
Expand Down

0 comments on commit 3d6182c

Please sign in to comment.