-
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
e9e8d3b
commit 968cc3f
Showing
6 changed files
with
119 additions
and
69 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
app/src/main/java/io/github/kabirnayeem99/islamqaorg/common/base/OneTimEvent.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,13 @@ | ||
package io.github.kabirnayeem99.islamqaorg.common.base | ||
|
||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
|
||
class OneTimeEvent<T> { | ||
private val channel = Channel<T>() | ||
|
||
fun sendEvent(event: T) = channel.trySend(event) | ||
|
||
fun asFlow(): Flow<T> = channel.receiveAsFlow() | ||
} |
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
38 changes: 3 additions & 35 deletions
38
...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 |
---|---|---|
@@ -1,47 +1,15 @@ | ||
package io.github.kabirnayeem99.islamqaorg.domain.useCase | ||
|
||
import androidx.work.Constraints | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequest | ||
import androidx.work.WorkManager | ||
import io.github.kabirnayeem99.islamqaorg.data.workers.BackgroundQAListFetcherWorker | ||
import timber.log.Timber | ||
import java.util.concurrent.TimeUnit | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
class FetchAndSavePeriodically | ||
@Inject constructor(private val workManager: WorkManager) { | ||
|
||
operator fun invoke() = schedulePeriodicSync() | ||
operator fun invoke(): Flow<Result<Boolean>>? = | ||
BackgroundQAListFetcherWorker.enqueue(workManager) | ||
|
||
private fun schedulePeriodicSync() { | ||
try { | ||
val tag = BackgroundQAListFetcherWorker.TAG | ||
|
||
val constraintsBuilder = Constraints.Builder() | ||
constraintsBuilder.apply { | ||
setRequiredNetworkType(NetworkType.CONNECTED) | ||
setRequiresBatteryNotLow(true) | ||
setRequiresStorageNotLow(true) | ||
} | ||
val constraints = constraintsBuilder.build() | ||
|
||
val periodicRequestBuilder = PeriodicWorkRequest.Builder( | ||
BackgroundQAListFetcherWorker::class.java, SYNC_INTERVAL_HOURS, TimeUnit.HOURS | ||
) | ||
periodicRequestBuilder.apply { | ||
setConstraints(constraints) | ||
addTag(tag) | ||
} | ||
val request = periodicRequestBuilder.build() | ||
workManager.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP, request) | ||
} catch (e: Exception) { | ||
Timber.e("schedulePeriodicSync: ${e.localizedMessage}", e) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val SYNC_INTERVAL_HOURS = 4L | ||
} | ||
} |
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
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
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