parent
31c2038857
commit
4c79b32c1f
@ -0,0 +1,114 @@ |
||||
package net.pokeranalytics.android.calculus.bankroll |
||||
|
||||
import io.realm.Realm |
||||
import io.realm.RealmResults |
||||
import kotlinx.coroutines.Dispatchers |
||||
import kotlinx.coroutines.GlobalScope |
||||
import kotlinx.coroutines.async |
||||
import kotlinx.coroutines.launch |
||||
import net.pokeranalytics.android.model.realm.Bankroll |
||||
import net.pokeranalytics.android.model.realm.ComputableResult |
||||
import net.pokeranalytics.android.model.realm.Transaction |
||||
import timber.log.Timber |
||||
import java.util.* |
||||
import kotlin.coroutines.CoroutineContext |
||||
|
||||
object BankrollReportManager { |
||||
|
||||
val coroutineContext: CoroutineContext |
||||
get() = Dispatchers.Main |
||||
|
||||
private var reports: MutableMap<String?, BankrollReport> = mutableMapOf() |
||||
|
||||
private var computableResults: RealmResults<ComputableResult> |
||||
private var bankrolls: RealmResults<Bankroll> |
||||
private var transactions: RealmResults<Transaction> |
||||
|
||||
init { |
||||
|
||||
val realm = Realm.getDefaultInstance() |
||||
computableResults = realm.where(ComputableResult::class.java).findAll() |
||||
bankrolls = realm.where(Bankroll::class.java).findAll() |
||||
transactions = realm.where(Transaction::class.java).findAll() |
||||
|
||||
initializeListeners() |
||||
realm.close() |
||||
} |
||||
|
||||
/** |
||||
* Listens to all objects that might have an impact on any bankroll report |
||||
*/ |
||||
private fun initializeListeners() { |
||||
|
||||
this.computableResults.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it]?.session?.bankroll }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
this.bankrolls.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it] }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
this.transactions.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it]?.bankroll }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
} |
||||
|
||||
fun reportForBankroll(bankrollId: String?, handler: (BankrollReport) -> Unit) { |
||||
|
||||
// if the report exists, return it |
||||
val existingReport: BankrollReport? = this.reports[bankrollId] |
||||
if (existingReport != null) { |
||||
handler(existingReport) |
||||
return |
||||
} |
||||
|
||||
// otherwise compute it |
||||
GlobalScope.launch(coroutineContext) { |
||||
|
||||
var report: BankrollReport? = null |
||||
val scope = GlobalScope.async { |
||||
val s = Date() |
||||
Timber.d(">>>>> start computing bankroll...") |
||||
|
||||
val realm = Realm.getDefaultInstance() |
||||
|
||||
val setup = BankrollReportSetup(bankrollId) |
||||
report = BankrollCalculator.computeReport(realm, setup) |
||||
|
||||
realm.close() |
||||
|
||||
val e = Date() |
||||
val duration = (e.time - s.time) / 1000.0 |
||||
Timber.d(">>>>> ended in $duration seconds") |
||||
|
||||
} |
||||
scope.await() |
||||
report?.let { handler(it) } |
||||
|
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Notifies the manager of cases not managed by RealmResults listener, such as deletions |
||||
*/ |
||||
fun notifyBankrollReportImpact(bankrollId: String) { |
||||
this.reports.remove(bankrollId) |
||||
this.reports.remove(null) |
||||
} |
||||
|
||||
private fun updateBankrolls(bankrolls: Set<Bankroll>) { |
||||
this.invalidateReport(bankrolls) |
||||
} |
||||
|
||||
private fun invalidateReport(bankrolls: Set<Bankroll>) { |
||||
this.reports.remove(null) |
||||
bankrolls.forEach { br -> |
||||
this.reports.remove(br.id) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,12 @@ |
||||
package net.pokeranalytics.android.ui.view.rowrepresentable |
||||
|
||||
import net.pokeranalytics.android.ui.fragment.BankrollRowRepresentable |
||||
import net.pokeranalytics.android.ui.view.RowViewType |
||||
|
||||
class BankrollMainRow : BankrollRowRepresentable { |
||||
|
||||
override var bankrollId: String? = null |
||||
|
||||
override val viewType: Int = RowViewType.LEGEND_DEFAULT.ordinal |
||||
|
||||
} |
||||
Loading…
Reference in new issue