2019-05-02 22:33:03 +02:00
// 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 .
import WatchKit
import Foundation
2020-08-17 03:20:16 +02:00
import WatchConnectivity
2019-05-02 22:33:03 +02:00
class WalletDetailsInterfaceController : WKInterfaceController {
var wallet : Wallet ?
static let identifier = " WalletDetailsInterfaceController "
@IBOutlet weak var walletBasicsGroup : WKInterfaceGroup !
@IBOutlet weak var walletBalanceLabel : WKInterfaceLabel !
2019-11-11 07:26:39 +01:00
@IBOutlet weak var createInvoiceButton : WKInterfaceButton !
2019-05-02 22:33:03 +02:00
@IBOutlet weak var walletNameLabel : WKInterfaceLabel !
@IBOutlet weak var receiveButton : WKInterfaceButton !
2021-09-29 05:38:57 +02:00
@IBOutlet weak var viewXPubButton : WKInterfaceButton !
2019-05-02 22:33:03 +02:00
@IBOutlet weak var noTransactionsLabel : WKInterfaceLabel !
@IBOutlet weak var transactionsTable : WKInterfaceTable !
override func awake ( withContext context : Any ? ) {
super . awake ( withContext : context )
guard let identifier = context as ? Int else {
pop ( )
return
}
2024-01-25 05:39:01 +01:00
loadWalletDetails ( identifier : identifier )
}
private func loadWalletDetails ( identifier : Int ) {
let wallet = WatchDataSource . shared . wallets [ identifier ]
self . wallet = wallet
updateWalletUI ( wallet : wallet )
updateTransactionsTable ( forWallet : wallet )
}
private func updateWalletUI ( wallet : Wallet ) {
walletBalanceLabel . setHidden ( wallet . hideBalance )
walletBalanceLabel . setText ( wallet . hideBalance ? " " : wallet . balance )
walletNameLabel . setText ( wallet . label )
walletBasicsGroup . setBackgroundImageNamed ( WalletGradient ( rawValue : wallet . type ) ? . imageString )
let isLightningWallet = wallet . type = = WalletGradient . LightningCustodial . rawValue || wallet . type = = WalletGradient . LightningLDK . rawValue
createInvoiceButton . setHidden ( ! isLightningWallet )
receiveButton . setHidden ( wallet . receiveAddress . isEmpty )
viewXPubButton . setHidden ( ! isXPubAvailable ( wallet : wallet ) )
2021-02-06 06:15:41 +01:00
}
2024-01-25 05:39:01 +01:00
private func isXPubAvailable ( wallet : Wallet ) -> Bool {
return ( wallet . type != WalletGradient . LightningCustodial . rawValue && wallet . type != WalletGradient . LightningLDK . rawValue ) && ! ( wallet . xpub ? ? " " ) . isEmpty
2020-08-17 05:56:05 +02:00
}
2024-01-25 05:39:01 +01:00
private func updateTransactionsTable ( forWallet wallet : Wallet ) {
let transactions = wallet . transactions
transactionsTable . setNumberOfRows ( transactions . count , withRowType : TransactionTableRow . identifier )
for index in 0. . < transactions . count {
guard let controller = transactionsTable . rowController ( at : index ) as ? TransactionTableRow else { continue }
let transaction = transactions [ index ]
controller . configure ( with : transaction )
}
transactionsTable . setHidden ( transactions . isEmpty )
noTransactionsLabel . setHidden ( ! transactions . isEmpty )
}
2021-09-29 05:38:57 +02:00
@IBAction func toggleBalanceVisibility ( _ sender : Any ) {
2020-08-17 05:56:05 +02:00
guard let wallet = wallet else {
2024-01-25 05:39:01 +01:00
return
2020-08-17 05:56:05 +02:00
}
2024-01-25 05:39:01 +01:00
2021-02-06 06:15:41 +01:00
if wallet . hideBalance {
2021-09-29 05:38:57 +02:00
showBalanceMenuItemTapped ( )
2024-01-25 05:39:01 +01:00
} else {
2021-09-29 05:38:57 +02:00
hideBalanceMenuItemTapped ( )
2021-02-06 06:15:41 +01:00
}
}
@objc func showBalanceMenuItemTapped ( ) {
guard let identifier = wallet ? . identifier else { return }
WatchDataSource . toggleWalletHideBalance ( walletIdentifier : identifier , hideBalance : false ) { [ weak self ] _ in
DispatchQueue . main . async {
WatchDataSource . postDataUpdatedNotification ( )
2024-01-25 05:39:01 +01:00
self ? . loadWalletDetails ( identifier : identifier )
2021-02-06 06:15:41 +01:00
}
}
}
@objc func hideBalanceMenuItemTapped ( ) {
guard let identifier = wallet ? . identifier else { return }
WatchDataSource . toggleWalletHideBalance ( walletIdentifier : identifier , hideBalance : true ) { [ weak self ] _ in
DispatchQueue . main . async {
WatchDataSource . postDataUpdatedNotification ( )
2024-01-25 05:39:01 +01:00
self ? . loadWalletDetails ( identifier : identifier )
2021-02-06 06:15:41 +01:00
}
}
2020-08-17 05:56:05 +02:00
}
2021-09-29 05:38:57 +02:00
@IBAction func viewXPubMenuItemTapped ( ) {
2020-08-17 05:56:05 +02:00
guard let xpub = wallet ? . xpub else {
return
}
presentController ( withName : ViewQRCodefaceController . identifier , context : xpub )
2019-05-02 22:33:03 +02:00
}
override func willActivate ( ) {
super . willActivate ( )
2024-01-25 05:39:01 +01:00
guard let wallet = wallet else { return }
updateTransactionsTable ( forWallet : wallet )
2019-05-02 22:33:03 +02:00
}
@IBAction func receiveMenuItemTapped ( ) {
2024-01-25 05:39:01 +01:00
guard let wallet = wallet else { return }
2019-11-11 07:26:39 +01:00
presentController ( withName : ReceiveInterfaceController . identifier , context : ( wallet , " receive " ) )
2019-05-02 22:33:03 +02:00
}
2019-11-11 07:26:39 +01:00
@IBAction func createInvoiceTapped ( ) {
2024-01-25 05:39:01 +01:00
if WatchDataSource . shared . companionWalletsInitialized {
guard let wallet = wallet else { return }
pushController ( withName : ReceiveInterfaceController . identifier , context : ( wallet . identifier , " createInvoice " ) )
2021-07-31 07:38:31 +02:00
} else {
2024-01-25 05:39:01 +01:00
WKInterfaceDevice . current ( ) . play ( . failure )
2021-07-31 07:38:31 +02:00
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 ( )
2024-01-25 05:39:01 +01:00
} ) ] )
}
2019-11-11 07:26:39 +01:00
}
2019-05-02 22:33:03 +02:00
override func contextForSegue ( withIdentifier segueIdentifier : String ) -> Any ? {
2024-01-25 05:39:01 +01:00
guard let wallet = wallet else { return nil }
return ( wallet . identifier , " receive " )
2019-05-02 22:33:03 +02:00
}
}