Skip to content

Commit

Permalink
feat: implement background fetch worker (cont.)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirnayeem99 committed Jan 25, 2024
1 parent 46627d4 commit 3f4df13
Showing 1 changed file with 30 additions and 0 deletions.
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
)
}
}

0 comments on commit 3f4df13

Please sign in to comment.