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
// getLiveData gets MutableLiveData associated with a key. | |
// When the value associated with the key updates, the MutableLiveData does as well. | |
private val _userId : MutableLiveData<String> = savedStateHandle.getLiveData(USER_KEY) | |
// Only expose a immutable LiveData | |
val userId : LiveData<String> = _userId |
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
class MyViewModel(state : SavedStateHandle) : ViewModel() { | |
// Keep the key as a constant | |
companion object { | |
private val USER_KEY = "userId" | |
} | |
private val savedStateHandle = state | |
fun saveCurrentUser(userId: String) { |
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 ktx requires at least androidx.fragment:fragment-ktx:1.1.0 or | |
// androidx.activity:activity-ktx:1.0.0 | |
val viewModel by viewModels { SavedStateVMFactory(this) } | |
// Or the non-ktx way... | |
val viewModel = ViewModelProvider(this, SavedStateVMFactory(this)) | |
.get(MyViewModel::class.java) |
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
// In your UI (activity, fragment, etc) | |
WorkManager.getInstance().getWorkInfoByIdLiveData(uploadWorkRequest.id) | |
.observe(lifecycleOwner, Observer { workInfo -> | |
// Check if the current work's state is "successfully finished" | |
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) { | |
displayImage(workInfo.outputData.getString(KEY_IMAGE_URI)) | |
} | |
}) |
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
val compressWorkRequest = OneTimeWorkRequestBuilder<CompressWorker>() | |
.setInputMerger(ArrayCreatingInputMerger::class.java) | |
.setConstraints(constraints) | |
.build() |
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
WorkManager.getInstance() | |
.beginWith(Arrays.asList( | |
filterImageOneWorkRequest, | |
filterImageTwoWorkRequest, | |
filterImageThreeWorkRequest)) | |
.then(compressWorkRequest) | |
.then(uploadWorkRequest) | |
.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
WorkManager.getInstance().enqueue(uploadWorkRequest) |
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
// Create the Constraints | |
val constraints = Constraints.Builder() | |
.setRequiredNetworkType(NetworkType.CONNECTED) | |
.build() | |
// Define the input | |
val imageData = workDataOf(Constants.KEY_IMAGE_URI to imageUriString) | |
// Bring it all together by creating the WorkRequest; this also sets the back off criteria | |
val uploadWorkRequest = OneTimeWorkRequestBuilder<UploadWorker>() |
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
val constraints = Constraints.Builder() | |
.setRequiresBatteryNotLow(true) | |
.setRequiredNetworkType(NetworkType.CONNECTED) | |
.setRequiresCharging(true) | |
.setRequiresStorageNotLow(true) | |
.setRequiresDeviceIdle(true) | |
.build() |
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
val constraints = Constraints.Builder() | |
.setRequiredNetworkType(NetworkType.CONNECTED) | |
.build() |