update WeatherWorker logic, improve widget state handling, and increase hourly forecast limit

This commit is contained in:
PranshulGG committed 2026-06-20 15:16:32 +05:30
1 parent 641bbe726c
commit b4decf5381
3 files changed
+62 -38

No files matched your search

@@ -6,6 +6,7 @@ import android.util.Log
import androidx.annotation.RequiresPermission
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
@@ -22,6 +23,7 @@ import com.pranshulgg.weather_master_app.data.worker.widgets.WeatherWidgetUpdate
import com.pranshulgg.weather_master_app.data.worker.widgets.widgetWeatherMapper
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import java.util.concurrent.TimeUnit
@HiltWorker
class WeatherWorker @AssistedInject constructor(
@@ -40,7 +42,7 @@ class WeatherWorker @AssistedInject constructor(
inputData.getBoolean(KEY_SKIP_FOREGROUND_CHECK, false)
// Only run if app is backgrounded
if (!skipForegroundCheck && appVisibility.isForeground) {
if (appVisibility.isForeground) {
return Result.success()
}
@@ -60,9 +62,7 @@ class WeatherWorker @AssistedInject constructor(
* Show a notification whenever the worker runs
* Don't really need it but why not, i wanna know if its working
*/
if (!skipForegroundCheck) {
WeatherNotification.showNotification(default.name)
}
WeatherNotification.showNotification(default.name)
// Get the repository
@@ -75,6 +75,7 @@ class WeatherWorker @AssistedInject constructor(
isForceRefresh = false
)
if (result !is WeatherResult.Success) {
return Result.success()
}
@@ -99,19 +100,6 @@ class WeatherWorker @AssistedInject constructor(
companion object {
const val KEY_SKIP_FOREGROUND_CHECK = "skip_foreground_check"
// fun startNow(context: Context, skipForegroundCheck: Boolean = false) {
// val inputData = workDataOf(
// KEY_SKIP_FOREGROUND_CHECK to skipForegroundCheck
// )
// val request =
// OneTimeWorkRequestBuilder<WeatherWorker>()
// .setInputData(inputData)
// .build()
//
// WorkManager.getInstance(context)
// .enqueue(request)
// }
suspend fun updateAllWidgets(
context: Context,
data: Weather,
@@ -122,5 +110,17 @@ class WeatherWorker @AssistedInject constructor(
WeatherWidgetUpdater(context).update(json)
}
fun runWorkerOnce(context: Context) {
val request = OneTimeWorkRequestBuilder<WeatherWorker>()
.setInitialDelay(0, TimeUnit.SECONDS)
.build()
WorkManager.getInstance(context).enqueueUniqueWork(
"@pranshulgg_weather_master_updates_once",
ExistingWorkPolicy.REPLACE,
request
)
}
}
}
@@ -58,7 +58,7 @@ fun widgetWeatherMapper(
val hourly = weather.hourly
.drop(hourlyStartIndex)
.take(6)
.take(12)
.map {
val temperature =
TemperatureUnit.CELSIUS.convert(it.temperature, units.tempUnit)?.roundToInt()
@@ -5,13 +5,12 @@ import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.state.updateAppWidgetState
import com.pranshulgg.weather_master_app.widgets.WeatherWidgetStateDefinition
import com.pranshulgg.weather_master_app.widgets.WeatherWidgetStateJson
import com.pranshulgg.weather_master_app.widgets.config.WidgetConfig
import com.pranshulgg.weather_master_app.widgets.glance.GlanceWidget
import com.pranshulgg.weather_master_app.widgets.pill.WidgetPill
import com.pranshulgg.weather_master_app.widgets.summary.SummaryWidget
import com.pranshulgg.weather_master_app.widgets.weather.WeatherWidget
import com.pranshulgg.weather_master_app.widgets.weatherhorizontal.WeatherHorizontalWidget
import com.pranshulgg.weather_master_app.widgets.weatherhorizontal.ui.WeatherWidgetHorizontal
import com.pranshulgg.weather_master_app.widgets.weathersmall.WeatherSmallWidget
import com.pranshulgg.weather_master_app.widgets.weathersmall.ui.WeatherWidgetSmall
class WeatherWidgetUpdater(
private val context: Context
@@ -20,10 +19,10 @@ class WeatherWidgetUpdater(
private val widget = WeatherWidget()
private val pill = WidgetPill()
private val summary = SummaryWidget()
private val widgetSmall = WeatherSmallWidget()
private val widgetHorizontal = WeatherHorizontalWidget()
private val widgetGlance = GlanceWidget()
suspend fun update(json: String) {
val manager =
@@ -32,42 +31,67 @@ class WeatherWidgetUpdater(
val widgetIds = manager.getGlanceIds(WeatherWidget::class.java)
val pillIds = manager.getGlanceIds(WidgetPill::class.java)
val summaryIds = manager.getGlanceIds(SummaryWidget::class.java)
val widgetSmallIds = manager.getGlanceIds(WeatherSmallWidget::class.java)
val widgetHorizontalIds = manager.getGlanceIds(WeatherHorizontalWidget::class.java)
val widgetGlanceIds = manager.getGlanceIds(GlanceWidget::class.java)
widgetIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) {
WeatherWidgetStateJson(json)
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) { current ->
current.copy(json = json, config = current.config)
}
widget.update(context, it)
}
pillIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) {
WeatherWidgetStateJson(json)
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) { current ->
current.copy(json = json, config = current.config)
}
pill.update(context, it)
}
summaryIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) {
WeatherWidgetStateJson(json)
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) { current ->
current.copy(json = json, config = current.config)
}
summary.update(context, it)
}
widgetSmallIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) {
WeatherWidgetStateJson(json)
}
widgetSmall.update(context, it)
}
widgetHorizontalIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) {
WeatherWidgetStateJson(json)
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) { current ->
current.copy(json = json, config = current.config)
}
widgetHorizontal.update(context, it)
}
widgetGlanceIds.forEach {
updateAppWidgetState(context, WeatherWidgetStateDefinition, it) { current ->
current.copy(json = json, config = current.config)
}
widgetGlance.update(context, it)
}
}
suspend fun saveWidgetConfig(
context: Context,
widgetId: Int,
config: WidgetConfig
) {
val manager =
GlanceAppWidgetManager(context)
val glanceId =
manager.getGlanceIdBy(widgetId)
updateAppWidgetState(
context,
WeatherWidgetStateDefinition,
glanceId
) {
it.copy(
config = config
)
}
}
}