2019-05-02 16:33:03 -04:00
//
// W a l l e t D e t a i l s 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 / 1 1 / 1 9 .
// C o p y r i g h t © 2 0 1 9 F a c e b o o k . A l l r i g h t s r e s e r v e d .
//
import WatchKit
import Foundation
2020-08-16 21:20:16 -04:00
import WatchConnectivity
2019-05-02 16:33:03 -04:00
class WalletDetailsInterfaceController : WKInterfaceController {
var wallet : Wallet ?
static let identifier = " WalletDetailsInterfaceController "
@IBOutlet weak var walletBasicsGroup : WKInterfaceGroup !
@IBOutlet weak var walletBalanceLabel : WKInterfaceLabel !
2019-11-11 01:26:39 -05:00
@IBOutlet weak var createInvoiceButton : WKInterfaceButton !
2019-05-02 16:33:03 -04:00
@IBOutlet weak var walletNameLabel : WKInterfaceLabel !
@IBOutlet weak var receiveButton : WKInterfaceButton !
@IBOutlet weak var noTransactionsLabel : WKInterfaceLabel !
@IBOutlet weak var transactionsTable : WKInterfaceTable !
2019-11-11 01:26:39 -05:00
2019-05-02 16:33:03 -04:00
override func awake ( withContext context : Any ? ) {
super . awake ( withContext : context )
guard let identifier = context as ? Int else {
pop ( )
return
}
2021-02-06 00:15:41 -05:00
processInterface ( identifier : identifier )
}
func processInterface ( identifier : Int ) {
let wallet = WatchDataSource . shared . wallets [ identifier ]
self . wallet = wallet
walletBalanceLabel . setHidden ( wallet . hideBalance )
walletBalanceLabel . setText ( wallet . hideBalance ? " " : wallet . balance )
walletNameLabel . setText ( wallet . label )
walletBasicsGroup . setBackgroundImageNamed ( WalletGradient ( rawValue : wallet . type ) ? . imageString )
createInvoiceButton . setHidden ( wallet . type != " lightningCustodianWallet " )
processWalletsTable ( )
addMenuItems ( )
2020-08-16 23:56:05 -04:00
}
func addMenuItems ( ) {
guard let wallet = wallet else {
return
}
2021-02-06 00:15:41 -05:00
clearAllMenuItems ( )
2020-08-16 23:56:05 -04:00
if wallet . type != " lightningCustodianWallet " && ! ( wallet . xpub ? ? " " ) . isEmpty {
addMenuItem ( with : . share , title : " View XPub " , action : #selector ( viewXPubMenuItemTapped ) )
}
2021-02-06 00:15:41 -05:00
if wallet . hideBalance {
addMenuItem ( with : . accept , title : " Show Balance " , action : #selector ( showBalanceMenuItemTapped ) )
} else {
addMenuItem ( with : . decline , title : " Hide Balance " , action : #selector ( hideBalanceMenuItemTapped ) )
}
}
@objc func showBalanceMenuItemTapped ( ) {
guard let identifier = wallet ? . identifier else { return }
WatchDataSource . toggleWalletHideBalance ( walletIdentifier : identifier , hideBalance : false ) { [ weak self ] _ in
DispatchQueue . main . async {
WatchDataSource . postDataUpdatedNotification ( )
self ? . processInterface ( identifier : identifier )
}
}
}
@objc func hideBalanceMenuItemTapped ( ) {
guard let identifier = wallet ? . identifier else { return }
WatchDataSource . toggleWalletHideBalance ( walletIdentifier : identifier , hideBalance : true ) { [ weak self ] _ in
DispatchQueue . main . async {
WatchDataSource . postDataUpdatedNotification ( )
self ? . processInterface ( identifier : identifier )
}
}
2020-08-16 23:56:05 -04:00
}
@objc func viewXPubMenuItemTapped ( ) {
guard let xpub = wallet ? . xpub else {
return
}
presentController ( withName : ViewQRCodefaceController . identifier , context : xpub )
2019-05-02 16:33:03 -04:00
}
override func willActivate ( ) {
super . willActivate ( )
transactionsTable . setHidden ( wallet ? . transactions . isEmpty ? ? true )
noTransactionsLabel . setHidden ( ! ( wallet ? . transactions . isEmpty ? ? false ) )
2019-11-11 01:26:39 -05:00
receiveButton . setHidden ( wallet ? . receiveAddress . isEmpty ? ? true )
2019-05-02 16:33:03 -04:00
}
@IBAction func receiveMenuItemTapped ( ) {
2019-11-11 01:26:39 -05:00
presentController ( withName : ReceiveInterfaceController . identifier , context : ( wallet , " receive " ) )
2019-05-02 16:33:03 -04:00
}
2019-11-11 01:26:39 -05:00
2019-05-02 16:33:03 -04:00
@objc private func processWalletsTable ( ) {
transactionsTable . setNumberOfRows ( wallet ? . transactions . count ? ? 0 , withRowType : TransactionTableRow . identifier )
for index in 0. . < transactionsTable . numberOfRows {
guard let controller = transactionsTable . rowController ( at : index ) as ? TransactionTableRow , let transaction = wallet ? . transactions [ index ] else { continue }
controller . amount = transaction . amount
controller . type = transaction . type
controller . memo = transaction . memo
controller . time = transaction . time
}
transactionsTable . setHidden ( wallet ? . transactions . isEmpty ? ? true )
noTransactionsLabel . setHidden ( ! ( wallet ? . transactions . isEmpty ? ? false ) )
}
2019-11-11 01:26:39 -05:00
@IBAction func createInvoiceTapped ( ) {
2021-07-31 11:52:32 -04:00
if ( WatchDataSource . shared . companionWalletsInitialized ) {
2020-08-16 21:20:16 -04:00
pushController ( withName : ReceiveInterfaceController . identifier , context : ( wallet ? . identifier , " createInvoice " ) )
2021-07-31 01:38:31 -04:00
} 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-11-11 01:26:39 -05:00
}
2019-05-02 16:33:03 -04:00
override func contextForSegue ( withIdentifier segueIdentifier : String ) -> Any ? {
2019-11-11 01:26:39 -05:00
return ( wallet ? . identifier , " receive " )
2019-05-02 16:33:03 -04:00
}
}