Various fixes

powerreport
Laurent 3 years ago
parent 925ab12faf
commit af1024c7bc
  1. 117
      app/src/main/java/net/pokeranalytics/android/calculus/ReportWhistleBlower.kt
  2. 12
      app/src/main/java/net/pokeranalytics/android/ui/fragment/ReportsFragment.kt
  3. 5
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/BaseFragment.kt
  4. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/rows/StaticReport.kt
  5. 1
      app/src/main/res/layout/fragment_reports.xml

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

@ -11,8 +11,8 @@ import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.Realm import io.realm.Realm
import io.realm.RealmResults import io.realm.RealmResults
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
@ -117,10 +117,12 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
} else if (requestCode == RequestCode.DEFAULT.value && resultCode == Activity.RESULT_OK) { } else if (requestCode == RequestCode.DEFAULT.value && resultCode == Activity.RESULT_OK) {
val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName) val itemToDeleteId = data?.getStringExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName)
itemToDeleteId?.let { id -> itemToDeleteId?.let { id ->
GlobalScope.launch(Dispatchers.Main) {
CoroutineScope(coroutineContext).launch {
delay(300) delay(300)
deleteItem(dataListAdapter, reportSetups, id) deleteItem(dataListAdapter, reportSetups, id)
} }
} }
} }
@ -161,7 +163,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
ReportCreationActivity.newInstanceForResult(this, requireContext()) ReportCreationActivity.newInstanceForResult(this, requireContext())
} }
this.paApplication.reportWhistleBlower?.addListener(this) this.paApplication?.reportWhistleBlower?.addListener(this)
} }
@ -199,7 +201,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
for (performance in performances) { for (performance in performances) {
val report = performance.toStaticReport(getRealm()) val report = performance.toStaticReport(getRealm())
val badge = this.paApplication.reportWhistleBlower?.has(performance.id) ?: false val badge = this.paApplication?.reportWhistleBlower?.has(performance.id) ?: false
val reportRow = PerformanceRow(performance, report, badge) val reportRow = PerformanceRow(performance, report, badge)
sections.firstOrNull { it.report == report }?.let { section -> sections.firstOrNull { it.report == report }?.let { section ->
@ -268,7 +270,7 @@ class ReportsFragment : DeletableItemFragment(), StaticRowRepresentableDataSourc
showLoader() showLoader()
GlobalScope.launch { CoroutineScope(coroutineContext).launch {
val startDate = Date() val startDate = Date()
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()

@ -62,9 +62,8 @@ abstract class BaseFragment : Fragment() {
CrashLogging.log("$this.localClassName onActivityCreated") CrashLogging.log("$this.localClassName onActivityCreated")
} }
val paApplication: PokerAnalyticsApplication?
val paApplication: PokerAnalyticsApplication get() { return (this.activity as? BaseActivity)?.paApplication }
get() { return (this.requireActivity() as BaseActivity).paApplication }
/** /**
* Method called when the activity override onBackPressed and send the information to the fragment * Method called when the activity override onBackPressed and send the information to the fragment

@ -56,8 +56,8 @@ sealed class StaticReport(override var uniqueIdentifier: Int) : RowRepresentable
} }
val basicReports: Set<StaticReport> = setOf(Blinds) // setOf(General, Blinds, TournamentBuyin, val basicReports: Set<StaticReport> = setOf(General, Blinds, TournamentBuyin,
// DayOfWeek, Location, TournamentType, Game, TableSize, Duration, OptimalDuration) DayOfWeek, Location, TournamentType, Game, TableSize, Duration, OptimalDuration)
} }

@ -30,6 +30,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:paddingBottom="72dp" android:paddingBottom="72dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"

Loading…
Cancel
Save