better synchronize graph rendering

This commit is contained in:
Milos Kozak 2020-08-21 10:29:23 +02:00
parent 4d32b65aab
commit 7282f460fc

View file

@ -159,6 +159,8 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
private var carbAnimation: AnimationDrawable? = null private var carbAnimation: AnimationDrawable? = null
private val graphLock = Object()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? { savedInstanceState: Bundle?): View? {
@ -478,43 +480,43 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
} }
@Synchronized
private fun prepareGraphs() { private fun prepareGraphs() {
val numOfGraphs = overviewMenus.setting.size synchronized(graphLock) {
val numOfGraphs = overviewMenus.setting.size
if (numOfGraphs != secondaryGraphs.size - 1) { if (numOfGraphs != secondaryGraphs.size - 1) {
//aapsLogger.debug("New secondary graph count ${numOfGraphs-1}") //aapsLogger.debug("New secondary graph count ${numOfGraphs-1}")
// rebuild needed // rebuild needed
secondaryGraphs.clear() secondaryGraphs.clear()
secondaryGraphsLabel.clear() secondaryGraphsLabel.clear()
overview_iobgraph.removeAllViews() overview_iobgraph.removeAllViews()
for (i in 1 until numOfGraphs) { for (i in 1 until numOfGraphs) {
val relativeLayout = RelativeLayout(context) val relativeLayout = RelativeLayout(context)
relativeLayout.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) relativeLayout.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val graph = GraphView(context) val graph = GraphView(context)
graph.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, resourceHelper.dpToPx(skinProvider.activeSkin().secondaryGraphHeight)).also { it.setMargins(0, resourceHelper.dpToPx(15), 0, resourceHelper.dpToPx(10)) } graph.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, resourceHelper.dpToPx(skinProvider.activeSkin().secondaryGraphHeight)).also { it.setMargins(0, resourceHelper.dpToPx(15), 0, resourceHelper.dpToPx(10)) }
graph.gridLabelRenderer?.gridColor = resourceHelper.gc(R.color.graphgrid) graph.gridLabelRenderer?.gridColor = resourceHelper.gc(R.color.graphgrid)
graph.gridLabelRenderer?.reloadStyles() graph.gridLabelRenderer?.reloadStyles()
graph.gridLabelRenderer?.isHorizontalLabelsVisible = false graph.gridLabelRenderer?.isHorizontalLabelsVisible = false
graph.gridLabelRenderer?.labelVerticalWidth = axisWidth graph.gridLabelRenderer?.labelVerticalWidth = axisWidth
graph.gridLabelRenderer?.numVerticalLabels = 3 graph.gridLabelRenderer?.numVerticalLabels = 3
graph.viewport.backgroundColor = Color.argb(20, 255, 255, 255) // 8% of gray graph.viewport.backgroundColor = Color.argb(20, 255, 255, 255) // 8% of gray
relativeLayout.addView(graph) relativeLayout.addView(graph)
val label = TextView(context) val label = TextView(context)
val layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT).also { it.setMargins(resourceHelper.dpToPx(30), resourceHelper.dpToPx(25), 0, 0) } val layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT).also { it.setMargins(resourceHelper.dpToPx(30), resourceHelper.dpToPx(25), 0, 0) }
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP) layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT) layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
label.layoutParams = layoutParams label.layoutParams = layoutParams
relativeLayout.addView(label) relativeLayout.addView(label)
secondaryGraphsLabel.add(label) secondaryGraphsLabel.add(label)
overview_iobgraph.addView(relativeLayout) overview_iobgraph.addView(relativeLayout)
secondaryGraphs.add(graph) secondaryGraphs.add(graph)
}
} }
} }
} }
private fun scheduleUpdateGUI(from: String) { private fun scheduleUpdateGUI(from: String) {
@ -533,7 +535,6 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
scheduledUpdate = worker.schedule(task, 500, TimeUnit.MILLISECONDS) scheduledUpdate = worker.schedule(task, 500, TimeUnit.MILLISECONDS)
} }
@Synchronized
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun updateGUI(from: String) { fun updateGUI(from: String) {
aapsLogger.debug("UpdateGUI from $from") aapsLogger.debug("UpdateGUI from $from")
@ -857,53 +858,57 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
graphData.addNowLine(now) graphData.addNowLine(now)
// ------------------ 2nd graph // ------------------ 2nd graph
for (g in 0 until min(secondaryGraphs.size, overviewMenus.setting.size + 1)) { synchronized(graphLock) {
val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPlugin, treatmentsPlugin) for (g in 0 until min(secondaryGraphs.size, overviewMenus.setting.size + 1)) {
var useABSForScale = false val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPlugin, treatmentsPlugin)
var useIobForScale = false var useABSForScale = false
var useCobForScale = false var useIobForScale = false
var useDevForScale = false var useCobForScale = false
var useRatioForScale = false var useDevForScale = false
var useDSForScale = false var useRatioForScale = false
var useIAForScale = false var useDSForScale = false
when { var useIAForScale = false
overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] -> useABSForScale = true when {
overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] -> useIobForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] -> useABSForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] -> useCobForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] -> useIobForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] -> useDevForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] -> useCobForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] -> useRatioForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] -> useDevForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] -> useIAForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] -> useRatioForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] -> useDSForScale = true overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] -> useIAForScale = true
overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] -> useDSForScale = true
}
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal]) secondGraphData.addAbsIob(fromTime, now, useABSForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal]) secondGraphData.addIob(fromTime, now, useIobForScale, 1.0, overviewMenus.setting[g + 1][OverviewMenus.CharType.PRE.ordinal])
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal]) secondGraphData.addCob(fromTime, now, useCobForScale, if (useCobForScale) 1.0 else 0.5)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal]) secondGraphData.addDeviations(fromTime, now, useDevForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal]) secondGraphData.addRatio(fromTime, now, useRatioForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal]) secondGraphData.addActivity(fromTime, endTime, useIAForScale, 0.8)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] && buildHelper.isDev()) secondGraphData.addDeviationSlope(fromTime, now, useDSForScale, 1.0)
// set manual x bounds to have nice steps
secondGraphData.formatAxis(fromTime, endTime)
secondGraphData.addNowLine(now)
secondaryGraphsData.add(secondGraphData)
} }
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal]) secondGraphData.addAbsIob(fromTime, now, useABSForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal]) secondGraphData.addIob(fromTime, now, useIobForScale, 1.0, overviewMenus.setting[g + 1][OverviewMenus.CharType.PRE.ordinal])
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal]) secondGraphData.addCob(fromTime, now, useCobForScale, if (useCobForScale) 1.0 else 0.5)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal]) secondGraphData.addDeviations(fromTime, now, useDevForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal]) secondGraphData.addRatio(fromTime, now, useRatioForScale, 1.0)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal]) secondGraphData.addActivity(fromTime, endTime, useIAForScale, 0.8)
if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] && buildHelper.isDev()) secondGraphData.addDeviationSlope(fromTime, now, useDSForScale, 1.0)
// set manual x bounds to have nice steps
secondGraphData.formatAxis(fromTime, endTime)
secondGraphData.addNowLine(now)
secondaryGraphsData.add(secondGraphData)
} }
} }
// finally enforce drawing of graphs in UI thread // finally enforce drawing of graphs in UI thread
graphData.performUpdate() graphData.performUpdate()
for (g in 0 until min(secondaryGraphs.size, overviewMenus.setting.size + 1)) { synchronized(graphLock) {
secondaryGraphsLabel[g].text = overviewMenus.enabledTypes(g + 1) for (g in 0 until min(secondaryGraphs.size, overviewMenus.setting.size + 1)) {
secondaryGraphs[g].visibility = ( secondaryGraphsLabel[g].text = overviewMenus.enabledTypes(g + 1)
overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] || secondaryGraphs[g].visibility = (
overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] ||
overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] ||
overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] ||
overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] ||
overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] ||
overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] ||
).toVisibility() overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal]
secondaryGraphsData[g].performUpdate() ).toVisibility()
secondaryGraphsData[g].performUpdate()
}
} }
} }
} }