Releases: skydoves/lazybones
1.0.4
1.0.3
🎉 A new version 1.0.3
has been released! 🎉
Introduce
Now Lazybones supports lifecycleAware
for Jetpack ViewModel to track and observe the lifecycle changes of the ViewModel. Basically, Lazybones-ViewModel allows you to observe two lifecycle changes: Initialize and Clear.
class MyViewModel : ViewModel() {
private val lifecycleAwareCompositeDisposable = lifecycleAware { CompositeDisposable() }
.onInitialize {
Log.d(TAG, "ViewModel is initialized")
}.onClear {
Log.d(TAG, "ViewModel is cleared")
dispose() // dispose CompositeDisposable when viewModel is getting cleared
}
}
What's Changed
- Migrate to Lifecycle 2.4.0 by @skydoves in #3
- New Feature: lazybones-viewmodel by @skydoves in #4
- Implement lazybones viewmodel extensions by @skydoves in #5
Full Changelog: 1.0.2...1.0.3
1.0.2
🎉 Released a new version 1.0.2
! 🎉
Coroutines and Flow
Add a dependency code to your module's build.gradle
file.
dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$versions.lifecycle" // over the 2.4.0-alpha01
}
We can apply to the coroutines lifecycleScope
for launching suspend functions. The Lifecycle-runtime-ktx supports launchWhenStarted
, launchWhenCreated
, and launchWhenResumed
for the lifecycleScope
. However those coroutines jobs will not be canceled automatically, so they must be canceled
on a specific lifecycle. And we can declare canceling together with initialization like the below.
private val job: Job by lifecycleAware {
lifecycleScope.launchWhenCreated {
// call suspend
}
}.onDestroy {
cancel() // cancel when the lifecycle is destroyed.
}.lazy()
We can reduce the above codes like the below using the launchOnStarted
, launchOnCreated
, and launchOnResume
.
private val job: Job by launchOnStarted {
// call suspend
}.onDestroy {
cancel()
}.lazy()
If we need to collect one flow on the coroutines lifecycle scope, we can use like the below.
private val job: Job by launchOnStarted(repository.fetchesDataFlow()) {
// collected value from the repository.fetchesDataFlow()
}.onDestroy {
cancel()
}.lazy()
addOnRepeatingJob
The addRepeatingJob extension has been added in the new version of the Lifecycle-runtime-ktx.
Launches and runs the given block in a coroutine when this LifecycleOwner's Lifecycle is at least at state. The launched coroutine will be canceled when the lifecycle state falls below the state.
We can collect a flow on the coroutines lifecycle scope and cancel it automatically if the lifecycle falls below that state, and will restart if it's in that state again.
private val job: Job by addOnRepeatingJob(Lifecycle.State.CREATED, simpleFlow()) {
// collected value from the fetchesDataFlow()
}.lazy()
1.0.1
Released version 1.0.1
.
- The lifecycle related to
lifecycleAware
functionalities receives the parameter as applied.
So we can omit theit
receiver.
Before
private val balloon: Balloon by lifecycleAware { BalloonUtils.getProfileBalloon(baseContext) }
.onCreate { it.showAlignTop(button) }
.onDestroy { it.dismiss() }
.lazy()
After
private val balloon: Balloon by lifecycleAware { BalloonUtils.getProfileBalloon(baseContext) }
.onCreate { showAlignTop(button) }
.onDestroy { dismiss() }
.lazy()
- Implemented LifecycleAwareProperty's kotlin dsl way for observing.
private val lifecycleAwareProperty = lifecycleAware(getDarkThemeDialog())
.observe {
onCreate { show() }
onResume { restart() }
onDestroy { dismiss() }
}