Skip to content

Commit

Permalink
feat: notify glance widget of data updates
Browse files Browse the repository at this point in the history
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
Chesire committed Oct 8, 2023
1 parent b1cea1c commit 4ad87a2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ dependencies {
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.core)
implementation(libs.androidx.fragment)
implementation(libs.androidx.glance.appwidget)
implementation(libs.androidx.hilt.work)
implementation(libs.androidx.lifecycle.extensions)
implementation(libs.androidx.lifecycle.livedata)
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/chesire/nekome/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import androidx.work.Configuration
import com.chesire.lifecyklelog.LifecykleLog
import com.chesire.lifecyklelog.LogHandler
import com.chesire.nekome.core.preferences.ApplicationPreferences
import com.chesire.nekome.services.DataRefreshNotifier
import com.chesire.nekome.services.WorkerQueue
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber

/**
Expand All @@ -27,6 +31,10 @@ class App : Application(), Configuration.Provider {
@Inject
lateinit var workerQueue: WorkerQueue

@Inject
lateinit var dataRefreshNotifier: DataRefreshNotifier

@OptIn(DelicateCoroutinesApi::class)
override fun onCreate() {
super.onCreate()

Expand All @@ -46,6 +54,9 @@ class App : Application(), Configuration.Provider {
workerQueue.enqueueAuthRefresh()
workerQueue.enqueueSeriesRefresh()
workerQueue.enqueueUserRefresh()
GlobalScope.launch {
dataRefreshNotifier.initialize()
}
}

override fun getWorkManagerConfiguration() =
Expand Down
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 app/src/main/java/com/chesire/nekome/services/WidgetDataWorker.kt
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()
}
}

0 comments on commit 4ad87a2

Please sign in to comment.