-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement background fetch worker (cont.)
- Loading branch information
1 parent
46627d4
commit 3f4df13
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...c/main/java/io/github/kabirnayeem99/islamqaorg/domain/useCase/FetchAndSavePeriodically.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,30 @@ | ||
package io.github.kabirnayeem99.islamqaorg.domain.useCase | ||
|
||
import android.content.Context | ||
import androidx.work.Constraints | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequest | ||
import androidx.work.WorkManager | ||
import io.github.kabirnayeem99.islamqaorg.data.workers.BackgroundQAListFetcherWorker | ||
import javax.inject.Inject | ||
|
||
class FetchAndSavePeriodically | ||
@Inject constructor(private val appContext: Context) { | ||
|
||
operator suspend fun invoke() { | ||
schedulePeriodicSync(appContext) | ||
} | ||
|
||
suspend fun schedulePeriodicSync(context: Context) { | ||
val constraints = | ||
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build() | ||
|
||
val periodicRequest = PeriodicWorkRequest.Builder( | ||
BackgroundQAListFetcherWorker::class.java, SYNC_INTERVAL_HOURS, TimeUnit.HOURS | ||
).setConstraints(constraints).addTag(WORK_TAG).build() | ||
|
||
WorkManager.getInstance(context).enqueueUniquePeriodicWork( | ||
WORK_TAG, ExistingPeriodicWorkPolicy.KEEP, periodicRequest | ||
) | ||
} | ||
} |