From c206354dae41165e28226d745557ace0d120787f Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 24 Jan 2020 10:58:49 +0100 Subject: [PATCH] keyboards refactoring --- .../android/model/handhistory/HHBuilder.kt | 6 +- .../modules/handhistory/HandHistoryAdapter.kt | 1 - .../handhistory/HandHistoryFragment.kt | 27 ++------ .../handhistory/views/KeyboardAmountView.kt | 26 ++++++- .../handhistory/views/KeyboardContainer.kt | 69 ++++++++++++++----- 5 files changed, 88 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt b/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt index 6d8c17b4..b66beb2c 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.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? { @@ -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() diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt index be205e3e..a583c6c6 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt @@ -220,7 +220,6 @@ class HandHistoryAdapter( if (selected) { amountEditText.requestFocus() -// requestFocusAndShowKeyboard(amountEditText) } else { amountEditText.clearFocus() } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt index 7a57fcca..63babc57 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt @@ -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 = 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(R.id.amountEditText) + val inputConnection = it.itemView.findViewById(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 diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt index 4b4ac39b..f854a1ee 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardAmountView.kt @@ -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") + } + } } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt index fa527ecf..6dee9118 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardContainer.kt @@ -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() + private val keyboards = HashMap() -// 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