Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. package com.quantfury.business.utils
  2.  
  3. import android.content.res.ColorStateList
  4. import android.content.res.Resources
  5. import android.text.Editable
  6. import android.text.TextWatcher
  7. import android.util.TypedValue
  8. import android.widget.EditText
  9. import androidx.core.content.res.ResourcesCompat
  10. import com.google.android.material.textfield.TextInputLayout
  11. import com.quantfury.R
  12. import com.quantfury.business.appModels.Theme
  13. import com.quantfury.business.extensions.isCurrency
  14. import com.quantfury.business.models.Instrument
  15. import com.quantfury.fragments.holders.StopOrderTargetOrderFragment
  16. import com.quantfury.ui.QuantfuryTextInputLayout
  17. import com.quantfury.utils.Common
  18. import com.quantfury.utils.asNumber
  19. import com.quantfury.utils.kotlin.getDimension
  20. import java.math.BigDecimal
  21.  
  22. fun EditText?.setZerosAndClearFocus(instrument: Instrument, fraction: Int = instrument.round) {
  23. val text = this?.text
  24. if (text.isNullOrEmpty()) {
  25. this?.clearFocus()
  26. return
  27. }
  28. val profitTakenText =
  29. this?.text.toString().replace(instrument.currency.symbol, "").replace(",", "")
  30. if (profitTakenText.isNotEmpty()) {
  31. val takeProfitValue = profitTakenText.parseAsDouble()
  32. if (takeProfitValue != null) {
  33. this?.setText(takeProfitValue.asNumber(fraction))
  34. }
  35. }
  36. this?.clearFocus()
  37. }
  38.  
  39. fun EditText?.setZerosAndClearFocusAmount(instrument: Instrument) {
  40. if (instrument.isCurrency()) {
  41. setZerosAndClearFocus(instrument, instrument.baseCurrency.fraction)
  42. } else {
  43. setZerosAndClearFocus(instrument, 0)
  44. }
  45. }
  46.  
  47. fun EditText?.parseEditTextAsBigDecimal(instrument: Instrument): BigDecimal? {
  48. if (this == null) return null
  49. val currency = instrument.currency.symbol
  50. val profitTakenText = this.text.toString().replace(currency, "")
  51. return profitTakenText.parseAsDouble()
  52. }
  53.  
  54. fun String.parseAsDouble(): BigDecimal? {
  55. try {
  56. return if (this.isNotEmpty() && this != "." && this != ",") {
  57. this.replace(",", "").toBigDecimal()
  58. } else {
  59. null
  60. }
  61. } catch (e: NumberFormatException) {
  62. return null
  63. }
  64. }
  65.  
  66. fun EditText?.updateEditTextStyle() {
  67. if (this == null) return
  68. if (!this.text.isNullOrEmpty() || this.hasFocus()) {
  69. this.typeface =
  70. ResourcesCompat.getFont(this.context, R.font.roboto_mono_regular_qf)
  71. this.setTextSize(
  72. TypedValue.COMPLEX_UNIT_PX,
  73. this.getDimension(R.dimen.text_size_14)
  74. )
  75. } else {
  76. this.typeface = ResourcesCompat.getFont(this.context, R.font.uni_sans_regular)
  77. this.setTextSize(
  78. TypedValue.COMPLEX_UNIT_PX,
  79. this.getDimension(R.dimen.text_size_12)
  80. )
  81. }
  82. }
  83.  
  84. fun EditText?.setMoneyDecimalInputFilter(instrument: Instrument, fraction: Int = instrument.round) {
  85. val currency = InstrumentUtils.getInstrumentCurrency(instrument)
  86. this?.addTextChangedListener(object : TextWatcher {
  87. var ignoreChanges: Boolean = false
  88.  
  89. override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
  90.  
  91. override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
  92. if (ignoreChanges) return
  93. ignoreChanges = true
  94. var text = s.toString()
  95.  
  96. if (countDot(text) == 2) {
  97. if (start <= StopOrderTargetOrderFragment.limitNumbers + 1) {
  98. for (i in 0 until text.length) {
  99. if (i == start) continue
  100. if (text[i] == '.') {
  101. text = StringBuilder(text).deleteCharAt(i).toString()
  102. this@setMoneyDecimalInputFilter.setSelection(text.indexOf('.') + 1)
  103. break
  104. }
  105. }
  106. } else {
  107. text = StringBuilder(text).deleteCharAt(start).toString()
  108. this@setMoneyDecimalInputFilter.setSelection(start)
  109. }
  110. }
  111.  
  112. text = text.replace(currency, "")
  113. if (text.isNotEmpty()) {
  114. val modifiedText = clearBeforeDot(clearAfterDot(text, fraction))
  115. text = if (text.length == 1 && text == ".") {
  116. ""
  117. } else {
  118. currency + modifiedText
  119. }
  120. }
  121. if (text != s.toString()) {
  122. this@setMoneyDecimalInputFilter.setTextKeepState(text)
  123. if (start == 0 && count == 1) {
  124. this@setMoneyDecimalInputFilter.setSelection(text.length)
  125. }
  126. }
  127. ignoreChanges = false
  128. }
  129.  
  130. override fun afterTextChanged(s: Editable) {}
  131. })
  132. }
  133.  
  134. private fun countDot(string: String): Int {
  135. var count = 0
  136. for (i in 0 until string.length) {
  137. if (string[i] == '.') {
  138. count++
  139. }
  140. }
  141. return count
  142. }
  143.  
  144. private fun clearAfterDot(text: String, fraction: Int): String {
  145. var resultText = text
  146. val index = resultText.indexOf('.')
  147. if (index != -1 && resultText.length - index - 1 > fraction) {
  148. resultText = resultText.substring(0, index + fraction + 1)
  149. }
  150. return resultText
  151. }
  152.  
  153. private fun clearBeforeDot(text: String): String {
  154. var resultText = text
  155. val index = resultText.indexOf('.')
  156. var numberOfCommas = 0
  157. for (i in 0 until resultText.length) {
  158. if (resultText[i] == ',') {
  159. numberOfCommas++
  160. }
  161. }
  162.  
  163. val limitBeforeDot = StopOrderTargetOrderFragment.limitNumbers + numberOfCommas
  164. if (index == -1 && resultText.length > limitBeforeDot) {
  165. resultText = resultText.substring(0, limitBeforeDot)
  166. }
  167. if (index != -1 && index > limitBeforeDot) {
  168. val split = resultText
  169. .split("\\.".toRegex())
  170. .dropLastWhile { it.isEmpty() }
  171. .toTypedArray()
  172. resultText = split[0].substring(0, limitBeforeDot) + "." + split[1]
  173. }
  174. return resultText
  175. }
  176.  
  177. fun EditText?.setMoneyDecimalInputFilterWithIcon(instrument: Instrument, theme: Resources.Theme) {
  178.  
  179. this?.addTextChangedListener(object : TextWatcher {
  180. var ignoreChanges: Boolean = false
  181.  
  182. override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
  183.  
  184. override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
  185. if (ignoreChanges) return
  186. ignoreChanges = true
  187. var text = s.toString()
  188.  
  189. if (countDot(text) == 2) {
  190. if (start <= StopOrderTargetOrderFragment.limitNumbers + 1) {
  191. for (i in 0 until text.length) {
  192. if (i == start) continue
  193. if (text[i] == '.') {
  194. text = StringBuilder(text).deleteCharAt(i).toString()
  195. this@setMoneyDecimalInputFilterWithIcon.setSelection(text.indexOf('.') + 1)
  196. break
  197. }
  198. }
  199. } else {
  200. text = StringBuilder(text).deleteCharAt(start).toString()
  201. this@setMoneyDecimalInputFilterWithIcon.setSelection(start)
  202. }
  203. }
  204.  
  205. val firstDigit = text.firstOrNull { it.isDigit() }
  206. if (firstDigit != null) {
  207. val index = text.indexOf(firstDigit)
  208. text = text.substring(index, text.length)
  209. } else {
  210. text = ""
  211. }
  212.  
  213. if (text.isNotEmpty()) {
  214. text = clearBeforeDot(clearAfterDot(text, instrument.baseCurrency.fraction))
  215. }
  216.  
  217. if (text.isNotEmpty()) {
  218. val finishText = Common.makeTitleWithImage(
  219. context!!,
  220. InstrumentUtils.getCryptoIcon(instrument.baseCurrency, 13),
  221. Theme.getResourceIdFromAttr(theme, R.attr.textColorPrimary),
  222. null,
  223. text
  224. )
  225. this@setMoneyDecimalInputFilterWithIcon.setTextKeepState(finishText)
  226. if (start == 0 && count == 1) {
  227. this@setMoneyDecimalInputFilterWithIcon.setSelection(finishText.length)
  228. }
  229. } else {
  230. this@setMoneyDecimalInputFilterWithIcon.setTextKeepState("")
  231. this@setMoneyDecimalInputFilterWithIcon.setSelection(0)
  232. }
  233. ignoreChanges = false
  234. }
  235.  
  236. override fun afterTextChanged(s: Editable) {}
  237. })
  238. }
  239.  
  240. fun QuantfuryTextInputLayout.setHintColor(color: Int) {
  241. try {
  242. val field = TextInputLayout::class.java.getDeclaredField("defaultHintTextColor")
  243. field.isAccessible = true
  244. val states = arrayOf(intArrayOf())
  245. val colors = intArrayOf(color)
  246. val myList = ColorStateList(states, colors)
  247. field.set(this, myList)
  248.  
  249. val method = TextInputLayout::class.java
  250. .getDeclaredMethod("updateLabelState", Boolean::class.javaPrimitiveType)
  251. method.isAccessible = true
  252. method.invoke(this, true)
  253.  
  254. } catch (e: Exception) {
  255. e.printStackTrace()
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement