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

520 lines
22 KiB
Swift
Raw Normal View History

2023-06-08 06:36:06 +00:00
import UIKit
class HomeViewController: CustomViewController<HomeViewModel> {
// private let password = "sshhhhhh"
// private var unlockWallet : UIButton!
// private var getInfo : UIButton!
// private var listChannels : UIButton!
// private var openChannel : UIButton!
// private var showInvoices : UIButton!
// private var showPayments : UIButton!
// private var hideButton : UIButton!
// private var sendPayment : UIButton!
// private var receivePayment : UIButton!
// private var cancel : UIButton!
// private var generateInvoice : UIButton!
// private var payInvoice : UIButton!
// private var debugStatus : UILabel!
// private var resultMessage : UILabel!
// private var balance : UILabel!
// private var invoiceLbl : UILabel!
// var isShowPayments: Bool = true
// lazy var contentViewSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
// lazy var scrollView: UIScrollView = {
// let view = UIScrollView(frame: .zero)
// view.backgroundColor = .white
// view.frame = self.view.bounds
// view.contentSize = contentViewSize
// view.translatesAutoresizingMaskIntoConstraints = false
// return view
// }()
// lazy var stackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var debugStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var walletStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var infoStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var channelStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var paymentStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .vertical
// stackView.distribution = .fill
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// lazy var sendStackView: UIStackView = {
// let stackView = UIStackView()
// stackView.axis = .horizontal
// stackView.distribution = .fillEqually
// stackView.alignment = .fill
// stackView.spacing = 10
// stackView.translatesAutoresizingMaskIntoConstraints = false
// return stackView
// }()
// let textView: UITextField = {
// let textView = UITextField()
// textView.textAlignment = .left
// textView.layer.cornerRadius = 5.0
// textView.layer.borderWidth = 0.2
// textView.layer.borderColor = UIColor.gray.cgColor
// textView.layer.masksToBounds = true
// textView.tintColor = .black
// textView.translatesAutoresizingMaskIntoConstraints = false //enable autolayout
// textView.heightAnchor.constraint(equalToConstant: 100).isActive = true
// textView.isHidden = true
// textView.placeholder = "Enter Invoice..."
//// textView.text = "lntb2500n1p30mapmpp53jy8q39upxn0acs5a2e0hzducxn25f2r9xrp3y7kyje2ug68t4lqdqgf4jhycmfcqzpgxqrrsssp5qe7unjq3j2lw7f8etvlhsgpac8ec7q7pd3ydx6mmaa33vcjcl5as9qyyssq2w3e8d074cz72tmmne9z7c3j9f9u8n3elc2sgzdm7j760847c5az2ca6900tdg7hx8wnaethzqkq60ym4ug7zrr4mlxyzcc5lrvgwycqmyzteq"
// return textView
// }()
// lazy var tableView: UITableView = {
// let tableView: UITableView = UITableView()
// tableView.backgroundColor = .white
// tableView.translatesAutoresizingMaskIntoConstraints = false
// tableView.heightAnchor.constraint(equalToConstant: 200).isActive = true
// tableView.delegate = self
// tableView.dataSource = self
// return tableView
// }()
//
//
// override func viewDidLoad() {
// super.viewDidLoad()
// title = "LND test"
// view.backgroundColor = .white
// view.addSubview(scrollView)
// scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
// scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
// scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
// scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// setup()
// scrollView.addSubview(stackView)
//
// stackView.translatesAutoresizingMaskIntoConstraints = false
// stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -40).isActive = true
// stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 10).isActive = true
// stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20).isActive = true
// stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 20).isActive = true
// stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 10).isActive = true
// updateStatus()
// }
//
// override func viewDidAppear(_ animated: Bool) {
// super.viewDidAppear(animated)
// subscribe()
// }
//
// private func subscribe() {
// EventBus.onMainThread(self, eventType: .lndStateChange) { [weak self] (_) in
// self?.updateStatus()
// }
// EventBus.onMainThread(self, eventType: .lndWalletUnlocked) { [weak self] (_) in
// self?.setupInfo()
// self?.setupChannel()
// self?.walletStackView.isHidden = true
// self?.viewModel.getWalletBalance()
// }
// }
//
// private func updateStatus() {
// if LightningStateMonitor.shared.state.lndRunning {
// setupWallet()
// }
//
// debugStatus.text = LightningStateMonitor.shared.state.debuggingStatus.joined(separator: "\n")
// }
//
// private func createButton(_ title: String, action: @escaping () -> Void) -> UIButton {
// let button = CustomButton(action: action)
// button.title = title
// button.translatesAutoresizingMaskIntoConstraints = false
// button.heightAnchor.constraint(equalToConstant: CGFloat(50)).isActive = true
// return button
// }
//
// private func setup() {
// let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
// view.addGestureRecognizer(tap) // Add gesture recognizer to background view
// showLNDStatus()
//
// textView.delegate = self
//
// }
//
// // 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()
// }
// }
//
// viewModel.randomInt.observe = { [weak self] randomInt in
// self?.showContentView()
// }
//
// viewModel.error.observe = { [weak self] error in
// self?.showErrorView()
// }
//
// viewModel.resultMessage.observe = { [weak self] message in
// self?.resultMessage.text = message
// self?.showContentView()
// }
//
// viewModel.newAddress.observe = { [weak self] address in
// self?.resultMessage.text = address
// 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.payments.observe = { [weak self] payments in
// self?.tableView.reloadData()
// }
// viewModel.channels.observe = { [weak self] payments in
// self?.tableView.reloadData()
// }
// viewModel.balance.observe = { [weak self] balance in
// self?.balance.text = balance
// }
// viewModel.invoice.observe = { [weak self] invoice in
// self?.invoiceLbl.text = invoice
// }
// viewModel.load()
//
// }
//
//}
//extension HomeViewController: UITextFieldDelegate{
// @objc func handleTap() {
// textView.resignFirstResponder() // dismiss keyoard
// }
//
// func textFieldDidBeginEditing(_ textField: UITextField) {
// scrollView.setContentOffset(CGPoint(x: 0, y: textField.frame.height), animated: true)
// }
// func textFieldDidEndEditing(_ textField: UITextField) {
//
// if textField.hasText{
// payInvoice.isHidden = false
// }
// scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
// }
// func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// textField.resignFirstResponder() // dismiss keyboard
// return true
// }
//}
//extension HomeViewController: UITextPasteDelegate {
// func textPasteConfigurationSupporting(_ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting, shouldAnimatePasteOf attributedString: NSAttributedString, to textRange: UITextRange) -> Bool {
// return false
// }
//}
////MARK: - Wallet Management
//extension HomeViewController{
// private func setupWallet(){
//
// // let createButton = addDebugButton("Create wallet", action: {
// // self.viewModel.createWallet(password: self.password)
// // })
// // let newAddressButton = addDebugButton("New address (copies to clipboard)", action: {
// // self.viewModel.getNewAddress()
// // })
// // let wipeButton = addDebugButton("Wipe (and close) wallet", action: {
// // self.viewModel.wipeWallet()
// // })
// unlockWallet = createButton("Unlock wallet", action: {
// self.viewModel.unlockWallet(password: self.password)
// self.balance = UILabel()
// self.balance.text = "..."
// self.balance.textColor = .blue
// self.balance.translatesAutoresizingMaskIntoConstraints = false
// self.balance.textAlignment = .center
// self.balance.numberOfLines = 0
// self.stackView.addArrangedSubview(self.balance)
// })
// walletStackView.addArrangedSubview(unlockWallet)
//
// stackView.addArrangedSubview(walletStackView)
//
// }
//}
////MARK: - Info
//extension HomeViewController{
// private func showLNDStatus(){
//
// resultMessage = UILabel()
// resultMessage.text = "..."
// resultMessage.translatesAutoresizingMaskIntoConstraints = false
// resultMessage.textColor = Theme.inverseBackgroundColor
// resultMessage.textAlignment = .center
// resultMessage.numberOfLines = 0
//
// debugStatus = UILabel()
// debugStatus.translatesAutoresizingMaskIntoConstraints = false
// debugStatus.textColor = Theme.inverseBackgroundColor
// debugStatus.textAlignment = .center
// debugStatus.numberOfLines = 0
// debugStatus.text = "Debug status"
// debugStackView.addArrangedSubview(resultMessage)
// debugStackView.addArrangedSubview(debugStatus)
// stackView.addArrangedSubview(debugStackView)
// }
// private func setupInfo(){
// getInfo = createButton("Show info", action: {
// self.viewModel.getInfo()
// self.viewModel.getWalletBalance()
// })
// infoStackView.addArrangedSubview(getInfo)
// stackView.addArrangedSubview(infoStackView)
//
// }
//}
//
////MARK: - Channel
//extension HomeViewController{
//
// private func setupChannel(){
// openChannel = createButton("Open channel", action: {
// self.viewModel.openChannel()
// })
// listChannels = createButton("List channels", action: {
// self.isShowPayments = false
// self.showHideViews(showChannels: false, showPayments: true)
// self.viewModel.listChannels()
//
// })
// channelStackView.addArrangedSubview(openChannel)
// channelStackView.addArrangedSubview(listChannels)
// stackView.addArrangedSubview(channelStackView)
//
//
// if viewModel.infoResponse.value?.numActiveChannels == 0 {
// listChannels.isHidden = false
// }else{
// openChannel.isHidden = true
// }
// setupPayments()
// }
//
//}
//
////MARK: - Payments Management
//extension HomeViewController: UITableViewDelegate, UITableViewDataSource{
// func showHideViews(showChannels: Bool, showPayments: Bool){
// let isListShowing = (showChannels == showPayments) && showChannels
// let isOneMissing = (showChannels && !showPayments) || (!showChannels && showPayments)
// self.showPayments.isHidden = !showPayments
// self.listChannels.isHidden = !showChannels
// self.hideButton.isHidden = !isOneMissing
// self.tableView.isHidden = !isOneMissing
// self.debugStackView.isHidden = !isListShowing
// self.infoStackView.isHidden = !isListShowing
// self.sendStackView.isHidden = !isListShowing
// }
// func showHideViews(showPay: Bool, showReceive: Bool){
// let hideOthers = !(showPay || showReceive)
// self.showHideViews(showChannels: hideOthers, showPayments: hideOthers)
// self.sendPayment.isHidden = showPay || showReceive
// self.receivePayment.isHidden = showPay || showReceive
// self.cancel.isHidden = showPay && showReceive
// self.payInvoice.isHidden = true
// self.generateInvoice.isHidden = true
// self.invoiceLbl.isHidden = true
// self.textView.isHidden = !showPay
// self.sendStackView.isHidden = false
//
// }
// private func setupPayments(){
//
// showInvoices = createButton("Show invoices", action: {
// self.viewModel.listInvoices()
// })
// hideButton = createButton("Hide", action: {
// self.showHideViews(showChannels: true, showPayments: true)
// })
// showPayments = createButton("Show Payments History", action: {
// self.isShowPayments = true
// self.showHideViews(showChannels: true, showPayments: false)
// self.viewModel.listPayments()
// })
//
//
// sendPayment = createButton("Send", action: {
// self.showHideViews(showPay: true, showReceive: false)
// })
// receivePayment = createButton("Receive", action: {
// self.showHideViews(showPay: false, showReceive: true)
// self.generateInvoice.isHidden = false
// self.invoiceLbl.isHidden = false
// })
// cancel = createButton("Cancel", action: {
// self.showHideViews(showChannels: true, showPayments: true)
// self.showHideViews(showPay: false, showReceive: false)
// self.cancel.isHidden = true
// })
// generateInvoice = createButton("Generate Invoice", action: {
// self.showHideViews(showChannels: true, showPayments: true)
// self.showHideViews(showPay: false, showReceive: false)
// self.viewModel.createInvoice(amount: 200, comment: self.textView.text ?? "Test")
// self.viewModel.getWalletBalance()
// })
// invoiceLbl = UILabel()
// invoiceLbl.text = "..."
// invoiceLbl.translatesAutoresizingMaskIntoConstraints = false
// invoiceLbl.numberOfLines = 0
//
// payInvoice = createButton("Pay Invoice", action: {
// if let text = self.textView.text{
// debugPrint("Invoice ", text)
// self.viewModel.payInvoice(invoice: text)
//
// }else{
//
// }
//
// self.viewModel.getWalletBalance()
// self.showHideViews(showChannels: true, showPayments: true)
// self.showHideViews(showPay: false, showReceive: false)
//
// })
//
// payInvoice.isHidden = true
// generateInvoice.isHidden = true
// invoiceLbl.isHidden = true
// tableView.isHidden = true
// hideButton.isHidden = true
// cancel.isHidden = true
// paymentStackView.addArrangedSubview(showInvoices)
// paymentStackView.addArrangedSubview(showPayments)
// paymentStackView.addArrangedSubview(hideButton)
//
//
// sendStackView.addArrangedSubview(sendPayment)
// sendStackView.addArrangedSubview(receivePayment)
// sendStackView.addArrangedSubview(cancel)
// sendStackView.addArrangedSubview(generateInvoice)
// sendStackView.addArrangedSubview(payInvoice)
//
// paymentStackView.addArrangedSubview(sendStackView)
// paymentStackView.addArrangedSubview(textView)
// paymentStackView.addArrangedSubview(invoiceLbl)
//
// paymentStackView.addArrangedSubview(tableView)
// stackView.addArrangedSubview(paymentStackView)
// }
//
// func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// if isShowPayments{
// if let noOfPayments = viewModel.payments.value?.count{
// return noOfPayments
// }else{
// let emptyLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
// emptyLabel.text = "No Payments"
// emptyLabel.textAlignment = NSTextAlignment.center
// self.tableView.backgroundView = emptyLabel
// self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
// return 0
// }
// }else{
// if let noOfChannels = viewModel.channels.value?.count{
// return noOfChannels
// }else{
// let emptyLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
// emptyLabel.text = "No Channels"
// emptyLabel.textAlignment = NSTextAlignment.center
// self.tableView.backgroundView = emptyLabel
// self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
// return 0
// }
// }
//
//
//
// }
//
// func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let reuseCellIdentifier = "cellIdentifier"
// var cell = tableView.dequeueReusableCell(withIdentifier: reuseCellIdentifier)
// if (!(cell != nil)) {
//
// cell = UITableViewCell(style: .subtitle, reuseIdentifier: reuseCellIdentifier)
// }
// cell?.textLabel?.numberOfLines = 0
// cell?.detailTextLabel?.numberOfLines = 0
// if isShowPayments{
// if let payment = viewModel.payments.value?[indexPath.row]{
// cell?.textLabel?.text = "Status: \(payment.status) \nAmount: \(payment.value) sat"
// cell?.detailTextLabel?.text = "\nDate: \(Date(timeIntervalSince1970: TimeInterval(payment.creationDate))) \nPayment hash:\n\(payment.paymentRequest.description)"
// }
// }else{
// if let channel = viewModel.channels.value?[indexPath.row]{
// cell?.textLabel?.text = "Active: \(channel.active) \nLocal Balance: \(channel.localBalance) sat \nRemote Balance: \(channel.remoteBalance) sat"
// let pendingTransactions = channel.pendingHtlcs.count == 0 ? "" : "\nPending transactions: \(channel.pendingHtlcs)"
// cell?.detailTextLabel?.text = "\nPub key: \(channel.remotePubkey) \(pendingTransactions)"
// }
// }
//
//
// return cell!
// }
}
public extension String {
func setColor(_ color: UIColor, ofSubstring substring: String) -> NSMutableAttributedString {
let range = (self as NSString).range(of: substring)
let attributedString = NSMutableAttributedString(string: self)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
return attributedString
}
}