-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: notify glance widget of data updates
When the data updates such as updating a series progress, or adding a new series, we need to notify the glance widget. This is done via a singleton class that listens to the repository, and on updates to the data fires the required worker to update the widget.
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/chesire/nekome/services/DataRefreshNotifier.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,35 @@ | ||
package com.chesire.nekome.services | ||
|
||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import com.chesire.nekome.datasource.series.SeriesRepository | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
private const val WIDGET_DATA_NOTIFY_TAG = "WidgetData" | ||
private const val WIDGET_DATA_UNIQUE_NAME = "WidgetSync" | ||
|
||
@Singleton | ||
class DataRefreshNotifier @Inject constructor( | ||
private val workManager: WorkManager, | ||
private val seriesRepository: SeriesRepository | ||
) { | ||
|
||
/** | ||
* Initialize the notifier and listen to any data updates. | ||
*/ | ||
suspend fun initialize() { | ||
seriesRepository.getSeries().collect { | ||
val request = OneTimeWorkRequestBuilder<WidgetDataWorker>() | ||
.addTag(WIDGET_DATA_NOTIFY_TAG) | ||
.build() | ||
|
||
workManager.enqueueUniqueWork( | ||
WIDGET_DATA_UNIQUE_NAME, | ||
ExistingWorkPolicy.REPLACE, | ||
request | ||
) | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/chesire/nekome/services/WidgetDataWorker.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,18 @@ | ||
package com.chesire.nekome.services | ||
|
||
import android.content.Context | ||
import androidx.glance.appwidget.updateAll | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
import com.chesire.nekome.feature.serieswidget.ui.SeriesWidget | ||
|
||
class WidgetDataWorker( | ||
private val context: Context, | ||
params: WorkerParameters, | ||
) : CoroutineWorker(context, params) { | ||
|
||
override suspend fun doWork(): Result { | ||
SeriesWidget().updateAll(context) | ||
return Result.success() | ||
} | ||
} |