Skip to content

Commit

Permalink
DPMS-11800 Make forecastComponent reactive (Part2) (oxeanbits#20)
Browse files Browse the repository at this point in the history
* Add auxiliar anvil components to do renderization

* Make forecastComponent reactive
  • Loading branch information
dnovaes authored Apr 15, 2020
1 parent c81ba7f commit 52f6373
Showing 1 changed file with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package com.oxeanbits.forecastchart.core.ui.component

import android.content.Context
import android.graphics.Typeface.BOLD
import android.widget.LinearLayout
import com.github.mikephil.charting.charts.CombinedChart
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.oxeanbits.forecastchart.core.model.Line
import com.oxeanbits.forecastchart.core.ui.anvil.LinearLayoutComponent
import com.oxeanbits.forecastchart.core.util.Colors
import com.oxeanbits.forecastchart.core.util.DateFormatter.DEFAULT_DATE_FORMAT
import com.oxeanbits.forecastchart.core.util.ForecastChart
import trikita.anvil.Anvil
import trikita.anvil.Anvil.currentView
import trikita.anvil.BaseDSL.MATCH
import trikita.anvil.BaseDSL.WRAP
import trikita.anvil.BaseDSL.init
import trikita.anvil.BaseDSL.margin
import trikita.anvil.BaseDSL.size
import trikita.anvil.BaseDSL.textSize
Expand All @@ -30,7 +29,7 @@ inline fun forecastChartComponent(crossinline func: ForecastChartComponent.() ->
highOrderComponent(func)
}

class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Renderable {
class ForecastChartComponent(context: Context) : LinearLayoutComponent(context) {
private var combinedChart: CombinedChart? = null
private var expectedData: Line = emptyLine()
private var actualData: Line? = null
Expand All @@ -42,34 +41,22 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re
private var zoomEnabled: Boolean = false
private var detailsEnable: Boolean = false

public override fun onAttachedToWindow() {
super.onAttachedToWindow()
Anvil.mount(this, this)
}

public override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
Anvil.unmount(this, false)
}

override fun view() {
linearLayout{
size(MATCH, MATCH)
orientation(VERTICAL)
size(MATCH, MATCH)
orientation(VERTICAL)

if(detailsEnable) {
renderDetailsLayout()
}
renderChart()
if(detailsEnable) {
renderDetailsLayout()
}
renderChart()
}

private fun renderDetailsLayout(){
linearLayout {
size(WRAP, WRAP)
orientation(HORIZONTAL)

val expectedData = this.expectedData ?: return@linearLayout
val expectedData = this.expectedData
val actualData = this.actualData ?: return@linearLayout
val forecastedData = this.forecastedData ?: return@linearLayout

Expand Down Expand Up @@ -128,32 +115,31 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re
}
}

private fun renderChart(){
private fun renderChart() {
val expectedData = this.expectedData
val actualData = this.actualData ?: return
val forecastedData = this.forecastedData ?: return

v(CombinedChart::class.java) {
size(MATCH, MATCH)
init {
this.combinedChart = Anvil.currentView()
val combinedChart = this.combinedChart ?: return@init
val expectedData = this.expectedData
val actualData = this.actualData ?: return@init
val forecastedData = this.forecastedData ?: return@init
val endDateData = this.endDateData

if(actualData.values.isNotEmpty() || forecastedData.values.isNotEmpty()
|| expectedData.values.isNotEmpty()) {
ForecastChart.createForecastChart(
context,
combinedChart,
expectedData,
actualData,
forecastedData,
endDateData,
unit,
dateFormat,
decimalFormat,
zoomEnabled
)
}
this.combinedChart = currentView()
val combinedChart = this.combinedChart ?: return@v
val endDateData = this.endDateData

if(actualData.values.isNotEmpty() || forecastedData.values.isNotEmpty()
|| expectedData.values.isNotEmpty()) {
ForecastChart.createForecastChart(
context,
combinedChart,
expectedData,
actualData,
forecastedData,
endDateData,
unit,
dateFormat,
decimalFormat,
zoomEnabled
)
}
}
}
Expand All @@ -174,6 +160,8 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re
fun actualLine(arrayData: ArrayList<Entry>, label: String,
color: Int, forecasted: Boolean = false){
this.actualData = Line(arrayData, label, color, forecasted)
render()
combinedChart?.invalidate()
}

fun forecastedLine(arrayData: ArrayList<Entry>, label: String,
Expand Down

0 comments on commit 52f6373

Please sign in to comment.