Skip to content

Commit

Permalink
Fixed annual preference bug
Browse files Browse the repository at this point in the history
  • Loading branch information
r-token committed Apr 13, 2024
1 parent c840517 commit 8caee17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
9 changes: 1 addition & 8 deletions CatchUp-SwiftUI/Utilities/Conversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,7 @@ struct Converter {
var day: String
var year: String

var monthPreference: Int = 0
if contact.preferenceIsCustom() {
monthPreference = contact.notification_preference_custom_month
} else if contact.preferenceIsAnnually() {
monthPreference = contact.notification_preference_custom_month+1
}

switch monthPreference {
switch contact.notification_preference_custom_month {
case 1:
month = "January"
break
Expand Down
28 changes: 12 additions & 16 deletions CatchUp-SwiftUI/Utilities/NotificationHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ struct NotificationHelper {
} else if contact.preferenceIsWeekly() || contact.preferenceIsMonthly() {
components.hour = contact.notification_preference_hour
components.minute = contact.notification_preference_minute
components.weekday = contact.notification_preference_weekday + 1
components.weekday = contact.notification_preference_weekday+1
if contact.notification_preference_week_of_month != 0 {
components.weekOfMonth = contact.notification_preference_week_of_month + 1
components.weekOfMonth = contact.notification_preference_week_of_month+1
}
} else if contact.preferenceIsQuarterly() {
print("Quarterly is handled separately by UNTimeIntervalNotificationTrigger")
print("Quarterly is handled separately by UNTimeIntervalNotificationTrigger. Fallthrough.")
} else if contact.preferenceIsAnnually() || contact.preferenceIsCustom() {
components.hour = contact.notification_preference_hour
components.minute = contact.notification_preference_minute
components.hour = contact.notification_preference_hour
components.day = contact.notification_preference_custom_day
components.month = contact.notification_preference_custom_month
if contact.preferenceIsAnnually() {
components.month = contact.notification_preference_custom_month + 1
components.year = nil
} else if contact.preferenceIsCustom() {
components.month = contact.notification_preference_custom_month
components.year = contact.notification_preference_custom_year
}
components.day = contact.notification_preference_custom_day
components.year = contact.notification_preference_custom_year
}

var soonestUpcomingNotificationDateString = ""
Expand Down Expand Up @@ -163,12 +163,12 @@ struct NotificationHelper {
dateComponents.weekOfMonth = contact.notification_preference_week_of_month
} else if contact.preferenceIsAnnually() || contact.preferenceIsCustom() {
if contact.preferenceIsAnnually() {
dateComponents.month = contact.notification_preference_custom_month+1
dateComponents.year = nil
} else if contact.preferenceIsCustom() {
dateComponents.month = contact.notification_preference_custom_month
dateComponents.year = contact.notification_preference_custom_year
}
dateComponents.month = contact.notification_preference_custom_month
dateComponents.day = contact.notification_preference_custom_day
dateComponents.year = contact.notification_preference_custom_year
dateComponents.hour = contact.notification_preference_hour
dateComponents.minute = contact.notification_preference_minute
}
Expand Down Expand Up @@ -399,20 +399,16 @@ struct NotificationHelper {
return formattedDate
}

// print("returning Unknown")
return "Unknown"
}

static func setYearPreference(for contact: SelectedContact) {
var contactDateComponents = NotificationHelper.getNotificationDateComponents(for: contact)
contactDateComponents.year = Calendar.current.component(.year, from: Date())
if let nextNotificationDate = Calendar.current.date(from: contactDateComponents) {
print("nextNotificationDate for bee: \(nextNotificationDate)")
if nextNotificationDate < Date() {
print("\(Date()) for bee is greater than \(nextNotificationDate)")
contact.notification_preference_custom_year = Calendar.current.component(.year, from: Date()) + 1
contact.notification_preference_custom_year = Calendar.current.component(.year, from: Date())+1
} else {
print("\(Date()) for bee is less than \(nextNotificationDate)")
contact.notification_preference_custom_year = Calendar.current.component(.year, from: Date())
}
print("set \(contact.name)'s year preference to \(contact.notification_preference_custom_year)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct NotificationPreferenceView: View {
// Month Picker
Picker(selection: $contact.notification_preference_custom_month, label: Text("What month?")) {
ForEach(0..<monthOptions.count, id: \.self) { index in
Text(monthOptions[index].rawValue).tag(index)
Text(monthOptions[index].rawValue).tag(index+1)
}
}

Expand Down Expand Up @@ -134,9 +134,14 @@ struct NotificationPreferenceView: View {
print("notificationPreferenceTime changed")
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute], from : newTime)
NotificationHelper.updateNotificationTime(for: contact, hour: components.hour!, minute: components.minute!)

resetNotificationsForContact()

if let hour = components.hour, let minute = components.minute {
NotificationHelper.setYearPreference(for: contact)
NotificationHelper.updateNotificationTime(for: contact, hour: hour, minute: minute)
resetNotificationsForContact()
} else {
notificationPreferenceTime = initialTime
}
}
}

Expand Down Expand Up @@ -175,16 +180,16 @@ struct NotificationPreferenceView: View {
let calendar = Calendar.current
let timeComponents = DateComponents(
calendar: calendar,
hour: Int(contact.notification_preference_hour),
minute: Int(contact.notification_preference_minute)
hour: contact.notification_preference_hour,
minute: contact.notification_preference_minute
)
let time = Calendar.current.date(from: timeComponents)

let customDateComponents = DateComponents(
calendar: calendar,
year: Int(contact.notification_preference_custom_year),
month: Int(contact.notification_preference_custom_month),
day: Int(contact.notification_preference_custom_day)
year: contact.notification_preference_custom_year,
month: contact.notification_preference_custom_month,
day: contact.notification_preference_custom_day
)
let customDate = Calendar.current.date(from: customDateComponents)

Expand Down

0 comments on commit 8caee17

Please sign in to comment.