From 430085f8afbf0f0f241561de24641cc3413d021c Mon Sep 17 00:00:00 2001 From: dtvv Date: Wed, 12 Sep 2018 14:17:03 -0500 Subject: [PATCH] Specialiced spinners --- .../spinners/CryptoNetAccountsSpinner.kt | 165 ++++++++++++++++++ .../spinners/InternalMaterialSpinner.kt | 80 +++++++++ 2 files changed, 245 insertions(+) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/CryptoNetAccountsSpinner.kt create mode 100644 app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/InternalMaterialSpinner.kt diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/CryptoNetAccountsSpinner.kt b/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/CryptoNetAccountsSpinner.kt new file mode 100644 index 0000000..f6db966 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/CryptoNetAccountsSpinner.kt @@ -0,0 +1,165 @@ +package cy.agorise.crystalwallet.views.natives.spinners + +import android.R +import android.content.Context +import android.util.AttributeSet +import android.view.View +import cy.agorise.crystalwallet.models.CryptoNetAccount +import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequests +import cy.agorise.crystalwallet.requestmanagers.ValidateExistBitsharesAccountRequest +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ValidationField +import cy.agorise.crystalwallet.views.CryptoNetAccountAdapter + +class CryptoNetAccountsSpinner : InternalMaterialSpinner { + + /* + * Contains the list of accounts + * */ + private var cryptoNetAccounts: MutableList? = null + + /* + * Contains the current adapter + * */ + private var fromSpinnerAdapter:CryptoNetAccountAdapter? = null + + /* + * Listeners + * */ + private var onValidationField:OnValidationFailed? = null + private var onValidationSucceeded:OnValidationSucceeded? = null + private var onAccountNotExists:OnAccountNotExists? = null + private var onItemSelectedListener:OnItemSelectedListener? = null + private var onAccountExists:OnAccountExists? = null + + /* + * Current selected account + * */ + private var cryptoNetAccount:CryptoNetAccount? = null + + + + + constructor(context: Context?, attrs: AttributeSet?) : super(context,attrs){ + + /* + * Initialy starts with the android layout + * */ + setAndroidLayout() + + /* + * When the user changes the item selection save the model + * */ + setOnItemSelectedListener(OnItemSelectedListener { view, position, id, item -> + + /* + * Save the current model + * */ + cryptoNetAccount = cryptoNetAccounts?.get(position) + + /* + * Deliver response + * */ + if(onItemSelectedListener != null){ + onItemSelectedListener?.onItemSelectedListener(cryptoNetAccount!!) + } + }) + } + + /* + * Return the current selected cryptonetaccount + * */ + fun getCryptoNetAccountSelected() : CryptoNetAccount{ + return this.cryptoNetAccount!! + } + + + /* + * Set the current layoutview + * */ + fun setLayout(layout:Int){ + this.layout = layout + } + + /* + * Init the spinner, before call this method, this list of items should be set + * */ + fun initCryptoNetAccountAdapter(){ + + fromSpinnerAdapter = CryptoNetAccountAdapter(context, this.layout!!, cryptoNetAccounts) + setAdapter(fromSpinnerAdapter!!) + } + + + fun setCryptoAccountItems(cryptoNetAccounts:MutableList){ + this.cryptoNetAccounts = cryptoNetAccounts + } + + /* + * Validate if the "selected" account exists + * */ + fun validateExistBitsharesAccountRequest(){ + + if(cryptoNetAccount != null){ + + val request = ValidateExistBitsharesAccountRequest(cryptoNetAccount?.name) + request.setListener { + if (!request.accountExists) { + if(onAccountNotExists != null){ + onAccountNotExists?.onAccountNotExists(this_!!) + } + + } + else { + if(onAccountExists != null){ + onAccountExists?.onAccountExists(this_!!) + } + } + } + CryptoNetInfoRequests.getInstance().addRequest(request) + } + } + + + /* + * Listener for validations + * */ + fun onValidationFailed(onValidationField:OnValidationFailed){ + this.onValidationField = onValidationField + } + fun onValidationSucceeded(onValidationSucceeded: OnValidationSucceeded){ + this.onValidationSucceeded = onValidationSucceeded + } + fun onAccountNotExists(onAccountNotExists: OnAccountNotExists){ + this.onAccountNotExists = onAccountNotExists + } + fun onItemSelectedListener(onItemSelectedListener: OnItemSelectedListener){ + this.onItemSelectedListener = onItemSelectedListener + } + fun onAccountExists(onAccountExists: OnAccountExists){ + this.onAccountExists = onAccountExists + } + /* + * End of Listener for validations + * */ + + /* + * Add the items list to the spinner + * */ + fun getCryptoAccountsList() : List{ + return this.cryptoNetAccounts!! + } + + + /* + * Interface for validation failed + * */ + interface OnAccountNotExists{ + fun onAccountNotExists(field: View) + } + interface OnAccountExists{ + fun onAccountExists(field: View) + } + interface OnItemSelectedListener{ + fun onItemSelectedListener(cryptoNetAccount: CryptoNetAccount) + } +} \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/InternalMaterialSpinner.kt b/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/InternalMaterialSpinner.kt new file mode 100644 index 0000000..cd6065e --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/views/natives/spinners/InternalMaterialSpinner.kt @@ -0,0 +1,80 @@ +package cy.agorise.crystalwallet.views.natives.spinners + +import android.app.Activity +import android.content.Context +import android.util.AttributeSet +import android.view.View +import com.jaredrummler.materialspinner.MaterialSpinner +import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ValidationField + +open class InternalMaterialSpinner : MaterialSpinner { + + /* + * Contains the current layout for the rows + * */ + protected var layout:Int? = null + + /* + * Listeners + * */ + private var onItemNotSelected: OnItemNotSelected? = null + + /* + * Contains the this for interfaces + * */ + protected var this_: View? = null + + + + + constructor(context: Context?, attrs: AttributeSet?) : super(context,attrs){ + + /* + * Save the current this + * */ + this_ = this + } + + /* + * Select the first item in the spinner + * */ + fun selectFirstItem(){ + this.selectedIndex = 0 + } + + /* + * Set the default android layout + * */ + fun setAndroidLayout(){ + this.layout = android.R.layout.simple_spinner_item + } + + fun onItemNotSelected(onItemNotSelected:OnItemNotSelected){ + this.onItemNotSelected = onItemNotSelected + } + + /* + * Validation with listener for item selected or not + * */ + fun validateOnItemNotSelected(){ + if(this.selectedIndex == -1){ + if(onItemNotSelected != null){ + onItemNotSelected?.onItemNotSelected() + } + } + } + + /* + * Interface for validation failed + * */ + interface OnValidationFailed{ + fun onValidationFailed(field: ValidationField) + } + interface OnValidationSucceeded{ + fun onValidationSucceeded(field: ValidationField) + } + interface OnItemNotSelected{ + fun onItemNotSelected() + } +} \ No newline at end of file