diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index d2cc66b1..fc2f3259 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -36,7 +36,6 @@ import net.pokeranalytics.android.util.extensions.hourMinute import net.pokeranalytics.android.util.extensions.shortDateTime import net.pokeranalytics.android.util.extensions.toCurrency import net.pokeranalytics.android.util.extensions.toMinutes -import timber.log.Timber import java.text.DateFormat import java.text.NumberFormat import java.text.ParseException @@ -251,10 +250,6 @@ open class Session : RealmObject(), Savable, RowRepresentable, Timed, * The start date of the break */ override var pauseDate: Date? = null - set(value) { - field = value -// this.updateRowRepresentation() - } // The session set containing the sessions, which can contain multiple endedSessions var sessionSet: SessionSet? = null @@ -385,7 +380,6 @@ open class Session : RealmObject(), Savable, RowRepresentable, Timed, set(value) { field = value this.computeNumberOfRebuy() -// SessionManager.sessionNetChanged(this.session) this.computeNet(true) } @@ -857,7 +851,7 @@ open class Session : RealmObject(), Savable, RowRepresentable, Timed, return "Session ${this.creationDate}" } - fun updateValue(value: Any?, row: RowRepresentable, realm: Realm) { + fun updateValue(value: Any?, row: RowRepresentable) { when (row) { SessionPropertiesRow.BANKROLL -> bankroll = value as Bankroll? @@ -1106,7 +1100,7 @@ open class Session : RealmObject(), Savable, RowRepresentable, Timed, @Ignore override val realmObjectClass: Class = Session::class.java - fun charSequenceForRow(row: RowRepresentable, context: Context): String { + fun charSequenceForRow(row: RowRepresentable, context: Context, realm: Realm): String { return when (row) { SessionPropertiesRow.BANKROLL -> bankroll?.name ?: NULL_TEXT @@ -1146,11 +1140,14 @@ open class Session : RealmObject(), Savable, RowRepresentable, Timed, } } SessionPropertiesRow.TOURNAMENT_NAME -> tournamentName?.name ?: NULL_TEXT - SessionPropertiesRow.HANDS -> this.handHistories?.size.toString() + SessionPropertiesRow.HANDS -> { + val handHistories = realm.where(HandHistory::class.java).equalTo("session.id", this.id).findAll() + handHistories.size.toString() + } SessionPropertiesRow.HANDS_COUNT -> this.handsCountFormatted(context) SessionPropertiesRow.NUMBER_OF_TABLES -> this.numberOfTables.toString() is CustomField -> { - Timber.d("entries count = ${customFieldEntries.size}") +// Timber.d("entries count = ${customFieldEntries.size}") customFieldEntries.find { it.customField?.id == row.id }?.let { customFieldEntry -> return customFieldEntry.getFormattedValue(currency) } 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 1a41419d..a207b961 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 @@ -198,8 +198,16 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable, handSetup.ante?.let { this.ante = it } handSetup.blinds?.let { this.blinds = it } - this.session = handSetup.session - this.date = this.session?.handHistoryAutomaticDate ?: Date() + handSetup.session?.let { session -> + this.date = session.handHistoryAutomaticDate + session.realm?.let { realm -> + this.session = realm.copyFromRealm(session) + } ?: run { + this.session = session + } + } ?: run { + this.date = Date() + } this.createActions(handSetup) } diff --git a/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt b/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt index c2a01596..adfd487e 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/utils/FavoriteSessionFinder.kt @@ -11,11 +11,11 @@ import net.pokeranalytics.android.ui.view.rows.SessionPropertiesRow * Returns all significant parameters concatenated in a String * Not suitable for display */ -private fun Session.parameterRepresentation(context: Context): String { +private fun Session.parameterRepresentation(context: Context, realm: Realm): String { var representation = "" this.significantFields().forEach { - representation += this.charSequenceForRow(it, context) + representation += this.charSequenceForRow(it, context, realm) } return representation @@ -117,7 +117,7 @@ class FavoriteSessionFinder { val counters = hashMapOf() lastSessions.forEach { session -> - val representation = session.parameterRepresentation(context) + val representation = session.parameterRepresentation(context, realm) val counter = counters[representation] if (counter != null) { counter.increment() diff --git a/app/src/main/java/net/pokeranalytics/android/model/utils/SessionManager.kt b/app/src/main/java/net/pokeranalytics/android/model/utils/SessionManager.kt index 1a0a47b8..8a79bf94 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/utils/SessionManager.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/utils/SessionManager.kt @@ -23,15 +23,15 @@ object SessionManager { // private var results: RealmResults? = null private var dateModifiedSessionIds: MutableSet = mutableSetOf() - private var netModifiedSessionIds: MutableSet = mutableSetOf() +// private var netModifiedSessionIds: MutableSet = mutableSetOf() private var statsToComputeSessionIds: MutableSet = mutableSetOf() init { val realm = Realm.getDefaultInstance() - this.sessions = realm.where(Session::class.java).findAll() - this.sessions?.addChangeListener { results -> + this.sessions = realm.where(Session::class.java).findAllAsync() + this.sessions?.addChangeListener { results, changeSet -> if (this.dateModifiedSessionIds.isNotEmpty()) { results.realm.writeAsync { asyncRealm -> @@ -48,14 +48,14 @@ object SessionManager { } } - if (this.netModifiedSessionIds.isNotEmpty()) { - results.realm.writeAsync { asyncRealm -> - for (sessionId in netModifiedSessionIds) { - asyncRealm.findById(sessionId)?.computeNet(false) - } - this.dateModifiedSessionIds.clear() - } - } +// if (this.netModifiedSessionIds.isNotEmpty()) { +// results.realm.writeAsync { asyncRealm -> +// for (sessionId in netModifiedSessionIds) { +// asyncRealm.findById(sessionId)?.computeNet(false) +// } +// this.dateModifiedSessionIds.clear() +// } +// } if (this.statsToComputeSessionIds.isNotEmpty()) { results.realm.writeAsync { asyncRealm -> @@ -78,9 +78,9 @@ object SessionManager { fun sessionDateChanged(session: Session) { this.dateModifiedSessionIds.add(session.id) } - fun sessionNetChanged(session: Session) { - this.netModifiedSessionIds.add(session.id) - } +// fun sessionNetChanged(session: Session) { +// this.netModifiedSessionIds.add(session.id) +// } fun sessionToCompute(session: Session) { this.statsToComputeSessionIds.add(session.id) } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt index 9750a4ed..ea55c043 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt @@ -28,6 +28,7 @@ import net.pokeranalytics.android.model.extensions.getState import net.pokeranalytics.android.model.extensions.scheduleStopNotification import net.pokeranalytics.android.model.interfaces.SaveValidityStatus import net.pokeranalytics.android.model.realm.* +import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.utils.FavoriteSessionFinder import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate @@ -255,7 +256,8 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr showBottomSheet(row, this, data, false, session.currency) } SessionPropertiesRow.HANDS -> { - val hhIds = session.handHistories?.map { it.id }?.toTypedArray() + val handHistories = getRealm().where(HandHistory::class.java).equalTo("session.id", session.id).findAll() + val hhIds = handHistories?.map { it.id }?.toTypedArray() DataListActivity.newInstance(this, LiveData.HAND_HISTORY, false, hhIds, false) } else -> showBottomSheet(row, this, data, currentCurrency = session.currency) @@ -265,7 +267,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr override fun onRowValueChanged(value: Any?, row: RowRepresentable) { this.sessionHasBeenUserCustomized = true try { - this.currentSession.updateValue(value, row, getRealm()) + this.currentSession.updateValue(value, row) this.writeChanges() } catch (e: PAIllegalStateException) { Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() @@ -499,7 +501,10 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr val id = currentSession.id getRealm().writeAsync { asyncRealm -> - asyncRealm.findById(id)?.delete() + asyncRealm.findById(id)?.let { session -> + session.cleanup() + session.deleteFromRealm() + } } bankrollId?.let { @@ -549,7 +554,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate, StaticRowRepr } override fun charSequenceForRow(row: RowRepresentable, context: Context): String { - return this.currentSession.charSequenceForRow(row, context) + return this.currentSession.charSequenceForRow(row, context, getRealm()) } override fun actionIconForRow(row: RowRepresentable): Int? { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionViewModel.kt index d897b826..16cbc226 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionViewModel.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionViewModel.kt @@ -99,11 +99,14 @@ class SessionViewModel : ViewModel() { } // Rows - rows.addAll(SessionPropertiesRow.getRows(session, this.resultCaptureType, context)) + rows.addAll(SessionPropertiesRow.getRows(session, this.resultCaptureType, context, realm)) // Add custom fields rows.add(SeparatorRow()) - rows.addAll(realm.sorted()) + + val customFields = realm.sorted() + val unmanaged = customFields.map { realm.copyFromRealm(it) } + rows.addAll(unmanaged) this.rows = rows } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/rows/SessionPropertiesRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/rows/SessionPropertiesRow.kt index f40ba622..fad93fb8 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/rows/SessionPropertiesRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/rows/SessionPropertiesRow.kt @@ -2,6 +2,7 @@ package net.pokeranalytics.android.ui.view.rows import android.content.Context import android.text.InputType +import io.realm.Realm import io.realm.RealmResults import net.pokeranalytics.android.R import net.pokeranalytics.android.model.TournamentType @@ -9,6 +10,7 @@ import net.pokeranalytics.android.model.extensions.SessionState import net.pokeranalytics.android.model.extensions.getState import net.pokeranalytics.android.model.realm.ResultCaptureType import net.pokeranalytics.android.model.realm.Session +import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor @@ -48,7 +50,7 @@ enum class SessionPropertiesRow : RowRepresentable { /** * Return the rows to display for the current session state */ - fun getRows(session: Session, resultCaptureType: ResultCaptureType?, context: Context): List { + fun getRows(session: Session, resultCaptureType: ResultCaptureType?, context: Context, realm: Realm): List { val state = session.getState() when (session.type) { Session.Type.TOURNAMENT.ordinal -> { @@ -77,6 +79,8 @@ enum class SessionPropertiesRow : RowRepresentable { TIPS)) fields.add(SeparatorRow()) fields.add(COMMENT) + + if (session.handHistories?.isNotEmpty() == true) { fields.add(HANDS) } @@ -133,7 +137,9 @@ enum class SessionPropertiesRow : RowRepresentable { // PROPERTIES fields.add(SeparatorRow()) fields.add(COMMENT) - if (session.handHistories?.isNotEmpty() == true) { + + val handHistories = realm.where(HandHistory::class.java).equalTo("session.id", session.id).findAll() + if (handHistories?.isNotEmpty() == true) { fields.add(HANDS) } fields.add(SeparatorRow()) diff --git a/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/BottomSheetViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/BottomSheetViewModel.kt index f6ce2370..b965df46 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/BottomSheetViewModel.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/viewmodel/BottomSheetViewModel.kt @@ -226,7 +226,13 @@ class BottomSheetViewModel(var row: RowRepresentable) : ViewModel() { private fun selectedRowsRealmCopy(): List<*> { val realmObjects = this.selectedRows.filterIsInstance() return if (realmObjects.isNotEmpty()) { - realmObjects.map { it.realm.copyFromRealm(it) } + realmObjects.map { obj -> + if (obj.isManaged) { + obj.realm.copyFromRealm(obj) + } else { + obj + } + } } else { this.selectedRows }