-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change account expiry text to display 'less than a day'
- Loading branch information
1 parent
1654b73
commit 5b24541
Showing
6 changed files
with
128 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
ios/MullvadVPN/Notifications/Notification Providers/AccountExpiry.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// AccountExpiry.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2023-11-08. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct AccountExpiry { | ||
var expiryDate: Date? | ||
|
||
var triggerDate: Date? { | ||
guard let expiryDate else { return nil } | ||
|
||
return Calendar.current.date( | ||
byAdding: .day, | ||
value: -NotificationConfiguration.closeToExpiryTriggerInterval, | ||
to: expiryDate | ||
) | ||
} | ||
|
||
var formattedDuration: String? { | ||
let now = Date() | ||
|
||
guard | ||
let expiryDate, | ||
let triggerDate, | ||
let duration = CustomDateComponentsFormatting.localizedString( | ||
from: now, | ||
to: expiryDate, | ||
unitsStyle: .full | ||
), | ||
now >= triggerDate, | ||
now < expiryDate | ||
else { | ||
return nil | ||
} | ||
|
||
return duration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// AccountExpiryTests.swift | ||
// MullvadVPNTests | ||
// | ||
// Created by Jon Petersson on 2023-11-07. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
class AccountExpiryTests: XCTestCase { | ||
func testNoDateReturnsNoDuration() { | ||
let accountExpiry = AccountExpiry() | ||
XCTAssertNil(accountExpiry.formattedDuration) | ||
} | ||
|
||
func testDateNowReturnsNoDuration() { | ||
let accountExpiry = AccountExpiry(expiryDate: Date()) | ||
XCTAssertNil(accountExpiry.formattedDuration) | ||
} | ||
|
||
func testDateInPastReturnsNoDuration() { | ||
let accountExpiry = AccountExpiry(expiryDate: Date().addingTimeInterval(-10)) | ||
XCTAssertNil(accountExpiry.formattedDuration) | ||
} | ||
|
||
func testDateWithinTriggerIntervalReturnsDuration() { | ||
let date = Calendar.current.date( | ||
byAdding: .day, | ||
value: NotificationConfiguration.closeToExpiryTriggerInterval - 1, | ||
to: Date() | ||
) | ||
|
||
let accountExpiry = AccountExpiry(expiryDate: date) | ||
XCTAssertNotNil(accountExpiry.formattedDuration) | ||
} | ||
|
||
func testDateNotWithinTriggerIntervalReturnsNoDuration() { | ||
let date = Calendar.current.date( | ||
byAdding: .day, | ||
value: NotificationConfiguration.closeToExpiryTriggerInterval + 1, | ||
to: Date() | ||
) | ||
|
||
let accountExpiry = AccountExpiry(expiryDate: date) | ||
XCTAssertNil(accountExpiry.formattedDuration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters