/ Tool

A super lazy and fluent Kotlin expressions for initializing lifecycle-aware property

A super lazy and fluent Kotlin expressions for initializing lifecycle-aware property

Lazybones

A super lazy and fluent Kotlin expression for initializing lifecycle-aware property.

Including in your project

Download
Jitpack

Gradle

Add below codes to your root build.gradle file (not your module build.gradle file).

allprojects {
    repositories {
        jcenter()
    }
}

And add a dependency code to your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:lazybones:1.0.1"
}

Usage

lifecycleAware

We can initialize a lifecycle-aware object lazily using the lifecycleAware keyword.

The lifecycleAware functionality can be used to register & unregister listeners, clear something,
show & dismiss and dispose of disposable objects as lifecycle changes by lifecycle owner(Activity, Fragment).

If we want to initialize an object lazily, we should use it with by keyword and lazy() method.

val myDialog: Dialog by lifecycleAware { getDarkThemeDialog(baseContext) }
    .onCreate { this.show() } // show the dialog when the lifecycle's state is onCreate.
    .onDestroy { this.dismiss() } // dismiss the dialog when the lifecycle's state is onDestroy.
    .lazy() // initlize the dialog lazily.

In the onCreate and onDestroy lambda function, we can omit the this keyword.

So we can use like below.

val myDialog: Dialog by lifecycleAware { getDarkThemeDialog(baseContext) }
    .onCreate { show() } // show the dialog when the lifecycle's state is onCreate.
    .onDestroy { dismiss() } // dismiss the dialog when the lifecycle's state is onDestroy.
    .lazy() // initlize the dialog lazily.

CompositeDisposable in RxJava2

Here is an example of CompositeDisposable in RxJava2.

At the same time as initializing lazily the CompositeDisposable, the dispose() method will be
invoked automatically when onDestroy.

val compositeDisposable by lifecycleAware { CompositeDisposable() }
    .onDestroy { dispose() } // call the dispose() method when onDestroy this activity.
    .lazy() // initialize a CompositeDisposable lazily.

Lifecycle related methods

We can invoke lambda functions as lifecycle changes and here are eight lifecycle-related methods of lifecycleAware.

.onCreate { } // the lambda will be invoked when onCreate.
.onStart { } // the lambda will be invoked when onStart.
.onResume { } // the lambda will be invoked when onResume.
.onPause { } // the lambda will be invoked when onPause.
.onStop { } // the lambda will be invoked when onStop.
.onDestroy { }  // the lambda will be invoked when onDestroy.
.onAny { } // the lambda will be invoked whenever the lifecycle state is changed.
.on(On.Create) { } // we can set the lifecycle state manually as an attribute.

Usages in the non-lifecycle owner class

The lifecycleAware is an extension of lifecycleOwner so it can be used on non- lifecycle-owner classes.

class MainViewModel(lifecycleOwner: LifecycleOwner) : ViewModel() {

  private val compositeDisposable by lifecycleOwner.lifecycleAware { CompositeDisposable() }
    .onDestroy { it.dispose() }
    .lazy()

   ...

LifecycleAwareProperty

If we don't need to initialize lazily, here is a more simple way.

We can declare a LifecycleAwareProperty using the lifecycleAware keyword.
The attribute value will not be initialized lazily. so we don't need to use it with by keyword and lazy() method.

private val lifecycleAwareProperty = lifecycleAware(CompositeDisposable())
    // observe lifecycle's state and call the dispose() method when onDestroy  
    .observeOnDestroy { dispose() }

And we can access the original property via the value field.

lifecycleAwareProperty.value.add(disposable)
lifecycleAwareProperty.value.dispose()

We can observe the lifecycle changes using observe_ method.

class MainActivity : AppCompatActivity() {

  private val lifecycleAwareProperty = lifecycleAware(DialogUtil.getDarkTheme())
    .observeOnCreate { show() }
    .observeOnDestroy { dismiss() }
    .observeOnAny { .. }
    .observeOn(On.CREATE) { .. }

    ...

Here is the kotlin dsl way.

private val lifecycleAwareProperty = lifecycleAware(getDarkThemeDialog())
    .observe {
      onCreate { show() }
      onResume { restart() }
      onDestroy { dismiss() }
    }

Using in the non-lifecycle owner class

The lifecycleAware is an extension of lifecycleOwner so it can be used on non- lifecycle-owner classes.

class MainViewModel(lifecycleOwner: LifecycleOwner) : ViewModel() {

  private val TAG = MainViewModel::class.java.simpleName
  private val lifecycleAwareProperty = lifecycleOwner.lifecycleAware(Rabbit())

 init {
    this.lifecycleAwareProperty
      .observeOnCreate { Log.d(TAG, "OnCreate: $this") }
      .observeOnStart { Log.d(TAG, "OnStart: $this") }
      .observeOnResume { Log.d(TAG, "OnResume: $this") }
      .observeOnPause { Log.d(TAG, "OnPause: $this") }
      .observeOnStop { Log.d(TAG, "OnStop: $this") }
      .observeOnDestroy { Log.d(TAG, "OnDestroy: $this") }
      .observeOnAny { }
      .observeOn(On.CREATE) { }
  }
  ...

GitHub

Comments