Update Calendar UI & data management

feature/top10
Aurelien Hubert 7 years ago
parent cb54142fbf
commit 56d73c4974
  1. 134
      app/src/main/java/net/pokeranalytics/android/ui/fragment/CalendarFragment.kt

@ -38,7 +38,11 @@ import kotlin.coroutines.CoroutineContext
class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRepresentableDataSource, RowRepresentableDelegate {
enum class TimeFilter {
private enum class SessionType {
ALL, CASH, TOURNAMENT
}
private enum class TimeFilter {
MONTH, YEAR
}
@ -64,7 +68,10 @@ 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
private var currentSessionType = SessionType.ALL
private var currentTimeFilter = TimeFilter.MONTH
private var currentStat = Stat.NETRESULT
// Life Cycle
@ -107,15 +114,16 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
when (tab.position) {
0 -> displayData(Stat.NETRESULT)
1 -> displayData(Stat.HOURLY_RATE)
2 -> displayData(Stat.NUMBER_OF_GAMES)
3 -> displayData(Stat.WIN_RATIO)
4 -> displayData(Stat.STANDARD_DEVIATION_HOURLY)
5 -> displayData(Stat.AVERAGE)
6 -> displayData(Stat.AVERAGE_DURATION)
7 -> displayData(Stat.DURATION)
0 -> currentStat = Stat.NETRESULT
1 -> currentStat = Stat.HOURLY_RATE
2 -> currentStat = Stat.NUMBER_OF_GAMES
3 -> currentStat = Stat.WIN_RATIO
4 -> currentStat = Stat.STANDARD_DEVIATION_HOURLY
5 -> currentStat = Stat.AVERAGE
6 -> currentStat = Stat.AVERAGE_DURATION
7 -> currentStat = Stat.DURATION
}
displayData()
}
override fun onTabUnselected(tab: TabLayout.Tab) {
@ -123,8 +131,61 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
override fun onTabReselected(tab: TabLayout.Tab) {
}
})
// Manage session type filter
filterSessionAll.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
currentSessionType = SessionType.ALL
filterSessionCash.isChecked = false
filterSessionTournament.isChecked = false
launchStatComputation()
} else if (currentSessionType == SessionType.ALL) {
filterSessionAll.isChecked = true
}
}
filterSessionCash.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
currentSessionType = SessionType.CASH
filterSessionAll.isChecked = false
filterSessionTournament.isChecked = false
launchStatComputation()
} else if (currentSessionType == SessionType.CASH) {
filterSessionCash.isChecked = true
}
}
filterSessionTournament.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
currentSessionType = SessionType.TOURNAMENT
filterSessionAll.isChecked = false
filterSessionCash.isChecked = false
launchStatComputation()
} else if (currentSessionType == SessionType.TOURNAMENT) {
filterSessionTournament.isChecked = true
}
}
// Manage time filter
filterTimeMonth.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
currentTimeFilter = TimeFilter.MONTH
filterTimeYear.isChecked = false
displayData()
} else if (currentTimeFilter == TimeFilter.MONTH) {
filterTimeMonth.isChecked = true
}
}
filterTimeYear.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
currentTimeFilter = TimeFilter.YEAR
filterTimeMonth.isChecked = false
displayData()
} else if (currentTimeFilter == TimeFilter.YEAR) {
filterTimeYear.isChecked = true
}
}
val viewManager = LinearLayoutManager(requireContext())
calendarAdapter = RowRepresentableAdapter(this, this)
@ -154,13 +215,19 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
val yearlyReports: HashMap<Date, ComputedResults> = HashMap()
// 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())
val monthConditions = when(currentSessionType) {
SessionType.ALL -> listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR).combined()
SessionType.CASH -> listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR, Comparator.CASH).combined()
SessionType.TOURNAMENT -> listOf(Comparator.YEAR, Comparator.MONTH_OF_YEAR, Comparator.TOURNAMENT).combined()
}
monthConditions.forEach {conditions ->
val report = Calculator.computeStatsWithComparators(realm, conditions = conditions, options = Calculator.Options())
report.results.forEach { computedResults ->
if (!computedResults.isEmpty) {
// Set date data
it.forEach { condition ->
conditions.forEach { condition ->
condition.valueMap?.get("year")?.let { year ->
calendar.set(Calendar.YEAR, year as Int)
}
@ -177,38 +244,47 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
calendar.time = Date().startOfYear()
// Compute data per YEAR
val yearConditions = Comparator.YEAR.queryConditions
yearConditions.forEach { condition ->
val report = Calculator.computeStatsWithComparators(realm, conditions = listOf(condition), options = Calculator.Options())
val yearConditions = when(currentSessionType) {
SessionType.ALL -> listOf(Comparator.YEAR).combined()
SessionType.CASH -> listOf(Comparator.YEAR, Comparator.CASH).combined()
SessionType.TOURNAMENT -> listOf(Comparator.YEAR, Comparator.TOURNAMENT).combined()
}
yearConditions.forEach { conditions ->
val report = Calculator.computeStatsWithComparators(realm, conditions = conditions, options = Calculator.Options())
report.results.forEach { computedResults ->
if (!computedResults.isEmpty) {
// Set date data
condition.valueMap?.get("year")?.let { year ->
calendar.set(Calendar.YEAR, year as Int)
conditions.forEach { condition ->
condition.valueMap?.get("year")?.let { year ->
calendar.set(Calendar.YEAR, year as Int)
}
}
yearlyReports[calendar.time] = computedResults
}
}
}
// Logs
sortedMonthlyReports = monthlyReports.toSortedMap(compareByDescending { it })
sortedYearlyReports = yearlyReports.toSortedMap(compareByDescending { it })
// Logs
/*
Timber.d("Computation: ${System.currentTimeMillis() - startDate.time}ms")
Timber.d("========== YEAR x MONTH")
sortedMonthlyReports = monthlyReports.toSortedMap(compareByDescending { it })
sortedMonthlyReports.keys.forEach {
Timber.d("$it => ${sortedMonthlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
}
Timber.d("========== YEARLY")
sortedYearlyReports = yearlyReports.toSortedMap(compareByDescending { it })
sortedYearlyReports.keys.forEach {
Timber.d("$it => ${sortedYearlyReports[it]?.computedStat(Stat.NETRESULT)?.value}")
}
*/
GlobalScope.launch(Dispatchers.Main) {
displayData(Stat.NETRESULT)
displayData()
}
}
@ -218,16 +294,12 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
/**
* Display data
*/
private fun displayData(stat: Stat) {
private fun displayData() {
val startDate = Date()
rows.clear()
Timber.d("currentTimeFilter: $currentTimeFilter")
currentTimeFilter = TimeFilter.MONTH
when (currentTimeFilter) {
// Create monthly reports
@ -246,7 +318,7 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
)
}
sortedMonthlyReports[date]?.computedStat(stat)?.let { computedStat ->
sortedMonthlyReports[date]?.computedStat(currentStat)?.let { computedStat ->
rows.add(
CustomizableRowRepresentable(
customViewType = RowViewType.TITLE_VALUE_ARROW,
@ -263,7 +335,7 @@ class CalendarFragment : SessionObserverFragment(), CoroutineScope, StaticRowRep
sortedYearlyReports.keys.forEach { date ->
Timber.d("$date => ${sortedYearlyReports[date]?.computedStat(Stat.NETRESULT)?.value}")
sortedYearlyReports[date]?.computedStat(stat)?.let { computedStat ->
sortedYearlyReports[date]?.computedStat(currentStat)?.let { computedStat ->
rows.add(
CustomizableRowRepresentable(
customViewType = RowViewType.TITLE_VALUE_ARROW,

Loading…
Cancel
Save