diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Card.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Card.kt index 0f8d53e2..4e1a9fd4 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Card.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Card.kt @@ -32,14 +32,10 @@ open class Card : RealmObject() { companion object { fun newInstance(realm: Realm, value: Int? = null, suit: Suit? = null, index: Int = 0) : Card { - - val card = Card() + val card = realm.createObject(Card::class.java) value?.let { card.value = it } suit?.let { card.suit = it} card.index = index - - realm.copyToRealm(card) - return card } } diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt index fbb517a9..da677968 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt @@ -175,4 +175,11 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab return copy.toList() } + fun createPlayerSetup(positionIndex: Int): PlayerSetup { + val playerSetup = this.realm.createObject(PlayerSetup::class.java) + playerSetup.position = positionIndex + this.playerSetups.add(playerSetup) + return playerSetup + } + } \ No newline at end of file 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 2109928b..70a9ee00 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 @@ -545,22 +545,42 @@ class HandHistoryAdapter( val state = setup.state this.delegate = adapter.delegate + // Title itemView.title.text = setup.title(itemView.context) - val visibility = if (state == PlayerSetupRowRepresentable.State.SHOW_POSITION) View.VISIBLE else View.GONE + // Position recycler + val visibility = if (state == PlayerSetupRowRepresentable.State.SETUP_ONLY) View.GONE else View.VISIBLE itemView.positionRecyclerView.visibility = visibility + this.positionAdapter.positions = adapter.dataSource.contentForRow(row, itemView.context, Position::class) + this.positionAdapter.setOnClickListener { pos -> + setup.closePosition() + this.delegate?.onRowValueChanged(pos, row) + } + + // Position Button itemView.posButton.text = setup.position?.value + itemView.posButton.setOnClickListener { + setup.showPosition() + itemView.positionRecyclerView.visibility = View.VISIBLE + itemView.posButton.backgroundTintList = ColorStateList.valueOf(color(true)) + } + val positionSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRowRepresentable.Tag.POSITION.ordinal) + itemView.posButton.backgroundTintList = ColorStateList.valueOf(color(positionSelected)) - itemView.settings_container.visibility = if (state == PlayerSetupRowRepresentable.State.POSITION_ONLY) View.GONE else View.VISIBLE + // Settings + itemView.settings_container.visibility = if (state == PlayerSetupRowRepresentable.State.POSITIONS_ONLY) View.GONE else View.VISIBLE + // Hand itemView.handEditText.setText(setup.playerSetup?.cards?.formatted(itemView.context)) + val handSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRowRepresentable.Tag.CARDS.ordinal) + itemView.handEditText.setBackgroundColor(color(handSelected)) + + // Stack itemView.stackEditText.setText(setup.playerSetup?.stack?.formatted()) + val stackSelected = adapter.dataSource.isSelected(position, row, PlayerSetupRowRepresentable.Tag.STACK.ordinal) + itemView.stackEditText.setBackgroundColor(color(stackSelected)) - 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)) 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 e358f84b..a21e257e 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 @@ -83,7 +83,9 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL ?: throw PAIllegalStateException("HandHistory not found") this.model.setHandHistory(handHistory) } ?: run { - this.model.createNewHandHistory(HandSetup()) + getRealm().executeTransaction { + this.model.createNewHandHistory(it, HandSetup()) + } } } @@ -250,8 +252,10 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL is PlayerSetupRowRepresentable -> { when (value) { is Int -> { - row.playerSetup?.position = value - this.handHistoryAdapter.notifyItemChanged(this.indexOfRowRepresentable(row)) + this.model.setPlayerSetupPosition(row, value) + val index = this.indexOfRowRepresentable(row) + this.handHistoryAdapter.notifyItemChanged(index) + this.findNextActionToEdit(index) } } } 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 f1740c72..a651467d 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 @@ -3,6 +3,7 @@ package net.pokeranalytics.android.ui.modules.handhistory.model import android.content.Context import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import io.realm.Realm import net.pokeranalytics.android.R import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.HandSetup @@ -133,15 +134,12 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra /*** * Creates and configures a new HandHistory object using a [handSetup] */ - fun createNewHandHistory(handSetup: HandSetup) { - + fun createNewHandHistory(realm: Realm, handSetup: HandSetup) { this.handSetup = handSetup val handHistory = HandHistory() handHistory.configure(handSetup) this.playerHandMaxCards = handSetup.game?.playerHandMaxCards - - this.loadHandHistory(handHistory) - + this.loadHandHistory(realm.copyToRealm(handHistory)) } /*** @@ -586,6 +584,11 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra } + /*** + * Returns the list of PlayerSetupRowRepresentable for the hand + * Will show rows for existing PlayerSetup, as well as a row for the hero if not set, + * and a row for a new player if necessary + */ private fun playerSetups(): List { val hh = this.handHistory @@ -608,4 +611,32 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra } + /*** + * Sets the both [positionIndex] and position for a PlayerSetup inside a [psRowRepresentable] + */ + fun setPlayerSetupPosition(psRowRepresentable: PlayerSetupRowRepresentable, positionIndex: Int) { + + var setupCreated = false + psRowRepresentable.playerSetup?.let { + it.position = positionIndex + } ?: run { + psRowRepresentable.playerSetup = this.handHistory.createPlayerSetup(positionIndex) + setupCreated = true + } + + val position = this.sortedActions.positions.elementAt(positionIndex) + psRowRepresentable.position = position + + if (!psRowRepresentable.hero && setupCreated) { + + if (this.handHistory.undefinedPositions().isNotEmpty()) { + val newPlayerRow = PlayerSetupRowRepresentable(false, null) + val index = this.indexOfRowRepresentable(psRowRepresentable) + this.rowsLiveData.value?.add(index + 1, newPlayerRow) + } + + } + + } + } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRowRepresentable.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRowRepresentable.kt index bb5108ba..7fb6e9f1 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRowRepresentable.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRowRepresentable.kt @@ -2,45 +2,81 @@ package net.pokeranalytics.android.ui.modules.handhistory.model import android.content.Context import net.pokeranalytics.android.R +import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.Position +import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.PlayerSetup import net.pokeranalytics.android.ui.modules.handhistory.HandRowType -import net.pokeranalytics.android.ui.view.RowRepresentable class PlayerSetupRowRepresentable(var hero: Boolean = false, var playerSetup: PlayerSetup? = null) : - RowRepresentable { + HandHistoryRow { - var positionEdited: Boolean = false var position: Position? = null enum class State { - POSITION_ONLY, - SHOW_POSITION, - DEFAULT, + POSITIONS_ONLY, + SETUP_ONLY, + BOTH, } - val state: State - get() { - return when { - this.positionEdited -> State.SHOW_POSITION - (this.playerSetup == null) -> State.POSITION_ONLY - else -> State.DEFAULT - } - } + enum class Tag { + POSITION, + CARDS, + STACK + } + + var state: State = State.POSITIONS_ONLY fun title(context: Context): String { return if (this.hero) { context.getString(R.string.hero) } else { - this.playerSetup?.player?.name?.let { - it + this.playerSetup?.let { setup -> + setup.player?.name?.let { + it + } ?: run { + this.position?.value + } } ?: run { - context.getString(R.string.vilain) + context.getString(R.string.add_vilain) } } } + fun showPosition() { + this.state = State.BOTH + } + + fun closePosition() { + this.state = State.SETUP_ONLY + } + override val viewType: Int = HandRowType.PLAYER_SETUP.ordinal + override fun tagForCompletion(handHistory: HandHistory): Int? { + this.playerSetup?.let { + if (it.cards.isEmpty()) { + return Tag.CARDS.ordinal + } else if (it.stack == null) { + return Tag.STACK.ordinal + } + } + return null + } + + override fun keyboardForTag(tag: Int): HHKeyboard { + return when (tag) { + Tag.CARDS.ordinal -> HHKeyboard.CARD + Tag.STACK.ordinal -> HHKeyboard.AMOUNT + else -> throw PAIllegalStateException("unmanaged tag: $tag") + } + } + + override fun amountForTag(handHistory: HandHistory, tag: Int): Double? { + return when (tag) { + Tag.STACK.ordinal -> this.playerSetup?.stack + else -> throw PAIllegalStateException("unmanaged tag: $tag") + } + } } \ 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 8f0915c5..891e0cfd 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 @@ -8,7 +8,6 @@ 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.model.realm.handhistory.HandHistory -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 @@ -175,13 +174,13 @@ class StreetCardsRow(var street: Street, var handHistory: HandHistory) : CardsRo } -class PlayerCardsRow(var position: Position, var handHistory: HandHistory, cardHolder: CardHolder?, var maxCards: Int? = null) : CardsRow(cardHolder) { +class PlayerCardsRow(var position: Position, var positionIndex: Int, 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) + return PlayerCardsRow(position, positionIndex, handHistory, playerSetup, maxCards) } } @@ -189,10 +188,7 @@ class PlayerCardsRow(var position: Position, var handHistory: HandHistory, cardH override val viewType: Int = HandRowType.PLAYER_SUMMARY.ordinal override fun createHolder(): CardHolder { - val playerSetup = PlayerSetup() - realmInstance.copyToRealm(playerSetup) - this.handHistory.playerSetups.add(playerSetup) - return playerSetup + return this.handHistory.createPlayerSetup(this.positionIndex) } override fun cardLimit() : Int? { diff --git a/app/src/main/res/layout/row_hhsettings_player_setup.xml b/app/src/main/res/layout/row_hhsettings_player_setup.xml index 9d74711c..f7734390 100644 --- a/app/src/main/res/layout/row_hhsettings_player_setup.xml +++ b/app/src/main/res/layout/row_hhsettings_player_setup.xml @@ -2,13 +2,14 @@ + android:layout_height="wrap_content"> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0f99ee83..d85c4bcd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -792,6 +792,6 @@ r_allin mississipi - vilain + vilain