Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: use more rx in ProductUploaderWorker.kt
- Loading branch information
Showing
7 changed files
with
88 additions
and
86 deletions.
There are no files selected for viewing
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
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
70 changes: 0 additions & 70 deletions
70
app/src/main/java/openfoodfacts/github/scrachx/openfood/jobs/OfflineProductWorker.kt
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
app/src/main/java/openfoodfacts/github/scrachx/openfood/jobs/ProductUploaderWorker.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,72 @@ | ||
package openfoodfacts.github.scrachx.openfood.jobs | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.util.Log | ||
import androidx.hilt.work.HiltWorker | ||
import androidx.work.* | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedInject | ||
import io.reactivex.Single | ||
import openfoodfacts.github.scrachx.openfood.R | ||
import openfoodfacts.github.scrachx.openfood.utils.OfflineProductService | ||
import javax.inject.Inject | ||
|
||
@HiltWorker | ||
class ProductUploaderWorker @AssistedInject constructor( | ||
@Assisted context: Context, | ||
@Assisted workerParams: WorkerParameters | ||
) : RxWorker(context, workerParams) { | ||
|
||
@Inject | ||
lateinit var offlineProductService: OfflineProductService | ||
|
||
override fun createWork() = Single.fromCallable { inputData.getBoolean(KEY_INCLUDE_IMAGES, false) } | ||
.doOnSuccess { Log.d(WORK_TAG, "[START] (includeImages=$it)") } | ||
.flatMap { includeImages -> offlineProductService.uploadAll(includeImages) } | ||
.map { shouldRetry -> | ||
if (shouldRetry) { | ||
Log.d(WORK_TAG, "[RETRY]") | ||
Result.retry() | ||
} else { | ||
Log.d(WORK_TAG, "[SUCCESS]") | ||
Result.success() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val WORK_TAG = "OFFLINE_WORKER_TAG" | ||
const val KEY_INCLUDE_IMAGES = "includeImages" | ||
private fun inputData(includeImages: Boolean) = Data.Builder() | ||
.putBoolean(KEY_INCLUDE_IMAGES, includeImages) | ||
.build() | ||
|
||
fun scheduleProductUpload(context: Context, sharedPreferences: SharedPreferences) { | ||
|
||
val constData = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
val uploadDataWorkRequest = OneTimeWorkRequest.Builder(ProductUploaderWorker::class.java) | ||
.setInputData(inputData(false)) | ||
.setConstraints(constData) | ||
.build() | ||
|
||
val constPics = Constraints.Builder() | ||
.setRequiredNetworkType( | ||
if (sharedPreferences.getBoolean(context.getString(R.string.pref_enable_mobile_data_key), true)) | ||
NetworkType.CONNECTED | ||
else | ||
NetworkType.UNMETERED | ||
) | ||
val uploadPicturesWorkRequest = OneTimeWorkRequest.Builder(ProductUploaderWorker::class.java) | ||
.setInputData(inputData(true)) | ||
.setConstraints(constPics.build()) | ||
.build() | ||
|
||
WorkManager.getInstance(context) | ||
.beginUniqueWork(WORK_TAG, ExistingWorkPolicy.REPLACE, uploadDataWorkRequest) | ||
.then(uploadPicturesWorkRequest) | ||
.enqueue() | ||
} | ||
} | ||
} |
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