2020-01-16 13:50:30 -07:00
//
// UIViewController-Extensions.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 1/16/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import UIKit
import RSCore
import Account
extension UIViewController {
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
func presentError ( _ error : Error , dismiss : (() -> Void )? = nil ) {
if let accountError = error as ? AccountError , accountError . isCredentialsError {
presentAccountError ( accountError , dismiss : dismiss )
2021-09-21 10:43:12 +08:00
} else if let decodingError = error as ? DecodingError {
let errorTitle = NSLocalizedString ( "Error" , comment : "Error" )
2021-09-29 14:16:08 -05:00
var informativeText : String = ""
2021-09-21 10:43:12 +08:00
switch decodingError {
case . typeMismatch ( let type , _ ):
2021-09-24 09:28:32 +08:00
let localizedError = NSLocalizedString ( "This theme cannot be used because the the type—“%@”—is mismatched in the Info.plist" , comment : "Type mismatch" )
informativeText = NSString . localizedStringWithFormat ( localizedError as NSString , type as ! CVarArg ) as String
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 10:43:12 +08:00
case . valueNotFound ( let value , _ ):
2021-09-24 09:28:32 +08:00
let localizedError = NSLocalizedString ( "This theme cannot be used because the the value—“%@”—is not found in the Info.plist." , comment : "Decoding value missing" )
informativeText = NSString . localizedStringWithFormat ( localizedError as NSString , value as ! CVarArg ) as String
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 10:43:12 +08:00
case . keyNotFound ( let codingKey , _ ):
2021-09-24 09:28:32 +08:00
let localizedError = NSLocalizedString ( "This theme cannot be used because the the key—“%@”—is not found in the Info.plist." , comment : "Decoding key missing" )
informativeText = NSString . localizedStringWithFormat ( localizedError as NSString , codingKey . stringValue ) as String
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-24 09:28:32 +08:00
case . dataCorrupted ( let context ):
guard let error = context . underlyingError as NSError ?,
let debugDescription = error . userInfo [ "NSDebugDescription" ] as ? String else {
informativeText = error . localizedDescription
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-24 09:28:32 +08:00
return
}
let localizedError = NSLocalizedString ( "This theme cannot be used because of data corruption in the Info.plist. %@." , comment : "Decoding key missing" )
informativeText = NSString . localizedStringWithFormat ( localizedError as NSString , debugDescription ) as String
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2025-11-05 11:22:10 -08:00
2021-09-21 10:43:12 +08:00
default :
2021-09-24 09:28:32 +08:00
informativeText = error . localizedDescription
2021-09-29 14:16:08 -05:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 10:43:12 +08:00
}
2020-01-16 13:50:30 -07:00
} else {
2025-09-22 18:09:43 -07:00
// Check if error supports recovery options
if let recoverableError = error as ? ( RecoverableError & LocalizedError ),
! recoverableError . recoveryOptions . isEmpty {
presentErrorWithRecovery ( error : recoverableError , dismiss : dismiss )
} else {
let errorTitle = NSLocalizedString ( "Error" , comment : "Error" )
presentError ( title : errorTitle , message : error . localizedDescription , dismiss : dismiss )
}
2020-01-16 13:50:30 -07:00
}
}
}
private extension UIViewController {
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
func presentAccountError ( _ error : AccountError , dismiss : (() -> Void )? = nil ) {
let title = NSLocalizedString ( "Account Error" , comment : "Account Error" )
let alertController = UIAlertController ( title : title , message : error . localizedDescription , preferredStyle : . alert )
2025-11-05 11:22:10 -08:00
2025-11-13 17:11:34 -08:00
let account = AccountError . account ( from : error )
if account ?. type == . feedbin {
2020-01-16 13:50:30 -07:00
let credentialsTitle = NSLocalizedString ( "Update Credentials" , comment : "Update Credentials" )
let credentialsAction = UIAlertAction ( title : credentialsTitle , style : . default ) { [ weak self ] _ in
dismiss ?()
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
let navController = UIStoryboard . account . instantiateViewController ( withIdentifier : "FeedbinAccountNavigationViewController" ) as ! UINavigationController
navController . modalPresentationStyle = . formSheet
let addViewController = navController . topViewController as ! FeedbinAccountViewController
2025-11-13 17:11:34 -08:00
addViewController . account = account
2020-01-16 13:50:30 -07:00
self ?. present ( navController , animated : true )
}
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
alertController . addAction ( credentialsAction )
2021-01-11 19:57:14 -06:00
alertController . preferredAction = credentialsAction
2020-01-16 13:50:30 -07:00
}
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
let dismissTitle = NSLocalizedString ( "OK" , comment : "OK" )
let dismissAction = UIAlertAction ( title : dismissTitle , style : . default ) { _ in
dismiss ?()
}
alertController . addAction ( dismissAction )
2025-11-05 11:22:10 -08:00
2020-01-16 13:50:30 -07:00
self . present ( alertController , animated : true , completion : nil )
}
2025-11-05 11:22:10 -08:00
2025-09-22 18:09:43 -07:00
func presentErrorWithRecovery ( error : RecoverableError & LocalizedError , dismiss : (() -> Void )? = nil ) {
let title = error . errorDescription ?? NSLocalizedString ( "Error" , comment : "Error" )
let message = [ error . failureReason , error . recoverySuggestion ]. compactMap { $0 }. joined ( separator : " " )
2025-11-05 11:22:10 -08:00
2025-09-22 18:09:43 -07:00
let alertController = UIAlertController ( title : title , message : message , preferredStyle : . alert )
2025-11-05 11:22:10 -08:00
2025-09-22 18:09:43 -07:00
// Add recovery options as buttons
for ( index , option ) in error . recoveryOptions . enumerated () {
let action = UIAlertAction ( title : option , style : index == 0 ? . default : . cancel ) { _ in
dismiss ?()
_ = error . attemptRecovery ( optionIndex : index )
}
alertController . addAction ( action )
2025-11-05 11:22:10 -08:00
2025-09-22 18:09:43 -07:00
// Make the first option the preferred action
if index == 0 {
alertController . preferredAction = action
}
}
2025-11-05 11:22:10 -08:00
2025-09-22 18:09:43 -07:00
self . present ( alertController , animated : true , completion : nil )
}
2020-01-16 13:50:30 -07:00
}