2018-09-18 19:47:43 +00:00
//
// C o p y r i g h t © 2 0 1 8 W i r e G u a r d L L C . A l l r i g h t s r e s e r v e d .
//
import UIKit
2018-09-26 09:26:52 +00:00
import PromiseKit
enum GoVersionError : Error {
case noDelegate
}
2018-09-18 19:47:43 +00:00
protocol SettingsTableViewControllerDelegate : class {
func exportTunnels ( settingsTableViewController : SettingsTableViewController , sourceView : UIView )
2018-09-26 09:26:52 +00:00
func goVersionInformation ( ) -> Promise < String >
2018-09-18 19:47:43 +00:00
}
class SettingsTableViewController : UITableViewController {
weak var delegate : SettingsTableViewControllerDelegate ?
@IBOutlet weak var versionInfoCell : UITableViewCell !
@IBOutlet weak var goVersionInfoCell : UITableViewCell !
@IBOutlet weak var exportCell : UITableViewCell !
@IBOutlet weak var versionInfoLabel : UILabel !
@IBOutlet weak var goVersionInfoLabel : UILabel !
override func viewDidLoad ( ) {
super . viewDidLoad ( )
versionInfoLabel . text = versionInformation
2018-09-26 09:26:52 +00:00
_ = firstly { ( ) -> Promise < String > in
self . goVersionInfoLabel . text = NSLocalizedString ( " Loading... " , comment : " " )
return goVersionInformation ( )
} . then { ( goVersion : String ) -> Guarantee < Void > in
if let label = self . goVersionInfoLabel {
label . text = goVersion
}
return Guarantee . value ( ( ) )
} . recover ( { ( _ ) in
self . goVersionInfoLabel . text = NSLocalizedString ( " Unknown " , comment : " " )
} )
2018-09-18 19:47:43 +00:00
}
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
if let cell = tableView . cellForRow ( at : indexPath ) {
switch cell {
case versionInfoCell , goVersionInfoCell :
2018-09-26 09:26:52 +00:00
UIPasteboard . general . string = [ " WireGuard for iOS: " , versionInformation , " Go userspace backend: " , goVersionInfoLabel . text ? ? " " ] . joined ( separator : " \n " )
2018-09-18 19:47:43 +00:00
showCopyConfirmation ( )
case exportCell :
delegate ? . exportTunnels ( settingsTableViewController : self , sourceView : exportCell )
default :
( )
}
tableView . deselectRow ( at : indexPath , animated : true )
}
}
var versionInformation : String {
var versionElements : [ String ] = [ ]
if let appBuildNumber = Bundle . main . infoDictionary ! [ " CFBundleVersion " ] as ? String , let appVersion = Bundle . main . infoDictionary ! [ " CFBundleShortVersionString " ] as ? String {
versionElements . append ( appVersion )
versionElements . append ( " ( \( appBuildNumber ) ) " )
}
return versionElements . joined ( separator : " " )
}
2018-09-26 09:26:52 +00:00
func goVersionInformation ( ) -> Promise < String > {
return self . delegate ? . goVersionInformation ( ) ? ? Promise ( error : GoVersionError . noDelegate )
2018-09-18 19:47:43 +00:00
}
private func showCopyConfirmation ( ) {
let confirmationAlertController = UIAlertController ( title : NSLocalizedString ( " Copied version information " , comment : " " ) , message : UIPasteboard . general . string , preferredStyle : . alert )
confirmationAlertController . addAction ( UIAlertAction ( title : NSLocalizedString ( " Ok " , comment : " Generic OK button " ) , style : . default , handler : nil ) )
present ( confirmationAlertController , animated : true , completion : nil )
}
}
extension SettingsTableViewController : Identifyable { }