From d4dd65a09d8f23ad0863d4a849576293a1f18c1d Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 14 Feb 2020 17:13:07 +0100 Subject: [PATCH] Start of edit/save + blind setting from the actions --- .../handhistory/HandHistoryFragment.kt | 40 +++++++++++++++++-- .../modules/handhistory/model/ActionList.kt | 15 +++++++ .../handhistory/model/ComputedAction.kt | 9 ++++- .../handhistory/model/HandHistoryViewModel.kt | 29 ++++++++++++-- .../android/ui/view/RowRepresentable.kt | 3 +- .../main/res/menu/toolbar_hand_history.xml | 10 +++++ 6 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 app/src/main/res/menu/toolbar_hand_history.xml 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 74d762cb..1ba7a4c2 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 @@ -1,9 +1,7 @@ package net.pokeranalytics.android.ui.modules.handhistory import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import kotlinx.android.synthetic.main.fragment_hand_history.* @@ -39,6 +37,11 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL */ private lateinit var handHistoryAdapter: HandHistoryAdapter + /*** + * The fragment's menu + */ + private var menu: Menu? = null + companion object { fun newInstance(id: String? = null): HandHistoryFragment { @@ -153,6 +156,28 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL this.keyboard.setCardCentralizer(this.model) } + override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { + menu?.clear() + inflater?.inflate(R.menu.toolbar_hand_history, menu) + updateMenuUI() + super.onCreateOptionsMenu(menu, inflater) + } + + /** + * Update menu UI + */ + private fun updateMenuUI() { + val titleResId = if (this.model.isEdited) R.string.save else R.string.edit + this.menu?.findItem(R.id.edit_save)?.setTitle(titleResId) + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item!!.itemId) { + R.id.edit_save -> saveOrEdit() + } + return true + } + private fun retrieveEditTextInputConnection(selection: HHSelection) { val handRow = this.model.rowRepresentableForPosition(selection.index) as? HandRowType @@ -167,6 +192,15 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } + private fun saveOrEdit() { + this.model.isEdited = !this.model.isEdited + + if (this.model.isEdited) { + this.findNextActionToEdit(0) + } + this.handHistoryAdapter.notifyDataSetChanged() + } + private fun edit() { this.model.isEdited = true this.findNextActionToEdit(0) diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt index f61ff601..44aa951a 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt @@ -15,6 +15,7 @@ interface ActionManager { fun getPlayerNextStreetActions(index: Int): List fun dropNextActions(index: Int) fun allinAmountSet(positionIndex: Int) + fun blindSet(type: Action.Type, amount: Double) } interface ActionListListener : PlayerSetupCreationListener { @@ -456,6 +457,20 @@ class ActionList(var listener: ActionListListener) : ArrayList() } } + /*** + * Sets the blinds value in the hand history + */ + override fun blindSet(type: Action.Type, amount: Double) { + when (type) { + Action.Type.POST_SB -> this.handHistory.smallBlind = amount + Action.Type.POST_BB -> this.handHistory.bigBlind = amount + else -> throw PAIllegalStateException("Should never happen") + } + } + + /*** + * Returns whether the player at [position] is all-in and its amount is set + */ fun isPlayerAllinWithAmount(position: Int): Boolean { val computedAction = this.lastOrNull { it.action.position == position && it.action.type?.isAllin == true } return computedAction?.action?.amount != null diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt index 67d94554..3a7d1f27 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt @@ -122,8 +122,13 @@ class ComputedAction(var manager: ActionManager, else -> {} } - if (this.action.type?.isAllin == true) { - this.manager.allinAmountSet(this.action.position) + when { + this.action.type?.isAllin == true -> { + this.manager.allinAmountSet(this.action.position) + } + this.action.type?.isBlind == true -> { + this.manager.blindSet(this.action.type!!, amount) + } } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt index 6e4031c3..a998b660 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt @@ -53,6 +53,10 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra * Indicates whether the hand history is being edited or not */ var isEdited = true + set(value) { + field = value + createRowRepresentation() + } /*** * The user selection's live data inside the editor @@ -167,6 +171,25 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra * Uses the action list to create the list of RowRepresentables, displayed to the user */ private fun createRowRepresentation() { + + this.rowsLiveData.value = if (this.isEdited) { + editionRowRepresentation() + } else { + readRowRepresentation() + } + + } + + private fun readRowRepresentation(): MutableList { + val rows: MutableList = mutableListOf() + + // TODO remove useless settings, collapse multifolds + + return rows + } + + private fun editionRowRepresentation(): MutableList { + val rows: MutableList = mutableListOf() rows.add(HandRowType.COMMENT) @@ -200,8 +223,8 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra if (this.sortedActions.activePositions(lastActionIndex).size < 2 || this.sortedActions.isStreetActionClosed(lastActionIndex) == Street.SUMMARY) { addStreetHeader(rows, street) - val positions = this.sortedActions.activePositions(lastActionIndex, true) - positions.forEach { position -> + val activePositions = this.sortedActions.activePositions(lastActionIndex, true) + activePositions.forEach { position -> val positionIndex = this.sortedActions.positions.indexOf(position) val playerCardsRow = PlayerCardsRow(this, position, @@ -221,7 +244,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra } } } - this.rowsLiveData.value = rows + return rows } /*** diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt index 069d566f..2239258c 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt @@ -1,7 +1,6 @@ package net.pokeranalytics.android.ui.view import android.content.Context -import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.LiveData import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType import net.pokeranalytics.android.util.NULL_TEXT @@ -58,7 +57,7 @@ interface Displayable : Localizable { */ val viewType: Int get() { - throw PAIllegalStateException("The Displayable instance $this must return a valid viewType") + return -1 } val relatedResultsRepresentable: LiveData? diff --git a/app/src/main/res/menu/toolbar_hand_history.xml b/app/src/main/res/menu/toolbar_hand_history.xml new file mode 100644 index 00000000..52037b10 --- /dev/null +++ b/app/src/main/res/menu/toolbar_hand_history.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file