keyboards refactoring

hh
Laurent 6 years ago
parent 20f8151394
commit c206354dae
  1. 6
      app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt
  2. 1
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt
  3. 27
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt
  4. 26
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt
  5. 69
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt

@ -146,7 +146,9 @@ class HHBuilder {
* If the user changes the current action,
* for convenience we remove all the following actions to avoid managing complex cases
* Also calculates the player effective amounts in proper cases
* Returns the list of modified action indexes that are not the action at [index]
* Returns either:
* - null when actions have been deleted, requiring a whole table refresh, because streets might me lost
* - the list of modified action indexes that are not the action at [index]
*/
fun selectAction(index: Int, actionType: Action.Type) : List<Int>? {
@ -176,7 +178,7 @@ class HHBuilder {
else -> {}
}
val dropedIndex = dropNextActionsIfNecessary(index) // TODO returns the value
val dropedIndex = dropNextActionsIfNecessary(index)
// Automatically sets action for the previous empty actions
val modifiedActions = mutableListOf<ComputedAction>()

@ -220,7 +220,6 @@ class HandHistoryAdapter(
if (selected) {
amountEditText.requestFocus()
// requestFocusAndShowKeyboard(amountEditText)
} else {
amountEditText.clearFocus()
}

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.EditText
import androidx.lifecycle.ViewModelProviders
import kotlinx.android.synthetic.main.fragment_hand_history.*
@ -35,8 +34,6 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
private var rows: List<RowRepresentable> = listOf()
private var inputConnection: InputConnection? = null
companion object {
fun newInstance(id: String? = null): HandHistoryFragment {
@ -117,7 +114,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
Timber.d("Current selection is ${selection.index} / ${selection.keyboard}")
retrieveEditTextInputConnection(selection.index)
} ?: run {
this.inputConnection = null
this.keyboard.setInputConnection(null)
}
}
@ -138,8 +135,9 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
val holder = recyclerView.findViewHolderForAdapterPosition(position) as? HandHistoryAdapter.RowHandAction
holder?.let {
this.inputConnection = it.itemView.findViewById<EditText>(R.id.amountEditText)
val inputConnection = it.itemView.findViewById<EditText>(R.id.amountEditText)
.onCreateInputConnection(EditorInfo())
this.keyboard.setInputConnection(inputConnection)
Timber.d("***** inputConnection = $inputConnection")
} ?: run {
Timber.d("no holder, or not RowHandAction")
@ -286,26 +284,15 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
}
override fun closeKeyboard() {
this.model.currentSelection.value?.index?.let {
this.handHistoryAdapter.notifyItemChanged(it)
this.model.currentSelection.value = null
}
this.keyboard?.hide()
}
override fun amountKeyTyped(key: NumericKey) {
this.model.amountKeyTyped(key)
this.inputConnection?.let {
when {
key.isBackSpace -> { it.deleteSurroundingText(1, 0) }
key.isDecimalSeparator -> {
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) }
}
}
}
// Table refresh

@ -2,6 +2,7 @@ package net.pokeranalytics.android.ui.modules.handhistory.views
import android.content.Context
import android.view.LayoutInflater
import android.view.inputmethod.InputConnection
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.view_hand_keyboard_amount.view.*
import net.pokeranalytics.android.R
@ -13,6 +14,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 java.text.DecimalFormatSymbols
class NumericKey : RowRepresentable {
@ -59,6 +61,8 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
private var dataAdapter: RowRepresentableAdapter
var inputConnection: InputConnection? = null
init {
LayoutInflater.from(context)
.inflate(R.layout.view_hand_keyboard_amount, this, true)
@ -106,7 +110,27 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context),
}
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
this.keyboardListener?.amountKeyTyped(row as NumericKey)
val key = row as NumericKey
this.keyboardListener?.amountKeyTyped(key)
this.inputConnection?.let {
when {
key.isBackSpace -> { it.deleteSurroundingText(1, 0) }
key.isDecimalSeparator -> {
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) }
}
} ?: run {
throw PAIllegalStateException("Requires an input connection to handle key selections")
}
}
}

@ -3,9 +3,11 @@ 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.FrameLayout
import androidx.core.view.isVisible
import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.handhistory.HHKeyboard
import net.pokeranalytics.android.model.realm.handhistory.Action
import net.pokeranalytics.android.model.realm.handhistory.Card
@ -23,14 +25,38 @@ interface KeyboardListener {
class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
var keyboardListener: KeyboardListener? = null
val keyboards = HashMap<HHKeyboard, AbstractKeyboardView>()
private val keyboards = HashMap<HHKeyboard, AbstractKeyboardView>()
// private lateinit var constraintLayout: ConstraintLayout
var keyboardListener: KeyboardListener? = null
set(value) {
field = value
HHKeyboard.values().forEach {
this.keyboards[it]?.keyboardListener = value
}
}
init {
this.setBackgroundColor(context.getColor(R.color.kaki_darker))
createKeyboards()
}
private fun createKeyboards() {
HHKeyboard.values().forEach {
val view = when (it) {
HHKeyboard.ACTION -> {
KeyboardActionView(context)
}
HHKeyboard.AMOUNT -> {
KeyboardAmountView(context)
}
HHKeyboard.CARD -> {
KeyboardCardView(context)
}
}
this.keyboards[it] = view
addView(view)
}
}
private fun show() {
@ -52,20 +78,24 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
show()
var view = this.keyboards[type]
if (view == null) {
view = when(type) {
HHKeyboard.ACTION -> { KeyboardActionView(context) }
HHKeyboard.AMOUNT -> { KeyboardAmountView(context) }
HHKeyboard.CARD -> { KeyboardCardView(context) }
}
view.keyboardListener = this.keyboardListener
this.keyboards[type] = view
addView(view)
// var view = this.keyboards[type]
//
// if (view == null) {
// view = when(type) {
// HHKeyboard.ACTION -> { KeyboardActionView(context) }
// HHKeyboard.AMOUNT -> { KeyboardAmountView(context) }
// HHKeyboard.CARD -> { KeyboardCardView(context) }
// }
// view.keyboardListener = this.keyboardListener
// this.keyboards[type] = view
// addView(view)
// }
this.keyboards[type]?.let {
show(it)
} ?: run {
throw PAIllegalStateException("missing keyboard")
}
show(view)
}
private fun show(keyboardView: AbstractKeyboardView) {
@ -74,6 +104,11 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
}
}
fun setInputConnection(inputConnection: InputConnection?) {
val amountKeyboard = this.keyboards[HHKeyboard.AMOUNT] as KeyboardAmountView
amountKeyboard.inputConnection = inputConnection
}
// private fun loadView(layoutId: Int) {
// val layoutInflater = LayoutInflater.from(context)
// constraintLayout = layoutInflater.inflate(layoutId, this, false) as ConstraintLayout

Loading…
Cancel
Save