-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ErgoAuth iOS ErgoAuthenticationViewController introduced #112
- Loading branch information
1 parent
ff755d7
commit 5558bbe
Showing
5 changed files
with
224 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
ios/src/main/java/org/ergoplatform/ios/ergoauth/ErgoAuthenticationViewController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package org.ergoplatform.ios.ergoauth | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import org.ergoplatform.ios.transactions.ErgoPaySigningViewController | ||
import org.ergoplatform.ios.ui.* | ||
import org.ergoplatform.uilogic.* | ||
import org.ergoplatform.uilogic.ergoauth.ErgoAuthUiLogic | ||
import org.robovm.apple.coregraphics.CGRect | ||
import org.robovm.apple.foundation.NSArray | ||
import org.robovm.apple.uikit.* | ||
|
||
class ErgoAuthenticationViewController( | ||
private val request: String, | ||
private val walletId: Int?, | ||
) : CoroutineViewController() { | ||
|
||
private val uiLogic = IosUiLogic() | ||
private lateinit var authRequestContainer: AuthenticationRequestStack | ||
private lateinit var fetchingContainer: ErgoPaySigningViewController.FetchDataContainer | ||
private lateinit var texts: IosStringProvider | ||
private val stateDoneContainer = CardView() | ||
|
||
override fun viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
val appDelegate = getAppDelegate() | ||
texts = IosStringProvider(appDelegate.texts) | ||
fetchingContainer = ErgoPaySigningViewController.FetchDataContainer(texts) | ||
authRequestContainer = AuthenticationRequestStack() | ||
|
||
title = texts.getString(STRING_TITLE_ERGO_AUTH_REQUEST) | ||
view.backgroundColor = UIColor.systemBackground() | ||
navigationController.navigationBar?.tintColor = UIColor.label() | ||
|
||
view.layoutMargins = UIEdgeInsets.Zero() | ||
|
||
val scrollingContainer = UIView(CGRect.Zero()) | ||
val scrollView = scrollingContainer.wrapInVerticalScrollView() | ||
|
||
view.addSubview(scrollView) | ||
view.addSubview(fetchingContainer) | ||
view.addSubview(stateDoneContainer) | ||
scrollingContainer.addSubview(authRequestContainer) | ||
|
||
fetchingContainer.widthMatchesSuperview(maxWidth = MAX_WIDTH).centerVertical() | ||
authRequestContainer.edgesToSuperview(maxWidth = MAX_WIDTH) | ||
stateDoneContainer.centerVertical().widthMatchesSuperview(maxWidth = MAX_WIDTH) | ||
scrollView.edgesToSuperview() | ||
|
||
uiLogic.init( | ||
request, | ||
walletId ?: -1, | ||
texts, | ||
appDelegate.database | ||
) | ||
} | ||
|
||
private fun showDoneInfo() { | ||
if (stateDoneContainer.contentView.subviews.isEmpty()) { | ||
val image = uiLogic.getDoneSeverity().getImage()?.let { | ||
UIImageView(getIosSystemImage(it, UIImageSymbolScale.Large)).apply { | ||
contentMode = UIViewContentMode.ScaleAspectFit | ||
tintColor = uiColorErgo | ||
fixedHeight(100.0) | ||
} | ||
} | ||
|
||
val descLabel = Body1Label() | ||
descLabel.text = uiLogic.getDoneMessage(texts) | ||
descLabel.textAlignment = NSTextAlignment.Center | ||
|
||
val dismissButton = PrimaryButton(texts.getString(STRING_LABEL_DISMISS)) | ||
dismissButton.addOnTouchUpInsideListener { _, _ -> navigationController.popViewController(true) } | ||
val doneButtonContainer = UIView() | ||
doneButtonContainer.addSubview(dismissButton) | ||
dismissButton.centerHorizontal().topToSuperview().bottomToSuperview().fixedWidth(150.0) | ||
|
||
val txDoneStack = UIStackView().apply { | ||
axis = UILayoutConstraintAxis.Vertical | ||
spacing = DEFAULT_MARGIN * 3 | ||
|
||
image?.let { addArrangedSubview(image) } | ||
addArrangedSubview(descLabel) | ||
addArrangedSubview(doneButtonContainer) | ||
} | ||
|
||
stateDoneContainer.contentView.addSubview(txDoneStack) | ||
txDoneStack.edgesToSuperview(inset = DEFAULT_MARGIN * 2) | ||
} | ||
} | ||
|
||
inner class AuthenticationRequestStack : UIStackView(CGRect.Zero()) { | ||
private val messageFromDApp = Body1Label() | ||
private val messageIcon = UIImageView().apply { | ||
tintColor = UIColor.secondaryLabel() | ||
contentMode = UIViewContentMode.ScaleAspectFit | ||
fixedWidth(40.0) | ||
} | ||
private val requestFromDappCard = CardView() | ||
private val mainRequestContainer = ChooseAddressAndSignContainer() | ||
|
||
init { | ||
axis = UILayoutConstraintAxis.Vertical | ||
spacing = DEFAULT_MARGIN | ||
|
||
addArrangedSubview(requestFromDappCard) | ||
addArrangedSubview(mainRequestContainer) | ||
|
||
requestFromDappCard.contentView.addSubview(messageFromDApp) | ||
requestFromDappCard.contentView.addSubview(messageIcon) | ||
|
||
val messageStackView = UIStackView(NSArray(messageIcon, messageFromDApp)).apply { | ||
axis = UILayoutConstraintAxis.Horizontal | ||
spacing = DEFAULT_MARGIN * 2 | ||
} | ||
requestFromDappCard.contentView.addSubview(messageStackView) | ||
messageStackView.edgesToSuperview(inset = DEFAULT_MARGIN) | ||
} | ||
|
||
fun showRequestInfo() { | ||
requestFromDappCard.isHidden = uiLogic.ergAuthRequest?.userMessage?.let { | ||
messageFromDApp.text = texts.getString(STRING_LABEL_MESSAGE_FROM_DAPP, it) | ||
messageIcon.isHidden = uiLogic.ergAuthRequest!!.messageSeverity.getImage()?.let { | ||
messageIcon.image = getIosSystemImage(it, UIImageSymbolScale.Medium) | ||
false | ||
} ?: true | ||
false | ||
} ?: true | ||
refreshAddressInfo() | ||
} | ||
|
||
fun refreshAddressInfo() { | ||
mainRequestContainer.walletChooser.text = uiLogic.walletConfig?.displayName | ||
} | ||
} | ||
|
||
inner class ChooseAddressAndSignContainer : UIView(CGRect.Zero()) { | ||
private val descLabel = Body1Label().apply { | ||
text = texts.getString(STRING_DESC_AUTHENTICATION_WALLET) | ||
textAlignment = NSTextAlignment.Center | ||
} | ||
val walletChooser = Headline2Label().apply { | ||
numberOfLines = 1 | ||
} | ||
private val authButton = PrimaryButton(texts.getString(STRING_BUTTON_AUTHENTICATE)).apply { | ||
addOnTouchUpInsideListener { _, _ -> | ||
uiLogic.walletConfig?.let { walletConfig -> | ||
startAuthFlow(walletConfig) { secrets -> | ||
uiLogic.startResponse(secrets, texts) | ||
} | ||
} | ||
} | ||
} | ||
|
||
init { | ||
val walletChooseButton = walletChooser.wrapWithTrailingImage( | ||
getIosSystemImage(IMAGE_OPEN_LIST, UIImageSymbolScale.Small, 20.0)!!, | ||
keepWidth = true | ||
).apply { | ||
isUserInteractionEnabled = true | ||
addGestureRecognizer(UITapGestureRecognizer { | ||
presentViewController( | ||
null, // TODO | ||
true | ||
) {} | ||
}) | ||
} | ||
|
||
addSubview(descLabel) | ||
addSubview(walletChooseButton) | ||
addSubview(authButton) | ||
|
||
descLabel.topToSuperview().widthMatchesSuperview() | ||
walletChooseButton.topToBottomOf(descLabel).centerHorizontal(true) | ||
authButton.topToBottomOf(walletChooser, inset = DEFAULT_MARGIN * 2).bottomToSuperview().centerHorizontal() | ||
} | ||
} | ||
|
||
inner class IosUiLogic : ErgoAuthUiLogic() { | ||
override val coroutineScope: CoroutineScope | ||
get() = viewControllerScope | ||
|
||
override fun notifyStateChanged(newState: State) { | ||
runOnMainThread { | ||
stateDoneContainer.isHidden = newState != State.DONE | ||
fetchingContainer.isHidden = newState != State.FETCHING_DATA | ||
|
||
if (newState == State.DONE) { | ||
showDoneInfo() | ||
} else if (newState == State.WAIT_FOR_AUTH) { | ||
authRequestContainer.showRequestInfo() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters