Skip to content

Commit

Permalink
Change account expiry text to display 'less than a day'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson committed Nov 3, 2023
1 parent 7e9bb04 commit c976d9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
17 changes: 13 additions & 4 deletions ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ extension CustomDateComponentsFormatting {
///
/// The behaviour of that method differs from `DateComponentsFormatter`:
///
/// 1. Intervals of two years or more are formatted in years quantity.
/// 2. Otherwise intervals matching none of the above are formatted in days quantity.
/// 1. Intervals of less than a day return a custom string.
/// 2. Intervals of two years or more are formatted in years quantity.
/// 3. Otherwise intervals matching none of the above are formatted in days quantity.
///
static func localizedString(
from start: Date,
to end: Date,
calendar: Calendar = Calendar.current,
unitsStyle: DateComponentsFormatter.UnitsStyle
) -> String? {
let years = calendar.dateComponents([.year], from: start, to: max(start, end)).year ?? 0
let dateComponents = calendar.dateComponents([.year, .day], from: start, to: max(start, end))

guard dateComponents.day != nil else {
return NSLocalizedString(
"ACCOUNT_EXPIRY_INAPP_NOTIFICATION_LESS_THAN_ONE_DAY",
value: "Less than a day",
comment: ""
)
}

let formatter = DateComponentsFormatter()
formatter.calendar = calendar
formatter.unitsStyle = unitsStyle
formatter.maximumUnitCount = 1
formatter.allowedUnits = years >= 2 ? .year : .day
formatter.allowedUnits = (dateComponents.year ?? 0) >= 2 ? .year : .day

return formatter.string(from: start, to: max(start, end))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ final class AccountExpiryInAppNotificationProvider: NotificationProvider, InAppN
formatter.allowedUnits = [.minute, .hour, .day]
formatter.maximumUnitCount = 1

let duration: String?
if accountExpiry.timeIntervalSince(now) < .minutes(1) {
duration = NSLocalizedString(
"ACCOUNT_EXPIRY_INAPP_NOTIFICATION_LESS_THAN_ONE_MINUTE",
value: "Less than a minute",
comment: ""
)
} else {
duration = formatter.string(from: now, to: accountExpiry)
}

guard let duration else { return nil }
guard let duration = formatter.string(from: now, to: accountExpiry) else { return nil }

return InAppNotificationDescriptor(
identifier: identifier,
Expand Down

0 comments on commit c976d9c

Please sign in to comment.