2019-05-02 16:33:03 -04:00
//
// S p e c i f y I n t e r f a c e C o n t r o l l e r . s w i f t
// B l u e W a l l e t W a t c h E x t e n s i o n
//
// C r e a t e d b y M a r c o s R o d r i g u e z o n 3 / 2 3 / 1 9 .
2024-01-25 00:39:01 -04:00
2019-05-02 16:33:03 -04:00
//
import WatchKit
2020-08-16 21:20:16 -04:00
import WatchConnectivity
2019-05-02 16:33:03 -04:00
import Foundation
class SpecifyInterfaceController : WKInterfaceController {
static let identifier = " SpecifyInterfaceController "
@IBOutlet weak var descriptionButton : WKInterfaceButton !
@IBOutlet weak var amountButton : WKInterfaceButton !
2019-11-11 01:26:39 -05:00
@IBOutlet weak var createButton : WKInterfaceButton !
2019-05-02 16:33:03 -04:00
struct SpecificQRCodeContent {
var amount : Double ?
var description : String ?
var amountStringArray : [ String ] = [ " 0 " ]
var bitcoinUnit : NumericKeypadInterfaceController . NumericKeypadType = . BTC
}
var specifiedQRContent : SpecificQRCodeContent = SpecificQRCodeContent ( amount : nil , description : nil , amountStringArray : [ " 0 " ] , bitcoinUnit : . BTC )
var wallet : Wallet ?
struct NotificationName {
static let createQRCode = Notification . Name ( rawValue : " Notification.SpecifyInterfaceController.createQRCode " )
}
struct Notifications {
static let createQRCode = Notification ( name : NotificationName . createQRCode )
}
override func awake ( withContext context : Any ? ) {
super . awake ( withContext : context )
guard let identifier = context as ? Int , WatchDataSource . shared . wallets . count > identifier else {
return
}
let wallet = WatchDataSource . shared . wallets [ identifier ]
self . wallet = wallet
2019-11-11 01:26:39 -05:00
self . createButton . setAlpha ( 0.5 )
2021-09-09 12:00:11 +01:00
self . specifiedQRContent . bitcoinUnit = ( wallet . type = = WalletGradient . LightningCustodial . rawValue || wallet . type = = WalletGradient . LightningLDK . rawValue ) ? . SATS : . BTC
2019-05-02 16:33:03 -04:00
NotificationCenter . default . addObserver ( forName : NumericKeypadInterfaceController . NotificationName . keypadDataChanged , object : nil , queue : nil ) { [ weak self ] ( notification ) in
guard let amountObject = notification . object as ? [ String ] , ! amountObject . isEmpty else { return }
if amountObject . count = = 1 && ( amountObject . first = = " . " || amountObject . first = = " 0 " ) {
return
}
var title = " "
for amount in amountObject {
let isValid = Double ( amount )
if amount = = " . " || isValid != nil {
title . append ( String ( amount ) )
}
}
self ? . specifiedQRContent . amountStringArray = amountObject
if let amountDouble = Double ( title ) , let keyPadType = self ? . specifiedQRContent . bitcoinUnit {
self ? . specifiedQRContent . amount = amountDouble
self ? . amountButton . setTitle ( " \( title ) \( keyPadType ) " )
2019-11-11 01:26:39 -05:00
2020-08-16 21:20:16 -04:00
var isShouldCreateButtonBeEnabled = amountDouble > 0 && ! title . isEmpty
2021-09-09 12:00:11 +01:00
if ( wallet . type = = WalletGradient . LightningCustodial . rawValue || wallet . type = = WalletGradient . LightningLDK . rawValue ) && ! WCSession . default . isReachable {
2020-08-16 21:20:16 -04:00
isShouldCreateButtonBeEnabled = false
}
2019-11-11 01:26:39 -05:00
self ? . createButton . setEnabled ( isShouldCreateButtonBeEnabled )
self ? . createButton . setAlpha ( isShouldCreateButtonBeEnabled ? 1.0 : 0.5 )
2019-05-02 16:33:03 -04:00
}
}
}
override func didDeactivate ( ) {
// T h i s m e t h o d i s c a l l e d w h e n w a t c h v i e w c o n t r o l l e r i s n o l o n g e r v i s i b l e
super . didDeactivate ( )
NotificationCenter . default . removeObserver ( self , name : NumericKeypadInterfaceController . NotificationName . keypadDataChanged , object : nil )
}
@IBAction func descriptionButtonTapped ( ) {
presentTextInputController ( withSuggestions : nil , allowedInputMode : . allowEmoji ) { [ weak self ] ( result : [ Any ] ? ) in
DispatchQueue . main . async {
if let result = result , let text = result . first as ? String {
self ? . specifiedQRContent . description = text
self ? . descriptionButton . setTitle ( nil )
self ? . descriptionButton . setTitle ( text )
}
}
}
}
@IBAction func createButtonTapped ( ) {
2021-07-31 11:52:32 -04:00
if WatchDataSource . shared . companionWalletsInitialized {
2021-07-31 01:38:31 -04:00
NotificationCenter . default . post ( name : NotificationName . createQRCode , object : specifiedQRContent )
dismiss ( )
} else {
presentAlert ( withTitle : " Error " , message : " Unable to create invoice. Please open BlueWallet on your iPhone and unlock your wallets. " , preferredStyle : . alert , actions : [ WKAlertAction ( title : " OK " , style : . default , handler : { [ weak self ] in
self ? . dismiss ( )
} ) ] )
}
2019-05-02 16:33:03 -04:00
}
override func contextForSegue ( withIdentifier segueIdentifier : String ) -> Any ? {
if segueIdentifier = = NumericKeypadInterfaceController . identifier {
return specifiedQRContent
}
return nil
}
}