diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt index 62f702bf..65fec8b0 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt @@ -8,15 +8,19 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.realm.Realm import kotlinx.android.synthetic.main.fragment_calendar.* import kotlinx.android.synthetic.main.fragment_stats.recyclerView -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import net.pokeranalytics.android.R import net.pokeranalytics.android.calculus.Calculator -import net.pokeranalytics.android.calculus.Report +import net.pokeranalytics.android.calculus.ComputedResults import net.pokeranalytics.android.calculus.Stat import net.pokeranalytics.android.model.comparison.Comparator -import net.pokeranalytics.android.model.filter.QueryCondition +import net.pokeranalytics.android.model.comparison.combined import net.pokeranalytics.android.ui.fragment.components.SessionObserverFragment import net.pokeranalytics.android.ui.view.CalendarTabs +import timber.log.Timber import java.util.* import kotlin.coroutines.CoroutineContext @@ -36,6 +40,13 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope { } } + override val coroutineContext: CoroutineContext + get() = Dispatchers.Main + + + private var sortedMonthlyReports: SortedMap = HashMap().toSortedMap() + private var sortedYearlyReports: SortedMap = HashMap().toSortedMap() + // Life Cycle override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -58,150 +69,138 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope { */ private fun initData() { - val realm = getRealm() - - //example - //per year x month - val report = Calculator.computeStatsWithComparators(realm, comparators = listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR), options = Calculator.Options()) - - report.results.forEach { - //show net result per year x month - print(it.computedStat(Stat.NETRESULT)?.value) - - /* - si on voulait faire Year en même temps, il faudrait un array de yearData et la possibilité d'appeler un computedStat sur un array de computedResult - un yearData devra alors conteneir une variable "results" qui serait un array de tous des results de tous les mois d'une année (qu'on viendrait piocher dans report.results - A discuter: - - stocker les queryConditions dans chaque result ? - - appeler un computedStat sur un array de computedResult ? - - demander si on veut ou pas une entrée vide quand pas de sessions pour une query dans report.results - */ - } - - - //per year - val reportMonth = Calculator.computeStatsWithComparators(realm, comparators = listOf(Comparator.YEAR), options = Calculator.Options()) - - reportMonth.results.forEach { - //show net result per year - print(it.computedStat(Stat.NETRESULT)?.value) - } - - - - // compute per cell (year x month) - // build conditions array and save it - val conditions = listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR).combined() + } - // compute at index - conditions.forEach { - Calculator.computeStatsWithComparators(realm, conditions = it, options = Calculator.Options()) - } + /** + * Init UI + */ + private fun initUI() { - // compute per cell (year) - // build year conditions array and save it - val yearConditions = listOf(Comparator.YEAR) + CalendarTabs.values().forEach { + val tab = tabs.newTab() + tab.text = getString(it.resId) + tabs.addTab(tab) + } - // compute at index - yearConditions .forEach { - Calculator.computeStatsWithComparators(realm, conditions = it.queryConditions, options = Calculator.Options()) - } - } + val viewManager = LinearLayoutManager(requireContext()) - override val coroutineContext: CoroutineContext - get() = Dispatchers.Main + recyclerView.apply { + setHasFixedSize(true) + layoutManager = viewManager + //adapter = statsAdapter + } + } + /** + * Launch stat computation + */ private fun launchStatComputation() { - /* - val comparator = Comparator.YEAR // returns all months - GlobalScope.launch { - val realm = Realm.getDefaultInstance() - - val report = Calculator.computeStatsWithComparators(realm, comparators = listOf(comparator), options = Calculator.Options()) - Timber.d("Report results: ${report.results.size}") - - report.results.forEach { + val calendar = Calendar.getInstance() + calendar.set(Calendar.DAY_OF_MONTH, 1) + calendar.set(Calendar.HOUR_OF_DAY, 0) + calendar.set(Calendar.MINUTE, 0) + calendar.set(Calendar.SECOND, 0) + calendar.set(Calendar.MILLISECOND, 0) + val startDate = Date() + val realm = Realm.getDefaultInstance() + val montlyReports: HashMap = HashMap() + val yearlyReports: HashMap = HashMap() - val isEmpty = it.isEmpty - val statValue = it.computedStat(Stat.NETRESULT)?.value - Timber.d("isEmpty: $isEmpty / statValue: $statValue / Number of stats: ${it.numberOfStats()}") - } + // Compute data per YEAR x MONTH + val conditions = listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR).combined() - } - */ + conditions.forEach { - /* - conditions.forEach { + val report = Calculator.computeStatsWithComparators(realm, conditions = it, options = Calculator.Options()) + //Timber.d("======> report results: ${report.results.size}") + report.results.forEach { computedResults -> + //Timber.d("======> computedResults empty: ${computedResults.isEmpty}") + if (!computedResults.isEmpty) { - val realm = getRealm() + // Set date data + it.forEach { condition -> + condition.valueMap?.get("year")?.let { year -> + calendar.set(Calendar.YEAR, year as Int) + } + condition.valueMap?.get("month")?.let { month -> + calendar.set(Calendar.MONTH, month as Int) + } + } - GlobalScope.launch(coroutineContext) { + montlyReports[calendar.time] = computedResults - var r = Report() - val test = GlobalScope.async { - val s = Date() + //val statValue = computedResults.computedStat(Stat.NETRESULT)?.value + //Timber.d("======> statValue: $statValue / Number of stats: ${computedResults.numberOfStats()}") + } + } + } - val realm = Realm.getDefaultInstance() + // Compute data per YEAR + val yearConditions = Comparator.YEAR.queryConditions - Timber.d("Get report for: $it") + // compute at index + yearConditions.forEach { condition -> - val report = Calculator.computeStatsWithComparators(realm, conditions = listOf(it), options = Calculator.Options()) - Timber.d("Report: $report / Results: ${report.results.size}") + val report = Calculator.computeStatsWithComparators(realm, conditions = listOf(condition), options = Calculator.Options()) + //Timber.d("======> report results: ${report.results.size}") + report.results.forEach { computedResults -> + //Timber.d("======> computedResults empty: ${computedResults.isEmpty}") + if (!computedResults.isEmpty) { - report.results.firstOrNull()?.let { + calendar.set(Calendar.MONTH, 0) - val isEmpty = it.isEmpty - val statValue = it.computedStat(Stat.NETRESULT)?.value + // Set date data + condition.valueMap?.get("year")?.let { year -> + calendar.set(Calendar.YEAR, year as Int) + } - Timber.d("isEmpty: $isEmpty") - Timber.d("statValue: $statValue") + yearlyReports[calendar.time] = computedResults + /* + computedResults.computedStat(Stat.NETRESULT)?.let { computedStat -> + Timber.d("======> statValue: ${computedStat.value}") + } + */ } + } + } -// r = createSessionGroupsAndStartCompute(realm) -// report = r - realm.close() + Timber.d("Computation: ${System.currentTimeMillis() - startDate.time}ms") + Timber.d("========== YEAR x MONTH") + sortedMonthlyReports = montlyReports.toSortedMap(compareByDescending { it }) + sortedMonthlyReports.keys.forEach { + Timber.d("$it => ${sortedMonthlyReports[it]?.computedStat(Stat.NETRESULT)?.value}") + } - } - test.await() + Timber.d("========== YEARLY") + sortedYearlyReports = yearlyReports.toSortedMap(compareByDescending { it }) + sortedYearlyReports.keys.forEach { + Timber.d("$it => ${sortedYearlyReports[it]?.computedStat(Stat.NETRESULT)?.value}") + } - if (!isDetached) { -// showResults(r) - } + GlobalScope.launch(coroutineContext) { + displayData() } } - } - /** - * Init UI + * Display data */ - private fun initUI() { + private fun displayData() { - CalendarTabs.values().forEach { - val tab = tabs.newTab() - tab.text = getString(it.resId) - tabs.addTab(tab) - } - val viewManager = LinearLayoutManager(requireContext()) - recyclerView.apply { - setHasFixedSize(true) - layoutManager = viewManager - //adapter = statsAdapter - } } } \ No newline at end of file