diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0104ffc19..11d2a8517 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -473,6 +473,18 @@ android:name="android.appwidget.provider" android:resource="@xml/app_widget_sun_and_moon_chart_info" /> + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/AppWidgetBase.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/AppWidgetBase.kt index e438d69dd..14a7d3d44 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/AppWidgetBase.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/tools/widgets/AppWidgetBase.kt @@ -3,17 +3,26 @@ package com.kylecorry.trail_sense.tools.tools.widgets import android.appwidget.AppWidgetManager import android.content.Context import android.widget.RemoteViews +import com.kylecorry.andromeda.core.system.Resources import com.kylecorry.andromeda.widgets.AndromedaCoroutineWidget import com.kylecorry.trail_sense.R +import com.kylecorry.trail_sense.shared.UserPreferences import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools abstract class AppWidgetBase(private val widgetId: String) : - AndromedaCoroutineWidget(themeToReload = R.style.AppTheme) { + AndromedaCoroutineWidget(themeToReload = R.style.WidgetTheme) { override suspend fun getUpdatedRemoteViews( context: Context, appWidgetManager: AppWidgetManager ): RemoteViews { + val prefs = UserPreferences(context) + if (prefs.useDynamicColors) { + Resources.reloadTheme(context, R.style.WidgetTheme) + } else { + Resources.reloadTheme(context, R.style.AppTheme) + } + val widget = Tools.getWidget(context, widgetId)!! return widget.widgetView.getPopulatedView(context) } diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/weather/WeatherToolRegistration.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/WeatherToolRegistration.kt index 3fa80708d..0510b2b58 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/weather/WeatherToolRegistration.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/WeatherToolRegistration.kt @@ -27,7 +27,9 @@ import com.kylecorry.trail_sense.tools.weather.infrastructure.subsystem.WeatherS import com.kylecorry.trail_sense.tools.weather.quickactions.QuickActionWeatherMonitor import com.kylecorry.trail_sense.tools.weather.services.WeatherMonitorToolService import com.kylecorry.trail_sense.tools.weather.widgets.AppWidgetPressure +import com.kylecorry.trail_sense.tools.weather.widgets.AppWidgetPressureChart import com.kylecorry.trail_sense.tools.weather.widgets.AppWidgetWeather +import com.kylecorry.trail_sense.tools.weather.widgets.PressureChartToolWidgetView import com.kylecorry.trail_sense.tools.weather.widgets.PressureWidgetView import com.kylecorry.trail_sense.tools.weather.widgets.WeatherToolWidgetView @@ -65,6 +67,14 @@ object WeatherToolRegistration : ToolRegistration { PressureWidgetView(), AppWidgetPressure::class.java, updateBroadcasts = listOf(BROADCAST_WEATHER_PREDICTION_CHANGED) + ), + ToolWidget( + WIDGET_PRESSURE_CHART, + context.getString(R.string.pressure_chart), + ToolSummarySize.Full, + PressureChartToolWidgetView(), + AppWidgetPressureChart::class.java, + updateBroadcasts = listOf(BROADCAST_WEATHER_PREDICTION_CHANGED) ) ), isAvailable = { Sensors.hasBarometer(it) }, @@ -180,4 +190,5 @@ object WeatherToolRegistration : ToolRegistration { const val WIDGET_WEATHER = "weather-widget-weather" const val WIDGET_PRESSURE_TENDENCY = "weather-widget-pressure-tendency" + const val WIDGET_PRESSURE_CHART = "weather-widget-pressure-chart" } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/AppWidgetPressureChart.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/AppWidgetPressureChart.kt new file mode 100644 index 000000000..a33790820 --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/AppWidgetPressureChart.kt @@ -0,0 +1,6 @@ +package com.kylecorry.trail_sense.tools.weather.widgets + +import com.kylecorry.trail_sense.tools.tools.widgets.AppWidgetBase +import com.kylecorry.trail_sense.tools.weather.WeatherToolRegistration + +class AppWidgetPressureChart : AppWidgetBase(WeatherToolRegistration.WIDGET_PRESSURE_CHART) 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 new file mode 100644 index 000000000..3e5a4233d --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt @@ -0,0 +1,74 @@ +package com.kylecorry.trail_sense.tools.weather.widgets + +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 +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.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 + } + + override suspend fun getPopulatedView(context: Context): RemoteViews { + val weather = WeatherSubsystem.getInstance(context) + val prefs = UserPreferences(context) + + val history = weather.getHistory() + val displayReadings = history.filter { + Duration.between( + it.time, + Instant.now() + ) <= prefs.weather.pressureHistory + }.map { it.pressureReading() } + + + val bitmap = onMain { + val chart = Chart(context) + val pressureChart = PressureChart(chart) + pressureChart.plot(displayReadings) + + val text = Views.text(context, context.getString(R.string.pressure)) as TextView + 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) + } + + val views = getView(context) + + views.setViewVisibility(TITLE_TEXTVIEW, View.GONE) + views.setImageViewBitmap(CHART, bitmap) + + views.setOnClickPendingIntent( + ROOT, + NavigationUtils.toolPendingIntent(context, Tools.WEATHER) + ) + 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/res/layout/widget_preview_pressure_chart.xml b/app/src/main/res/layout/widget_preview_pressure_chart.xml new file mode 100644 index 000000000..fef50522b --- /dev/null +++ b/app/src/main/res/layout/widget_preview_pressure_chart.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 32c0cb418..516dec51d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1509,4 +1509,5 @@ pref_tool_widgets_header_key pref_tool_widgets Sun & moon chart + Pressure chart \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 000c2abfc..092f449db 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -26,6 +26,9 @@ @style/AppThemePreferenceThemeOverlay +