From 5799a17fa919d909abb3b6f895e700be487db657 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 17 Jan 2020 18:35:57 +0100 Subject: [PATCH] HH progress --- .../model/handhistory/ComputedAction.kt | 24 +++++- .../android/model/handhistory/HHBuilder.kt | 17 +++- .../android/model/realm/handhistory/Action.kt | 10 +++ .../model/realm/handhistory/HandHistory.kt | 8 +- .../android/ui/adapter/HandHistoryAdapter.kt | 77 ++++++++----------- .../ui/fragment/HandHistoryFragment.kt | 20 ++--- .../ui/view/handhistory/KeyboardContainer.kt | 4 +- .../ui/viewmodel/HandHistoryViewModel.kt | 4 +- app/src/main/res/layout/row_hand_action.xml | 19 ++--- 9 files changed, 105 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt b/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt index 63398ad0..53b53df6 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt @@ -62,6 +62,28 @@ class ComputedAction(var action: Action, * SB / BB cannot have their action type edited */ val actionTypeCanBeEdited: Boolean - get() { return action.index > 1 } + get() { + val actionCanBeEdited = when (this.action.type) { + Action.Type.POST_SB, Action.Type.POST_BB -> false + else -> true + } + Timber.d("Can be edited = $actionCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}") + return actionCanBeEdited + } + + /*** + * Returns whether the amount can be edited + */ + val amountCanBeEdited: Boolean + get() { + val amountCanBeEdited = when (this.action.type) { + Action.Type.POST_SB, Action.Type.POST_BB, + Action.Type.BET, Action.Type.RAISE -> true + Action.Type.BET_ALLIN, Action.Type.RAISE_ALLIN -> (this.playerRemainingStack == null) + else -> false + } +// Timber.d("Can be edited = $amountCanBeEdited, action = ${this.action.type}, pure index = ${this.action.index}") + return amountCanBeEdited + } } \ No newline at end of file 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 28be0aa8..8ff28fc3 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 @@ -152,7 +152,10 @@ class HHBuilder { Timber.d(">>> Sets $actionType at index: $index") val computedAction = this.actionForIndex(index) - computedAction.action.type = actionType + if (computedAction.action.type != actionType) { + computedAction.action.type = actionType + computedAction.action.amount = null + } dropNextActionsIfNecessary(index) @@ -328,15 +331,21 @@ class HHBuilder { /*** * Finds the index of the first incomplete action, if existing + * If the same selection is the same than current, pass to the next row */ - fun findIndexForEdition(startIndex: Int): HHSelection? { + fun findIndexForEdition(startIndex: Int, keyboard: HHKeyboard? = null): HHSelection? { this.currentRowRepresentables.forEachIndexed { index, rowRepresentable -> if (index >= startIndex && rowRepresentable is HandHistoryRow) { // Timber.d("Check keyboard for index = $index") - rowRepresentable.keyboardForCompletion()?.let { keyboard -> - return HHSelection(index, keyboard) + val foundKeyboard = rowRepresentable.keyboardForCompletion() + if (foundKeyboard != null && !(index == startIndex && keyboard == foundKeyboard)) { + return HHSelection(index, foundKeyboard) } + +// rowRepresentable.keyboardForCompletion()?.let { keyboard -> +// return HHSelection(index, keyboard) +// } } } return null diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt index a7691184..e9954ee1 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt @@ -5,6 +5,7 @@ import net.pokeranalytics.android.R import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowViewType +import net.pokeranalytics.android.util.extensions.formatted open class Action : RealmObject() { @@ -85,4 +86,13 @@ open class Action : RealmObject() { return this.type?.isSignificant ?: false } + val displayedFormattedAmount: String? + get() { + val amount = when (type) { + Type.CALL, Type.CALL_ALLIN -> this.effectiveAmount + else -> this.amount + } + return amount?.formatted() + } + } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt index eb3cb0b9..76a6b297 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt @@ -107,10 +107,10 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab action.type = Action.Type.POST_SB action.amount = this.smallBlind } - 1 -> { - action.type = Action.Type.POST_BB - action.amount = this.bigBlind - } +// 1 -> { +// action.type = Action.Type.POST_BB +// action.amount = this.bigBlind +// } else -> {} } this.actions.add(action) diff --git a/app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt index b062bdac..72f88502 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt @@ -1,6 +1,7 @@ package net.pokeranalytics.android.ui.adapter import android.text.Editable +import android.text.InputType import android.text.TextWatcher import android.view.LayoutInflater import android.view.View @@ -16,7 +17,6 @@ import net.pokeranalytics.android.model.handhistory.HHKeyboard import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.holder.RowViewHolder import net.pokeranalytics.android.ui.view.rowrepresentable.ViewIdentifier -import net.pokeranalytics.android.util.extensions.formatted import timber.log.Timber enum class HandRowType(var layoutRes: Int) : ViewIdentifier { @@ -97,20 +97,29 @@ class HandHistoryAdapter( inner class RowHandAction(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder { private var listener = TextListener() + private var currentPosition = 0 init { + // Action itemView.findViewById(R.id.actionEditText)?.let { actionEditText -> + + actionEditText.inputType = InputType.TYPE_NULL + actionEditText.setTextIsSelectable(false) + actionEditText.setOnFocusChangeListener { v, hasFocus -> - Timber.d("focus change = $hasFocus at position = $position") - actionEditText.setBackgroundColor(color(hasFocus)) + Timber.d("ACTION > focus change = $hasFocus at position = $currentPosition") + editTextSelected(actionEditText, hasFocus, HHKeyboard.ACTION.ordinal) } } + // Amount itemView.findViewById(R.id.amountEditText)?.let { amountEditText -> + amountEditText.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL + amountEditText.setOnFocusChangeListener { v, hasFocus -> - Timber.d("focus change = $hasFocus at position = $position") - amountEditText.setBackgroundColor(color(hasFocus)) + Timber.d("AMOUNT > focus change = $hasFocus at position = $currentPosition") + editTextSelected(amountEditText, hasFocus, HHKeyboard.AMOUNT.ordinal) } amountEditText.addTextChangedListener(this.listener) @@ -118,13 +127,23 @@ class HandHistoryAdapter( } } + private fun editTextSelected(editText: EditText, hasFocus: Boolean, tag: Int) { + editText.setBackgroundColor(color(hasFocus)) + if (hasFocus) { + val row = dataSource.rowRepresentableForPosition(currentPosition) + ?: throw PAIllegalStateException("Row Representable not found at index: $currentPosition") + delegate?.onRowSelected(currentPosition, row, tag) + } + } + private fun color(isFocused: Boolean) : Int { val color = if (isFocused) R.color.green_light else R.color.kaki_medium return itemView.context.getColor(color) } override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { - this.listener.position = position + + this.currentPosition = position val computedAction = row as ComputedAction @@ -137,7 +156,7 @@ class HandHistoryAdapter( itemView.findViewById(R.id.actionEditText)?.let { actionEditText -> val tag = HHKeyboard.ACTION.ordinal - actionEditText.isFocusable = computedAction.actionTypeCanBeEdited + val selected = adapter.dataSource.isSelected(position, row, tag) // Timber.d("Action at $position is selected: $selected") actionEditText.setBackgroundColor(color(selected)) @@ -148,53 +167,23 @@ class HandHistoryAdapter( actionEditText.text = null } - if (selected) actionEditText.requestFocus() else actionEditText.clearFocus() - -// actionEditText.setOnClickListener { -// -// Timber.d("OnClickListener at $position") -// val isSelected = adapter.dataSource.isSelected(position, row, tag) -// if (!isSelected && computedAction.actionTypeCanBeEdited) { -// actionEditText.requestFocus() -// adapter.delegate?.onRowSelected(position, row, tag) -// actionEditText.setBackgroundColor(color(true)) -// } else { -// adapter.delegate?.onRowDeselected(position, row) -// actionEditText.setBackgroundColor(color(false)) -// } -// } -// actionEditText.setOnFocusChangeListener { v, hasFocus -> -// Timber.d("focus change = $hasFocus at position = $position") -// actionEditText.setBackgroundColor(color(hasFocus)) -// } + if (selected) actionEditText.requestFocus()// else actionEditText.clearFocus() } // Amount itemView.findViewById(R.id.amountEditText)?.let { amountEditText -> + amountEditText.isFocusable = computedAction.amountCanBeEdited val tag = HHKeyboard.AMOUNT.ordinal val selected = adapter.dataSource.isSelected(position, row, tag) - Timber.d("Amount at $position is selected: $selected") +// Timber.d("Amount at $position is selected: $selected") amountEditText.setBackgroundColor(color(selected)) - amountEditText.setText(computedAction.action.amount?.formatted()) - - if (selected) amountEditText.requestFocus() else amountEditText.clearFocus() - -// amountEditText.setOnTouchListener { v, event -> -// Timber.d("OnClickListener at $position") -// adapter.delegate?.onRowSelected(position, row, tag) -// amountEditText.setBackgroundColor(color(true)) -// return@setOnTouchListener true -// } -// amountEditText.setOnFocusChangeListener { v, hasFocus -> -// Timber.d("focus change = $hasFocus at position = $position") -// amountEditText.setBackgroundColor(color(hasFocus)) -// } -// amountEditText.addTextChangedListener { -// adapter.delegate?.onRowValueChanged(it.toString(), row) -// } + amountEditText.setText(computedAction.action.displayedFormattedAmount) + + if (selected && !amountEditText.hasFocus()) amountEditText.requestFocus()// else amountEditText.clearFocus() + } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt index 788fc5cb..1f0ff374 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt @@ -105,26 +105,26 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr findNextActionToEdit(0) } + private fun closeEdition() { + this.model.isEdited = false + } private fun findNextActionToEdit() { - val index = this.model.currentSelection?.index ?: throw PAIllegalStateException("Request next with no selection") - this.findNextActionToEdit(index) + val selection = this.model.currentSelection + val index = selection?.index ?: throw PAIllegalStateException("Request next with no selection") + this.findNextActionToEdit(index, selection.keyboard) } - private fun findNextActionToEdit(startIndex: Int) { - this.model.findIndexForEdition(startIndex)?.let { + private fun findNextActionToEdit(startIndex: Int, keyboard: HHKeyboard? = null) { + this.model.findIndexForEdition(startIndex, keyboard)?.let { this.keyboard.show(it) - this.highlightCell(startIndex) + this.refreshCells(startIndex) } ?: run { this.keyboard.hide() } } - private fun closeEdition() { - this.model.isEdited = false - } - - private fun highlightCell(oldIndex: Int) { + private fun refreshCells(oldIndex: Int) { this.handHistoryAdapter.notifyItemChanged(oldIndex) this.model.currentSelection?.index?.let { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardContainer.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardContainer.kt index 4fc0e44a..15d546e8 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardContainer.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardContainer.kt @@ -2,6 +2,7 @@ package net.pokeranalytics.android.ui.view.handhistory import android.content.Context import android.util.AttributeSet +import android.view.View import android.widget.FrameLayout import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.view.isVisible @@ -51,6 +52,7 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co HHKeyboard.CARD -> { KeyboardCardView(context) } } view.keyboardListener = this.keyboardListener + this.keyboards[type] = view addView(view) } @@ -59,7 +61,7 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co private fun show(keyboardView: AbstractKeyboardView) { this.keyboards.values.forEach { kbView -> - kbView.isVisible = (kbView == keyboardView) + kbView.visibility = if (kbView == keyboardView) View.VISIBLE else View.GONE } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/HandHistoryViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/HandHistoryViewModel.kt index 2282ce56..5ecd9854 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/HandHistoryViewModel.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/HandHistoryViewModel.kt @@ -75,10 +75,10 @@ class HandHistoryViewModel : ViewModel() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - fun findIndexForEdition(startIndex: Int) : HHKeyboard? { + fun findIndexForEdition(startIndex: Int, keyboard: HHKeyboard? = null) : HHKeyboard? { builder.value?.let { builder -> - this.currentSelection = builder.findIndexForEdition(startIndex) + this.currentSelection = builder.findIndexForEdition(startIndex, keyboard) return currentSelection?.keyboard } ?: throw PAIllegalStateException("Builder not defined") diff --git a/app/src/main/res/layout/row_hand_action.xml b/app/src/main/res/layout/row_hand_action.xml index 2c74bf6a..e8a2a087 100644 --- a/app/src/main/res/layout/row_hand_action.xml +++ b/app/src/main/res/layout/row_hand_action.xml @@ -2,7 +2,9 @@ + android:layout_height="wrap_content" + android:descendantFocusability="beforeDescendants" + > - - - - - - - \ No newline at end of file