From f4a4f7786e5bf472ac1743583478bba475973d69 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 18 Mar 2020 12:19:14 +0100 Subject: [PATCH] Make edit/save work as expected --- .../android/model/realm/handhistory/Card.kt | 4 +- .../ui/adapter/RowRepresentableDataSource.kt | 4 ++ .../handhistory/HandHistoryFragment.kt | 44 +++++++------------ .../ui/modules/handhistory/model/CardsRow.kt | 7 ++- .../handhistory/model/HandHistoryViewModel.kt | 10 ++--- .../handhistory/model/PlayerCardsRow.kt | 4 +- .../handhistory/model/StreetCardsRow.kt | 5 +-- .../handhistory/views/KeyboardActionView.kt | 8 ++++ .../android/ui/view/holder/RowViewHolder.kt | 4 ++ 9 files changed, 47 insertions(+), 43 deletions(-) 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 01e5f82e..6c6a9024 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 @@ -39,8 +39,8 @@ fun List.formatted(context: Context) : CharSequence? { open class Card : RealmObject() { companion object { - fun newInstance(realm: Realm?, value: Int? = null, suit: Suit? = null, index: Int = 0) : Card { - val card = if (realm != null) realm.createObject(Card::class.java) else Card() + fun newInstance(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 diff --git a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableDataSource.kt b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableDataSource.kt index 86bec578..7adb2aed 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableDataSource.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableDataSource.kt @@ -176,6 +176,10 @@ interface DisplayableDataSource { return null } + fun textColor(position: Int, row: RowRepresentable): Int? { + return null + } + } /** 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 624faec3..b719896c 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 @@ -105,15 +105,14 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL handHistoryId?.let { val handHistory = getRealm().findById(it) ?: throw PAIllegalStateException("HandHistory not found") - this.model.setHandHistory(handHistory) + val hhCopy = getRealm().copyFromRealm(handHistory) + this.model.setHandHistory(hhCopy) this.setEditing(false) } ?: run { val configurationId= this.arguments?.getString(BundleKey.CONFIGURATION_ID.value) val attached= this.arguments?.getBoolean(BundleKey.ATTACHED.value) ?: false - getRealm().executeTransaction { - this.model.createNewHandHistory(it, configurationId, attached) - } + this.model.createNewHandHistory(getRealm(), configurationId, attached) this.setEditing(true) } @@ -438,9 +437,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL override fun amountValidated() { Timber.d(">>> amount validated") - getRealm().executeTransaction { - this.model.amountValidated() - } + this.model.amountValidated() this.findNextActionToEdit(userInitiated = true) } @@ -449,31 +446,23 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } override fun amountCleared() { - getRealm().executeTransaction { - this.model.clearAmount() - } + this.model.clearAmount() } override fun cardValueSelected(value: Card.Value) { - getRealm().executeTransaction { - this.model.cardValueSelected(value) - } + 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.model.cardSuitSelected(suit) this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index) this.findNextActionToEdit() } override fun clearCards() { - getRealm().executeTransaction { - this.model.clearCards() - } + this.model.clearCards() this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index) } @@ -483,9 +472,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL } override fun cardBackSpaceSelected() { - getRealm().executeTransaction { - this.model.deleteLastCardProperty() - } + this.model.deleteLastCardProperty() this.handHistoryAdapter.notifyItemChanged(this.model.currentSelection.index) } @@ -619,11 +606,16 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL * Finishes the activity to go back */ private fun deleteHand() { - if (this.model.handHistory.isManaged) { +// if (this.model.handHistory.isManaged) { +// this.model.handHistory.deleteFromRealm() +// } + + getRealm().findById(this.model.handHistory.id)?.let { hh -> getRealm().executeTransaction { - this.model.handHistory.deleteFromRealm() + hh.deleteFromRealm() } } + this.activity?.finish() } @@ -631,9 +623,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDelegate, KeyboardL * Creates a new hand using the current hand setup */ private fun addNewHand() { - getRealm().executeTransaction { - this.model.createNewHandHistoryWithCurrentSetup(it) - } + this.model.createNewHandHistoryWithCurrentSetup() this.findNextActionToEdit(0) } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt index 27a4daea..f2f1da74 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt @@ -1,6 +1,5 @@ package net.pokeranalytics.android.ui.modules.handhistory.model -import io.realm.Realm import io.realm.RealmList import io.realm.RealmModel import net.pokeranalytics.android.model.realm.handhistory.Card @@ -22,7 +21,7 @@ abstract class CardsRow : HandHistoryRow { abstract fun cardLimit() : Int? - abstract val realmInstance: Realm +// abstract val realmInstance: Realm abstract val cardHolder: CardHolder? @@ -45,7 +44,7 @@ abstract class CardsRow : HandHistoryRow { } fun valueSelected(value: Card.Value) { - val card = Card.newInstance(this.realmInstance, value.value) + val card = Card.newInstance(value.value) this.add(card) } @@ -61,7 +60,7 @@ abstract class CardsRow : HandHistoryRow { } ?: true if (addNewCard) { - val card = Card.newInstance(this.realmInstance, suit = suit) + val card = Card.newInstance(suit = suit) this.add(card) } } 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 6ebe1ff4..f01aeddd 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 @@ -152,24 +152,24 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra fun createNewHandHistory(realm: Realm, configurationId: String?, attached: Boolean) { this.handSetup = HandSetup.from(configurationId, attached, realm) - createHandHistory(realm) + createHandHistory() } /*** * Creates a new hand history using the current HandSetup */ - fun createNewHandHistoryWithCurrentSetup(realm: Realm) { - createHandHistory(realm) + fun createNewHandHistoryWithCurrentSetup() { + createHandHistory() } /*** * Creates a hand history using the c */ - private fun createHandHistory(realm: Realm) { + private fun createHandHistory() { val handHistory = HandHistory() handHistory.configure(this.handSetup) this.playerHandMaxCards = handSetup.game?.playerHandMaxCards - this.loadHandHistory(realm.copyToRealm(handHistory)) + this.loadHandHistory(handHistory) } /*** diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt index 3a0958d9..88b3b46a 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt @@ -34,8 +34,8 @@ open class PlayerCardsRow(private var playerSetupCreationListener: PlayerSetupCr return this.maxCards } - override val realmInstance: Realm - get() { return this.handHistory.realm } +// override val realmInstance: Realm +// get() { return this.handHistory.realm } override fun holderCreated() { this.playerSetupCreationListener?.playerSetupCreated() diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/StreetCardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/StreetCardsRow.kt index 6332c9fa..a0cb8b00 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/StreetCardsRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/StreetCardsRow.kt @@ -1,6 +1,5 @@ package net.pokeranalytics.android.ui.modules.handhistory.model -import io.realm.Realm import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.realm.handhistory.Card @@ -33,8 +32,8 @@ class StreetCardsRow(var street: Street, var handHistory: HandHistory) : CardsRo return this.street.totalBoardCards } - override val realmInstance: Realm - get() { return this.handHistory.realm } +// override val realmInstance: Realm +// get() { return this.handHistory.realm } override fun lastCard(): Card? { return when (this.street) { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardActionView.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardActionView.kt index d26d7ba7..93c1f1d1 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardActionView.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/KeyboardActionView.kt @@ -91,4 +91,12 @@ class KeyboardActionView(context: Context) : AbstractKeyboardView(context), this.dataAdapter.notifyDataSetChanged() } + override fun textColor(position: Int, row: RowRepresentable): Int? { + return if (isEnabled(row, 0)) { + R.color.white + } else { + R.color.kaki_light + } + } + } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/holder/RowViewHolder.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/holder/RowViewHolder.kt index 5ef3f0d1..75535805 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/holder/RowViewHolder.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/holder/RowViewHolder.kt @@ -103,6 +103,10 @@ class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), Bindabl } it.text = title + adapter.dataSource.textColor(position, row)?.let { textColor -> + it.setTextColor(itemView.context.getColor(textColor)) + } + // val color = if (adapter.dataSource.isEnabled(row, 0)) { // R.color.white // } else {