From d66a706c07ca89d8eb6a62d7000eef57e3341c6d Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 2 Mar 2020 13:50:00 +0100 Subject: [PATCH] Adds onItemClick for RowRepresentableDelegate --- .../android/ui/adapter/RecyclerAdapter.kt | 2 +- .../modules/handhistory/HandHistoryAdapter.kt | 68 +++++++++++-------- .../handhistory/HandHistoryFragment.kt | 40 ++++++++++- 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RecyclerAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RecyclerAdapter.kt index 7a989e8a..b4250788 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RecyclerAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RecyclerAdapter.kt @@ -14,7 +14,7 @@ interface RowRepresentableDelegate { fun onRowValueChanged(value: Any?, row: RowRepresentable) {} fun onRowDeleted(row: RowRepresentable) {} fun onRowLongClick(itemView: View, row: RowRepresentable, position: Int) {} - + fun onItemClick(position: Int, row: RowRepresentable, tag: Int = 0) {} } /** 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 a2a459f4..11294dcb 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 @@ -240,6 +240,15 @@ class HandHistoryAdapter( } } + protected fun setOnItemClickListener(button: Button) { + button.setOnClickListener { + button.backgroundTintList = ColorStateList.valueOf(color(true)) + val row = dataSource.rowRepresentableForPosition(currentPosition) + ?: throw PAIllegalStateException("Row Representable not found at index: $currentPosition") + delegate?.onItemClick(currentPosition, row, button.tag as Int) + } + } + private fun toggleFocus(editText: EditText, focused: Boolean) { if (focused) { editText.requestFocus() @@ -444,6 +453,35 @@ class HandHistoryAdapter( } } + abstract inner class AbstractRowHandPlayerSetup(itemView: View) : RowHandHolder(itemView) { + + protected var delegate: RowRepresentableDelegate? = null + + init { + itemView.ps_hand_layout.tag = PlayerSetupRow.Tag.HAND.ordinal + } + + override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { + super.onBind(position, row, adapter) + + val setupRow = row as PlayerSetupRow + val state = setupRow.state + this.delegate = adapter.delegate + + // Position Button + itemView.position_button.text = adapter.dataSource.charSequenceForRow(row, itemView.context, PlayerSetupRow.Tag.POSITION.ordinal) + val positionSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRow.Tag.POSITION.ordinal) + itemView.position_button.backgroundTintList = ColorStateList.valueOf(color(positionSelected)) + + // Settings + itemView.settings_container.visibility = if (state == PlayerSetupRow.State.POSITIONS_ONLY) View.GONE else View.VISIBLE + + configureCardsLayout(itemView.ps_hand_layout) + + } + + } + inner class RowHandReadOnlyPlayerSetup(itemView: View) : AbstractRowHandPlayerSetup(itemView) { init { @@ -466,7 +504,6 @@ class HandHistoryAdapter( init { itemView.ps_player_button.tag = PlayerSetupRow.Tag.PLAYER.ordinal - setOnClickListener(itemView.ps_player_button) itemView.stack_edit_text.tag = PlayerSetupRow.Tag.STACK.ordinal @@ -509,33 +546,4 @@ class HandHistoryAdapter( } - abstract inner class AbstractRowHandPlayerSetup(itemView: View) : RowHandHolder(itemView) { - - protected var delegate: RowRepresentableDelegate? = null - - init { - itemView.ps_hand_layout.tag = PlayerSetupRow.Tag.HAND.ordinal - } - - override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { - super.onBind(position, row, adapter) - - val setupRow = row as PlayerSetupRow - val state = setupRow.state - this.delegate = adapter.delegate - - // Position Button - itemView.position_button.text = adapter.dataSource.charSequenceForRow(row, itemView.context, PlayerSetupRow.Tag.POSITION.ordinal) - val positionSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRow.Tag.POSITION.ordinal) - itemView.position_button.backgroundTintList = ColorStateList.valueOf(color(positionSelected)) - - // Settings - itemView.settings_container.visibility = if (state == PlayerSetupRow.State.POSITIONS_ONLY) View.GONE else View.VISIBLE - - configureCardsLayout(itemView.ps_hand_layout) - - } - - } - } \ No newline at end of file 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 e5046403..bfdf693a 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 @@ -210,6 +210,9 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL this.updateMenuUI() } + /*** + * Retrieves the edit text corresponding to the provided [selection] + */ private fun retrieveEditTextInputConnection(selection: HHSelection) { // throw PAIllegalStateException("I believe this should not happen any more") @@ -226,6 +229,9 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } + /*** + * Saves or edit the hand history + */ private fun saveOrEdit() { this.setEditing(!this.model.isEdited) @@ -239,10 +245,16 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL this.handHistoryAdapter.notifyDataSetChanged() } + /*** + * Saves the model to Realm + */ private fun save() { this.model.save(getRealm()) } + /*** + * Looks for a field to edit + */ private fun findNextActionToEdit(index: Int? = null, userInitiated: Boolean = false) { val startIndex = index ?: this.model.currentSelection.index @@ -271,7 +283,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL return } - when (row.bottomSheetType) { // Comment, number of players + when (row.bottomSheetType) { // Comment, number of players... BottomSheetType.NONE -> {} else -> { val editDescriptors = this.model.editDescriptors(row) @@ -338,6 +350,32 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } } + } + + override fun onItemClick(position: Int, row: RowRepresentable, tag: Int) { + + when (row) { + is PlayerSetupRow -> { + when (tag) { + PlayerSetupRow.Tag.PLAYER.ordinal -> { + showPlayerSelectionFragment() + } + else -> { + Timber.d("onItemClick not configured for row: $row, position: $position, tag: $tag") + } + } + } + } + + } + + /*** + * Shows a window allowing a player selection + */ + private fun showPlayerSelectionFragment() { + + + } /***