Refactor graph fragment

feature/top10
Aurelien Hubert 7 years ago
parent bd0530a5ae
commit 4cea8a7f12
  1. 17
      app/src/main/java/net/pokeranalytics/android/calculus/Report.kt
  2. 21
      app/src/main/java/net/pokeranalytics/android/ui/adapter/ReportPagerAdapter.kt
  3. 66
      app/src/main/java/net/pokeranalytics/android/ui/fragment/GraphFragment.kt
  4. 32
      app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticDetailsFragment.kt

@ -36,15 +36,20 @@ class Report(var options: Calculator.Options) {
* Returns the list of entries corresponding to the provided [stat]
* One value will be returned by result
*/
fun lineEntries(stat: Stat, context: Context): LineDataSet {
fun lineEntries(stat: Stat? = null, context: Context): LineDataSet {
val entries = mutableListOf<Entry>()
val statToUse = stat ?: options.displayedStats.firstOrNull()
val statName = statToUse?.name ?: ""
this._results.forEachIndexed { index, results ->
results.computedStat(stat)?.progressValue?.let { progressValue ->
entries.add(Entry(index.toFloat(), progressValue.toFloat(), results))
statToUse?.let {
this._results.forEachIndexed { index, results ->
results.computedStat(it)?.progressValue?.let { progressValue ->
entries.add(Entry(index.toFloat(), progressValue.toFloat(), results))
}
}
}
return PALineDataSet(entries, stat.name, context)
return PALineDataSet(entries, statName, context)
}
fun barEntries(stat: Stat? = null): BarDataSet {
@ -61,7 +66,7 @@ class Report(var options: Calculator.Options) {
}
}
return BarDataSet(entries, stat?.name)
return BarDataSet(entries, statToUse?.name)
}
fun multiLineEntries(context: Context): List<List<Entry>> {

@ -15,16 +15,27 @@ import java.lang.ref.WeakReference
/**
* Home Adapter
*/
class ReportPagerAdapter(val context: Context, val fragmentManager: FragmentManager, val report: Report) : FragmentStatePagerAdapter(fragmentManager) {
class ReportPagerAdapter(val context: Context, val fragmentManager: FragmentManager, private val report: Report) : FragmentStatePagerAdapter(fragmentManager) {
var weakReferences = SparseArray<WeakReference<PokerAnalyticsFragment>>()
override fun getItem(position: Int): PokerAnalyticsFragment {
return when (position) {
0 -> GraphFragment.newInstance(report)
1 -> GraphFragment.newInstance(report)
2 -> GraphFragment.newInstance(report)
else -> GraphFragment.newInstance()
0 -> {
val dataSetList = listOf(report.barEntries())
GraphFragment.newInstance(dataSetList)
}
1 -> {
val dataSetList = listOf(report.lineEntries(context = context))
GraphFragment.newInstance(dataSetList)
PokerAnalyticsFragment()
}
2 -> {
//val dataSetList = listOf(report.barEntries())
//GraphFragment.newInstance(dataSetList)
PokerAnalyticsFragment()
}
else -> PokerAnalyticsFragment()
}
}

@ -11,31 +11,28 @@ import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
import kotlinx.android.synthetic.main.fragment_evograph.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.*
import net.pokeranalytics.android.calculus.ObjectIdentifier
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.calculus.StatEntry
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.graph.AxisFormatting
import net.pokeranalytics.android.ui.graph.setStyle
import net.pokeranalytics.android.ui.view.LegendView
import kotlin.coroutines.CoroutineContext
import timber.log.Timber
class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, CoroutineScope {
class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener {
companion object {
/**
* Create new instance
*/
fun newInstance(report: Report? = null): GraphFragment {
fun newInstance(dataSetList: List<DataSet<*>>): GraphFragment {
val fragment = GraphFragment()
report?.let {
fragment.selectedReport = it
}
fragment.dataSetList = dataSetList
val bundle = Bundle()
fragment.arguments = bundle
return fragment
@ -43,16 +40,12 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
}
private lateinit var parentActivity: PokerAnalyticsActivity
private lateinit var selectedReport: Report
private lateinit var legendView: LegendView
private lateinit var dataSetList: List<DataSet<*>>
private var chartView: BarLineChartBase<*>? = null
private var stat: Stat = Stat.NET_RESULT
private var aggregationType: AggregationType = AggregationType.SESSION
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main
private var axisFormatting: AxisFormatting = AxisFormatting.DEFAULT
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_evograph, container, false)
@ -67,11 +60,10 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
/**
* Set data
*/
fun setData(report: Report, stat: Stat, aggregationType: AggregationType) {
this.selectedReport = report
this.aggregationType = aggregationType
fun setData(dataSetList: List<DataSet<out Entry>>, stat: Stat, axisFormatting: AxisFormatting = AxisFormatting.DEFAULT) {
this.dataSetList = dataSetList
this.stat = stat
this.axisFormatting = axisFormatting
if (isAdded && !isDetached) {
loadGraph()
@ -96,32 +88,20 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
*/
private fun loadGraph() {
val ds = when (aggregationType) {
AggregationType.SESSION -> selectedReport.results.firstOrNull()?.defaultStatEntries(stat, requireContext())
AggregationType.DURATION -> {
selectedReport.results.firstOrNull()?.durationEntries(stat, requireContext())
}
AggregationType.MONTH, AggregationType.YEAR -> {
when (this.stat) {
Stat.NUMBER_OF_GAMES, Stat.NUMBER_OF_SETS -> selectedReport.barEntries(this.stat)
else -> selectedReport.lineEntries(this.stat, requireContext())
}
}
}
Timber.d("loadGraph")
ds?.let { dataSet ->
dataSetList.firstOrNull()?.let { dataSet ->
this.legendView.prepareWithStat(this.stat, dataSet.entryCount)
// initialize chart
if (this.chartView == null) {
this.chartView = when (dataSet) {
is LineDataSet -> LineChart(context)
is BarDataSet -> BarChart(context)
else -> null
}
this.chartContainer.addView(this.chartView)
this.chartContainer.removeAllViews()
this.chartView = when (dataSet) {
is LineDataSet -> LineChart(context)
is BarDataSet -> BarChart(context)
else -> null
}
this.chartContainer.addView(this.chartView)
when (dataSet) {
is LineDataSet -> {
@ -138,8 +118,6 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
}
}
val axisFormatting = aggregationType.axisFormatting
this.chartView?.let {
it.setStyle(false, axisFormatting, requireContext())
it.setOnChartValueSelectedListener(this)
@ -149,6 +127,7 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
}
}
// OnChartValueSelectedListener
@ -180,7 +159,6 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener, Co
this.legendView.setItemData(this.stat, formattedDate, entryValue, totalStatValue)
}
}
}

@ -81,7 +81,7 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() {
stat.aggregationTypes.firstOrNull()?.let { aggregationType ->
reports[aggregationType]?.let { report ->
graphFragment.setData(report, stat, aggregationType)
setGraphData(report, aggregationType)
}
}
@ -106,7 +106,7 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() {
val aggregationType = aggregationTypes[checkedId]
reports[aggregationType]?.let { report ->
graphFragment.setData(report, stat, aggregationType)
setGraphData(report, aggregationType)
} ?: run {
launchStatComputation(aggregationType)
}
@ -123,9 +123,6 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() {
graphContainer.hideWithAnimation()
progressBar.showWithAnimation()
//TODO: Create loader here
Timber.d("launchStatComputation: $aggregationType")
GlobalScope.launch {
val s = Date()
@ -143,13 +140,36 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() {
Timber.d(">>> ended in $duration seconds")
launch(Dispatchers.Main) {
graphFragment.setData(report, stat, aggregationType)
setGraphData(report, aggregationType)
progressBar.hideWithAnimation()
graphContainer.showWithAnimation()
}
}
}
/**
* Set the graph data set
*/
private fun setGraphData(report: Report, aggregationType: AggregationType) {
when (aggregationType) {
AggregationType.SESSION -> report.results.firstOrNull()?.defaultStatEntries(stat, requireContext())
AggregationType.DURATION -> {
report.results.firstOrNull()?.durationEntries(stat, requireContext())
}
AggregationType.MONTH, AggregationType.YEAR -> {
when (this.stat) {
Stat.NUMBER_OF_GAMES, Stat.NUMBER_OF_SETS -> report.barEntries(this.stat)
else -> report.lineEntries(this.stat, requireContext())
}
}
}?.let { dataSet ->
val dataSetList = listOf(dataSet)
graphFragment.setData(dataSetList, stat, aggregationType.axisFormatting)
}
}
/**
* Set data

Loading…
Cancel
Save