From 5e88813f806ae9a88546e8e5518a73609648a27c Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Thu, 4 Apr 2024 13:51:43 +0530 Subject: [PATCH] weather: Add support for narrow layout --- .../widget/WeatherAppWidgetProvider.kt | 20 ++- .../worker/WeatherUpdaterWorker.kt | 20 ++- .../main/res/layout/layout_weather_info.xml | 6 +- .../res/layout/layout_weather_info_narrow.xml | 167 ++++++++++++++++++ android/src/main/res/layout/weather_day.xml | 8 +- 5 files changed, 208 insertions(+), 13 deletions(-) create mode 100644 android/src/main/res/layout/layout_weather_info_narrow.xml diff --git a/android/src/main/kotlin/foundation/e/blissweather/widget/WeatherAppWidgetProvider.kt b/android/src/main/kotlin/foundation/e/blissweather/widget/WeatherAppWidgetProvider.kt index 9955587..443a897 100644 --- a/android/src/main/kotlin/foundation/e/blissweather/widget/WeatherAppWidgetProvider.kt +++ b/android/src/main/kotlin/foundation/e/blissweather/widget/WeatherAppWidgetProvider.kt @@ -7,9 +7,11 @@ */ package foundation.e.blissweather.widget +import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent +import android.os.Bundle import android.util.Log import androidx.work.BackoffPolicy import androidx.work.Constraints @@ -28,6 +30,16 @@ import javax.inject.Inject class WeatherAppWidgetProvider : AppWidgetProvider() { @Inject lateinit var appPrefs: AppPreferences + override fun onAppWidgetOptionsChanged( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetId: Int, + newOptions: Bundle? + ) { + refresh(context) + super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions) + } + override fun onEnabled(context: Context) { val workMgr = WorkManager.getInstance(context.applicationContext) val refreshInterval = appPrefs.weatherRefreshInterval @@ -55,14 +67,18 @@ class WeatherAppWidgetProvider : AppWidgetProvider() { } } + private fun refresh(context: Context) { + onDisabled(context) + onEnabled(context) + } + override fun onReceive(context: Context, intent: Intent?) { Log.d(TAG, "onReceive: $intent}") super.onReceive(context, intent) when (intent?.action) { ACTION_WEATHER_REFRESH, Intent.ACTION_LOCALE_CHANGED -> { - onDisabled(context) - onEnabled(context) + refresh(context) } } } diff --git a/android/src/main/kotlin/foundation/e/blissweather/worker/WeatherUpdaterWorker.kt b/android/src/main/kotlin/foundation/e/blissweather/worker/WeatherUpdaterWorker.kt index f47f4a3..53d688a 100644 --- a/android/src/main/kotlin/foundation/e/blissweather/worker/WeatherUpdaterWorker.kt +++ b/android/src/main/kotlin/foundation/e/blissweather/worker/WeatherUpdaterWorker.kt @@ -53,12 +53,22 @@ constructor( return if (weather?.current == null || weather.forecasts == null) { Result.retry() } else { - val views = - RemoteViews(applicationContext.packageName, R.layout.layout_weather_info).apply { - updateWeatherView(applicationContext, weather, appPreferences) - } + widgetIds.forEach { id -> + val maxWidth = + appWidgetManager + ?.getAppWidgetOptions(id) + ?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) + ?: 0 + val view = + RemoteViews( + applicationContext.packageName, + if (maxWidth <= 300) R.layout.layout_weather_info_narrow + else R.layout.layout_weather_info + ) + .apply { updateWeatherView(applicationContext, weather, appPreferences) } - appWidgetManager.updateAppWidget(widgetIds, views) + appWidgetManager.updateAppWidget(id, view) + } Result.success() } } diff --git a/android/src/main/res/layout/layout_weather_info.xml b/android/src/main/res/layout/layout_weather_info.xml index f172b33..f2914de 100644 --- a/android/src/main/res/layout/layout_weather_info.xml +++ b/android/src/main/res/layout/layout_weather_info.xml @@ -78,7 +78,8 @@ android:id="@+id/location" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:breakStrategy="high_quality" + android:ellipsize="middle" + android:maxLines="1" android:textColor="@color/e_primary_text_color_dark" android:textSize="18sp" /> @@ -86,6 +87,7 @@ android:id="@+id/weather_summary" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:ellipsize="end" android:maxLines="1" android:textColor="@color/e_secondary_text_color_dark" android:textSize="16sp" /> @@ -161,4 +163,4 @@ android:layout_height="wrap_content" android:orientation="horizontal" /> - + \ No newline at end of file diff --git a/android/src/main/res/layout/layout_weather_info_narrow.xml b/android/src/main/res/layout/layout_weather_info_narrow.xml new file mode 100644 index 0000000..aadb216 --- /dev/null +++ b/android/src/main/res/layout/layout_weather_info_narrow.xml @@ -0,0 +1,167 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/src/main/res/layout/weather_day.xml b/android/src/main/res/layout/weather_day.xml index 3318d47..f557c14 100644 --- a/android/src/main/res/layout/weather_day.xml +++ b/android/src/main/res/layout/weather_day.xml @@ -14,9 +14,9 @@ android:layout_gravity="center_horizontal" android:gravity="center_horizontal|fill_horizontal" android:maxLines="1" - android:text="mon" android:textAppearance="?android:attr/textAppearanceSmallInverse" - android:textColor="@color/e_secondary_text_color_dark" /> + android:textColor="@color/e_secondary_text_color_dark" + android:textSize="12sp" /> + android:textColor="@color/e_primary_text_color_dark" + android:textSize="12sp" /> -- GitLab