Skip to content

Commit

Permalink
Fix the 'ninetyDaysFromNow constant'
Browse files Browse the repository at this point in the history
  • Loading branch information
r-token committed Apr 12, 2024
1 parent bfeb4d4 commit 6a7b5ac
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 48 deletions.
2 changes: 1 addition & 1 deletion CatchUp-SwiftUI/Utilities/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import Foundation

struct Constants {
static let ninetyDaysInSeconds: TimeInterval = 77760000
static let ninetyDaysInSeconds: TimeInterval = 7776000
}
7 changes: 6 additions & 1 deletion CatchUp-SwiftUI/Utilities/ContactHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,18 @@ struct ContactHelper {
contact.name.components(separatedBy: " ").first ?? contact.name
}

static func getFriendlyNextCatchUpTime(for contact: SelectedContact) -> String {
static func getFriendlyNextCatchUpTime(for contact: SelectedContact, forQuarterlyPreference: Bool) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

if let date = dateFormatter.date(from: contact.next_notification_date_time) {
let friendlyFormatter = DateFormatter()

if forQuarterlyPreference {
friendlyFormatter.dateFormat = "h:mm a"
return "Quarterly at \(friendlyFormatter.string(from: date))"
}

if Calendar.current.isDateInToday(date) {
friendlyFormatter.dateFormat = "h:mm a"
return "Today at \(friendlyFormatter.string(from: date))"
Expand Down
42 changes: 1 addition & 41 deletions CatchUp-SwiftUI/Utilities/Conversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct Converter {
} else if contact.preferenceIsMonthly() {
return "Monthly on \(weekday)s"
} else if contact.preferenceIsQuarterly() {
return getQuarterlyPreferenceText(for: contact)
return ContactHelper.getFriendlyNextCatchUpTime(for: contact, forQuarterlyPreference: true)
} else if contact.preferenceIsAnnually() {
let customDate = convertCustomDateFromIntToString(for: contact, annuallyOrCustom: .annually)
return "Annually on \(customDate)"
Expand All @@ -132,46 +132,6 @@ struct Converter {
return ""
}

private static func getQuarterlyPreferenceText(for contact: SelectedContact) -> String {
let nextCatchUpDate = contact.notification_preference_quarterly_set_time.addingTimeInterval(Constants.ninetyDaysInSeconds)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nextCatchUpDateString = dateFormatter.string(from: nextCatchUpDate)
if let localNextCatchUpDateString = getLocalHoursMinutes(from: nextCatchUpDateString) {
return "Quarterly at \(localNextCatchUpDateString)"
} else {
return "Quarterly"
}
}

private static func getLocalHoursMinutes(from dateString: String) -> String? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

// Try converting the string to a Date object
guard let date = formatter.date(from: dateString) else {
return nil // Handle invalid date string
}

// Get current locale and create calendar
let currentLocale = Locale.current
let calendar = Calendar(identifier: currentLocale.calendar.identifier)

// Extract components (including time zone) from the date
let dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)

// Create a new Date object in the user's local time zone
guard let localDate = calendar.date(from: dateComponents) else {
return nil // Handle potential error creating local date
}

// Format the local date to extract hours and minutes
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "h:mm a" // Format for output time string

return timeFormatter.string(from: localDate)
}

static func convertWeekdayFromIntToString(for contact: SelectedContact) -> String {
let weekday: String

Expand Down
4 changes: 1 addition & 3 deletions CatchUp-SwiftUI/Utilities/NotificationHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ struct NotificationHelper {

static func getDateComponentsFromDate(_ date: Date) -> DateComponents {
let calendar = Calendar.current

let timeInterval: TimeInterval = Constants.ninetyDaysInSeconds
let newDate = date.addingTimeInterval(timeInterval)
let newDate = date.addingTimeInterval(Constants.ninetyDaysInSeconds)

let dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: newDate)
return dateComponents
Expand Down
2 changes: 1 addition & 1 deletion CatchUp-SwiftUI/Views/DetailScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct DetailScreen: View {

var nextCatchUpTime: String {
print("recalculating nextCatchUpTime")
return ContactHelper.getFriendlyNextCatchUpTime(for: contact)
return ContactHelper.getFriendlyNextCatchUpTime(for: contact, forQuarterlyPreference: false)
}

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct NextCatchUpsGridView: View {
Text(ContactHelper.getFirstName(for: contact))
.font(.headline)

Text(ContactHelper.getFriendlyNextCatchUpTime(for: contact))
Text(ContactHelper.getFriendlyNextCatchUpTime(for: contact, forQuarterlyPreference: false))
.foregroundStyle(.gray)
.font(.caption)
}
Expand Down

0 comments on commit 6a7b5ac

Please sign in to comment.