lnd-demo-app/wallet/ViewControllers/HomeVC.swift

167 lines
6.5 KiB
Swift
Raw Normal View History

2023-06-08 06:36:06 +00:00
//
// HomeVC.swift
// wallet
//
// Created by Adriana Epure on 19.08.2022.
// Copyright © 2022 Jason. All rights reserved.
//
import UIKit
class HomeVC: CustomViewController<HomeViewModel> {
private let password = "sshhhhhh"
/// LND Status
@IBOutlet weak var debugStatus: UILabel!
@IBOutlet weak var lndStatus: UIImageView!
@IBOutlet weak var syncToChainStatus: UIImageView!
@IBOutlet weak var walletStatus: UIImageView!
/// INFO
@IBOutlet weak var infoStack: UIStackView!
@IBOutlet weak var peersCount: UILabel!
@IBOutlet weak var blockHeightCount: UILabel!
@IBOutlet weak var activeChannelsCount: UILabel!
@IBOutlet weak var inactiveChannelsCount: UILabel!
@IBOutlet weak var pendingChannelsCount: UILabel!
/// Wallet
@IBOutlet weak var walletStack: UIStackView!
@IBOutlet weak var walletBalanceStack: UIStackView!
@IBOutlet weak var createWalletBtn: UIButton!
@IBOutlet weak var unlockWalletBtn: UIButton!
@IBOutlet weak var getBalanceBtn: UIButton!
@IBOutlet weak var walletInfo: UIButton!
@IBOutlet weak var totalAmountLbl: UILabel!
@IBOutlet weak var unconfirmedLbl: UILabel!
@IBOutlet weak var confirmedAmountLbl: UILabel!
@IBOutlet weak var showChannelsBtn: UIButton!
@IBOutlet weak var showPaymentsBtn: UIButton!
//MARK: View LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
title = "Home"
updateStatus()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
subscribe()
}
//MARK: ViewModel
// This will get called when the ViewModel for this ViewController is ready to use
// This gives us a simple place to observe all changes to the datasources
// and can update the views accordingly as they change in real time
override func viewModelDidLoad() {
viewModel.isLoading.observe = { [weak self] isLoading in
if (isLoading) {
self?.showLoadingView()
}else{
self?.showContentView()
}
}
viewModel.walletBalance.observe = { [weak self] balance in
self?.updateBalance(balance)
}
viewModel.newAddress.observe = { [weak self] address in
UIPasteboard.general.string = address
self?.showContentView()
}
viewModel.walletWipe.observe = { _ in
UIControl().sendAction(#selector(NSXPCConnection.suspend), to: UIApplication.shared, for: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
exit(0)
}
}
viewModel.load()
}
//MARK: Button Actions
@IBAction func didPressUnlockBtn(_ sender: Any) {
self.viewModel.unlockWallet(password: self.password)
}
@IBAction func didPressPaymentsBtn(_ sender: Any) {
self.navigationController?.pushViewController(PaymentsVC(), animated: true)
}
@IBAction func didPressChannelsBtn(_ sender: Any) {
self.navigationController?.pushViewController(ChannelsVC(), animated: true)
}
@IBAction func didPressWalletInfo(_ sender: Any) {
let walletAddress = LightningStateMonitor.shared.state.walletInfo.identityPubkey
self.showAlert(title: "My Wallet", address: walletAddress)
}
}
//MARK: Subscribe
extension HomeVC{
private func subscribe() {
EventBus.onMainThread(self, eventType: .lndStateChange) { [weak self] (_) in
self?.updateStatus()
}
EventBus.onMainThread(self, eventType: .lndStopped) { [weak self] (_) in
self?.updateStatus()
}
EventBus.onMainThread(self, eventType: .lndStarted) { [weak self] (_) in
self?.updateStatus()
}
EventBus.onMainThread(self, eventType: .lndRpcReady) { [weak self] (_) in
self?.updateStatus()
}
EventBus.onMainThread(self, eventType: .lndWalletUnlocked) { [weak self] (_) in
self?.updateStatus()
}
}
private func updateStatus() {
debugStatus.text = LightningStateMonitor.shared.state.debuggingStatus.joined(separator: "\n\n")
updateLND(isOn: LightningStateMonitor.shared.state.lndRunning)
updateRPC(isReady: LightningStateMonitor.shared.state.walletInfo.syncedToChain)
updateWallet(isUnlocked: LightningStateMonitor.shared.state.walletUnlocked)
updateInfo()
}
private func updateRPC(isReady: Bool){
self.syncToChainStatus.tintColor = isReady ? .green : .red
self.syncToChainStatus.image = isReady ? UIImage(systemName: "point.3.filled.connected.trianglepath.dotted") : UIImage(systemName: "point.3.connected.trianglepath.dotted")
}
private func updateLND(isOn: Bool){
self.unlockWalletBtn.isHidden = !isOn
self.unlockWalletBtn.isEnabled = isOn
self.lndStatus.tintColor = isOn ? .green : .red
self.lndStatus.image = isOn ? UIImage(systemName: "checkmark.circle.fill") : UIImage(systemName: "checkmark.circle.trianglebadge.exclamationmark")
}
private func updateInfo(){
self.peersCount.text = String(LightningStateMonitor.shared.state.walletInfo.numPeers)
self.blockHeightCount.text = String(LightningStateMonitor.shared.state.walletInfo.blockHeight)
self.activeChannelsCount.text = String(LightningStateMonitor.shared.state.walletInfo.numActiveChannels)
self.inactiveChannelsCount.text = String(LightningStateMonitor.shared.state.walletInfo.numInactiveChannels)
self.pendingChannelsCount.text = String(LightningStateMonitor.shared.state.walletInfo.numPendingChannels)
self.viewModel.getWalletBalance()
}
private func updateBalance(_ balance: WalletBalance){
self.totalAmountLbl.text = String(balance.total)
self.confirmedAmountLbl.text = String(balance.confirmed)
self.unconfirmedLbl.text = String(balance.unconfirmed)
}
private func updateWallet(isUnlocked: Bool){
self.unlockWalletBtn.isHidden = isUnlocked
self.walletStatus.tintColor = isUnlocked ? .green : .red
self.walletStatus.image = isUnlocked ? UIImage(systemName: "lock.open") : UIImage(systemName: "lock")
self.infoStack.isHidden = !isUnlocked
self.walletBalanceStack.isHidden = !isUnlocked
self.walletInfo.isEnabled = isUnlocked
// self.getBalanceBtn.isHidden = !isUnlocked
self.showChannelsBtn.isHidden = !isUnlocked
self.showPaymentsBtn.isEnabled = isUnlocked
}
}