Improves 10 App ID limit error handling

This commit is contained in:
Riley Testut
2020-01-24 14:14:08 -08:00
parent e823d5f621
commit b196981c89
7 changed files with 123 additions and 24 deletions

View File

@@ -24,6 +24,7 @@ enum OperationError: LocalizedError
case invalidParameters
case iOSVersionNotSupported(ALTApplication)
case maximumAppIDLimitReached(Date)
case noSources
@@ -49,6 +50,37 @@ enum OperationError: LocalizedError
let localizedDescription = String(format: NSLocalizedString("%@ requires %@.", comment: ""), name, version)
return localizedDescription
case .maximumAppIDLimitReached: return NSLocalizedString("Cannot register more than 10 App IDs.", comment: "")
}
}
var recoverySuggestion: String? {
switch self
{
case .maximumAppIDLimitReached(let date):
let remainingTime: String
let numberOfDays = date.numberOfCalendarDays(since: Date())
switch numberOfDays {
case 0:
let components = Calendar.current.dateComponents([.hour], from: Date(), to: date)
let numberOfHours = components.hour!
switch numberOfHours
{
case 1: remainingTime = NSLocalizedString("1 hour", comment: "")
default: remainingTime = String(format: NSLocalizedString("%@ hours", comment: ""), NSNumber(value: numberOfHours))
}
case 1: remainingTime = NSLocalizedString("1 day", comment: "")
default: remainingTime = String(format: NSLocalizedString("%@ days", comment: ""), NSNumber(value: numberOfDays))
}
let message = String(format: NSLocalizedString("Delete sideloaded apps to free up App ID slots. You can register another App ID in %@.", comment: ""), remainingTime)
return message
default: return nil
}
}
}

View File

@@ -231,7 +231,31 @@ private extension ResignAppOperation
else
{
ALTAppleAPI.shared.addAppID(withName: appName, bundleIdentifier: bundleIdentifier, team: team, session: session) { (appID, error) in
completionHandler(Result(appID, error))
do
{
do
{
let appID = try Result(appID, error).get()
completionHandler(.success(appID))
}
catch ALTAppleAPIError.maximumAppIDLimitReached
{
let sortedExpirationDates = appIDs.compactMap { $0.expirationDate }.sorted(by: { $0 < $1 })
if let expirationDate = sortedExpirationDates.first
{
throw OperationError.maximumAppIDLimitReached(expirationDate)
}
else
{
throw ALTAppleAPIError(.maximumAppIDLimitReached)
}
}
}
catch
{
completionHandler(.failure(error))
}
}
}
}