From 8a62ba136ee2eb1f433df5625130651477c94607 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Fri, 22 Nov 2024 18:15:32 -0500 Subject: [PATCH] Create base widget view for charts Signed-off-by: Kyle Corry --- .../widgets/SunAndMoonChartToolWidgetView.kt | 29 +++-------------- .../tides/widgets/TideChartToolWidgetView.kt | 28 +++------------- .../tools/widgets/ChartToolWidgetViewBase.kt | 32 +++++++++++++++++++ .../widgets/PressureChartToolWidgetView.kt | 28 +++------------- 4 files changed, 47 insertions(+), 70 deletions(-) create mode 100644 app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/ChartToolWidgetViewBase.kt diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/widgets/SunAndMoonChartToolWidgetView.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/widgets/SunAndMoonChartToolWidgetView.kt index 80b4121f0..719749d0c 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/widgets/SunAndMoonChartToolWidgetView.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/widgets/SunAndMoonChartToolWidgetView.kt @@ -3,9 +3,6 @@ package com.kylecorry.trail_sense.tools.astronomy.widgets import android.content.Context import android.view.View import android.widget.RemoteViews -import androidx.lifecycle.Lifecycle -import com.kylecorry.andromeda.core.system.Resources -import com.kylecorry.andromeda.core.ui.Views import com.kylecorry.andromeda.views.chart.Chart import com.kylecorry.luna.coroutines.onMain import com.kylecorry.trail_sense.R @@ -14,19 +11,11 @@ import com.kylecorry.trail_sense.tools.astronomy.domain.AstronomySubsystem import com.kylecorry.trail_sense.tools.astronomy.ui.AstroChart import com.kylecorry.trail_sense.tools.astronomy.ui.MoonPhaseImageMapper import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools -import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView +import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase import java.time.Duration import java.time.Instant -class SunAndMoonChartToolWidgetView : ToolWidgetView { - protected val LAYOUT = R.layout.widget_chart - protected val ROOT = R.id.widget_frame - protected val TITLE_TEXTVIEW = R.id.widget_title - protected val CHART = R.id.widget_chart - - override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) { - // Do nothing - } +class SunAndMoonChartToolWidgetView : ChartToolWidgetViewBase() { override suspend fun getPopulatedView(context: Context): RemoteViews { val astronomy = AstronomySubsystem.getInstance(context) @@ -43,7 +32,8 @@ class SunAndMoonChartToolWidgetView : ToolWidgetView { Duration.between(instant, it.time).abs() } - val bitmap = onMain { + val views = getView(context) + onMain { val chart = Chart(context) val astroChart = AstroChart(chart) {} astroChart.setMoonImage(R.drawable.ic_moon) @@ -55,15 +45,10 @@ class SunAndMoonChartToolWidgetView : ToolWidgetView { astroChart.moveSun(currentSun) astroChart.moveMoon(currentMoon, moon.tilt) - val width = Resources.dp(context, 400f).toInt() - val height = Resources.dp(context, 200f).toInt() - Views.renderViewAsBitmap(chart, width, height) + renderChart(context, views, chart) } - val views = getView(context) - views.setViewVisibility(TITLE_TEXTVIEW, View.GONE) - views.setImageViewBitmap(CHART, bitmap) views.setOnClickPendingIntent( ROOT, @@ -71,8 +56,4 @@ class SunAndMoonChartToolWidgetView : ToolWidgetView { ) return views } - - override fun getView(context: Context): RemoteViews { - return RemoteViews(context.packageName, LAYOUT) - } } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/tides/widgets/TideChartToolWidgetView.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/tides/widgets/TideChartToolWidgetView.kt index 77ba0297f..70dd5ffbb 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/tides/widgets/TideChartToolWidgetView.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/tides/widgets/TideChartToolWidgetView.kt @@ -4,8 +4,6 @@ import android.content.Context import android.view.View import android.widget.RemoteViews import android.widget.TextView -import androidx.lifecycle.Lifecycle -import com.kylecorry.andromeda.core.system.Resources import com.kylecorry.andromeda.core.ui.Views import com.kylecorry.andromeda.views.chart.Chart import com.kylecorry.luna.coroutines.onMain @@ -14,19 +12,11 @@ import com.kylecorry.trail_sense.shared.navigation.NavigationUtils import com.kylecorry.trail_sense.tools.tides.subsystem.TidesSubsystem import com.kylecorry.trail_sense.tools.tides.ui.TideChart import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools -import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView +import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase import java.time.Duration import java.time.Instant -class TideChartToolWidgetView : ToolWidgetView { - protected val LAYOUT = R.layout.widget_chart - protected val ROOT = R.id.widget_frame - protected val TITLE_TEXTVIEW = R.id.widget_title - protected val CHART = R.id.widget_chart - - override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) { - // Do nothing - } +class TideChartToolWidgetView : ChartToolWidgetViewBase() { override suspend fun getPopulatedView(context: Context): RemoteViews { val tides = TidesSubsystem.getInstance(context) @@ -35,7 +25,8 @@ class TideChartToolWidgetView : ToolWidgetView { Duration.between(Instant.now(), it.time).abs() } - val bitmap = onMain { + val views = getView(context) + onMain { val chart = Chart(context) val tideChart = TideChart(chart) if (tide != null) { @@ -56,15 +47,10 @@ class TideChartToolWidgetView : ToolWidgetView { text.textAlignment = View.TEXT_ALIGNMENT_CENTER val layout = Views.linear(listOf(text, chart)) - val width = Resources.dp(context, 400f).toInt() - val height = Resources.dp(context, 200f).toInt() - Views.renderViewAsBitmap(layout, width, height) + renderChart(context, views, layout) } - val views = getView(context) - views.setViewVisibility(TITLE_TEXTVIEW, View.GONE) - views.setImageViewBitmap(CHART, bitmap) views.setOnClickPendingIntent( ROOT, @@ -72,8 +58,4 @@ class TideChartToolWidgetView : ToolWidgetView { ) return views } - - override fun getView(context: Context): RemoteViews { - return RemoteViews(context.packageName, LAYOUT) - } } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/ChartToolWidgetViewBase.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/ChartToolWidgetViewBase.kt new file mode 100644 index 000000000..765640740 --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/ChartToolWidgetViewBase.kt @@ -0,0 +1,32 @@ +package com.kylecorry.trail_sense.tools.tools.widgets + +import android.content.Context +import android.view.View +import android.widget.RemoteViews +import androidx.lifecycle.Lifecycle +import com.kylecorry.andromeda.core.system.Resources +import com.kylecorry.andromeda.core.ui.Views +import com.kylecorry.trail_sense.R +import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView + +abstract class ChartToolWidgetViewBase: ToolWidgetView { + protected val LAYOUT = R.layout.widget_chart + protected val ROOT = R.id.widget_frame + protected val TITLE_TEXTVIEW = R.id.widget_title + protected val CHART = R.id.widget_chart + + override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) { + // Do nothing + } + + protected fun renderChart(context: Context, views: RemoteViews, view: View){ + val width = Resources.dp(context, 400f).toInt() + val height = Resources.dp(context, 200f).toInt() + val bitmap = Views.renderViewAsBitmap(view, width, height) + views.setImageViewBitmap(CHART, bitmap) + } + + override fun getView(context: Context): RemoteViews { + return RemoteViews(context.packageName, LAYOUT) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt index 3e5a4233d..f66d08d14 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt @@ -4,8 +4,6 @@ import android.content.Context import android.view.View import android.widget.RemoteViews import android.widget.TextView -import androidx.lifecycle.Lifecycle -import com.kylecorry.andromeda.core.system.Resources import com.kylecorry.andromeda.core.ui.Views import com.kylecorry.andromeda.views.chart.Chart import com.kylecorry.luna.coroutines.onMain @@ -13,21 +11,13 @@ import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.shared.UserPreferences import com.kylecorry.trail_sense.shared.navigation.NavigationUtils import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools -import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView +import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase import com.kylecorry.trail_sense.tools.weather.infrastructure.subsystem.WeatherSubsystem import com.kylecorry.trail_sense.tools.weather.ui.charts.PressureChart import java.time.Duration import java.time.Instant -class PressureChartToolWidgetView : ToolWidgetView { - protected val LAYOUT = R.layout.widget_chart - protected val ROOT = R.id.widget_frame - protected val TITLE_TEXTVIEW = R.id.widget_title - protected val CHART = R.id.widget_chart - - override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) { - // Do nothing - } +class PressureChartToolWidgetView : ChartToolWidgetViewBase() { override suspend fun getPopulatedView(context: Context): RemoteViews { val weather = WeatherSubsystem.getInstance(context) @@ -42,7 +32,8 @@ class PressureChartToolWidgetView : ToolWidgetView { }.map { it.pressureReading() } - val bitmap = onMain { + val views = getView(context) + onMain { val chart = Chart(context) val pressureChart = PressureChart(chart) pressureChart.plot(displayReadings) @@ -51,15 +42,10 @@ class PressureChartToolWidgetView : ToolWidgetView { text.textAlignment = View.TEXT_ALIGNMENT_CENTER val layout = Views.linear(listOf(text, chart)) - val width = Resources.dp(context, 400f).toInt() - val height = Resources.dp(context, 200f).toInt() - Views.renderViewAsBitmap(layout, width, height) + renderChart(context, views, layout) } - val views = getView(context) - views.setViewVisibility(TITLE_TEXTVIEW, View.GONE) - views.setImageViewBitmap(CHART, bitmap) views.setOnClickPendingIntent( ROOT, @@ -67,8 +53,4 @@ class PressureChartToolWidgetView : ToolWidgetView { ) return views } - - override fun getView(context: Context): RemoteViews { - return RemoteViews(context.packageName, LAYOUT) - } } \ No newline at end of file