Created
October 11, 2020 17:03
-
-
Save mbakgun/6d386490ff93882a54a02efef103c2f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.dolap.events.data.workmanager | |
| import android.content.Context | |
| import androidx.work.* | |
| import java.util.concurrent.TimeUnit | |
| class EventWorkManager(appContext: Context) { | |
| private val instance by lazy { WorkManager.getInstance(appContext) } | |
| fun sendEvent(id: Int) = instance.beginUniqueWork( | |
| TAG, | |
| ExistingWorkPolicy.APPEND, | |
| buildEvent(id) | |
| ).enqueue() | |
| private fun buildEvent(id: Int): OneTimeWorkRequest = | |
| OneTimeWorkRequest.Builder(EventWorker::class.java) | |
| .setInitialDelay(INITIAL_DELAY, TimeUnit.SECONDS) | |
| .setInputData(Data.Builder().putInt(DATA_TAG, id).build()) | |
| .setConstraints( | |
| Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build() | |
| ) | |
| .build() | |
| companion object { | |
| private const val TAG: String = "EventWorkManager" | |
| private const val INITIAL_DELAY = 6L | |
| const val DATA_TAG = "BannerId_EventWorkManager" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment