Improve Calendar Fragment

feature/top10
Aurelien Hubert 7 years ago
parent 842d98279e
commit de50455858
  1. 97
      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.RowViewType
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 java.util.*
import kotlin.coroutines.CoroutineContext
@ -35,6 +38,10 @@ import kotlin.coroutines.CoroutineContext
class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRepresentableDataSource, RowRepresentableDelegate {
enum class TimeFilter {
MONTH, YEAR
}
companion object {
/**
@ -57,6 +64,7 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
private var rows: ArrayList<CustomizableRowRepresentable> = ArrayList()
private var sortedMonthlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap()
private var sortedYearlyReports: SortedMap<Date, ComputedResults> = HashMap<Date, ComputedResults>().toSortedMap()
private var currentTimeFilter = TimeFilter.YEAR
// Life Cycle
@ -98,7 +106,7 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
when(tab.position) {
when (tab.position) {
0 -> displayData(Stat.NETRESULT)
1 -> displayData(Stat.HOURLY_RATE)
2 -> displayData(Stat.NUMBER_OF_GAMES)
@ -137,29 +145,20 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
GlobalScope.launch {
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)
calendar.time = Date().startOfMonth()
val startDate = Date()
val realm = Realm.getDefaultInstance()
val montlyReports: HashMap<Date, ComputedResults> = HashMap()
val monthlyReports: 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()
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) {
// Set date data
it.forEach { condition ->
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
val yearConditions = Comparator.YEAR.queryConditions
// compute at index
yearConditions.forEach { condition ->
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) {
calendar.set(Calendar.MONTH, 0)
// Set date data
condition.valueMap?.get("year")?.let { year ->
calendar.set(Calendar.YEAR, year as Int)
}
yearlyReports[calendar.time] = computedResults
}
}
}
// Logs
Timber.d("Computation: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("========== YEAR x MONTH")
sortedMonthlyReports = montlyReports.toSortedMap(compareByDescending { it })
sortedMonthlyReports = monthlyReports.toSortedMap(compareByDescending { it })
sortedMonthlyReports.keys.forEach {
Timber.d("$it => ${sortedMonthlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
}
@ -227,22 +220,64 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
*/
private fun displayData(stat: Stat) {
val startDate = Date()
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
sortedYearlyReports.keys.forEach {
Timber.d("$it => ${sortedYearlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
sortedYearlyReports[it]?.computedStat(stat)?.let { computedStat ->
TimeFilter.YEAR -> {
sortedYearlyReports.keys.forEach { date ->
Timber.d("$date => ${sortedYearlyReports[date]?.computedStat(Stat.NETRESULT)?.value}")
sortedYearlyReports[date]?.computedStat(stat)?.let { computedStat ->
rows.add(
CustomizableRowRepresentable(
customViewType = RowViewType.TITLE_VALUE_ARROW,
title = it.shortDate(),
value = computedStat.format(requireContext()).text
title = date.getDateYear(),
computedStat = computedStat
)
)
}
}
}
}
Timber.d("Display data: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("Rows: ${rows.size}")
calendarAdapter.notifyDataSetChanged()

Loading…
Cancel
Save