blockchainethereumblockchain-walleterc20erc721walletxdaidappdecentralizederc1155erc875iosswifttokens
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
3.3 KiB
80 lines
3.3 KiB
// Copyright SIX DAY LLC. All rights reserved.
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Result
|
|
import MBProgressHUD
|
|
import SafariServices
|
|
|
|
enum ConfirmationError: LocalizedError {
|
|
case cancel
|
|
}
|
|
|
|
extension UIViewController {
|
|
func displaySuccess(title: String? = .none, message: String? = .none) {
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
|
|
alertController.popoverPresentationController?.sourceView = self.view
|
|
alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", value: "OK", comment: ""), style: UIAlertActionStyle.default, handler: nil))
|
|
present(alertController, animated: true, completion: nil)
|
|
}
|
|
|
|
func displayError(error: Error) {
|
|
let alertController = UIAlertController(title: error.prettyError, message: "", preferredStyle: UIAlertControllerStyle.alert)
|
|
alertController.popoverPresentationController?.sourceView = self.view
|
|
alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", value: "OK", comment: ""), style: UIAlertActionStyle.default, handler: nil))
|
|
present(alertController, animated: true, completion: nil)
|
|
}
|
|
|
|
func confirm(
|
|
title: String? = .none,
|
|
message: String? = .none,
|
|
okTitle: String = NSLocalizedString("OK", value: "OK", comment: ""),
|
|
okStyle: UIAlertActionStyle = .default,
|
|
completion: @escaping (Result<Void, ConfirmationError>) -> Void
|
|
) {
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
alertController.popoverPresentationController?.sourceView = self.view
|
|
alertController.addAction(UIAlertAction(title: okTitle, style: okStyle, handler: { _ in
|
|
completion(.success(()))
|
|
}))
|
|
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel", value: "Cancel", comment: ""), style: .cancel, handler: { _ in
|
|
completion(.failure(ConfirmationError.cancel))
|
|
}))
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
|
|
func displayLoading(
|
|
text: String = String(format: NSLocalizedString("loading.dots", value: "Loading %@", comment: ""), "..."),
|
|
animated: Bool = true
|
|
) {
|
|
let hud = MBProgressHUD.showAdded(to: self.view, animated: animated)
|
|
hud.label.text = text
|
|
}
|
|
|
|
func hideLoading(animated: Bool = true) {
|
|
MBProgressHUD.hide(for: view, animated: animated)
|
|
}
|
|
|
|
func openURL(_ url: URL) {
|
|
let controller = SFSafariViewController(url: url)
|
|
// Don't attempt to change tint colors for SFSafariViewController. It doesn't well correctly especially because the controller sets more than 1 color for the title
|
|
present(controller, animated: true, completion: nil)
|
|
}
|
|
|
|
public var isVisible: Bool {
|
|
if isViewLoaded {
|
|
return view.window != nil
|
|
}
|
|
return false
|
|
}
|
|
|
|
public var isTopViewController: Bool {
|
|
if self.navigationController != nil {
|
|
return self.navigationController?.visibleViewController === self
|
|
} else if self.tabBarController != nil {
|
|
return self.tabBarController?.selectedViewController == self && self.presentedViewController == nil
|
|
}
|
|
return self.presentedViewController == nil && self.isVisible
|
|
}
|
|
|
|
}
|
|
|