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 4098fa54..ffa54bdc 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 @@ -19,6 +19,7 @@ import net.pokeranalytics.android.ui.adapter.BindableHolder import net.pokeranalytics.android.ui.adapter.RecyclerAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate +import net.pokeranalytics.android.ui.modules.handhistory.model.HHKeyboard import net.pokeranalytics.android.ui.modules.handhistory.views.PlayerCardsRow import net.pokeranalytics.android.ui.modules.handhistory.views.StreetCardsRow import net.pokeranalytics.android.ui.view.RowRepresentable 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 d0364f30..f24f7b15 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,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.EditText +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import kotlinx.android.synthetic.main.fragment_hand_history.* import kotlinx.android.synthetic.main.fragment_settings.recyclerView @@ -17,9 +18,10 @@ import net.pokeranalytics.android.model.realm.handhistory.Card import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.fragment.components.RealmFragment -import net.pokeranalytics.android.ui.modules.handhistory.model.BuilderListener import net.pokeranalytics.android.ui.modules.handhistory.model.ComputedAction -import net.pokeranalytics.android.ui.modules.handhistory.model.HHBuilder +import net.pokeranalytics.android.ui.modules.handhistory.model.HHKeyboard +import net.pokeranalytics.android.ui.modules.handhistory.model.HHSelection +import net.pokeranalytics.android.ui.modules.handhistory.model.HandHistoryViewModel import net.pokeranalytics.android.ui.modules.handhistory.views.KeyboardListener import net.pokeranalytics.android.ui.modules.handhistory.views.PlayerCardsRow import net.pokeranalytics.android.ui.modules.handhistory.views.StreetCardsRow @@ -29,7 +31,7 @@ import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.extensions.noGroupingFormatted import timber.log.Timber -class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardListener, BuilderListener { +class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardListener { private lateinit var model: HandHistoryViewModel @@ -75,18 +77,14 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL private fun initData() { val handHistoryId = this.arguments?.getString(BundleKey.PRIMARY_KEY.value) - val builder = handHistoryId?.let { + + handHistoryId?.let { val handHistory = getRealm().findById(it) ?: throw PAIllegalStateException("HandHistory not found") - HHBuilder( - handHistory - ) + this.model.setHandHistory(handHistory) } ?: run { - HHBuilder( - HandSetup() - ) + this.model.configure(HandSetup()) } - this.model.setBuilder(builder) } @@ -121,8 +119,12 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } ?: run { this.keyboard.hide() } + } + val observer = Observer> { + this.handHistoryAdapter.notifyDataSetChanged() } + this.model.rowsLiveData.observe(this, observer) // At first, the selection is defined before the holder is bound, // so we retrieve the editText inputConnection once the recycler view has been rendered @@ -135,7 +137,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } this.keyboard.keyboardListener = this - this.keyboard.setCardCentralizer(this.model.cardCentralizer) + this.keyboard.setCardCentralizer(this.model) } @@ -164,7 +166,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL private fun findNextActionToEdit(index: Int? = null) { val startIndex = index ?: this.model.currentSelection.index - this.model.findIndexForEdition(startIndex)?.let { selection -> + this.model.findSelectionForEdition(startIndex)?.let { selection -> this.recyclerView.smoothScrollToPosition(selection.index) if (selection.keyboard == HHKeyboard.ACTION) { @@ -325,8 +327,4 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } } - override fun rowRepresentablesCountChanged() { - this.handHistoryAdapter.notifyDataSetChanged() - } - } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryViewModel.kt deleted file mode 100644 index 79907c8f..00000000 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryViewModel.kt +++ /dev/null @@ -1,153 +0,0 @@ -package net.pokeranalytics.android.ui.modules.handhistory - -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import net.pokeranalytics.android.exceptions.PAIllegalStateException -import net.pokeranalytics.android.model.handhistory.Position -import net.pokeranalytics.android.model.realm.handhistory.Action -import net.pokeranalytics.android.model.realm.handhistory.Card -import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource -import net.pokeranalytics.android.ui.modules.handhistory.model.BuilderListener -import net.pokeranalytics.android.ui.modules.handhistory.model.HHBuilder -import net.pokeranalytics.android.ui.modules.handhistory.views.CardCentralizer -import net.pokeranalytics.android.ui.view.RowRepresentable -import timber.log.Timber - -enum class HHKeyboard { - ACTION, - AMOUNT, - CARD; -} - -class HHSelection(var index: Int, var keyboard: HHKeyboard) - -class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource { - - private var builderLiveData = MutableLiveData() - - private val builder: HHBuilder - get() { - return this.builderLiveData.value ?: throw PAIllegalStateException("Builder not found") - } - - var isEdited = true - - var selectionLiveData: MutableLiveData = MutableLiveData() - - val currentSelection: HHSelection - get() { return selectionLiveData.value ?: throw PAIllegalStateException("No selection") } - - var currentAmount: String? = null - - fun setBuilder(builder: HHBuilder) { - this.builderLiveData.value = builder - } - - private val actionIndexForSelection: Int - get() { - return this.builder.indexOfComputedAction(currentSelection.index) - } - - val cardCentralizer: CardCentralizer - get() { - return this.builder - } - - // Action - - fun actionSelected(action: Action.Type) { - return this.builder.selectAction(this.actionIndexForSelection, action) - } - - // Amount - - fun amountValidated() { - try { - this.currentAmount?.toDouble()?.let { amount -> - builderLiveData.value?.setAmount(this.actionIndexForSelection, amount) - this.currentAmount = null - } - } catch (e: NumberFormatException) { - Timber.w("Parsing exception: ${e.message}") - } - } - - fun clearAmount() { -// builderLiveData.value?.clearAmount(this.actionIndexForSelection) - this.currentAmount = null - } - - // Cards - - fun cardValueSelected(value: Card.Value) { - this.builder.cardValueSelected(value, this.currentSelection) - } - - fun cardSuitSelected(suit: Card.Suit) { - this.builder.cardSuitSelected(suit, this.currentSelection) - } - - fun clearCards() { - this.builder.clearCards(this.currentSelection) - } - - fun deleteLastCardProperty() { - this.builder.deleteLastCardProperty(this.currentSelection) - } - - fun findIndexForEdition(index: Int): HHSelection? { - val selection = this.builder.findIndexForEdition(index) - this.selectionLiveData.value = selection - return selection - } - - fun amountChanged(amount: String?) { - this.currentAmount = amount - } - - fun positionsForSelection(): List { - return this.builder.positionsAtIndex(this.actionIndexForSelection) - } - - fun nextActionIndexForPosition(position: Position): Int { - return this.builder.nextActionIndex(this.actionIndexForSelection, position) - } - - fun availableActions() : Set { - return this.builder.availableActions(this.actionIndexForSelection) - } - - // Row Representable Datasource - - override fun adapterRows(): List? { - return this.builder.rowRepresentables - } - - override fun rowRepresentableForPosition(position: Int): RowRepresentable? { - return this.builder.rowRepresentables[position] - } - - override fun numberOfRows(): Int { - return this.builder.rowRepresentables.size - } - - override fun viewTypeForPosition(position: Int): Int { - return this.builder.rowRepresentables[position].viewType - } - - override fun isSelected(position: Int, row: RowRepresentable, tag: Int): Boolean { - val currentSelection = this.selectionLiveData - val isSelectedIndex = (position == currentSelection.value?.index) - val isSelectedAction = (tag == currentSelection.value?.keyboard?.ordinal) - return isSelectedIndex && isSelectedAction - } - - fun cardSelectionEnded() { - this.builder.cardSelectionEnded(this.currentSelection.index) - } - - fun setBuilderListener(builderListener: BuilderListener) { - this.builder.setBuilderListener(builderListener) - } - -} \ No newline at end of file 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 e8c78179..bf39af4e 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 @@ -20,10 +20,19 @@ interface ActionListListener { fun actionCountChanged() } +/*** + * ActionList is the class that governs the poker rules and let the user create a HandHistory + */ class ActionList(var listener: ActionListListener) : ArrayList(), ActionManager { - private var countChanged: Boolean = false + /*** + * Indicates whether the list size has changed + */ + private var sizeChanged: Boolean = false + /*** + * Loads the action list with a HandHistory + */ fun load(handHistory: HandHistory) { this.positions = Position.positionsPerPlayers(handHistory.numberOfPlayers) @@ -103,13 +112,21 @@ class ActionList(var listener: ActionListListener) : ArrayList() fireListener() } + /*** + * Sets the amount for the action at the provided [index] + */ + fun setAmount(index: Int, amount: Double) { + val computedAction = this[index] + computedAction.setAmount(amount) + } + /*** * Fires the listener if the list count has changed */ private fun fireListener() { - if (this.countChanged) { + if (this.sizeChanged) { this.listener.actionCountChanged() - this.countChanged = false + this.sizeChanged = false } } @@ -191,7 +208,7 @@ class ActionList(var listener: ActionListListener) : ArrayList() val sizeAfter = this.size if (sizeBefore != sizeAfter) { - this.countChanged = true + this.sizeChanged = true } } @@ -295,7 +312,7 @@ class ActionList(var listener: ActionListListener) : ArrayList() ) this.add(computedAction) - this.countChanged = true + this.sizeChanged = true } /*** @@ -417,6 +434,9 @@ class ActionList(var listener: ActionListListener) : ArrayList() fireListener() } + /*** + * Returns the list of the player next street actions + */ override fun getPlayerNextStreetActions(index: Int): List { val computedAction = this[index] return this.drop(index + 1).filter { @@ -489,7 +509,7 @@ class ActionList(var listener: ActionListListener) : ArrayList() } /*** - * Returns a list of positions at the provided [index] + * Returns all positions of the street, identified by the action at the given [index] */ fun positionsAtIndex(index: Int): List { val currentStreet = this[index].street 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 c2d9abb8..1ce3f06e 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 @@ -4,7 +4,6 @@ import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.Position import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.realm.handhistory.Action -import net.pokeranalytics.android.ui.modules.handhistory.HHKeyboard import net.pokeranalytics.android.ui.modules.handhistory.HandRowType import net.pokeranalytics.android.ui.view.RowRepresentable import timber.log.Timber @@ -18,6 +17,10 @@ interface HandHistoryRow : RowRepresentable { fun keyboardForCompletion() : HHKeyboard? } +/*** + * ComputedAction is a convenience class which main role is to represent a hand history Action + * and is used to display it on screen. + */ class ComputedAction(var manager: ActionManager, var action: Action, var totalPotSize: Double = 0.0, @@ -131,7 +134,7 @@ class ComputedAction(var manager: ActionManager, * Sets the effective amount of the action * Also calculates the player remaining stack if possible */ - fun setEffectiveAmount(amount: Double) { + private fun setEffectiveAmount(amount: Double) { // if (amount <= 0.0) { // throw PAIllegalStateException("Something probably went wrong somewhere, attempting to make a call of $amount") @@ -152,6 +155,11 @@ class ComputedAction(var manager: ActionManager, } } + /*** + * Returns a keyboard to complete the action, or null if the action is filled + * All ComputedAction should have a type, so we return the action keyboard if the type is null + * If the action has a type, we return the amount keyboard if the action requires one + */ override fun keyboardForCompletion() : HHKeyboard? { Timber.d("index = ${action.index} / type = ${this.action.type} / amount = ${this.action.amount}") return if (this.action.type != null) { @@ -218,8 +226,6 @@ class ComputedAction(var manager: ActionManager, 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 } /*** diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HHBuilder.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt similarity index 60% rename from app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HHBuilder.kt rename to app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt index 0abfbdff..25306ac8 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HHBuilder.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt @@ -1,5 +1,7 @@ package net.pokeranalytics.android.ui.modules.handhistory.model +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel import net.pokeranalytics.android.R import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.HandSetup @@ -8,7 +10,7 @@ import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.realm.handhistory.Action import net.pokeranalytics.android.model.realm.handhistory.Card import net.pokeranalytics.android.model.realm.handhistory.HandHistory -import net.pokeranalytics.android.ui.modules.handhistory.HHSelection +import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource import net.pokeranalytics.android.ui.modules.handhistory.HandRowType import net.pokeranalytics.android.ui.modules.handhistory.views.CardCentralizer import net.pokeranalytics.android.ui.modules.handhistory.views.CardsRow @@ -19,27 +21,60 @@ import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepres import net.pokeranalytics.android.util.extensions.formatted import timber.log.Timber -interface BuilderListener { - fun rowRepresentablesCountChanged() +enum class HHKeyboard { + ACTION, + AMOUNT, + CARD; } -class HHBuilder : CardCentralizer, ActionListListener { +class HHSelection(var index: Int, var keyboard: HHKeyboard) + +class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener { + + /*** + * Indicates whether the hand history is being edited or not + */ + var isEdited = true + + /*** + * The user selection's live data inside the editor + */ + var selectionLiveData: MutableLiveData = MutableLiveData() + + /*** + * The current selection + */ + val currentSelection: HHSelection + get() { return selectionLiveData.value ?: throw PAIllegalStateException("No selection") } + + /*** + * The current amount edited by the user + */ + var currentAmount: String? = null + + /*** + * The action index + */ + private val actionIndexForSelection: Int + get() { + return this.indexOfComputedAction(currentSelection.index) + } /*** * The list of row representables, generated from the hand history actions */ - var rowRepresentables = mutableListOf() - private set + private val rowRepresentables: List + get() { return this.rowsLiveData.value ?: throw PAIllegalStateException("Rows not set") } + + /*** + * The live data object that contains the list of RowRepresentable + */ + var rowsLiveData: MutableLiveData> = MutableLiveData() /*** * The hand history */ - private var handHistory: HandHistory - set(value) { - field = value - setNumberOfPlayers(value.numberOfPlayers) - load() - } + private lateinit var handHistory: HandHistory /*** * All actions sorted by index @@ -57,40 +92,29 @@ class HHBuilder : CardCentralizer, ActionListListener { private var playerHandMaxCards: Int? = null set(value) { field = value - this.rowRepresentables.filterIsInstance().forEach { + this.rowsLiveData.value?.filterIsInstance()?.forEach { it.maxCards = value } } /*** - * Creates a builder using a [handSetup] - * Also creates a new Hand History and configures it according to the [handSetup] + * Configures a new HandHistory object using a [handSetup] */ - constructor(handSetup: HandSetup) { + fun configure(handSetup: HandSetup) { val handHistory = HandHistory() handHistory.configure(handSetup) this.playerHandMaxCards = handSetup.game?.playerHandMaxCards - this.handHistory = handHistory - } - - /*** - * Creates a builder using the parameter [handHistory] - */ - constructor(handHistory: HandHistory) { - this.handHistory = handHistory - } - - private var listener: BuilderListener? = null - fun setBuilderListener(builderListener: BuilderListener) { - this.listener = builderListener + this.setHandHistory(handHistory) } /*** - * Returns the action + * Sets the hand history and loads it */ - private fun actionForIndex(index: Int) : ComputedAction { - return this.sortedActions[index] + fun setHandHistory(handHistory: HandHistory) { + this.handHistory = handHistory + setNumberOfPlayers(handHistory.numberOfPlayers) + load() } /*** @@ -98,10 +122,7 @@ class HHBuilder : CardCentralizer, ActionListListener { * Pre-computes the potsizes for the video export */ private fun load() { - -// this.boardManager = BoardManager(this.handHistory.board, this) - this.sortedActions.load(this.handHistory) - + this.sortedActions.load(this.handHistory) this.createRowRepresentation() } @@ -109,77 +130,54 @@ class HHBuilder : CardCentralizer, ActionListListener { * Sets the number of players playing the hand * Defines the appropriate positions for this player count */ - fun setNumberOfPlayers(playerCount: Int) { + private fun setNumberOfPlayers(playerCount: Int) { this.handHistory.numberOfPlayers = playerCount this.sortedActions.positions = Position.positionsPerPlayers(playerCount) } - fun availableActions(actionIndexForSelection: Int): Set { - return this.sortedActions.availableActions(actionIndexForSelection) - } - - fun selectAction(index: Int, actionType: Action.Type) { - this.sortedActions.selectAction(index, actionType) - } - - /*** - * Sets the amount for the action at the provided [index] - */ - fun setAmount(index: Int, amount: Double) { - val computedAction = this.actionForIndex(index) - Timber.d(">>> Sets $amount at index: $index, for action ${computedAction.action.type}") - computedAction.setAmount(amount) - } - -// /*** -// * Creates a new street: -// * - Adds a Street Header -// * - Adds a Board if necessary -// * - Adds empty actions for the remaining players -// */ -// private fun createStreet(street: Street) { -// -// val lastComputedAction = this.sortedActions.last() -// val totalPotSize = lastComputedAction.totalPotSize -// -// addStreetHeader(this.rowRepresentables, street) -// -// val lastActionIndex = lastComputedAction.action.index -// val isShowDown = street == Street.SUMMARY -// activePositions(lastActionIndex, isShowDown).sortedBy { it.ordinal }.forEach { -// -// when (street) { -// Street.SUMMARY -> { -// this.rowRepresentables.add(PlayerCardsRow(it)) -// } -// else -> { -// addNewEmptyAction(it, street, totalPotSize, lastRemainingStack(it, lastActionIndex)) -// } -// } -// -// } -// } + /*** + * Returns the list of available actions at the selected index + */ + fun availableActions() : Set { + return this.sortedActions.availableActions(this.actionIndexForSelection) + } + + /*** + * Sets an action at the selected index + */ + fun actionSelected(action: Action.Type) { + this.sortedActions.selectAction(this.actionIndexForSelection, action) + } /*** * Returns a list of positions at the provided [index] */ - fun positionsAtIndex(index: Int): List { - return this.sortedActions.positionsAtIndex(index) + fun positionsForSelection(): List { + return this.sortedActions.positionsAtIndex(this.actionIndexForSelection) } /*** * Returns the index of a ComputedAction given its [rowRepresentableIndex] */ - fun indexOfComputedAction(rowRepresentableIndex: Int) : Int { + private fun indexOfComputedAction(rowRepresentableIndex: Int) : Int { val computedAction = this.rowRepresentables[rowRepresentableIndex] as ComputedAction return computedAction.action.index } + /*** + * Looks for a row and a keyboard, i.e. a HHSelection, suitable for edition + */ + fun findSelectionForEdition(index: Int): HHSelection? { + val selection = this.getSelectionForFirstEditableRow(index) + this.selectionLiveData.value = selection + return selection + } + /*** * 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? { + private fun getSelectionForFirstEditableRow(startIndex: Int): HHSelection? { this.rowRepresentables.forEachIndexed { index, rowRepresentable -> @@ -200,11 +198,10 @@ class HHBuilder : CardCentralizer, ActionListListener { * Returns the index, strictly superior to [actionIndexForSelection], * of the next action of the given [position] */ - fun nextActionIndex(actionIndexForSelection: Int, position: Position): Int { - - var i = actionIndexForSelection + 1 + fun nextActionIndexForPosition(position: Position): Int { + var i = this.actionIndexForSelection + 1 while (true) { - val computedAction = this.actionForIndex(i) + val computedAction = this.sortedActions[i] if (computedAction.position == position) { return this.rowRepresentables.indexOf(computedAction) } @@ -213,12 +210,37 @@ class HHBuilder : CardCentralizer, ActionListListener { } } + // Amount + + /*** + * Sets the typed amount in the relevant ComputedAction + */ + fun amountValidated() { + try { + this.currentAmount?.toDouble()?.let { amount -> + this.sortedActions.setAmount(this.actionIndexForSelection, amount) + this.currentAmount = null + } + } catch (e: NumberFormatException) { + Timber.w("Parsing exception: ${e.message}") + } + } + + /*** + * Sets the current amount to null + */ + fun clearAmount() { + this.currentAmount = null + } + + // Cards + /*** * Adds a card with the selected [value] */ - fun cardValueSelected(value: Card.Value, currentSelection: HHSelection) { + fun cardValueSelected(value: Card.Value) { this.lastValue = value - val row = this.rowRepresentables[currentSelection.index] as CardsRow + val row = this.rowRepresentables[this.currentSelection.index] as CardsRow row.valueSelected(value) } @@ -226,9 +248,9 @@ class HHBuilder : CardCentralizer, ActionListListener { * Either affect the [suit] to the current card, * or create a Card with an empty value and the [suit] */ - fun cardSuitSelected(suit: Card.Suit, currentSelection: HHSelection) { + fun cardSuitSelected(suit: Card.Suit) { - val row = this.rowRepresentables[currentSelection.index] as CardsRow + val row = this.rowRepresentables[this.currentSelection.index] as CardsRow row.suitSelected(suit) // TODO do we want to store the information right now ? @@ -238,9 +260,9 @@ class HHBuilder : CardCentralizer, ActionListListener { /*** * Deletes all the card of the selected street */ - fun clearCards(currentSelection: HHSelection) { + fun clearCards() { this.lastValue = null - val row = this.rowRepresentables[currentSelection.index] as CardsRow + val row = this.rowRepresentables[this.currentSelection.index] as CardsRow row.clear() } @@ -249,8 +271,8 @@ class HHBuilder : CardCentralizer, ActionListListener { * We don't want empty Card (value + suit), * so we delete the whole card if both information are null */ - fun deleteLastCardProperty(currentSelection: HHSelection) { - val row = this.rowRepresentables[currentSelection.index] as CardsRow + fun deleteLastCardProperty() { + val row = this.rowRepresentables[this.currentSelection.index] as CardsRow row.deleteLastCardProperty() } @@ -258,7 +280,7 @@ class HHBuilder : CardCentralizer, ActionListListener { * When a card selection is ended, * sets the playerHandMaxCards with the number of cards set to the player */ - fun cardSelectionEnded(index: Int) { + private fun cardSelectionEnded(index: Int) { val cardsRow = this.rowRepresentables[index] as CardsRow if (cardsRow is PlayerCardsRow) { this.playerHandMaxCards = cardsRow.cards.size @@ -272,7 +294,7 @@ class HHBuilder : CardCentralizer, ActionListListener { */ fun boardChanged(cards: List) { this.rowRepresentables.filterIsInstance().forEach { -// it.cards = cards + // it.cards = cards } } @@ -283,6 +305,9 @@ class HHBuilder : CardCentralizer, ActionListListener { } + /*** + * Uses the action list to create the list of RowRepresentables, displayed to the user + */ private fun createRowRepresentation() { val rows: MutableList = mutableListOf() @@ -314,11 +339,8 @@ class HHBuilder : CardCentralizer, ActionListListener { } } } - } - - this.rowRepresentables = rows - this.listener?.rowRepresentablesCountChanged() + this.rowsLiveData.value = rows } /*** @@ -382,9 +404,50 @@ class HHBuilder : CardCentralizer, ActionListListener { // Action List Listener + /*** + * Callback of the ActionListListener + * Creates the row representation when the action list changes + */ override fun actionCountChanged() { this.createRowRepresentation() } -} + fun amountChanged(amount: String?) { + this.currentAmount = amount + } + + // Row Representable Datasource + + override fun adapterRows(): List? { + return this.rowRepresentables + } + + override fun rowRepresentableForPosition(position: Int): RowRepresentable? { + return this.rowRepresentables[position] + } + + override fun numberOfRows(): Int { + return this.rowRepresentables.size + } + + override fun viewTypeForPosition(position: Int): Int { + return this.rowRepresentables[position].viewType + } + + override fun isSelected(position: Int, row: RowRepresentable, tag: Int): Boolean { + val currentSelection = this.selectionLiveData + val isSelectedIndex = (position == currentSelection.value?.index) + val isSelectedAction = (tag == currentSelection.value?.keyboard?.ordinal) + return isSelectedIndex && isSelectedAction + } + + fun cardSelectionEnded() { + this.cardSelectionEnded(this.currentSelection.index) + } + +// fun setBuilderListener(builderListener: BuilderListener) { +// this.builder.setBuilderListener(builderListener) +// } + +} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt index 3bfae1e8..2350de36 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt @@ -4,7 +4,7 @@ import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.Position import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.realm.handhistory.Card -import net.pokeranalytics.android.ui.modules.handhistory.HHKeyboard +import net.pokeranalytics.android.ui.modules.handhistory.model.HHKeyboard import net.pokeranalytics.android.ui.modules.handhistory.HandRowType import net.pokeranalytics.android.ui.modules.handhistory.model.HandHistoryRow 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 f6c42201..7a935bd3 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 @@ -11,7 +11,7 @@ import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.Position import net.pokeranalytics.android.model.realm.handhistory.Action import net.pokeranalytics.android.model.realm.handhistory.Card -import net.pokeranalytics.android.ui.modules.handhistory.HHKeyboard +import net.pokeranalytics.android.ui.modules.handhistory.model.HHKeyboard interface KeyboardListener {