2020-01-16 21:50:30 +01:00
//
// U I V i e w C o n t r o l l e r - E x t e n s i o n s . s w i f t
// N e t N e w s W i r e - i O S
//
// C r e a t e d b y M a u r i c e P a r k e r o n 1 / 1 6 / 2 0 .
// C o p y r i g h t © 2 0 2 0 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import UIKit
import RSCore
import Account
extension UIViewController {
func presentError ( _ error : Error , dismiss : ( ( ) -> Void ) ? = nil ) {
if let accountError = error as ? AccountError , accountError . isCredentialsError {
presentAccountError ( accountError , dismiss : dismiss )
2021-09-21 04:43:12 +02:00
} else if let decodingError = error as ? DecodingError {
let errorTitle = NSLocalizedString ( " Error " , comment : " Error " )
2021-09-29 21:16:08 +02:00
var informativeText : String = " "
2021-09-21 04:43:12 +02:00
switch decodingError {
case . typeMismatch ( let type , _ ) :
2021-09-24 03:28:32 +02: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 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 04:43:12 +02:00
case . valueNotFound ( let value , _ ) :
2021-09-24 03:28:32 +02: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 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 04:43:12 +02:00
case . keyNotFound ( let codingKey , _ ) :
2021-09-24 03:28:32 +02: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 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-24 03:28:32 +02: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 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-24 03:28:32 +02: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 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-24 03:28:32 +02:00
2021-09-21 04:43:12 +02:00
default :
2021-09-24 03:28:32 +02:00
informativeText = error . localizedDescription
2021-09-29 21:16:08 +02:00
presentError ( title : errorTitle , message : informativeText , dismiss : dismiss )
2021-09-21 04:43:12 +02:00
}
2020-01-16 21:50:30 +01:00
} else {
let errorTitle = NSLocalizedString ( " Error " , comment : " Error " )
presentError ( title : errorTitle , message : error . localizedDescription , dismiss : dismiss )
}
}
}
private extension UIViewController {
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 )
2022-01-04 23:25:20 +01:00
if error . account ? . type = = . feedbin {
2020-01-16 21:50:30 +01:00
let credentialsTitle = NSLocalizedString ( " Update Credentials " , comment : " Update Credentials " )
let credentialsAction = UIAlertAction ( title : credentialsTitle , style : . default ) { [ weak self ] _ in
dismiss ? ( )
let navController = UIStoryboard . account . instantiateViewController ( withIdentifier : " FeedbinAccountNavigationViewController " ) as ! UINavigationController
navController . modalPresentationStyle = . formSheet
let addViewController = navController . topViewController as ! FeedbinAccountViewController
2022-01-04 23:25:20 +01:00
addViewController . account = error . account
2020-01-16 21:50:30 +01:00
self ? . present ( navController , animated : true )
}
alertController . addAction ( credentialsAction )
2021-01-12 02:57:14 +01:00
alertController . preferredAction = credentialsAction
2020-01-16 21:50:30 +01:00
}
let dismissTitle = NSLocalizedString ( " OK " , comment : " OK " )
let dismissAction = UIAlertAction ( title : dismissTitle , style : . default ) { _ in
dismiss ? ( )
}
alertController . addAction ( dismissAction )
self . present ( alertController , animated : true , completion : nil )
}
}