From 36865f5d2ff9656a78576dd4584703b0e63962e7 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 24 Apr 2019 15:27:55 +0200 Subject: [PATCH] Legend refactoring --- .../pokeranalytics/android/calculus/Report.kt | 19 +++++---- .../android/model/realm/Session.kt | 21 +++++----- .../android/ui/adapter/ReportPagerAdapter.kt | 4 +- .../android/ui/fragment/GraphFragment.kt | 23 +++++----- .../android/ui/graph/GraphUnderlyingEntry.kt | 11 ++--- .../android/ui/view/LegendView.kt | 42 +++++++++++++------ .../android/ui/view/MultiLineLegendView.kt | 22 ++++++---- 7 files changed, 85 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt b/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt index 2b652d12..186ba556 100644 --- a/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt +++ b/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt @@ -13,7 +13,8 @@ import net.pokeranalytics.android.model.realm.SessionSet import net.pokeranalytics.android.ui.fragment.GraphFragment import net.pokeranalytics.android.ui.graph.GraphUnderlyingEntry import net.pokeranalytics.android.ui.graph.PALineDataSet -import net.pokeranalytics.android.ui.view.LegendView +import net.pokeranalytics.android.ui.view.DefaultLegendValues +import net.pokeranalytics.android.ui.view.LegendContent import net.pokeranalytics.android.util.ColorUtils import kotlin.math.abs @@ -448,23 +449,23 @@ class ComputedResults(group: ComputableGroup, shouldManageMultiGroupProgressValu override fun legendValues( stat: Stat, entry: Entry, - legendType: GraphFragment.LegendType, + style: GraphFragment.Style, groupName: String, context: Context - ): LegendView.Values { + ): LegendContent { - when (legendType) { + when (style) { - GraphFragment.LegendType.DEFAULT_BAR -> { + GraphFragment.Style.BAR -> { return when (stat) { Stat.NUMBER_OF_SETS, Stat.NUMBER_OF_GAMES, Stat.WIN_RATIO -> { val totalStatValue = stat.format(entry.y.toDouble(), currency = null) - LegendView.Values(this.entryTitle, totalStatValue) + DefaultLegendValues(this.entryTitle, totalStatValue) } else -> { val entryValue = this.formattedValue(stat) val countValue = this.computedStat(Stat.NUMBER_OF_GAMES)?.format() - LegendView.Values(this.entryTitle, entryValue, countValue) + DefaultLegendValues(this.entryTitle, entryValue, countValue) } } } @@ -473,12 +474,12 @@ class ComputedResults(group: ComputableGroup, shouldManageMultiGroupProgressValu return when (stat) { Stat.NUMBER_OF_SETS, Stat.NUMBER_OF_GAMES, Stat.WIN_RATIO -> { val totalStatValue = stat.format(entry.y.toDouble(), currency = null) - LegendView.Values(this.entryTitle, totalStatValue) + DefaultLegendValues(this.entryTitle, totalStatValue) } else -> { val entryValue = this.formattedValue(stat) val totalStatValue = stat.format(entry.y.toDouble(), currency = null) - LegendView.Values(this.entryTitle, entryValue, totalStatValue) + DefaultLegendValues(this.entryTitle, entryValue, totalStatValue) } } diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index 2a4f88f2..825af61d 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -28,10 +28,7 @@ import net.pokeranalytics.android.model.utils.SessionSetManager import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource import net.pokeranalytics.android.ui.adapter.UnmanagedRowRepresentableException import net.pokeranalytics.android.ui.fragment.GraphFragment -import net.pokeranalytics.android.ui.view.LegendView -import net.pokeranalytics.android.ui.view.RowRepresentable -import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor -import net.pokeranalytics.android.ui.view.RowViewType +import net.pokeranalytics.android.ui.view.* import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.SeparatorRow import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow @@ -963,19 +960,21 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat override fun legendValues( stat: Stat, entry: Entry, - legendType: GraphFragment.LegendType, + style: GraphFragment.Style, groupName: String, context: Context - ) : LegendView.Values { + ) : LegendContent { - when (legendType) { - GraphFragment.LegendType.MULTILINE -> { + when (style) { + GraphFragment.Style.MULTILINE -> { val secondTitle = stat.localizedTitle(context) val entryValue = this.formattedValue(stat) val dateValue = TextFormat(this.entryTitle) - return LegendView.MultiLineValues(groupName, secondTitle, entryValue, dateValue) + + + return MultilineLegendValues(groupName, secondTitle, entryValue, dateValue) } else -> { @@ -992,10 +991,10 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat } } - LegendView.Values(this.entryTitle, left, right) + DefaultLegendValues(this.entryTitle, left, right) } else -> { - super.legendValues(stat, entry, legendType, groupName, context) + super.legendValues(stat, entry, style, groupName, context) } } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/adapter/ReportPagerAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/adapter/ReportPagerAdapter.kt index 3838c29c..37440c01 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/adapter/ReportPagerAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/adapter/ReportPagerAdapter.kt @@ -24,11 +24,11 @@ class ReportPagerAdapter(val context: Context, val fragmentManager: FragmentMana return when (position) { 0 -> { val dataSetList = listOf(report.barEntries(null, context)) - GraphFragment.newInstance(barDataSets = dataSetList, legendType = GraphFragment.LegendType.DEFAULT_BAR) + GraphFragment.newInstance(barDataSets = dataSetList, style = GraphFragment.Style.BAR) } 1 -> { val dataSetList = report.multiLineEntries(context = context) - GraphFragment.newInstance(lineDataSets = dataSetList, legendType = GraphFragment.LegendType.MULTILINE) + GraphFragment.newInstance(lineDataSets = dataSetList, style = GraphFragment.Style.MULTILINE) } 2 -> { TableReportFragment.newInstance(report) diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/GraphFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/GraphFragment.kt index e296bf4c..0ae4886d 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/GraphFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/GraphFragment.kt @@ -26,9 +26,9 @@ import timber.log.Timber class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { - enum class LegendType { - DEFAULT, - DEFAULT_BAR, + enum class Style { + LINE, + BAR, MULTILINE, } @@ -37,9 +37,9 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { /** * Create new instance */ - fun newInstance(lineDataSets: List? = null, barDataSets: List? = null, legendType: LegendType = LegendType.DEFAULT): GraphFragment { + fun newInstance(lineDataSets: List? = null, barDataSets: List? = null, style: Style = Style.LINE): GraphFragment { val fragment = GraphFragment() - fragment.legendType = legendType + fragment.style = style fragment.lineDataSetList = lineDataSets fragment.barDataSetList = barDataSets val bundle = Bundle() @@ -51,7 +51,7 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { private lateinit var parentActivity: PokerAnalyticsActivity - private var legendType: LegendType = LegendType.DEFAULT + private var style: Style = Style.LINE private lateinit var legendView: LegendView private var lineDataSetList: List? = null @@ -103,8 +103,8 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { parentActivity = activity as PokerAnalyticsActivity parentActivity.title = stat.localizedTitle(requireContext()) - this.legendView = when (this.legendType) { - LegendType.MULTILINE -> MultiLineLegendView(requireContext()) + this.legendView = when (this.style) { + Style.MULTILINE -> MultiLineLegendView(requireContext()) else -> LegendView(requireContext()) } @@ -132,7 +132,7 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { this.chartView = lineChart dataSets.firstOrNull()?.let { - this.legendView.prepareWithStat(this.stat, it.entryCount, this.legendType) + this.legendView.prepareWithStat(this.stat, it.entryCount, this.style) lastEntry = it.getEntryForIndex(it.entryCount - 1) groupName = it.label } @@ -141,7 +141,7 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { this.barDataSetList?.let { dataSets -> - this.legendView.prepareWithStat(this.stat, legendType = this.legendType) + this.legendView.prepareWithStat(this.stat, style = this.style) val barChart = BarChart(context) if (stat.showXAxisZero) { barChart.xAxis.axisMinimum = 0.0f @@ -202,8 +202,9 @@ class GraphFragment : PokerAnalyticsFragment(), OnChartValueSelectedListener { is GraphUnderlyingEntry -> entry.data as GraphUnderlyingEntry? else -> null } + statEntry?.let { - val legendValue = it.legendValues(stat, entry, this.legendType, groupName, requireContext()) + val legendValue = it.legendValues(stat, entry, this.style, groupName, requireContext()) this.legendView.setItemData(legendValue) } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/graph/GraphUnderlyingEntry.kt b/app/src/main/java/net/pokeranalytics/android/ui/graph/GraphUnderlyingEntry.kt index d782cca5..432d713b 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/graph/GraphUnderlyingEntry.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/graph/GraphUnderlyingEntry.kt @@ -5,7 +5,8 @@ import com.github.mikephil.charting.data.Entry import net.pokeranalytics.android.calculus.Stat import net.pokeranalytics.android.calculus.TextFormat import net.pokeranalytics.android.ui.fragment.GraphFragment -import net.pokeranalytics.android.ui.view.LegendView +import net.pokeranalytics.android.ui.view.DefaultLegendValues +import net.pokeranalytics.android.ui.view.LegendContent interface GraphUnderlyingEntry { @@ -15,20 +16,20 @@ interface GraphUnderlyingEntry { fun legendValues( stat: Stat, entry: Entry, - legendType: GraphFragment.LegendType, + style: GraphFragment.Style, groupName: String, context: Context - ): LegendView.Values { + ): LegendContent { return when (stat) { Stat.NUMBER_OF_SETS, Stat.NUMBER_OF_GAMES, Stat.WIN_RATIO -> { val totalStatValue = stat.format(entry.y.toDouble(), currency = null) - LegendView.Values(this.entryTitle, totalStatValue) + DefaultLegendValues(this.entryTitle, totalStatValue) } else -> { val entryValue = this.formattedValue(stat) val totalStatValue = stat.format(entry.y.toDouble(), currency = null) - LegendView.Values(this.entryTitle, entryValue, totalStatValue) + DefaultLegendValues(this.entryTitle, entryValue, totalStatValue) } } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/LegendView.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/LegendView.kt index 9f0c33a4..e67def78 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/LegendView.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/LegendView.kt @@ -13,13 +13,26 @@ import net.pokeranalytics.android.calculus.TextFormat import net.pokeranalytics.android.ui.extensions.setTextFormat import net.pokeranalytics.android.ui.fragment.GraphFragment +interface LegendContent + +data class DefaultLegendValues( + var title: String, + var leftFormat: TextFormat, + var rightFormat: TextFormat? = null +) : LegendContent + /** * Display a row session */ open class LegendView : FrameLayout { - open class Values(var title: String, var leftFormat: TextFormat, var rightFormat: TextFormat? = null) - class MultiLineValues(var firstTitle: String, var secondTitle: String, leftFormat: TextFormat, rightFormat: TextFormat? = null) : Values("", leftFormat, rightFormat) +// open class Values(var title: String, var leftFormat: TextFormat, var rightFormat: TextFormat? = null) +// class MultiLineValues( +// var firstTitle: String, +// var secondTitle: String, +// leftFormat: TextFormat, +// rightFormat: TextFormat? = null +// ) : Values("", leftFormat, rightFormat) private lateinit var legendLayout: ConstraintLayout @@ -38,7 +51,7 @@ open class LegendView : FrameLayout { init() } - open protected fun getResourceLayout() : Int { + open protected fun getResourceLayout(): Int { return R.layout.layout_legend_default } @@ -55,15 +68,15 @@ open class LegendView : FrameLayout { /** * Set the stat data to the view */ - open fun prepareWithStat(stat: Stat, counter: Int? = null, legendType: GraphFragment.LegendType) { + open fun prepareWithStat(stat: Stat, counter: Int? = null, style: GraphFragment.Style) { - when (legendType) { - GraphFragment.LegendType.DEFAULT_BAR -> { + when (style) { + GraphFragment.Style.BAR -> { this.stat1Name.text = stat.localizedTitle(context) this.stat2Name.text = context.getString(R.string.sessions) this.counter.isVisible = false } - GraphFragment.LegendType.DEFAULT -> { + GraphFragment.Style.LINE -> { if (stat.significantIndividualValue) { this.stat1Name.text = stat.localizedTitle(context) this.stat2Name.text = stat.cumulativeLabelResId(context) @@ -87,15 +100,20 @@ open class LegendView : FrameLayout { /** * */ - open fun setItemData(values: Values) { + open fun setItemData(content: LegendContent) { + + if (content is DefaultLegendValues) { - this.title.text = values.title + this.title.text = content.title + + this.stat1Value.setTextFormat(content.leftFormat, context) + content.rightFormat?.let { + this.stat2Value.setTextFormat(it, context) + } - this.stat1Value.setTextFormat(values.leftFormat, context) - values.rightFormat?.let { - this.stat2Value.setTextFormat(it, context) } + } } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/MultiLineLegendView.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/MultiLineLegendView.kt index 757f8183..79c84b2c 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/MultiLineLegendView.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/MultiLineLegendView.kt @@ -4,27 +4,35 @@ import android.content.Context import kotlinx.android.synthetic.main.layout_legend_default.view.* import net.pokeranalytics.android.R import net.pokeranalytics.android.calculus.Stat +import net.pokeranalytics.android.calculus.TextFormat import net.pokeranalytics.android.ui.extensions.setTextFormat import net.pokeranalytics.android.ui.fragment.GraphFragment +data class MultilineLegendValues( + var firstTitle: String, + var secondTitle: String, + var leftFormat: TextFormat, + var rightFormat: TextFormat +) : LegendContent + class MultiLineLegendView(context: Context) : LegendView(context = context) { override fun getResourceLayout(): Int { return R.layout.layout_legend_color } - override fun prepareWithStat(stat: Stat, counter: Int?, legendType: GraphFragment.LegendType) { + override fun prepareWithStat(stat: Stat, counter: Int?, style: GraphFragment.Style) { } - override fun setItemData(values: Values) { + override fun setItemData(content: LegendContent) { - if (values is MultiLineValues) { + if (content is MultilineLegendValues) { - this.stat1Name.text = values.firstTitle - this.stat2Name.text = values.secondTitle + this.stat1Name.text = content.firstTitle + this.stat2Name.text = content.secondTitle - this.stat1Value.setTextFormat(values.leftFormat, context) - this.stat2Value.text = values.title + this.stat1Value.setTextFormat(content.leftFormat, context) + this.stat2Value.setTextFormat(content.rightFormat, context) } }