2020-01-16 21:50:30 +01: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 {
|
|
|
|
|
|
|
|
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")
|
|
|
|
switch decodingError {
|
|
|
|
case .typeMismatch(let type, _):
|
|
|
|
let str = "Type '\(type)' mismatch."
|
|
|
|
presentError(title: errorTitle, message: str, dismiss: dismiss)
|
|
|
|
case .valueNotFound(let value, _):
|
|
|
|
let str = "Value '\(value)' not found."
|
|
|
|
presentError(title: errorTitle, message: str, dismiss: dismiss)
|
|
|
|
case .keyNotFound(let codingKey, _):
|
|
|
|
let str = "Key '\(codingKey.stringValue)' not found."
|
|
|
|
presentError(title: errorTitle, message: str, dismiss: dismiss)
|
|
|
|
case .dataCorrupted( _):
|
|
|
|
presentError(title: errorTitle, message: error.localizedDescription, dismiss: dismiss)
|
|
|
|
default:
|
|
|
|
presentError(title: errorTitle, message: error.localizedDescription, dismiss: dismiss)
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
if error.acount?.type == .feedbin {
|
|
|
|
|
|
|
|
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
|
|
|
|
addViewController.account = error.acount
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|