:sync
This module handles background data synchronization using WorkManager. It ensures data consistency between local and remote data sources by performing periodic and on-demand sync operations.
Features
Background Synchronization
Periodic Sync Scheduling
Work Constraints Management
Progress Tracking
Error Handling
Hilt Worker Integration
Dependencies Graph
Usage
dependencies {
implementation(project(":sync"))
}
Content copied to clipboard
Setting Up Sync
Make your repository syncable:
interface YourRepository : Syncable {
override suspend fun sync(): Flow<SyncProgress>
}Content copied to clipboardCreate sync worker:
@HiltWorker
class SyncWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted params: WorkerParameters,
private val repository: YourRepository
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
repository.sync()
.collect { progress ->
setProgress(progress.toWorkData())
}
return Result.success()
}
}Content copied to clipboardRequest sync operation:
class YourRepositoryImpl @Inject constructor(
private val syncManager: SyncManager
) : YourRepository {
fun requestSync() {
syncManager.requestSync()
}
}Content copied to clipboard
Work Constraints
The sync operation respects the following constraints:
Network availability
Battery not low
Storage not low
Device idle (for periodic sync)
Progress Tracking
data class SyncProgress(
val total: Int = 0,
val current: Int = 0,
val message: String? = null
)
Content copied to clipboard
The sync progress can be observed from the WorkManager's progress updates.