Initialize sync manually

Change-Id: Ia26b1179270a87d3b87a915185b8d5fc7532c464
pull/650/head
Adetunji Dahunsi 2 years ago
parent f3faec8432
commit 489502ba13

@ -87,7 +87,6 @@ androidx-metrics = { group = "androidx.metrics", name = "metrics-performance", v
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxNavigation" } androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxNavigation" }
androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxNavigation" } androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxNavigation" }
androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" } androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" }
androidx-startup = { group = "androidx.startup", name = "startup-runtime", version.ref = "androidxStartup" }
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" } androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" } androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" }
androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" } androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" }

@ -33,7 +33,6 @@ dependencies {
implementation(project(":core:datastore")) implementation(project(":core:datastore"))
implementation(project(":core:model")) implementation(project(":core:model"))
implementation(libs.androidx.lifecycle.livedata.ktx) implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.startup)
implementation(libs.androidx.tracing.ktx) implementation(libs.androidx.tracing.ktx)
implementation(libs.androidx.work.ktx) implementation(libs.androidx.work.ktx)
implementation(libs.firebase.cloud.messaging) implementation(libs.firebase.cloud.messaging)

@ -18,17 +18,6 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application> <application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- TODO: b/2173216 Disable auto sync startup till it works well with instrumented tests -->
<meta-data
android:name="com.google.samples.apps.nowinandroid.sync.initializers.SyncInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
<service <service
android:name=".services.SyncNotificationsService" android:name=".services.SyncNotificationsService"
android:exported="false" android:exported="false"

@ -18,17 +18,6 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application> <application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- TODO: b/2173216 Disable auto sync startup till it works well with instrumented tests -->
<meta-data
android:name="com.google.samples.apps.nowinandroid.sync.initializers.SyncInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
<service <service
android:name=".services.SyncNotificationsService" android:name=".services.SyncNotificationsService"
android:exported="false"> android:exported="false">

@ -17,31 +17,14 @@
package com.google.samples.apps.nowinandroid.sync.initializers package com.google.samples.apps.nowinandroid.sync.initializers
import android.content.Context import android.content.Context
import androidx.startup.AppInitializer
import androidx.startup.Initializer
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.WorkManagerInitializer
import com.google.samples.apps.nowinandroid.sync.workers.SyncWorker import com.google.samples.apps.nowinandroid.sync.workers.SyncWorker
object Sync { object Sync {
// This method is a workaround to manually initialize the sync process instead of relying on // This method is initializes sync, the process that keeps the app's data current.
// automatic initialization with Androidx Startup. It is called from the app module's // It is called from the app module's Application.onCreate() and should be only done once.
// Application.onCreate() and should be only done once.
fun initialize(context: Context) { fun initialize(context: Context) {
AppInitializer.getInstance(context)
.initializeComponent(SyncInitializer::class.java)
}
}
// This name should not be changed otherwise the app may have concurrent sync requests running
internal const val SyncWorkName = "SyncWorkName"
/**
* Registers work to sync the data layer periodically on app startup.
*/
class SyncInitializer : Initializer<Sync> {
override fun create(context: Context): Sync {
WorkManager.getInstance(context).apply { WorkManager.getInstance(context).apply {
// Run sync on app startup and ensure only one sync worker runs at any time // Run sync on app startup and ensure only one sync worker runs at any time
enqueueUniqueWork( enqueueUniqueWork(
@ -50,10 +33,8 @@ class SyncInitializer : Initializer<Sync> {
SyncWorker.startUpSyncWork(), SyncWorker.startUpSyncWork(),
) )
} }
return Sync
} }
override fun dependencies(): List<Class<out Initializer<*>>> =
listOf(WorkManagerInitializer::class.java)
} }
// This name should not be changed otherwise the app may have concurrent sync requests running
internal const val SyncWorkName = "SyncWorkName"

Loading…
Cancel
Save