Skip to content

Commit

Permalink
Pressure chart widget (#2692)
Browse files Browse the repository at this point in the history
* Add pressure chart widget

Signed-off-by: Kyle Corry <[email protected]>

* Add dynamic color support to widgets

Signed-off-by: Kyle Corry <[email protected]>

* Remove unused deps

Signed-off-by: Kyle Corry <[email protected]>

* Add title to pressure chart

Signed-off-by: Kyle Corry <[email protected]>

---------

Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 authored Nov 18, 2024
1 parent 0819832 commit dcd411b
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,18 @@
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_sun_and_moon_chart_info" />
</receiver>

<receiver
android:name="com.kylecorry.trail_sense.tools.weather.widgets.AppWidgetPressureChart"
android:exported="false"
android:label="@string/pressure_chart">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_pressure_chart_info" />
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)
}
}
21 changes: 21 additions & 0 deletions app/src/main/res/layout/widget_preview_pressure_chart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget_background"
android:elevation="4dp"
android:orientation="vertical"
android:padding="8dp"
android:theme="@style/AppTheme">

<android.widget.ImageView
android:id="@+id/widget_chart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/ic_chart"
android:tint="?android:attr/textColorSecondary" />

</android.widget.LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1509,4 +1509,5 @@
<string name="pref_tool_widgets_header_key" translatable="false">pref_tool_widgets_header_key</string>
<string name="pref_tool_widgets" translatable="false">pref_tool_widgets</string>
<string name="sun_moon_chart">Sun &amp; moon chart</string>
<string name="pressure_chart">Pressure chart</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<item name="preferenceTheme">@style/AppThemePreferenceThemeOverlay</item>
</style>

<style name="WidgetTheme" parent="Theme.Material3.DynamicColors.DayNight" />


<!--Override this for other Android versions-->
<style name="AppTheme" parent="BaseAppTheme" />

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/xml/app_widget_pressure_chart_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget_chart"
android:initialLayout="@layout/widget_chart"
android:minWidth="150dp"
android:minHeight="40dp"
android:previewLayout="@layout/widget_preview_pressure_chart"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="4"
android:targetCellHeight="2"
android:updatePeriodMillis="14400000"
android:widgetCategory="home_screen" />

0 comments on commit dcd411b

Please sign in to comment.