|
|
|
|
@ -4,7 +4,8 @@ import android.content.Context |
|
|
|
|
import android.os.CountDownTimer |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import kotlinx.coroutines.GlobalScope |
|
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import net.pokeranalytics.android.model.realm.CustomField |
|
|
|
|
import net.pokeranalytics.android.model.realm.Performance |
|
|
|
|
@ -12,6 +13,7 @@ import net.pokeranalytics.android.model.realm.Result |
|
|
|
|
import net.pokeranalytics.android.model.realm.Session |
|
|
|
|
import net.pokeranalytics.android.ui.view.rows.StaticReport |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import kotlin.coroutines.CoroutineContext |
|
|
|
|
|
|
|
|
|
interface NewPerformanceListener { |
|
|
|
|
fun newBestPerformanceHandler() |
|
|
|
|
@ -24,39 +26,24 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
|
|
|
|
|
var timer: CountDownTimer? = null |
|
|
|
|
|
|
|
|
|
// private lateinit var realm: Realm |
|
|
|
|
private var currentTask: ReportTask? = null |
|
|
|
|
|
|
|
|
|
private val currentNotifications: MutableList<String> = mutableListOf() |
|
|
|
|
|
|
|
|
|
private val listeners: MutableList<NewPerformanceListener> = mutableListOf() |
|
|
|
|
|
|
|
|
|
// companion object { |
|
|
|
|
// |
|
|
|
|
// @Volatile private var INSTANCE: ReportWhistleBlower? = null |
|
|
|
|
// |
|
|
|
|
// fun getInstance(context: Context, realm: Realm): ReportWhistleBlower = |
|
|
|
|
// INSTANCE ?: synchronized(this) { |
|
|
|
|
// INSTANCE ?: newInstance(context, realm).also { INSTANCE = it } |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// private fun newInstance(context: Context, realm: Realm): ReportWhistleBlower { |
|
|
|
|
// return ReportWhistleBlower(context, realm) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
this.sessions = realm.where(Session::class.java).findAll() |
|
|
|
|
this.sessions?.addChangeListener { _ -> |
|
|
|
|
launchReports() |
|
|
|
|
requestReportLaunch() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.results = realm.where(Result::class.java).findAll() |
|
|
|
|
this.results?.addChangeListener { _ -> |
|
|
|
|
launchReports() |
|
|
|
|
requestReportLaunch() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -69,10 +56,10 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
synchronized(this) { |
|
|
|
|
this.timer?.cancel() |
|
|
|
|
|
|
|
|
|
this.timer = object : CountDownTimer(500L, 0L) { |
|
|
|
|
this.timer = object : CountDownTimer(500L, 500L) { |
|
|
|
|
override fun onTick(p0: Long) { } |
|
|
|
|
override fun onFinish() { |
|
|
|
|
launchReports() |
|
|
|
|
launchReport() |
|
|
|
|
timer = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -81,49 +68,83 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun launchReports() { |
|
|
|
|
fun launchReport() { |
|
|
|
|
Timber.d(">>> Launch report") |
|
|
|
|
|
|
|
|
|
// Basic |
|
|
|
|
for (basicReport in StaticReport.basicReports) { |
|
|
|
|
launchReport(basicReport) |
|
|
|
|
} |
|
|
|
|
this.currentTask?.cancel() |
|
|
|
|
val reportTask = ReportTask(this, this.context) |
|
|
|
|
this.currentTask = reportTask |
|
|
|
|
reportTask.start() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
fun has(performanceId: String): Boolean { |
|
|
|
|
return this.currentNotifications.contains(performanceId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// CustomField |
|
|
|
|
val customFields = realm.where(CustomField::class.java) |
|
|
|
|
.equalTo("type", CustomField.Type.LIST.uniqueIdentifier).findAll() |
|
|
|
|
for (customField in customFields) { |
|
|
|
|
launchReport(StaticReport.CustomFieldList(customField)) |
|
|
|
|
} |
|
|
|
|
fun notify(performance: Performance) { |
|
|
|
|
|
|
|
|
|
this.currentNotifications.add(performance.id) |
|
|
|
|
for (listener in this.listeners) { |
|
|
|
|
listener.newBestPerformanceHandler() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun launchReport(report: StaticReport) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Timber.d(">>> launch report = $report") |
|
|
|
|
class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Context) { |
|
|
|
|
|
|
|
|
|
val options = Calculator.Options( |
|
|
|
|
stats = report.stats, |
|
|
|
|
criterias = report.criteria |
|
|
|
|
) |
|
|
|
|
private var cancelled = false |
|
|
|
|
|
|
|
|
|
this.launchReportWithOptions(report, options) |
|
|
|
|
private val coroutineContext: CoroutineContext |
|
|
|
|
get() = Dispatchers.Main |
|
|
|
|
|
|
|
|
|
fun start() { |
|
|
|
|
launchReports() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun launchReportWithOptions(staticReport: StaticReport, options: Calculator.Options) { |
|
|
|
|
fun cancel() { |
|
|
|
|
this.cancelled = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
GlobalScope.launch { |
|
|
|
|
private fun launchReports() { |
|
|
|
|
CoroutineScope(coroutineContext).launch { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
// realm.refresh() |
|
|
|
|
|
|
|
|
|
val result = Calculator.computeStats(realm, options = options) |
|
|
|
|
analyseReport(realm, staticReport, result) |
|
|
|
|
// Basic |
|
|
|
|
for (basicReport in StaticReport.basicReports) { |
|
|
|
|
if (cancelled) { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
launchReport(realm, basicReport) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// CustomField |
|
|
|
|
val customFields = realm.where(CustomField::class.java) |
|
|
|
|
.equalTo("type", CustomField.Type.LIST.uniqueIdentifier).findAll() |
|
|
|
|
for (customField in customFields) { |
|
|
|
|
if (cancelled) { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
launchReport(realm, StaticReport.CustomFieldList(customField)) |
|
|
|
|
} |
|
|
|
|
realm.close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun launchReport(realm: Realm, report: StaticReport) { |
|
|
|
|
|
|
|
|
|
Timber.d(">>> launch report = $report") |
|
|
|
|
|
|
|
|
|
val options = Calculator.Options( |
|
|
|
|
stats = report.stats, |
|
|
|
|
criterias = report.criteria |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
val result = Calculator.computeStats(realm, options = options) |
|
|
|
|
analyseReport(realm, report, result) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun analyseReport(realm: Realm, staticReport: StaticReport, result: Report) { |
|
|
|
|
@ -174,7 +195,7 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
currentPerf.objectId = computedResults.group.query.objectId |
|
|
|
|
currentPerf.customFieldId = customField?.id |
|
|
|
|
} |
|
|
|
|
this.notify(currentPerf) |
|
|
|
|
this.whistleBlower.notify(currentPerf) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -189,7 +210,7 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
null |
|
|
|
|
) |
|
|
|
|
realm.executeTransaction { it.copyToRealm(performance) } |
|
|
|
|
this.notify(performance) |
|
|
|
|
this.whistleBlower.notify(performance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -202,16 +223,4 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun notify(performance: Performance) { |
|
|
|
|
|
|
|
|
|
this.currentNotifications.add(performance.id) |
|
|
|
|
for (listener in this.listeners) { |
|
|
|
|
listener.newBestPerformanceHandler() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun has(performanceId: String): Boolean { |
|
|
|
|
return this.currentNotifications.contains(performanceId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |