Manages card storage

hh
Laurent 6 years ago
parent 1d30f51e3c
commit 46089b13c6
  1. 30
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt
  2. 5
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/PlayerSetup.kt
  3. 50
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt
  4. 17
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt
  5. 70
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt

@ -10,11 +10,12 @@ import net.pokeranalytics.android.model.handhistory.Street
import net.pokeranalytics.android.model.interfaces.Identifiable
import net.pokeranalytics.android.model.interfaces.TimeFilterable
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.modules.handhistory.views.CardHolder
import net.pokeranalytics.android.ui.view.RowRepresentable
import java.util.*
open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterable, TimeFilterable {
open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterable, TimeFilterable, CardHolder {
@PrimaryKey
override var id = UUID.randomUUID().toString()
@ -127,19 +128,22 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab
fun cardsForStreet(street: Street): MutableList<Card> {
return this.board.sortedBy { it.index }.take(street.totalBoardCards).toMutableList()
}
fun playerSetupForPosition(position: Int) : PlayerSetup {
this.playerSetups.firstOrNull { it.position == position }?.let {
return it
}
val ps = PlayerSetup()
ps.position = position
return ps
//
// fun playerSetupForPosition(position: Int) : PlayerSetup {
// this.playerSetups.firstOrNull { it.position == position }?.let {
// return it
// }
//
// val ps = PlayerSetup()
// ps.position = position
// return ps
// }
fun playerSetupForPosition(position: Int): PlayerSetup? {
return this.playerSetups.firstOrNull { it.position == position }
}
fun cardsForPosition(position: Int): List<Card> {
return this.playerSetups.firstOrNull { it.position == position }?.cards ?: listOf()
}
override val cards: RealmList<Card>
get() { return this.board }
}

@ -3,8 +3,9 @@ package net.pokeranalytics.android.model.realm.handhistory
import io.realm.RealmList
import io.realm.RealmObject
import net.pokeranalytics.android.model.realm.Player
import net.pokeranalytics.android.ui.modules.handhistory.views.CardHolder
open class PlayerSetup : RealmObject() {
open class PlayerSetup : RealmObject(), CardHolder {
/***
* The player
@ -24,7 +25,7 @@ open class PlayerSetup : RealmObject() {
/***
* The cards of the player
*/
var cards: RealmList<Card> = RealmList()
override var cards: RealmList<Card> = RealmList()
fun cardValueSelected(value: Card.Value) {

@ -12,13 +12,13 @@ import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.ui.modules.handhistory.model.ComputedAction
import net.pokeranalytics.android.model.handhistory.Street
import net.pokeranalytics.android.model.realm.handhistory.formatted
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.ComputedAction
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
@ -102,26 +102,6 @@ class HandHistoryAdapter(
}
inner class RowHandActionTest(itemView: View) : RowHandHolder(itemView), BindableHolder {
override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) {
val computedAction = row as ComputedAction
itemView.findViewById<EditText>(R.id.amountEditText)?.let { amountEditText ->
// amountEditText.setText(computedAction.action.formattedAmount)
val selected = adapter.dataSource.isSelected(position, row, 0)
if (selected) {
amountEditText.requestFocus()
} else {
amountEditText.clearFocus()
}
}
}
}
/**
* Display a hand action
*/
@ -262,8 +242,8 @@ class HandHistoryAdapter(
flopEditText.isFocusable = (street == Street.FLOP)
flopEditText.isVisible = true
val flop = streetCardView.cards.take(3)
val text = flop.formatted(itemView.context)
val flop = streetCardView.cardHolder?.cards?.take(3)
val text = flop?.formatted(itemView.context)
flopEditText.setText(text)
val selected = adapter.dataSource.isSelected(position, row, Street.FLOP.ordinal)
@ -275,8 +255,8 @@ class HandHistoryAdapter(
// turnEditText.isVisible = streetCardView.street.ordinal >= Street.TURN.ordinal
turnEditText.isFocusable = (street == Street.TURN)
if (streetCardView.cards.size >= 4) {
val text = streetCardView.cards[3].formatted(itemView.context)
if (streetCardView.cardCount > 3) {
val text = streetCardView.cardAtIndex(3)?.formatted(itemView.context)
turnEditText.setText(text)
} else {
turnEditText.text = null
@ -290,8 +270,8 @@ class HandHistoryAdapter(
// riverEditText.isVisible = streetCardView.street.ordinal >= Street.RIVER.ordinal
riverEditText.isFocusable = (street == Street.RIVER)
if (streetCardView.cards.size >= 5) {
val text = streetCardView.cards[4].formatted(itemView.context)
if (streetCardView.cardCount > 4) {
val text = streetCardView.cardAtIndex(4)?.formatted(itemView.context)
riverEditText.setText(text)
} else {
riverEditText.text = null
@ -350,24 +330,24 @@ class HandHistoryAdapter(
}
// Amount
itemView.findViewById<EditText>(R.id.cardsEditText)?.let { amountEditText ->
itemView.findViewById<EditText>(R.id.cardsEditText)?.let { cardsEditText ->
val tag = HHKeyboard.AMOUNT.ordinal
val selected = adapter.dataSource.isSelected(position, row, tag)
// Both are required, otherwise requestFocus() fails
amountEditText.isFocusable = selected
amountEditText.isFocusableInTouchMode = selected
cardsEditText.isFocusable = selected
cardsEditText.isFocusableInTouchMode = selected
Timber.d("Amount at $position is selected: $selected, focusable = ${amountEditText.isFocusable}, isFocusableInTouchMode = ${amountEditText.isFocusableInTouchMode}, hasFocus = ${amountEditText.hasFocus()}, enabled = ${amountEditText.isEnabled}")
amountEditText.setBackgroundColor(color(selected))
Timber.d("Amount at $position is selected: $selected, focusable = ${cardsEditText.isFocusable}, isFocusableInTouchMode = ${cardsEditText.isFocusableInTouchMode}, hasFocus = ${cardsEditText.hasFocus()}, enabled = ${cardsEditText.isEnabled}")
cardsEditText.setBackgroundColor(color(selected))
amountEditText.setText(playerCardView.cards.formatted(itemView.context))
cardsEditText.setText(playerCardView.cardHolder?.cards?.formatted(itemView.context))
if (selected) {
amountEditText.requestFocus()
cardsEditText.requestFocus()
} else {
amountEditText.clearFocus()
cardsEditText.clearFocus()
}
}

@ -283,7 +283,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
private fun cardSelectionEnded(index: Int) {
val cardsRow = this.rowRepresentables[index] as CardsRow
if (cardsRow is PlayerCardsRow) {
this.playerHandMaxCards = cardsRow.cards.size
this.playerHandMaxCards = cardsRow.cardCount
}
}
@ -327,7 +327,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
val positions = this.sortedActions.activePositions(lastActionIndex, true)
positions.forEach {
val positionIndex = this.sortedActions.positions.indexOf(it)
val playerCardsRow = PlayerCardsRow(it, this.handHistory.cardsForPosition(positionIndex), this.playerHandMaxCards)
val playerCardsRow = PlayerCardsRow(it, this.handHistory.playerSetupForPosition(positionIndex), this.playerHandMaxCards)
rows.add(playerCardsRow)
}
}
@ -358,13 +358,8 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
rowRepresentables.add(headerView)
if (street.totalBoardCards > 0) {
// get board from last street
val lastBoardRow = this.rowRepresentables.lastOrNull { it is StreetCardsRow } as? StreetCardsRow
val cards = lastBoardRow?.cards ?: listOf<Card>()
// create new StreetCardsRow
val boardView = StreetCardsRow(street, cards)
val boardView = StreetCardsRow(street, this.handHistory)
rowRepresentables.add(boardView)
}
@ -377,8 +372,10 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
*/
private val usedCards: List<Card>
get() {
// TODO is my list always stored in the handHistory, or not?
return listOf()
val allCards = mutableListOf<Card>()
this.handHistory.playerSetups.forEach { allCards.addAll(it.cards) }
allCards.addAll(this.handHistory.board)
return allCards
}
/***

@ -1,31 +1,45 @@
package net.pokeranalytics.android.ui.modules.handhistory.views
import io.realm.RealmList
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.model.HHKeyboard
import net.pokeranalytics.android.model.realm.handhistory.PlayerSetup
import net.pokeranalytics.android.ui.modules.handhistory.HandRowType
import net.pokeranalytics.android.ui.modules.handhistory.model.HHKeyboard
import net.pokeranalytics.android.ui.modules.handhistory.model.HandHistoryRow
abstract class CardsRow(cards: List<Card>) :
HandHistoryRow {
interface CardHolder {
val cards: RealmList<Card>
}
abstract class CardsRow(var cardHolder: CardHolder?) : HandHistoryRow {
var cards: MutableList<Card> = cards.toMutableList()
private set
private val MAX_CARDS = 7
private val MAX_CARDS: Int = 7
val cardCount: Int
get() { return this.cardHolder?.cards?.size ?: 0 }
abstract fun createHolder() : CardHolder
abstract fun cardLimit() : Int?
override fun keyboardForCompletion(): HHKeyboard? {
return when {
cards.size < cardLimit() ?: MAX_CARDS -> HHKeyboard.CARD
cards.last().suit == null -> HHKeyboard.CARD
this.cardCount < cardLimit() ?: MAX_CARDS -> HHKeyboard.CARD
this.cardHolder?.cards?.lastOrNull()?.suit == null -> HHKeyboard.CARD
else -> null
}
}
fun cardAtIndex(index: Int): Card? {
if (this.cardCount > index) {
return this.cardHolder!!.cards[index]
}
return null
}
fun valueSelected(value: Card.Value) {
val card = Card.newInstance(value.value)
this.add(card)
@ -49,7 +63,7 @@ abstract class CardsRow(cards: List<Card>) :
}
protected open fun lastCard() : Card? {
return this.cards.lastOrNull()
return this.cardHolder?.cards?.lastOrNull()
}
/***
@ -61,37 +75,41 @@ abstract class CardsRow(cards: List<Card>) :
return
}
this.cards.lastOrNull()?.let {
if (this.cardHolder == null) {
this.createHolder()
}
this.cardHolder?.cards?.lastOrNull()?.let {
if (it.suit == null) {
it.suit = Card.Suit.UNDEFINED
}
}
if (this.cards.size == 5) {
if (this.cardCount == 5) {
throw PAIllegalStateException("Can't add anymore cards")
}
card.index = this.cards.size
card.index = this.cardCount
this.cards.add(card)
this.cardHolder?.cards?.add(card)
}
protected open fun canAddMoreCards(): Boolean {
this.cardLimit()?.let { limit ->
return this.cards.size < limit
return this.cardCount < limit
}
return true
}
open fun clear() {
this.cards.clear()
this.cardHolder?.cards?.clear()
}
/***
* Remove the given [card], notifies the listener
*/
private fun remove(card: Card) {
this.cards.remove(card)
this.cardHolder?.cards?.remove(card)
}
/***
@ -109,34 +127,42 @@ abstract class CardsRow(cards: List<Card>) :
}
class StreetCardsRow(var street: Street, cards: List<Card>) : CardsRow(cards) {
class StreetCardsRow(var street: Street, cardHolder: CardHolder) : CardsRow(cardHolder) {
override val viewType: Int = HandRowType.STREET.ordinal
override fun createHolder(): CardHolder {
throw PAIllegalStateException("This cannot happen")
}
override fun cardLimit() : Int {
return this.street.totalBoardCards
}
override fun lastCard(): Card? {
return when (this.street) {
Street.SUMMARY -> this.cards.lastOrNull()
else -> this.cards.lastOrNull { it.street == this.street }
Street.SUMMARY -> this.cardHolder?.cards?.lastOrNull()
else -> this.cardHolder?.cards?.lastOrNull { it.street == this.street }
}
}
override fun clear() {
when (this.street) {
Street.SUMMARY -> this.cards.clear()
else -> this.cards.removeAll { it.street == this.street }
Street.SUMMARY -> this.cardHolder?.cards?.clear()
else -> this.cardHolder?.cards?.removeAll { it.street == this.street }
}
}
}
class PlayerCardsRow(var position: Position, cards: List<Card> = listOf(), var maxCards: Int? = null) : CardsRow(cards) {
class PlayerCardsRow(var position: Position, cardHolder: CardHolder?, var maxCards: Int? = null) : CardsRow(cardHolder) {
override val viewType: Int = HandRowType.PLAYER_SUMMARY.ordinal
override fun createHolder(): CardHolder {
return PlayerSetup()
}
override fun cardLimit() : Int? {
return this.maxCards
}

Loading…
Cancel
Save