Fixes keyboard and amount edition issues

hh
Laurent 6 years ago
parent c206354dae
commit 658792e93a
  1. 35
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt
  2. 14
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryViewModel.kt
  3. 24
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt
  4. 8
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt
  5. 11
      app/src/main/java/net/pokeranalytics/android/util/extensions/NumbersExtension.kt

@ -4,7 +4,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.lifecycle.ViewModelProviders
import kotlinx.android.synthetic.main.fragment_hand_history.*
@ -19,11 +18,11 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.RealmFragment
import net.pokeranalytics.android.ui.modules.handhistory.views.KeyboardListener
import net.pokeranalytics.android.ui.modules.handhistory.views.NumericKey
import net.pokeranalytics.android.ui.modules.handhistory.views.StreetCardHeader
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager
import net.pokeranalytics.android.util.extensions.findById
import net.pokeranalytics.android.util.extensions.noGroupingFormatted
import timber.log.Timber
class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepresentableDelegate, KeyboardListener {
@ -114,7 +113,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
Timber.d("Current selection is ${selection.index} / ${selection.keyboard}")
retrieveEditTextInputConnection(selection.index)
} ?: run {
this.keyboard.setInputConnection(null)
this.keyboard.setEditText(null, null)
}
}
@ -133,12 +132,12 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
private fun retrieveEditTextInputConnection(position: Int) {
val computedAction = this.rowRepresentableForPosition(position) as? ComputedAction
val holder = recyclerView.findViewHolderForAdapterPosition(position) as? HandHistoryAdapter.RowHandAction
holder?.let {
val inputConnection = it.itemView.findViewById<EditText>(R.id.amountEditText)
.onCreateInputConnection(EditorInfo())
this.keyboard.setInputConnection(inputConnection)
Timber.d("***** inputConnection = $inputConnection")
val amountEditText = it.itemView.findViewById<EditText>(R.id.amountEditText)
this.keyboard.setEditText(amountEditText, computedAction?.action?.amount)
} ?: run {
Timber.d("no holder, or not RowHandAction")
}
@ -194,23 +193,24 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
return
}
// if (tag == HHKeyboard.ACTION.ordinal) {
this.model.currentSelection.value?.index?.let { oldIndex ->
refreshCells(oldIndex)
}
// }
this.model.currentSelection.value?.index?.let { oldIndex ->
refreshCells(oldIndex)
}
this.model.currentSelection.value = HHSelection(position, HHKeyboard.values()[tag])
// scrolls to selected position
// this.recyclerView.scrollToPosition(position)
this.recyclerView.smoothScrollToPosition(position)
val keyboard = when (row) {
is ComputedAction -> {
when (tag) {
HHKeyboard.ACTION.ordinal -> HHKeyboard.ACTION
HHKeyboard.AMOUNT.ordinal -> HHKeyboard.AMOUNT
HHKeyboard.AMOUNT.ordinal -> {
Timber.d("amount = ${row.action.amount}, toString = ${row.action.amount?.noGroupingFormatted}")
this.model.currentAmount = row.action.amount?.noGroupingFormatted
HHKeyboard.AMOUNT
}
else -> throw PAIllegalStateException("Unmanaged tag value: $tag")
}
}
@ -226,6 +226,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
override fun onRowDeselected(position: Int, row: RowRepresentable) {
this.model.currentSelection.value = null
this.model.currentAmount = null
}
override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
@ -280,7 +281,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
override fun clearAmount() {
this.model.clearAmount()
this.refreshCurrentRow()
// this.refreshCurrentRow()
}
override fun closeKeyboard() {
@ -291,8 +292,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
this.keyboard?.hide()
}
override fun amountKeyTyped(key: NumericKey) {
this.model.amountKeyTyped(key)
override fun amountChanged(amount: String?) {
this.model.amountChanged(amount)
}
// Table refresh

@ -9,7 +9,6 @@ import net.pokeranalytics.android.model.handhistory.HHSelection
import net.pokeranalytics.android.model.realm.handhistory.Action
import net.pokeranalytics.android.model.realm.handhistory.Card
import net.pokeranalytics.android.model.realm.handhistory.CardProperty
import net.pokeranalytics.android.ui.modules.handhistory.views.NumericKey
import timber.log.Timber
class HandHistoryViewModel : ViewModel() {
@ -68,6 +67,7 @@ class HandHistoryViewModel : ViewModel() {
fun clearAmount() {
builder.value?.clearAmount(this.actionIndexForSelection)
this.currentAmount = null
}
fun cardValueSelected(value: Int) {
@ -91,16 +91,8 @@ class HandHistoryViewModel : ViewModel() {
} ?: throw PAIllegalStateException("Builder not defined")
}
fun amountKeyTyped(key: NumericKey) {
var string = this.currentAmount
if (string == null && !key.isBackSpace) {
string = key.value
} else if (key.isBackSpace) {
string?.dropLast(1)
} else if (!(key.isDecimalSeparator && string!!.contains(key.value))) {
string += key.value
}
this.currentAmount = string
fun amountChanged(amount: String?) {
this.currentAmount = amount
}
}

@ -2,7 +2,9 @@ package net.pokeranalytics.android.ui.modules.handhistory.views
import android.content.Context
import android.view.LayoutInflater
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.EditText
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.view_hand_keyboard_amount.view.*
import net.pokeranalytics.android.R
@ -14,7 +16,7 @@ import net.pokeranalytics.android.ui.extensions.px
import net.pokeranalytics.android.ui.view.GridSpacingItemDecoration
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import timber.log.Timber
import net.pokeranalytics.android.util.extensions.noGroupingFormatted
import java.text.DecimalFormatSymbols
class NumericKey : RowRepresentable {
@ -61,7 +63,9 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
private var dataAdapter: RowRepresentableAdapter
var inputConnection: InputConnection? = null
private var editText: EditText? = null
private var inputConnection: InputConnection? = null
init {
LayoutInflater.from(context)
@ -86,6 +90,7 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
}
this.clearButton.setOnClickListener {
this.editText?.text = null
this.keyboardListener?.clearAmount()
}
@ -94,6 +99,14 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
}
}
fun setEditText(editText: EditText?, amount: Double?) {
this.editText = editText
editText?.setText(amount?.noGroupingFormatted)
this.inputConnection = editText?.onCreateInputConnection(EditorInfo())
}
override fun adapterRows(): List<RowRepresentable>? {
val keys = mutableListOf<NumericKey>()
(0 until 12).forEach { index ->
@ -113,8 +126,6 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
val key = row as NumericKey
this.keyboardListener?.amountKeyTyped(key)
this.inputConnection?.let {
when {
@ -123,10 +134,13 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
val text = it.getSelectedText(0)
if (text != null && !text.contains(key.value)) {
it.commitText(key.value, 1)
} else { false }
}
}
else -> { it.commitText(key.value, 1) }
}
this.keyboardListener?.amountChanged(this.editText?.text.toString())
} ?: run {
throw PAIllegalStateException("Requires an input connection to handle key selections")
}

@ -3,7 +3,7 @@ package net.pokeranalytics.android.ui.modules.handhistory.views
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.inputmethod.InputConnection
import android.widget.EditText
import android.widget.FrameLayout
import androidx.core.view.isVisible
import net.pokeranalytics.android.R
@ -15,7 +15,7 @@ import net.pokeranalytics.android.model.realm.handhistory.Card
interface KeyboardListener {
fun actionSelected(action: Action.Type)
fun amountKeyTyped(key: NumericKey)
fun amountChanged(amount: String?)
fun cardValueSelected(value: Int)
fun cardSuitSelected(suit: Card.Suit)
fun amountValidated()
@ -104,9 +104,9 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
}
}
fun setInputConnection(inputConnection: InputConnection?) {
fun setEditText(editText: EditText?, amount: Double?) {
val amountKeyboard = this.keyboards[HHKeyboard.AMOUNT] as KeyboardAmountView
amountKeyboard.inputConnection = inputConnection
amountKeyboard.setEditText(editText, amount)
}
// private fun loadView(layoutId: Int) {

@ -2,7 +2,6 @@ package net.pokeranalytics.android.util.extensions
import android.content.Context
import net.pokeranalytics.android.R
import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.UserDefaults
import java.lang.Math.abs
import java.math.RoundingMode
@ -50,6 +49,16 @@ fun Double.formatted(): String {
return format.format(this)
}
val Double.noGroupingFormatted: String
get() {
val format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
format.minimumFractionDigits = 0
format.isGroupingUsed = false
return format.format(this)
}
fun Double.toCurrency(currency: Currency? = null): String {
val currencyFormatter = NumberFormat.getCurrencyInstance(Locale.getDefault())

Loading…
Cancel
Save