-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update the app expiration alert for GitHub and Xcode builds #2052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dae9b62
Overwrite profile_expire_date for GitHub built
marionbarker c68d2cc
use GITHUB_ACTION to update profile expire date
marionbarker 2b58a68
Detection of github build, calculate date for testflight expire
bjorkert 78a75cd
TF text adjustments
bjorkert 874c393
Fix for UTC timezone
bjorkert 211a337
fix for modal alert
marionbarker b292371
test an alternative method for isTestFlightBuild
marionbarker 3909380
Revert changes not needed by isTestFlightBuild function
marionbarker b6eb08b
Add simulator check for completeness
marionbarker e91a2a5
Merge branch 'dev' into github-build-expiration-date
marionbarker 156401e
Refactoring and renaming
bjorkert 180eceb
Refactoring and renaming
bjorkert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,159 @@ | ||
| // | ||
| // AppExpirationAlerter.swift | ||
| // Loop | ||
| // | ||
| // Created by Pete Schwamb on 8/21/21. | ||
| // Copyright © 2021 LoopKit Authors. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
| import UserNotifications | ||
| import LoopCore | ||
|
|
||
|
|
||
| class AppExpirationAlerter { | ||
|
|
||
| static let expirationAlertWindow: TimeInterval = .days(20) | ||
| static let settingsPageExpirationWarningModeWindow: TimeInterval = .days(3) | ||
|
|
||
| static func alertIfNeeded(viewControllerToPresentFrom: UIViewController) { | ||
|
|
||
| let now = Date() | ||
|
|
||
| guard let profileExpiration = BuildDetails.default.profileExpiration else { | ||
| return | ||
| } | ||
|
|
||
| let expirationDate = calculateExpirationDate(profileExpiration: profileExpiration) | ||
|
|
||
| let timeUntilExpiration = expirationDate.timeIntervalSince(now) | ||
|
|
||
| if timeUntilExpiration > expirationAlertWindow { | ||
| return | ||
| } | ||
|
|
||
| let minimumTimeBetweenAlerts: TimeInterval = timeUntilExpiration > .hours(24) ? .days(2) : .hours(1) | ||
|
|
||
| if let lastAlertDate = UserDefaults.appGroup?.lastProfileExpirationAlertDate { | ||
| guard now > lastAlertDate + minimumTimeBetweenAlerts else { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| let formatter = DateComponentsFormatter() | ||
| formatter.allowedUnits = [.day, .hour] | ||
| formatter.unitsStyle = .full | ||
| formatter.zeroFormattingBehavior = .dropLeading | ||
| formatter.maximumUnitCount = 1 | ||
| let timeUntilExpirationStr = formatter.string(from: timeUntilExpiration) | ||
|
|
||
| let alertMessage = createVerboseAlertMessage(timeUntilExpirationStr: timeUntilExpirationStr!) | ||
|
|
||
| var dialog: UIAlertController | ||
| if isTestFlightBuild() { | ||
| dialog = UIAlertController( | ||
| title: NSLocalizedString("TestFlight Expires Soon", comment: "The title for notification of upcoming TestFlight expiration"), | ||
| message: alertMessage, | ||
| preferredStyle: .alert) | ||
| dialog.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Text for ok action on notification of upcoming TestFlight expiration"), style: .default, handler: nil)) | ||
| dialog.addAction(UIAlertAction(title: NSLocalizedString("More Info", comment: "Text for more info action on notification of upcoming TestFlight expiration"), style: .default, handler: { (_) in | ||
| UIApplication.shared.open(URL(string: "https://loopkit.github.io/loopdocs/gh-actions/gh-update/")!) | ||
| })) | ||
|
|
||
| } else { | ||
| dialog = UIAlertController( | ||
| title: NSLocalizedString("Profile Expires Soon", comment: "The title for notification of upcoming profile expiration"), | ||
| message: alertMessage, | ||
| preferredStyle: .alert) | ||
| dialog.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Text for ok action on notification of upcoming profile expiration"), style: .default, handler: nil)) | ||
| dialog.addAction(UIAlertAction(title: NSLocalizedString("More Info", comment: "Text for more info action on notification of upcoming profile expiration"), style: .default, handler: { (_) in | ||
| UIApplication.shared.open(URL(string: "https://loopkit.github.io/loopdocs/build/updating/")!) | ||
| })) | ||
| } | ||
| viewControllerToPresentFrom.present(dialog, animated: true, completion: nil) | ||
|
|
||
| UserDefaults.appGroup?.lastProfileExpirationAlertDate = now | ||
| } | ||
|
|
||
| static func createVerboseAlertMessage(timeUntilExpirationStr:String) -> String { | ||
| if isTestFlightBuild() { | ||
| return String(format: NSLocalizedString("%1$@ will stop working in %2$@. You will need to rebuild before that.", comment: "Format string for body for notification of upcoming expiration. (1: app name) (2: amount of time until expiration"), Bundle.main.bundleDisplayName, timeUntilExpirationStr) | ||
| } else { | ||
| return String(format: NSLocalizedString("%1$@ will stop working in %2$@. You will need to update before that, with a new provisioning profile.", comment: "Format string for body for notification of upcoming provisioning profile expiration. (1: app name) (2: amount of time until expiration"), Bundle.main.bundleDisplayName, timeUntilExpirationStr) | ||
| } | ||
| } | ||
|
|
||
| static func isNearExpiration(expirationDate:Date) -> Bool { | ||
| return expirationDate.timeIntervalSinceNow < settingsPageExpirationWarningModeWindow | ||
| } | ||
|
|
||
| static func createProfileExpirationSettingsMessage(expirationDate:Date) -> String { | ||
| let nearExpiration = isNearExpiration(expirationDate: expirationDate) | ||
| let maxUnitCount = nearExpiration ? 2 : 1 // only include hours in the msg if near expiration | ||
| let readableRelativeTime: String? = relativeTimeFormatter(maxUnitCount: maxUnitCount).string(from: expirationDate.timeIntervalSinceNow) | ||
| let relativeTimeRemaining: String = readableRelativeTime ?? NSLocalizedString("Unknown time", comment: "Unknown amount of time in settings' profile expiration section") | ||
| let verboseMessage = createVerboseAlertMessage(timeUntilExpirationStr: relativeTimeRemaining) | ||
| let conciseMessage = relativeTimeRemaining + NSLocalizedString(" remaining", comment: "remaining time in setting's profile expiration section") | ||
| return nearExpiration ? verboseMessage : conciseMessage | ||
| } | ||
|
|
||
| private static func relativeTimeFormatter(maxUnitCount:Int) -> DateComponentsFormatter { | ||
| let formatter = DateComponentsFormatter() | ||
| let includeHours = maxUnitCount == 2 | ||
| formatter.allowedUnits = includeHours ? [.day, .hour] : [.day] | ||
| formatter.unitsStyle = .full | ||
| formatter.zeroFormattingBehavior = .dropLeading | ||
| formatter.maximumUnitCount = maxUnitCount | ||
| return formatter | ||
| } | ||
|
|
||
| static func buildDate() -> Date? { | ||
| let dateFormatter = DateFormatter() | ||
| dateFormatter.dateFormat = "EEE MMM d HH:mm:ss 'UTC' yyyy" | ||
| dateFormatter.locale = Locale(identifier: "en_US_POSIX") // Set locale to ensure parsing works | ||
| dateFormatter.timeZone = TimeZone(identifier: "UTC") | ||
|
|
||
| guard let dateString = BuildDetails.default.buildDateString, | ||
| let date = dateFormatter.date(from: dateString) else { | ||
| return nil | ||
| } | ||
|
|
||
| return date | ||
| } | ||
|
|
||
| static func isTestFlightBuild() -> Bool { | ||
| // If the target environment is a simulator, then | ||
| // this is not a TestFlight distribution. Return false. | ||
| #if targetEnvironment(simulator) | ||
| return false | ||
| #endif | ||
|
|
||
| // If an "embedded.mobileprovision" is present in the main bundle, then | ||
| // this is an Xcode, Ad-Hoc, or Enterprise distribution. Return false. | ||
| if Bundle.main.url(forResource: "embedded", withExtension: "mobileprovision") != nil { | ||
| return false | ||
| } | ||
|
|
||
| // If an app store receipt is not present in the main bundle, then we cannot | ||
| // say whether this is a TestFlight or App Store distribution. Return false. | ||
| guard let receiptName = Bundle.main.appStoreReceiptURL?.lastPathComponent else { | ||
| return false | ||
| } | ||
|
|
||
| // A TestFlight distribution presents a "sandboxReceipt", while an App Store | ||
| // distribution presents a "receipt". Return true if we have a TestFlight receipt. | ||
| return "sandboxReceipt".caseInsensitiveCompare(receiptName) == .orderedSame | ||
| } | ||
|
|
||
| static func calculateExpirationDate(profileExpiration: Date) -> Date { | ||
| let isTestFlight = isTestFlightBuild() | ||
|
|
||
| if isTestFlight, let buildDate = buildDate() { | ||
| let testflightExpiration = Calendar.current.date(byAdding: .day, value: 90, to: buildDate)! | ||
|
|
||
| return profileExpiration < testflightExpiration ? profileExpiration : testflightExpiration | ||
| } else { | ||
| return profileExpiration | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.