-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
0819832
commit dcd411b
Showing
9 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/kylecorry/trail_sense/tools/weather/widgets/AppWidgetPressureChart.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
74 changes: 74 additions & 0 deletions
74
.../main/java/com/kylecorry/trail_sense/tools/weather/widgets/PressureChartToolWidgetView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |