mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-23 11:33:42 -05:00
add WeatherWorker and WeatherUpdateScheduler to handle background weather updates and widget refreshing
This commit is contained in:
1 parent
99f604eeb7
commit
2e2a9e4c44
2 files changed
+133
No files matched your search
+42
@@ -0,0 +1,42 @@
|
||||
package com.pranshulgg.weather_master_app.data.worker
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.NetworkType
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object WeatherUpdateScheduler {
|
||||
fun scheduleWeatherUpdates(
|
||||
context: Context,
|
||||
repeatInterval: Int,
|
||||
) {
|
||||
|
||||
val constraints = Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build()
|
||||
|
||||
val request =
|
||||
PeriodicWorkRequestBuilder<WeatherWorker>(
|
||||
repeatInterval.toLong(),
|
||||
TimeUnit.MINUTES
|
||||
)
|
||||
.setConstraints(constraints)
|
||||
.build()
|
||||
|
||||
|
||||
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
|
||||
"@pranshulgg_weather_master_updates",
|
||||
ExistingPeriodicWorkPolicy.UPDATE,
|
||||
request
|
||||
)
|
||||
}
|
||||
|
||||
fun disableWeatherUpdates(context: Context) {
|
||||
WorkManager.getInstance(context)
|
||||
.cancelUniqueWork("@pranshulgg_weather_master_updates")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.pranshulgg.weather_master_app.data.worker
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import androidx.annotation.RequiresPermission
|
||||
import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.CoroutineWorker
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.WorkerParameters
|
||||
import com.pranshulgg.weather_master_app.core.model.weather.WeatherResult
|
||||
import com.pranshulgg.weather_master_app.data.provider.WeatherRepositoryProvider
|
||||
import com.pranshulgg.weather_master_app.data.repository.LocationsRepository
|
||||
import com.pranshulgg.weather_master_app.data.repository.WeatherUnitsRepository
|
||||
import com.pranshulgg.weather_master_app.data.worker.notification.WeatherNotification
|
||||
import com.pranshulgg.weather_master_app.data.worker.widgets.WeatherWidgetUpdater
|
||||
import com.pranshulgg.weather_master_app.data.worker.widgets.widgetWeatherMapper
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
|
||||
@HiltWorker
|
||||
class WeatherWorker @AssistedInject constructor(
|
||||
@Assisted context: Context,
|
||||
@Assisted params: WorkerParameters,
|
||||
private val repositoryProvider: WeatherRepositoryProvider,
|
||||
private val locationsRepository: LocationsRepository,
|
||||
private val appVisibility: AppVisibility,
|
||||
private val weatherUnitsRepository: WeatherUnitsRepository
|
||||
) : CoroutineWorker(context, params) {
|
||||
|
||||
@RequiresPermission(Manifest.permission.POST_NOTIFICATIONS)
|
||||
override suspend fun doWork(): Result {
|
||||
|
||||
// We only run the worker if app is in the background
|
||||
if (appVisibility.isForeground) {
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
return try {
|
||||
|
||||
val locations = locationsRepository.getLocationsOnce()
|
||||
val default = locations.find { it.isDefault }
|
||||
val units = weatherUnitsRepository.getUnitsOnce()
|
||||
|
||||
if (default == null || units == null) {
|
||||
return Result.success()
|
||||
}
|
||||
WeatherNotification.showNotification(default.name)
|
||||
|
||||
|
||||
val repo = repositoryProvider.getRepository(default.source)
|
||||
|
||||
val result = repo.getWeather(
|
||||
location = default,
|
||||
isManualRefresh = false,
|
||||
isForceRefresh = true
|
||||
)
|
||||
|
||||
if (result !is WeatherResult.Success) {
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
val weather = result.weather
|
||||
|
||||
val json = widgetWeatherMapper(weather, applicationContext, units)
|
||||
|
||||
|
||||
WeatherWidgetUpdater(applicationContext).update(json)
|
||||
|
||||
return Result.success()
|
||||
|
||||
} catch (e: Exception) {
|
||||
Result.failure()
|
||||
} finally {
|
||||
WeatherNotification.hideNotification()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun startNow(context: Context) {
|
||||
|
||||
val request =
|
||||
OneTimeWorkRequestBuilder<WeatherWorker>()
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context)
|
||||
.enqueue(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user