Create method to validate if the suggested user account given by the user is an available account name or not and display a message to the user with the result, in CreateAccountFragment.

This commit is contained in:
Severiano Jaramillo 2019-01-07 20:35:29 -06:00
parent e530dc531e
commit 64d30f42ab
2 changed files with 40 additions and 3 deletions

View file

@ -15,6 +15,8 @@ import cy.agorise.bitsybitshareswallet.utils.toast
import cy.agorise.graphenej.Address import cy.agorise.graphenej.Address
import cy.agorise.graphenej.BrainKey import cy.agorise.graphenej.BrainKey
import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.ConnectionStatusUpdate
import cy.agorise.graphenej.api.calls.GetAccountByName
import cy.agorise.graphenej.models.AccountProperties
import cy.agorise.graphenej.models.JsonRpcResponse import cy.agorise.graphenej.models.JsonRpcResponse
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.fragment_create_account.* import kotlinx.android.synthetic.main.fragment_create_account.*
@ -31,6 +33,8 @@ class CreateAccountFragment : ConnectedFragment() {
private const val BRAINKEY_FILE = "brainkeydict.txt" private const val BRAINKEY_FILE = "brainkeydict.txt"
private const val MIN_ACCOUNT_NAME_LENGTH = 8 private const val MIN_ACCOUNT_NAME_LENGTH = 8
private const val RESPONSE_GET_ACCOUNT_BY_NAME = 1
} }
private lateinit var mBrainKey: BrainKey private lateinit var mBrainKey: BrainKey
@ -41,6 +45,9 @@ class CreateAccountFragment : ConnectedFragment() {
private var isPINConfirmationValid = false private var isPINConfirmationValid = false
private var isAccountValidAndAvailable = false private var isAccountValidAndAvailable = false
// Map used to keep track of request and response id pairs
private val responseMap = HashMap<Long, Int>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true) setHasOptionsMenu(true)
@ -54,7 +61,7 @@ class CreateAccountFragment : ConnectedFragment() {
mDisposables.add( mDisposables.add(
tietAccountName.textChanges() tietAccountName.textChanges()
.skipInitialValue() .skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(800, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { validateAccountName(it.toString()) } .subscribe { validateAccountName(it.toString()) }
) )
@ -89,11 +96,15 @@ class CreateAccountFragment : ConnectedFragment() {
isAccountValidAndAvailable = false isAccountValidAndAvailable = false
if ( !isAccountNameValid(accountName) ) { if ( !isAccountNameValid(accountName) ) {
tilAccountName.error = getString(R.string.error__invalid_account_name)
tilAccountName.helperText = "" tilAccountName.helperText = ""
tilAccountName.error = getString(R.string.error__invalid_account_name)
} else { } else {
tilAccountName.isErrorEnabled = false tilAccountName.isErrorEnabled = false
tilAccountName.helperText = getString(R.string.text__verifying_account_availability) tilAccountName.helperText = getString(R.string.text__verifying_account_availability)
val id = mNetworkService?.sendMessage(GetAccountByName(accountName), GetAccountByName.REQUIRED_API)
if (id != null)
responseMap[id] = RESPONSE_GET_ACCOUNT_BY_NAME
} }
enableDisableCreateButton() enableDisableCreateButton()
@ -143,13 +154,37 @@ class CreateAccountFragment : ConnectedFragment() {
} }
override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) { override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. if (responseMap.containsKey(response.id)) {
val responseType = responseMap[response.id]
when (responseType) {
RESPONSE_GET_ACCOUNT_BY_NAME -> handleAccountName(response.result)
}
responseMap.remove(response.id)
}
} }
override fun handleConnectionStatusUpdate(connectionStatusUpdate: ConnectionStatusUpdate) { override fun handleConnectionStatusUpdate(connectionStatusUpdate: ConnectionStatusUpdate) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
/**
* Handles the response from the NetworkService's GetAccountByName call to decide if the user's suggested
* account is available or not.
*/
private fun handleAccountName(result: Any?) {
if (result is AccountProperties) {
tilAccountName.helperText = ""
tilAccountName.error = getString(R.string.error__account_not_available)
isAccountValidAndAvailable = false
} else {
tilAccountName.isErrorEnabled = false
tilAccountName.helperText = getString(R.string.text__account_is_available)
isAccountValidAndAvailable = true
}
enableDisableCreateButton()
}
/** /**
* Method that generates a fresh key that will be controlling the newly created account. * Method that generates a fresh key that will be controlling the newly created account.
*/ */

View file

@ -30,6 +30,8 @@
<string name="error__read_dict_file">Error reading dictionary file</string> <string name="error__read_dict_file">Error reading dictionary file</string>
<string name="error__invalid_account_name">The account name has to either have more than 8 characters, contain a number or have no vowels. The underscore character is also not allowed. </string> <string name="error__invalid_account_name">The account name has to either have more than 8 characters, contain a number or have no vowels. The underscore character is also not allowed. </string>
<string name="text__verifying_account_availability">Verifying account availability…</string> <string name="text__verifying_account_availability">Verifying account availability…</string>
<string name="error__account_not_available">Account not available</string>
<string name="text__account_is_available">Account is available</string>
<!-- Home --> <!-- Home -->
<string name="title_transactions">Transactions</string> <string name="title_transactions">Transactions</string>