Improve Calendar Fragment

feature/top10
Aurelien Hubert 7 years ago
parent 842d98279e
commit de50455858
  1. 95
      app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt

@ -27,7 +27,10 @@ import net.pokeranalytics.android.ui.view.CalendarTabs
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.util.extensions.shortDate import net.pokeranalytics.android.util.extensions.getDateMonth
import net.pokeranalytics.android.util.extensions.getDateYear
import net.pokeranalytics.android.util.extensions.startOfMonth
import net.pokeranalytics.android.util.extensions.startOfYear
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -35,6 +38,10 @@ import kotlin.coroutines.CoroutineContext
class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRepresentableDataSource, RowRepresentableDelegate { class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRepresentableDataSource, RowRepresentableDelegate {
enum class TimeFilter {
MONTH, YEAR
}
companion object { companion object {
/** /**
@ -57,6 +64,7 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
private var rows: ArrayList<CustomizableRowRepresentable> = ArrayList() private var rows: ArrayList<CustomizableRowRepresentable> = ArrayList()
private var sortedMonthlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap() private var sortedMonthlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap()
private var sortedYearlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap() private var sortedYearlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap()
private var currentTimeFilter = TimeFilter.YEAR
// Life Cycle // Life Cycle
@ -137,29 +145,20 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
GlobalScope.launch { GlobalScope.launch {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.set(Calendar.DAY_OF_MONTH, 1) calendar.time = Date().startOfMonth()
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 startDate = Date()
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val montlyReports: HashMap<Date, ComputedResults> = HashMap() val monthlyReports: HashMap<Date, ComputedResults> = HashMap()
val yearlyReports: HashMap<Date, ComputedResults> = HashMap() val yearlyReports: HashMap<Date, ComputedResults> = HashMap()
// Compute data per YEAR x MONTH // Compute data per YEAR and MONTH
val conditions = listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR).combined() val conditions = listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR).combined()
conditions.forEach { conditions.forEach {
val report = Calculator.computeStatsWithComparators(realm, conditions = it, options = Calculator.Options()) val report = Calculator.computeStatsWithComparators(realm, conditions = it, options = Calculator.Options())
//Timber.d("======> report results: ${report.results.size}")
report.results.forEach { computedResults -> report.results.forEach { computedResults ->
//Timber.d("======> computedResults empty: ${computedResults.isEmpty}")
if (!computedResults.isEmpty) { if (!computedResults.isEmpty) {
// Set date data // Set date data
it.forEach { condition -> it.forEach { condition ->
condition.valueMap?.get("year")?.let { year -> condition.valueMap?.get("year")?.let { year ->
@ -170,40 +169,34 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
} }
} }
montlyReports[calendar.time] = computedResults monthlyReports[calendar.time] = computedResults
} }
} }
} }
calendar.time = Date().startOfYear()
// Compute data per YEAR // Compute data per YEAR
val yearConditions = Comparator.YEAR.queryConditions val yearConditions = Comparator.YEAR.queryConditions
// compute at index
yearConditions.forEach { condition -> yearConditions.forEach { condition ->
val report = Calculator.computeStatsWithComparators(realm, conditions = listOf(condition), options = Calculator.Options()) val report = Calculator.computeStatsWithComparators(realm, conditions = listOf(condition), options = Calculator.Options())
//Timber.d("======> report results: ${report.results.size}")
report.results.forEach { computedResults -> report.results.forEach { computedResults ->
//Timber.d("======> computedResults empty: ${computedResults.isEmpty}")
if (!computedResults.isEmpty) { if (!computedResults.isEmpty) {
calendar.set(Calendar.MONTH, 0)
// Set date data // Set date data
condition.valueMap?.get("year")?.let { year -> condition.valueMap?.get("year")?.let { year ->
calendar.set(Calendar.YEAR, year as Int) calendar.set(Calendar.YEAR, year as Int)
} }
yearlyReports[calendar.time] = computedResults yearlyReports[calendar.time] = computedResults
} }
} }
} }
// Logs
Timber.d("Computation: ${System.currentTimeMillis() - startDate.time}ms") Timber.d("Computation: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("========== YEAR x MONTH") Timber.d("========== YEAR x MONTH")
sortedMonthlyReports = montlyReports.toSortedMap(compareByDescending { it }) sortedMonthlyReports = monthlyReports.toSortedMap(compareByDescending { it })
sortedMonthlyReports.keys.forEach { sortedMonthlyReports.keys.forEach {
Timber.d("$it => ${sortedMonthlyReports[it]?.computedStat(Stat.NETRESULT)?.value}") Timber.d("$it => ${sortedMonthlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
} }
@ -227,22 +220,64 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
*/ */
private fun displayData(stat: Stat) { private fun displayData(stat: Stat) {
val startDate = Date()
rows.clear() rows.clear()
Timber.d("currentTimeFilter: $currentTimeFilter")
currentTimeFilter = TimeFilter.MONTH
when (currentTimeFilter) {
// Create monthly reports
TimeFilter.MONTH -> {
val years: ArrayList<String> = ArrayList()
sortedMonthlyReports.keys.forEach { date ->
if (!years.contains(date.getDateYear())) {
years.add(date.getDateYear())
rows.add(
CustomizableRowRepresentable(
customViewType = RowViewType.HEADER_TITLE,
title = date.getDateYear()
)
)
}
sortedMonthlyReports[date]?.computedStat(stat)?.let { computedStat ->
rows.add(
CustomizableRowRepresentable(
customViewType = RowViewType.TITLE_VALUE_ARROW,
title = date.getDateMonth(),
computedStat = computedStat
)
)
}
}
}
// Create yearly reports // Create yearly reports
sortedYearlyReports.keys.forEach { TimeFilter.YEAR -> {
Timber.d("$it => ${sortedYearlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
sortedYearlyReports[it]?.computedStat(stat)?.let { computedStat -> sortedYearlyReports.keys.forEach { date ->
Timber.d("$date => ${sortedYearlyReports[date]?.computedStat(Stat.NETRESULT)?.value}")
sortedYearlyReports[date]?.computedStat(stat)?.let { computedStat ->
rows.add( rows.add(
CustomizableRowRepresentable( CustomizableRowRepresentable(
customViewType = RowViewType.TITLE_VALUE_ARROW, customViewType = RowViewType.TITLE_VALUE_ARROW,
title = it.shortDate(), title = date.getDateYear(),
value = computedStat.format(requireContext()).text computedStat = computedStat
) )
) )
} }
} }
}
}
Timber.d("Display data: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("Rows: ${rows.size}") Timber.d("Rows: ${rows.size}")
calendarAdapter.notifyDataSetChanged() calendarAdapter.notifyDataSetChanged()

Loading…
Cancel
Save