commit
506d095db2
@ -1,60 +0,0 @@ |
|||||||
package net.pokeranalytics.android.ui.view |
|
||||||
|
|
||||||
import android.content.Context |
|
||||||
import android.util.AttributeSet |
|
||||||
import androidx.appcompat.widget.AppCompatTextView |
|
||||||
import androidx.core.content.ContextCompat |
|
||||||
import net.pokeranalytics.android.R |
|
||||||
import net.pokeranalytics.android.util.toCurrency |
|
||||||
|
|
||||||
/** |
|
||||||
* Extended TextView to display amount in the right color / currency |
|
||||||
* Work in progress |
|
||||||
*/ |
|
||||||
class DynamicValueTextView: AppCompatTextView { |
|
||||||
|
|
||||||
var isCurrency: Boolean = false |
|
||||||
var amount: Double = 0.0 |
|
||||||
set(value) { |
|
||||||
field = value |
|
||||||
updateUI() |
|
||||||
} |
|
||||||
|
|
||||||
constructor(context: Context?) : super(context) { |
|
||||||
init(null) |
|
||||||
} |
|
||||||
|
|
||||||
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { |
|
||||||
init(attrs) |
|
||||||
} |
|
||||||
|
|
||||||
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { |
|
||||||
init(attrs) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Init class |
|
||||||
*/ |
|
||||||
private fun init(attrs: AttributeSet?) { |
|
||||||
context.theme.obtainStyledAttributes( attrs, R.styleable.DynamicValueTextView, 0, 0).apply { |
|
||||||
try { |
|
||||||
isCurrency = getBoolean(R.styleable.DynamicValueTextView_isCurrency, false) |
|
||||||
amount = getFloat(R.styleable.DynamicValueTextView_amount, 0f).toDouble() |
|
||||||
updateUI() |
|
||||||
} finally { |
|
||||||
recycle() |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Update the UI to manage currency format & color management |
|
||||||
*/ |
|
||||||
private fun updateUI() { |
|
||||||
if (isCurrency) { |
|
||||||
setTextColor(ContextCompat.getColor(context, if (amount >= 0) R.color.green else R.color.red)) |
|
||||||
text = amount.toCurrency() |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,47 @@ |
|||||||
|
package net.pokeranalytics.android.ui.view |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import android.util.AttributeSet |
||||||
|
import androidx.appcompat.widget.AppCompatTextView |
||||||
|
import net.pokeranalytics.android.calculus.TextFormat |
||||||
|
|
||||||
|
/** |
||||||
|
* Extended TextView to display a TextFormat object |
||||||
|
*/ |
||||||
|
class FormattedTextView: AppCompatTextView { |
||||||
|
|
||||||
|
var textFormat: TextFormat? = null |
||||||
|
set(value) { |
||||||
|
field = value |
||||||
|
updateUI() |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?) : super(context) { |
||||||
|
init(null) |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { |
||||||
|
init(attrs) |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { |
||||||
|
init(attrs) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Init class |
||||||
|
*/ |
||||||
|
private fun init(attrs: AttributeSet?) { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the UI to manage currency format & color management |
||||||
|
*/ |
||||||
|
private fun updateUI() { |
||||||
|
textFormat?.let { |
||||||
|
setTextColor(it.getColor(context)) |
||||||
|
text = it.text |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,6 +1,6 @@ |
|||||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||||
<resources> |
<resources> |
||||||
<declare-styleable name="DynamicValueTextView"> |
<declare-styleable name="FormattedTextView"> |
||||||
<attr name="isCurrency" format="boolean" /> |
<attr name="isCurrency" format="boolean" /> |
||||||
<attr name="amount" format="float" /> |
<attr name="amount" format="float" /> |
||||||
</declare-styleable> |
</declare-styleable> |
||||||
|
|||||||
Loading…
Reference in new issue