Custom numeric keyboard management - unfinished

hh
Laurent 6 years ago
parent e7782599e1
commit 4c91718967
  1. 10
      app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt
  2. 7
      app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt
  3. 64
      app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt
  4. 31
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/HandHistoryViewModel.kt

@ -93,12 +93,12 @@ class ComputedAction(var action: Action,
*/ */
val actionTypeCanBeEdited: Boolean val actionTypeCanBeEdited: Boolean
get() { get() {
val actionCanBeEdited = when (this.action.type) { return when (this.action.type) {
Action.Type.POST_SB, Action.Type.POST_BB -> false Action.Type.POST_SB, Action.Type.POST_BB -> false
else -> true else -> true
} }
Timber.d("Can be edited = $actionCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}") // Timber.d("Can be edited = $actionCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}")
return actionCanBeEdited // return actionCanBeEdited
} }
/*** /***
@ -106,14 +106,14 @@ class ComputedAction(var action: Action,
*/ */
val amountCanBeEdited: Boolean val amountCanBeEdited: Boolean
get() { get() {
val amountCanBeEdited = when (this.action.type) { return when (this.action.type) {
Action.Type.POST_SB, Action.Type.POST_BB, Action.Type.POST_SB, Action.Type.POST_BB,
Action.Type.BET, Action.Type.RAISE -> true Action.Type.BET, Action.Type.RAISE -> true
Action.Type.BET_ALLIN, Action.Type.RAISE_ALLIN -> (this.playerRemainingStack == null) Action.Type.BET_ALLIN, Action.Type.RAISE_ALLIN -> (this.playerRemainingStack == null)
else -> false else -> false
} }
// Timber.d("Can be edited = $amountCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}") // Timber.d("Can be edited = $amountCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}")
return amountCanBeEdited // return amountCanBeEdited
} }
} }

@ -114,7 +114,7 @@ class HandHistoryAdapter(
amountEditText.isFocusableInTouchMode = true amountEditText.isFocusableInTouchMode = true
amountEditText.setOnTouchListener { v, event -> amountEditText.setOnTouchListener { v, event ->
Timber.d("touch action = ${event.action}") // Timber.d("touch action = ${event.action}")
if (this.amountCanBeEdited) { if (this.amountCanBeEdited) {
if (event.action == MotionEvent.ACTION_UP) { if (event.action == MotionEvent.ACTION_UP) {
@ -164,7 +164,7 @@ class HandHistoryAdapter(
override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) {
// Timber.d("onbind @position = $position") Timber.d("onbind @position = $position")
this.currentPosition = position this.currentPosition = position
@ -212,7 +212,8 @@ class HandHistoryAdapter(
amountEditText.setText(computedAction.action.displayedFormattedAmount) amountEditText.setText(computedAction.action.displayedFormattedAmount)
if (selected) { if (selected) {
requestFocusAndShowKeyboard(amountEditText) amountEditText.requestFocus()
// requestFocusAndShowKeyboard(amountEditText)
} else { } else {
amountEditText.clearFocus() amountEditText.clearFocus()
} }

@ -4,6 +4,9 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.EditText
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import kotlinx.android.synthetic.main.fragment_hand_history.* import kotlinx.android.synthetic.main.fragment_hand_history.*
import kotlinx.android.synthetic.main.fragment_settings.recyclerView import kotlinx.android.synthetic.main.fragment_settings.recyclerView
@ -34,6 +37,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
private var rows: List<RowRepresentable> = listOf() private var rows: List<RowRepresentable> = listOf()
private var inputConnection: InputConnection? = null
companion object { companion object {
fun newInstance(id: String? = null): HandHistoryFragment { fun newInstance(id: String? = null): HandHistoryFragment {
@ -64,6 +69,13 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
initData() initData()
initUI() initUI()
this.edit()
this.model.currentSelection.value?.index?.let {
Timber.d(">>>> attempt to retrieveEditTextInputConnection")
this.retrieveEditTextInputConnection(it)
}
} }
private fun initData() { private fun initData() {
@ -77,6 +89,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
this.model.setBuilder(builder) this.model.setBuilder(builder)
this.rows = this.model.builder.value?.rowRepresentables() ?: listOf()
} }
private fun initUI() { private fun initUI() {
@ -87,7 +101,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
// SmoothScrollLinearLayoutManager(requireContext()) // SmoothScrollLinearLayoutManager(requireContext())
// val viewManager = LinearLayoutManager(requireContext()) // val viewManager = LinearLayoutManager(requireContext())
handHistoryAdapter = HandHistoryAdapter(this, this) this.handHistoryAdapter = HandHistoryAdapter(this, this)
recyclerView.apply { recyclerView.apply {
setHasFixedSize(true) setHasFixedSize(true)
@ -95,11 +109,25 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
adapter = handHistoryAdapter adapter = handHistoryAdapter
} }
this.rows = this.model.builder.value?.rowRepresentables() ?: listOf() this.model.currentSelection.observeForever { selection ->
Timber.d("Current selection is ${selection.index} / ${selection.keyboard}")
retrieveEditTextInputConnection(selection.index)
}
this.keyboard.keyboardListener = this this.keyboard.keyboardListener = this
this.edit() }
private fun retrieveEditTextInputConnection(position: Int) {
val holder = recyclerView.findViewHolderForAdapterPosition(position) as? HandHistoryAdapter.RowHandAction
holder?.let {
this.inputConnection = it.itemView.findViewById<EditText>(R.id.amountEditText)
.onCreateInputConnection(EditorInfo())
} ?: run {
Timber.d("no holder, or not RowHandAction")
}
} }
private fun edit() { private fun edit() {
@ -112,7 +140,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
private fun findNextActionToEdit() { private fun findNextActionToEdit() {
val selection = this.model.currentSelection val selection = this.model.currentSelection.value
val index = selection?.index ?: throw PAIllegalStateException("Request next with no selection") val index = selection?.index ?: throw PAIllegalStateException("Request next with no selection")
this.findNextActionToEdit(index, selection.keyboard) this.findNextActionToEdit(index, selection.keyboard)
} }
@ -152,12 +180,12 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
// if (tag == HHKeyboard.ACTION.ordinal) { // if (tag == HHKeyboard.ACTION.ordinal) {
this.model.currentSelection?.index?.let { oldIndex -> this.model.currentSelection.value?.index?.let { oldIndex ->
refreshCells(oldIndex) refreshCells(oldIndex)
} }
// } // }
this.model.currentSelection = HHSelection(position, HHKeyboard.values()[tag]) this.model.currentSelection.value = HHSelection(position, HHKeyboard.values()[tag])
// scrolls to selected position // scrolls to selected position
// this.recyclerView.scrollToPosition(position) // this.recyclerView.scrollToPosition(position)
@ -182,7 +210,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
override fun onRowDeselected(position: Int, row: RowRepresentable) { override fun onRowDeselected(position: Int, row: RowRepresentable) {
this.model.currentSelection = null this.model.currentSelection.value = null
} }
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
@ -191,8 +219,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
override fun isSelected(position: Int, row: RowRepresentable, tag: Int): Boolean { override fun isSelected(position: Int, row: RowRepresentable, tag: Int): Boolean {
val currentSelection = this.model.currentSelection val currentSelection = this.model.currentSelection
val isSelectedIndex = (position == currentSelection?.index) val isSelectedIndex = (position == currentSelection.value?.index)
val isSelectedAction = (tag == currentSelection?.keyboard?.ordinal) val isSelectedAction = (tag == currentSelection.value?.keyboard?.ordinal)
// Timber.d("position = $position, tag = $tag, current index = ${currentSelection?.index}, kb = ${currentSelection?.keyboard}") // Timber.d("position = $position, tag = $tag, current index = ${currentSelection?.index}, kb = ${currentSelection?.keyboard}")
return isSelectedIndex && isSelectedAction return isSelectedIndex && isSelectedAction
} }
@ -240,6 +268,22 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
override fun amountKeyTyped(key: NumericKey) { override fun amountKeyTyped(key: NumericKey) {
this.model.amountKeyTyped(key) 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 // Table refresh
@ -250,7 +294,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
private fun refreshCurrentRow() { private fun refreshCurrentRow() {
this.model.currentSelection?.index?.let { this.model.currentSelection.value?.index?.let {
Timber.d("refreshes row at index = $it") Timber.d("refreshes row at index = $it")
this.handHistoryAdapter.notifyItemChanged(it) this.handHistoryAdapter.notifyItemChanged(it)
} }

@ -16,15 +16,17 @@ class HandHistoryViewModel : ViewModel() {
var isEdited = true var isEdited = true
var currentSelection: HHSelection? = null var currentSelection: MutableLiveData<HHSelection> = MutableLiveData()
set(value) {
field = value // var currentSelection: HHSelection? = null
value?.let { // set(value) {
Timber.d("Current selection is ${it.index} / ${it.keyboard}") // field = value
} ?: run { // value?.let {
Timber.d("No selection") // Timber.d("Current selection is ${it.index} / ${it.keyboard}")
} // } ?: run {
} // Timber.d("No selection")
// }
// }
var currentAmount: String? = null var currentAmount: String? = null
@ -42,7 +44,7 @@ class HandHistoryViewModel : ViewModel() {
private val actionIndexForSelection: Int private val actionIndexForSelection: Int
get() { get() {
this.currentSelection?.let { selection -> this.currentSelection.value?.let { selection ->
return builder.value?.indexOfComputedAction(selection.index) ?: throw PAIllegalStateException("No builder") return builder.value?.indexOfComputedAction(selection.index) ?: throw PAIllegalStateException("No builder")
} ?: throw PAIllegalStateException("No selection") } ?: throw PAIllegalStateException("No selection")
} }
@ -68,7 +70,7 @@ class HandHistoryViewModel : ViewModel() {
fun cardValueSelected(value: Int) { fun cardValueSelected(value: Int) {
this.currentSelection?.let { selection -> this.currentSelection.value?.let { selection ->
selection.index selection.index
// get the appropriate card list, board or player's hand and give the information // get the appropriate card list, board or player's hand and give the information
} }
@ -81,13 +83,10 @@ class HandHistoryViewModel : ViewModel() {
} }
fun findIndexForEdition(startIndex: Int, keyboard: HHKeyboard? = null) : HHKeyboard? { fun findIndexForEdition(startIndex: Int, keyboard: HHKeyboard? = null) : HHKeyboard? {
builder.value?.let { builder -> builder.value?.let { builder ->
this.currentSelection = builder.findIndexForEdition(startIndex, keyboard) this.currentSelection.value = builder.findIndexForEdition(startIndex, keyboard)
return currentSelection.value?.keyboard
return currentSelection?.keyboard
} ?: throw PAIllegalStateException("Builder not defined") } ?: throw PAIllegalStateException("Builder not defined")
} }
fun amountKeyTyped(key: NumericKey) { fun amountKeyTyped(key: NumericKey) {

Loading…
Cancel
Save