Update for player setup + realm

hh
Laurent 6 years ago
parent e7b820dc33
commit 40e84c8a96
  1. 7
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Card.kt
  2. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt
  3. 6
      app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableDataSource.kt
  4. 30
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt
  5. 71
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryFragment.kt
  6. 12
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt
  7. 34
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/CardsRow.kt
  8. 12
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/PositionAdapter.kt

@ -4,6 +4,7 @@ import android.content.Context
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import io.realm.Realm
import io.realm.RealmObject
import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException
@ -30,11 +31,15 @@ fun List<Card>.formatted(context: Context) : CharSequence? {
open class Card : RealmObject() {
companion object {
fun newInstance(value: Int? = null, suit: Suit? = null, index: Int = 0) : Card {
fun newInstance(realm: Realm, value: Int? = null, suit: Suit? = null, index: Int = 0) : Card {
val card = Card()
value?.let { card.value = it }
suit?.let { card.suit = it}
card.index = index
realm.copyToRealm(card)
return card
}
}

@ -166,4 +166,13 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab
}
}
fun undefinedPositions(): List<Position> {
val positions = Position.positionsPerPlayers(this.numberOfPlayers)
val copy = positions.clone() as LinkedHashSet<Position>
this.playerSetups.forEach {
copy.remove(positions.elementAt(it.position))
}
return copy.toList()
}
}

@ -5,6 +5,7 @@ import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.util.TextFormat
import kotlin.reflect.KClass
/**
* Base Interface to provide the RowRepresentable to the adapter
@ -159,6 +160,11 @@ interface DisplayableDataSource {
fun isSelectable(row: RowRepresentable): Boolean {
return true
}
fun <T : Any> contentForRow(row: RowRepresentable, context: Context, clazz: KClass<T>) : List<T> {
return listOf()
}
}
/**

@ -9,6 +9,7 @@ import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip
import kotlinx.android.synthetic.main.row_hand_action.view.*
@ -30,7 +31,6 @@ import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheet
import net.pokeranalytics.android.ui.modules.handhistory.model.*
import net.pokeranalytics.android.ui.modules.handhistory.views.PlayerCardsRow
import net.pokeranalytics.android.ui.modules.handhistory.views.PositionAdapter
import net.pokeranalytics.android.ui.modules.handhistory.views.PositionSelectionListener
import net.pokeranalytics.android.ui.modules.handhistory.views.StreetCardsRow
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.holder.RowViewHolder
@ -520,15 +520,21 @@ class HandHistoryAdapter(
}
}
inner class RowHandPlayerSetup(itemView: View) : RowHandHolder(itemView), PositionSelectionListener {
inner class RowHandPlayerSetup(itemView: View) : RowHandHolder(itemView) {
private var positionAdapter: PositionAdapter = PositionAdapter(this)
private var positionAdapter: PositionAdapter = PositionAdapter()
private var delegate: RowRepresentableDelegate? = null
init {
// TODO configure recycler view
// Position Recycler
val positionViewManager = LinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
// itemView.positionRecyclerView
itemView.positionRecyclerView.apply {
setHasFixedSize(true)
layoutManager = positionViewManager
adapter = positionAdapter
}
}
@ -537,6 +543,7 @@ class HandHistoryAdapter(
val setup = row as PlayerSetupRowRepresentable
val state = setup.state
this.delegate = adapter.delegate
itemView.title.text = setup.title(itemView.context)
@ -550,21 +557,16 @@ class HandHistoryAdapter(
itemView.handEditText.setText(setup.playerSetup?.cards?.formatted(itemView.context))
itemView.stackEditText.setText(setup.playerSetup?.stack?.formatted())
this.positionAdapter.positions = listOf() // TODO
this.positionAdapter.positions = adapter.dataSource.contentForRow(row, itemView.context, Position::class)
this.positionAdapter.setOnClickListener { pos ->
this.delegate?.onRowValueChanged(pos, row)
}
// itemView.handEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 0))
// itemView.stackEditText.setText(adapter.dataSource.stringForRow(row, itemView.context, 1))
}
override fun positionSelected(position: Position) {
// TODO sets position for PlayerSetup
}
// position, stack, hand
}
}

@ -120,22 +120,6 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
this.keyboard.show(keyboard)
// when (row) {
// is ComputedAction -> {
// when (it.tag) {
// 0 -> {
// val positions = this.model.positionsToAct()
// this.keyboard.setPositions(positions)
// }
// 1 -> {
// retrieveEditTextInputConnection(selection.index)
// }
// else -> {}
// }
// }
// else -> {}
// }
} ?: run {
this.keyboard.hide()
}
@ -230,7 +214,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
this.model.selectionLiveData.value?.index?.let { oldIndex ->
refreshCells(oldIndex)
refreshRowAtIndexAndCurrent(oldIndex)
}
this.scrollToPosition(position)
@ -245,10 +229,13 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
getRealm().executeTransaction {
when (row) {
HandRowType.COMMENT -> {
this.model.handHistory.comment = value as? String
refreshCells(this.model.indexOfRowRepresentable(row))
this.handHistoryAdapter.notifyItemChanged(this.indexOfRowRepresentable(row))
}
HandRowType.PLAYER_NUMBER -> {
this.model.setNumberOfPlayers(value as Int)
@ -260,9 +247,25 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
is StraddleRowRepresentable -> {
this.model.changeStraddleSelection(value as LinkedHashSet<Position>)
}
is PlayerSetupRowRepresentable -> {
when (value) {
is Int -> {
row.playerSetup?.position = value
this.handHistoryAdapter.notifyItemChanged(this.indexOfRowRepresentable(row))
}
}
}
}
}
}
/***
* Returns the index of the row representable from the rows list
*/
private fun indexOfRowRepresentable(row: RowRepresentable): Int {
return this.model.indexOfRowRepresentable(row)
}
// Keyboard Listener
@ -273,19 +276,25 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
override fun cardValueSelected(value: Card.Value) {
getRealm().executeTransaction {
this.model.cardValueSelected(value)
}
this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index)
this.findNextActionToEdit()
}
override fun cardSuitSelected(suit: Card.Suit) {
getRealm().executeTransaction {
this.model.cardSuitSelected(suit)
}
this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index)
this.findNextActionToEdit()
}
override fun clearCards() {
getRealm().executeTransaction {
this.model.clearCards()
}
this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index)
}
@ -295,13 +304,17 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
override fun cardBackSpaceSelected() {
getRealm().executeTransaction {
this.model.deleteLastCardProperty()
}
this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index)
}
override fun amountValidated() {
Timber.d(">>> amount validated")
getRealm().executeTransaction {
this.model.amountValidated()
}
// this.handHistoryAdapter.notifyDataSetChanged()
this.findNextActionToEdit()
}
@ -311,12 +324,20 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
}
override fun amountCleared() {
getRealm().executeTransaction {
this.model.clearAmount()
}
}
/***
* Configures the action keyboard:
* - Sets the available actions
* - Sets the position shortcuts
*/
private fun configureActionKeyboard() {
val availableActions = this.model.availableActions()
this.keyboard.setAvailableAction(availableActions)
val positions = this.model.positionsToAct()
this.keyboard.setPositions(positions)
}
@ -347,12 +368,18 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL
this.recyclerView.smoothScrollToPosition(index - 1)
}
private fun refreshCells(oldIndex: Int) {
this.handHistoryAdapter.notifyItemChanged(oldIndex)
refreshCurrentRow()
/***
* Refreshes the cell at [index] and also selected row
*/
private fun refreshRowAtIndexAndCurrent(index: Int) {
this.handHistoryAdapter.notifyItemChanged(index)
refreshSelectedRow()
}
private fun refreshCurrentRow() {
/***
* Refreshes the selected row
*/
private fun refreshSelectedRow() {
this.model.selectionLiveData.value?.index?.let {
Timber.d("refreshes row at index = $it")
this.handHistoryAdapter.notifyItemChanged(it)

@ -21,6 +21,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.util.extensions.formatted
import timber.log.Timber
import kotlin.reflect.KClass
enum class HHKeyboard {
ACTION,
@ -205,7 +206,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.playerSetupForPosition(positionIndex), this.playerHandMaxCards)
val playerCardsRow = PlayerCardsRow.newInstance(it, this.handHistory, positionIndex, this.playerHandMaxCards)
rows.add(playerCardsRow)
}
}
@ -557,6 +558,15 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
}
override fun <T : Any> contentForRow(row: RowRepresentable, context: Context, clazz: KClass<T>) : List<T> {
when (clazz) {
Position::class -> {
return this.handHistory.undefinedPositions() as List<T>
}
}
return listOf()
}
fun changeStraddleSelection(positions: LinkedHashSet<Position>) {
if (positions.isEmpty()) {

@ -1,6 +1,8 @@
package net.pokeranalytics.android.ui.modules.handhistory.views
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmModel
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.handhistory.Position
import net.pokeranalytics.android.model.handhistory.Street
@ -11,7 +13,7 @@ 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
interface CardHolder {
interface CardHolder : RealmModel {
val cards: RealmList<Card>
}
@ -26,6 +28,8 @@ abstract class CardsRow(var cardHolder: CardHolder?) : HandHistoryRow {
abstract fun cardLimit() : Int?
abstract val realmInstance: Realm
override fun tagForCompletion(handHistory: HandHistory): Int? {
return when {
this.cardCount < cardLimit() ?: MAXCARDS -> 0
@ -46,7 +50,7 @@ abstract class CardsRow(var cardHolder: CardHolder?) : HandHistoryRow {
}
fun valueSelected(value: Card.Value) {
val card = Card.newInstance(value.value)
val card = Card.newInstance(this.realmInstance, value.value)
this.add(card)
}
@ -62,7 +66,7 @@ abstract class CardsRow(var cardHolder: CardHolder?) : HandHistoryRow {
} ?: true
if (addNewCard) {
val card = Card.newInstance(suit = suit)
val card = Card.newInstance(this.realmInstance, suit = suit)
this.add(card)
}
}
@ -132,7 +136,7 @@ abstract class CardsRow(var cardHolder: CardHolder?) : HandHistoryRow {
}
class StreetCardsRow(var street: Street, cardHolder: CardHolder) : CardsRow(cardHolder) {
class StreetCardsRow(var street: Street, var handHistory: HandHistory) : CardsRow(handHistory) {
override val viewType: Int = HandRowType.STREET.ordinal
@ -152,6 +156,9 @@ class StreetCardsRow(var street: Street, cardHolder: CardHolder) : CardsRow(card
return this.street.totalBoardCards
}
override val realmInstance: Realm
get() { return this.handHistory.realm }
override fun lastCard(): Card? {
return when (this.street) {
Street.SUMMARY -> this.cardHolder?.cards?.lastOrNull()
@ -168,16 +175,31 @@ class StreetCardsRow(var street: Street, cardHolder: CardHolder) : CardsRow(card
}
class PlayerCardsRow(var position: Position, cardHolder: CardHolder?, var maxCards: Int? = null) : CardsRow(cardHolder) {
class PlayerCardsRow(var position: Position, var handHistory: HandHistory, cardHolder: CardHolder?, var maxCards: Int? = null) : CardsRow(cardHolder) {
companion object {
fun newInstance(position: Position, handHistory: HandHistory, positionIndex: Int, maxCards: Int? = null): PlayerCardsRow {
val playerSetup = handHistory.playerSetupForPosition(positionIndex)
return PlayerCardsRow(position, handHistory, playerSetup, maxCards)
}
}
override val viewType: Int = HandRowType.PLAYER_SUMMARY.ordinal
override fun createHolder(): CardHolder {
return PlayerSetup()
val playerSetup = PlayerSetup()
realmInstance.copyToRealm(playerSetup)
this.handHistory.playerSetups.add(playerSetup)
return playerSetup
}
override fun cardLimit() : Int? {
return this.maxCards
}
override val realmInstance: Realm
get() { return this.handHistory.realm }
}

@ -14,13 +14,15 @@ import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.holder.RowViewHolder
import timber.log.Timber
class PositionAdapter(var listener: PositionSelectionListener) :
class PositionAdapter(var listener: PositionSelectionListener? = null) :
RecyclerView.Adapter<RecyclerView.ViewHolder>(),
RowRepresentableDataSource, RowRepresentableDelegate, RecyclerAdapter {
override var dataSource: RowRepresentableDataSource = this
override var delegate: RowRepresentableDelegate? = this
private var rawListener: ((Int) -> (Unit))? = null
var positions: List<Position> = listOf()
set(value) {
field = value
@ -60,8 +62,12 @@ class PositionAdapter(var listener: PositionSelectionListener) :
}
override fun onRowSelected(position: Int, row: RowRepresentable, tag: Int) {
Timber.d("/////onRowSelected")
this.listener.positionSelected(this.positions[position])
this.listener?.positionSelected(this.positions[position])
this.rawListener?.invoke(position)
}
fun setOnClickListener(listener: (Int) -> (Unit)) {
this.rawListener = listener
}
}
Loading…
Cancel
Save