@ -4,7 +4,6 @@ import android.content.Context
import io.realm.Realm
import io.realm.RealmQuery
import io.realm.RealmResults
import io.realm.kotlin.where
import net.pokeranalytics.android.exceptions.ModelException
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.realm.Currency
@ -37,18 +36,21 @@ object DataManager {
val realm = Realm . getDefaultInstance ( )
sessions = realm . where ( Session :: class . java ) . findAllAsync ( )
sessions ?. addChangeListener { results ->
results . realm . writeAsync { asyncRealm ->
sessions ?. addChangeListener { sessions ->
// if (this.dateModifiedSessionIds.isNotEmpty() || this.statsToComputeSessionIds.isNotEmpty()) {
sessions . realm . writeAsync { asyncRealm ->
computeStatsIfNecessary ( asyncRealm )
computeDatesIfNecessary ( asyncRealm )
reportWhistleBlower ?. requestReportLaunch ( )
}
// }
}
this . currencies = realm . where ( Currency :: class . java ) . findAll ( )
this . currencies ?. addChangeListener { _ , _ ->
this . currencies ?. addChangeListener { currencies , _ ->
if ( changedCurrencies . isNotEmpty ( ) ) {
realm . writeAsync { asyncRealm ->
currencies . realm . writeAsync { asyncRealm ->
for ( currencyId in this . changedCurrencies ) {
asyncRealm . findById < Currency > ( currencyId ) ?. let { currency ->
Timber . d ( " Compute currency ${currency.code} " )
@ -68,25 +70,25 @@ object DataManager {
}
fun sessionToCompute ( session : Session ) {
Timber . d ( " sessionToCompute " )
// Timber.d("sessionToCompute, date = ${session.startDate} / ${session.endDate}" )
statsToComputeSessionIds . add ( session . id )
}
fun sessionDateChanged ( session : Session ) {
Timber . d ( " sessionDateChanged " )
// Timber.d("sessionDateChanged" )
dateModifiedSessionIds . add ( session . id )
}
fun currencyToCompute ( currency : Currency ) {
Timber . d ( " sessionToCompute " )
// Timber.d("currencyToCompute" )
changedCurrencies . add ( currency . id )
}
private fun computeStatsIfNecessary ( realm : Realm ) {
if ( statsToComputeSessionIds . isNotEmpty ( ) ) {
for ( sessionId in statsToComputeSessionIds ) {
Timber . d ( " Session Manager > compute stats " )
realm . findById < Session > ( sessionId ) ?. let { session ->
// Timber.d("Session Manager > compute stats, set = ${session.sessionSet}")
session . computeStats ( )
session . sessionSet ?. computeStats ( )
}
@ -98,8 +100,8 @@ object DataManager {
private fun computeDatesIfNecessary ( realm : Realm ) {
if ( dateModifiedSessionIds . isNotEmpty ( ) ) {
for ( sessionId in dateModifiedSessionIds ) {
Timber . d ( " Session Manager > manage dates " )
realm . findById < Session > ( sessionId ) ?. let { session ->
// Timber.d("Session Manager > manage dates, set = ${session.sessionSet}")
if ( session . endDate != null ) {
updateTimeline ( session )
} else if ( session . sessionSet != null ) {
@ -116,7 +118,7 @@ object DataManager {
* /
fun updateTimeline ( session : Session ) {
Timber . d ( " updateTimeline... " )
// Timber.d("updateTimeline..." )
if ( ! session . realm . isInTransaction ) {
throw PAIllegalStateException ( " realm should be in transaction at this point " )
@ -160,7 +162,7 @@ object DataManager {
* Does that by deleting then recreating
* /
private fun cleanupSessionSets ( session : Session , sessionSets : RealmResults < SessionSet > ) {
Timber . d ( " cleanupSessionSets... " )
// Timber.d("cleanupSessionSets..." )
// get all endedSessions from sets
val allImpactedSessions = mutableSetOf < Session > ( )
@ -183,7 +185,7 @@ object DataManager {
* Update the global timeline using the impacted [ sessionSets ] and the updated [ session ]
* /
private fun updateTimeFrames ( sessionSets : RealmResults < SessionSet > , session : Session ) {
Timber . d ( " updateTimeFrames... " )
// Timber.d("updateTimeFrames..." )
when ( sessionSets . size ) {
0 -> createOrUpdateSessionSet ( session )
@ -196,7 +198,7 @@ object DataManager {
* Creates or update the session set for the [ session ]
* /
private fun createOrUpdateSessionSet ( session : Session ) {
Timber . d ( " createOrUpdateSessionSet... " )
// Timber.d("createOrUpdateSessionSet..." )
val set = session . sessionSet
if ( set != null ) {
@ -212,7 +214,7 @@ object DataManager {
* Create a set and affect it to the [ session ]
* /
private fun createSessionSet ( session : Session ) {
Timber . d ( " createSessionSet... " )
// Timber.d("createSessionSet..." )
val realm = session . realm
val set = SessionSet . newInstance ( realm )
@ -222,11 +224,11 @@ object DataManager {
session . sessionSet = set
set . computeStats ( )
Timber . d ( " SET SESSION count = ${set.sessions?.size} " )
val t = 0
val f = realm . where < SessionSet > ( ) . equalTo ( " sessions.type " , t ) . findAll ( )
Timber . d ( " CASH SET COUNT = ${f.size} " )
// Timber.d("SET SESSION count = ${set.sessions?.size}" )
//
// val t = 0
// val f = realm.where<SessionSet>().equalTo("sessions.type", t).findAll( )
// Timber.d("CASH SET COUNT = ${f.size}" )
}
@ -236,7 +238,7 @@ object DataManager {
* /
private fun mergeSessionGroups ( session : Session , sessionSets : RealmResults < SessionSet > ) {
Timber . d ( " mergeSessionGroups " )
// Timber.d("mergeSessionGroups" )
var startDate = session . startDate !!
var endDate = session . endDate !!
@ -291,7 +293,7 @@ object DataManager {
* /
fun removeFromTimeline ( session : Session ) {
Timber . d ( " removeFromTimeline " )
// Timber.d("removeFromTimeline" )
if ( ! session . realm . isInTransaction ) {
throw PAIllegalStateException ( " realm should be in transaction at this point " )
@ -304,7 +306,7 @@ object DataManager {
sessionSet . sessions ?. asIterable ( ) ?. let { sessions . addAll ( it ) }
sessions . remove ( session )
Timber . d ( " >>> sessionSet.deleteFromRealm " )
// Timber.d(">>> sessionSet.deleteFromRealm" )
sessionSet . deleteFromRealm ( )
sessions . forEach {