Skip to content

Commit

Permalink
ErgoAuth iOS completed #112
Browse files Browse the repository at this point in the history
  • Loading branch information
MrStahlfelge committed Apr 20, 2022
1 parent 5558bbe commit 25f954d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.ergoplatform.ios.ergoauth
import kotlinx.coroutines.CoroutineScope
import org.ergoplatform.ios.transactions.ErgoPaySigningViewController
import org.ergoplatform.ios.ui.*
import org.ergoplatform.ios.wallet.ChooseWalletViewController
import org.ergoplatform.uilogic.*
import org.ergoplatform.uilogic.ergoauth.ErgoAuthUiLogic
import org.robovm.apple.coregraphics.CGRect
Expand Down Expand Up @@ -126,11 +127,7 @@ class ErgoAuthenticationViewController(
} ?: true
false
} ?: true
refreshAddressInfo()
}

fun refreshAddressInfo() {
mainRequestContainer.walletChooser.text = uiLogic.walletConfig?.displayName
mainRequestContainer.refreshAddressInfo()
}
}

Expand All @@ -139,8 +136,10 @@ class ErgoAuthenticationViewController(
text = texts.getString(STRING_DESC_AUTHENTICATION_WALLET)
textAlignment = NSTextAlignment.Center
}
val walletChooser = Headline2Label().apply {

private val walletNameLabel = Headline2Label().apply {
numberOfLines = 1
textColor = uiColorErgo
}
private val authButton = PrimaryButton(texts.getString(STRING_BUTTON_AUTHENTICATE)).apply {
addOnTouchUpInsideListener { _, _ ->
Expand All @@ -153,14 +152,17 @@ class ErgoAuthenticationViewController(
}

init {
val walletChooseButton = walletChooser.wrapWithTrailingImage(
val walletChooseButton = walletNameLabel.wrapWithTrailingImage(
getIosSystemImage(IMAGE_OPEN_LIST, UIImageSymbolScale.Small, 20.0)!!,
keepWidth = true
).apply {
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
presentViewController(
null, // TODO
ChooseWalletViewController {
uiLogic.walletConfig = it
refreshAddressInfo()
},
true
) {}
})
Expand All @@ -172,7 +174,12 @@ class ErgoAuthenticationViewController(

descLabel.topToSuperview().widthMatchesSuperview()
walletChooseButton.topToBottomOf(descLabel).centerHorizontal(true)
authButton.topToBottomOf(walletChooser, inset = DEFAULT_MARGIN * 2).bottomToSuperview().centerHorizontal()
authButton.topToBottomOf(walletNameLabel, inset = DEFAULT_MARGIN * 2).bottomToSuperview()
.centerHorizontal().fixedWidth(200.0)
}

fun refreshAddressInfo() {
walletNameLabel.text = uiLogic.walletConfig?.displayName
}
}

Expand All @@ -184,6 +191,7 @@ class ErgoAuthenticationViewController(
runOnMainThread {
stateDoneContainer.isHidden = newState != State.DONE
fetchingContainer.isHidden = newState != State.FETCHING_DATA
authRequestContainer.isHidden = newState != State.WAIT_FOR_AUTH

if (newState == State.DONE) {
showDoneInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import org.ergoplatform.ios.ui.*
import org.ergoplatform.transactions.isErgoPaySigningRequest
import org.ergoplatform.parsePaymentRequest
import org.ergoplatform.persistance.Wallet
import org.ergoplatform.uilogic.STRING_BUTTON_SEND
import org.ergoplatform.uilogic.STRING_DESC_CHOOSE_WALLET
import org.ergoplatform.uilogic.STRING_LABEL_TO
import org.ergoplatform.uilogic.STRING_TITLE_ERGO_PAY_REQUEST
import org.ergoplatform.uilogic.*
import org.ergoplatform.wallet.getBalanceForAllAddresses
import org.ergoplatform.wallet.getTokensForAllAddresses
import org.robovm.apple.coregraphics.CGRect
import org.robovm.apple.uikit.*

Expand All @@ -23,10 +21,11 @@ class ChooseSpendingWalletViewController(
) : CoroutineViewController() {

private lateinit var walletsStackView: UIStackView
private lateinit var texts: StringProvider

override fun viewDidLoad() {
super.viewDidLoad()
val texts = getAppDelegate().texts
texts = IosStringProvider(getAppDelegate().texts)

view.backgroundColor = UIColor.systemBackground()
addCloseButton()
Expand All @@ -36,7 +35,7 @@ class ChooseSpendingWalletViewController(
}

val toLabel = Body1Label().apply {
text = texts.get(STRING_LABEL_TO)
text = texts.getString(STRING_LABEL_TO)
textAlignment = NSTextAlignment.Center
}

Expand All @@ -49,7 +48,7 @@ class ChooseSpendingWalletViewController(
}

val descLabel = Body1Label().apply {
text = texts.get(STRING_DESC_CHOOSE_WALLET)
text = texts.getString(STRING_DESC_CHOOSE_WALLET)
textAlignment = NSTextAlignment.Center
}

Expand Down Expand Up @@ -81,12 +80,12 @@ class ChooseSpendingWalletViewController(
amountLabel.isHidden = true
recipientLabel.text = ""
toLabel.text = ""
titleLabel.text = texts.get(STRING_TITLE_ERGO_PAY_REQUEST)
titleLabel.text = texts.getString(STRING_TITLE_ERGO_PAY_REQUEST)
} else parsePaymentRequest(paymentRequest)?.let {
amountLabel.setErgoAmount(it.amount)
amountLabel.isHidden = it.amount.isZero()
recipientLabel.text = it.address
titleLabel.text = texts.get(STRING_BUTTON_SEND)
titleLabel.text = texts.getString(STRING_BUTTON_SEND)
}
}

Expand All @@ -98,19 +97,27 @@ class ChooseSpendingWalletViewController(
runOnMainThread {
walletsStackView.clearArrangedSubviews()
wallets.sortedBy { it.walletConfig.displayName?.lowercase() }.forEach { wallet ->
walletsStackView.addArrangedSubview(WalletItem(wallet))
walletsStackView.addArrangedSubview(ChooseWalletItem(wallet, texts).apply {
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
navigateToSendFundsScreen(wallet.walletConfig.id)
})
})
}
}
}
}

fun navigateToSendFundsScreen(walletId: Int) {
private fun navigateToSendFundsScreen(walletId: Int) {
callback.invoke(walletId)
dismissViewController(true) {
}
dismissViewController(true) {}
}

private inner class WalletItem(wallet: Wallet) : UIView(CGRect.Zero()) {
class ChooseWalletItem(
wallet: Wallet,
texts: StringProvider,
showTokenNum: Boolean = false
) : UIView(CGRect.Zero()) {
init {
val name = Body1BoldLabel().apply {
textColor = uiColorErgo
Expand All @@ -120,22 +127,23 @@ class ChooseSpendingWalletViewController(
val balance = ErgoAmountView(true).apply {
setErgoAmount(ErgoAmount(wallet.getBalanceForAllAddresses()))
}
val tokenNumLabel = Body1BoldLabel()
val tokenNum = if (!showTokenNum) 0 else wallet.getTokensForAllAddresses().size
tokenNumLabel.text = if (tokenNum > 0)
texts.getString(STRING_LABEL_WALLET_TOKEN_BALANCE, tokenNum) else ""

addSubview(name)
addSubview(balance)
addSubview(tokenNumLabel)

name.topToSuperview().bottomToSuperview().leftToSuperview()
// lower hugging and compression resistance so that balance will keep its intrinsic
// width. It does not work using balance.keepIntrinsicWidth here due to
// ErgoAmountViews inner structure already using it
name.setContentCompressionResistancePriority(500f, UILayoutConstraintAxis.Horizontal)
name.setContentHuggingPriority(100f, UILayoutConstraintAxis.Horizontal)
balance.rightToSuperview().topToSuperview().bottomToSuperview().leftToRightOf(name)

isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
navigateToSendFundsScreen(wallet.walletConfig.id)
})
balance.rightToSuperview().topToSuperview().leftToRightOf(name)
tokenNumLabel.rightToRightOf(balance).topToBottomOf(balance).bottomToSuperview()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.ergoplatform.ios.wallet

import kotlinx.coroutines.launch
import org.ergoplatform.ios.transactions.ChooseSpendingWalletViewController
import org.ergoplatform.ios.ui.*
import org.ergoplatform.persistance.WalletConfig
import org.ergoplatform.uilogic.StringProvider
import org.robovm.apple.coregraphics.CGRect
import org.robovm.apple.uikit.*

class ChooseWalletViewController(
val callback: ((WalletConfig) -> Unit)
) : CoroutineViewController() {
private lateinit var walletsStackView: UIStackView
private lateinit var texts: StringProvider

override fun viewDidLoad() {
super.viewDidLoad()
texts = IosStringProvider(getAppDelegate().texts)

view.backgroundColor = UIColor.systemBackground()
val closeButton = addCloseButton()

walletsStackView = UIStackView(CGRect.Zero()).apply {
axis = UILayoutConstraintAxis.Vertical
}
val scrollView = walletsStackView.wrapInVerticalScrollView()

view.addSubview(scrollView)

scrollView.topToBottomOf(closeButton, DEFAULT_MARGIN)
.widthMatchesSuperview(inset = DEFAULT_MARGIN * 2, maxWidth = MAX_WIDTH)
.bottomToSuperview(bottomInset = DEFAULT_MARGIN)

}

override fun viewWillAppear(animated: Boolean) {
super.viewWillAppear(animated)
viewControllerScope.launch {
val wallets = getAppDelegate().database.walletDbProvider.getWalletsWithStates()

runOnMainThread {
walletsStackView.clearArrangedSubviews()
wallets.sortedBy { it.walletConfig.displayName?.lowercase() }.forEach { wallet ->
walletsStackView.addArrangedSubview(
ChooseSpendingWalletViewController.ChooseWalletItem(
wallet,
texts,
true
).apply {
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
callback.invoke(wallet.walletConfig)
dismissViewController(true) {}
})
})
}
}
}
}

}

0 comments on commit 25f954d

Please sign in to comment.