Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.quantfury.business.utils
- import android.content.res.ColorStateList
- import android.content.res.Resources
- import android.text.Editable
- import android.text.TextWatcher
- import android.util.TypedValue
- import android.widget.EditText
- import androidx.core.content.res.ResourcesCompat
- import com.google.android.material.textfield.TextInputLayout
- import com.quantfury.R
- import com.quantfury.business.appModels.Theme
- import com.quantfury.business.extensions.isCurrency
- import com.quantfury.business.models.Instrument
- import com.quantfury.fragments.holders.StopOrderTargetOrderFragment
- import com.quantfury.ui.QuantfuryTextInputLayout
- import com.quantfury.utils.Common
- import com.quantfury.utils.asNumber
- import com.quantfury.utils.kotlin.getDimension
- import java.math.BigDecimal
- fun EditText?.setZerosAndClearFocus(instrument: Instrument, fraction: Int = instrument.round) {
- val text = this?.text
- if (text.isNullOrEmpty()) {
- this?.clearFocus()
- return
- }
- val profitTakenText =
- this?.text.toString().replace(instrument.currency.symbol, "").replace(",", "")
- if (profitTakenText.isNotEmpty()) {
- val takeProfitValue = profitTakenText.parseAsDouble()
- if (takeProfitValue != null) {
- this?.setText(takeProfitValue.asNumber(fraction))
- }
- }
- this?.clearFocus()
- }
- fun EditText?.setZerosAndClearFocusAmount(instrument: Instrument) {
- if (instrument.isCurrency()) {
- setZerosAndClearFocus(instrument, instrument.baseCurrency.fraction)
- } else {
- setZerosAndClearFocus(instrument, 0)
- }
- }
- fun EditText?.parseEditTextAsBigDecimal(instrument: Instrument): BigDecimal? {
- if (this == null) return null
- val currency = instrument.currency.symbol
- val profitTakenText = this.text.toString().replace(currency, "")
- return profitTakenText.parseAsDouble()
- }
- fun String.parseAsDouble(): BigDecimal? {
- try {
- return if (this.isNotEmpty() && this != "." && this != ",") {
- this.replace(",", "").toBigDecimal()
- } else {
- null
- }
- } catch (e: NumberFormatException) {
- return null
- }
- }
- fun EditText?.updateEditTextStyle() {
- if (this == null) return
- if (!this.text.isNullOrEmpty() || this.hasFocus()) {
- this.typeface =
- ResourcesCompat.getFont(this.context, R.font.roboto_mono_regular_qf)
- this.setTextSize(
- TypedValue.COMPLEX_UNIT_PX,
- this.getDimension(R.dimen.text_size_14)
- )
- } else {
- this.typeface = ResourcesCompat.getFont(this.context, R.font.uni_sans_regular)
- this.setTextSize(
- TypedValue.COMPLEX_UNIT_PX,
- this.getDimension(R.dimen.text_size_12)
- )
- }
- }
- fun EditText?.setMoneyDecimalInputFilter(instrument: Instrument, fraction: Int = instrument.round) {
- val currency = InstrumentUtils.getInstrumentCurrency(instrument)
- this?.addTextChangedListener(object : TextWatcher {
- var ignoreChanges: Boolean = false
- override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
- override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
- if (ignoreChanges) return
- ignoreChanges = true
- var text = s.toString()
- if (countDot(text) == 2) {
- if (start <= StopOrderTargetOrderFragment.limitNumbers + 1) {
- for (i in 0 until text.length) {
- if (i == start) continue
- if (text[i] == '.') {
- text = StringBuilder(text).deleteCharAt(i).toString()
- this@setMoneyDecimalInputFilter.setSelection(text.indexOf('.') + 1)
- break
- }
- }
- } else {
- text = StringBuilder(text).deleteCharAt(start).toString()
- this@setMoneyDecimalInputFilter.setSelection(start)
- }
- }
- text = text.replace(currency, "")
- if (text.isNotEmpty()) {
- val modifiedText = clearBeforeDot(clearAfterDot(text, fraction))
- text = if (text.length == 1 && text == ".") {
- ""
- } else {
- currency + modifiedText
- }
- }
- if (text != s.toString()) {
- this@setMoneyDecimalInputFilter.setTextKeepState(text)
- if (start == 0 && count == 1) {
- this@setMoneyDecimalInputFilter.setSelection(text.length)
- }
- }
- ignoreChanges = false
- }
- override fun afterTextChanged(s: Editable) {}
- })
- }
- private fun countDot(string: String): Int {
- var count = 0
- for (i in 0 until string.length) {
- if (string[i] == '.') {
- count++
- }
- }
- return count
- }
- private fun clearAfterDot(text: String, fraction: Int): String {
- var resultText = text
- val index = resultText.indexOf('.')
- if (index != -1 && resultText.length - index - 1 > fraction) {
- resultText = resultText.substring(0, index + fraction + 1)
- }
- return resultText
- }
- private fun clearBeforeDot(text: String): String {
- var resultText = text
- val index = resultText.indexOf('.')
- var numberOfCommas = 0
- for (i in 0 until resultText.length) {
- if (resultText[i] == ',') {
- numberOfCommas++
- }
- }
- val limitBeforeDot = StopOrderTargetOrderFragment.limitNumbers + numberOfCommas
- if (index == -1 && resultText.length > limitBeforeDot) {
- resultText = resultText.substring(0, limitBeforeDot)
- }
- if (index != -1 && index > limitBeforeDot) {
- val split = resultText
- .split("\\.".toRegex())
- .dropLastWhile { it.isEmpty() }
- .toTypedArray()
- resultText = split[0].substring(0, limitBeforeDot) + "." + split[1]
- }
- return resultText
- }
- fun EditText?.setMoneyDecimalInputFilterWithIcon(instrument: Instrument, theme: Resources.Theme) {
- this?.addTextChangedListener(object : TextWatcher {
- var ignoreChanges: Boolean = false
- override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
- override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
- if (ignoreChanges) return
- ignoreChanges = true
- var text = s.toString()
- if (countDot(text) == 2) {
- if (start <= StopOrderTargetOrderFragment.limitNumbers + 1) {
- for (i in 0 until text.length) {
- if (i == start) continue
- if (text[i] == '.') {
- text = StringBuilder(text).deleteCharAt(i).toString()
- this@setMoneyDecimalInputFilterWithIcon.setSelection(text.indexOf('.') + 1)
- break
- }
- }
- } else {
- text = StringBuilder(text).deleteCharAt(start).toString()
- this@setMoneyDecimalInputFilterWithIcon.setSelection(start)
- }
- }
- val firstDigit = text.firstOrNull { it.isDigit() }
- if (firstDigit != null) {
- val index = text.indexOf(firstDigit)
- text = text.substring(index, text.length)
- } else {
- text = ""
- }
- if (text.isNotEmpty()) {
- text = clearBeforeDot(clearAfterDot(text, instrument.baseCurrency.fraction))
- }
- if (text.isNotEmpty()) {
- val finishText = Common.makeTitleWithImage(
- context!!,
- InstrumentUtils.getCryptoIcon(instrument.baseCurrency, 13),
- Theme.getResourceIdFromAttr(theme, R.attr.textColorPrimary),
- null,
- text
- )
- this@setMoneyDecimalInputFilterWithIcon.setTextKeepState(finishText)
- if (start == 0 && count == 1) {
- this@setMoneyDecimalInputFilterWithIcon.setSelection(finishText.length)
- }
- } else {
- this@setMoneyDecimalInputFilterWithIcon.setTextKeepState("")
- this@setMoneyDecimalInputFilterWithIcon.setSelection(0)
- }
- ignoreChanges = false
- }
- override fun afterTextChanged(s: Editable) {}
- })
- }
- fun QuantfuryTextInputLayout.setHintColor(color: Int) {
- try {
- val field = TextInputLayout::class.java.getDeclaredField("defaultHintTextColor")
- field.isAccessible = true
- val states = arrayOf(intArrayOf())
- val colors = intArrayOf(color)
- val myList = ColorStateList(states, colors)
- field.set(this, myList)
- val method = TextInputLayout::class.java
- .getDeclaredMethod("updateLabelState", Boolean::class.javaPrimitiveType)
- method.isAccessible = true
- method.invoke(this, true)
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement