All Projects → skydoves → Lazybones

skydoves / Lazybones

Licence: apache-2.0
😴 A super lazy and fluent Kotlin expressions for initializing lifecycle-aware property.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Lazybones

Whatif
☔ Fluent expressions of Kotlin for handling single if-else statements, nullable, collections, and boolean.
Stars: ✭ 450 (+394.51%)
Mutual labels:  extensions, expression
Tablemanager
An extension of UITableView. The way it should be. 👍
Stars: ✭ 85 (-6.59%)
Mutual labels:  extensions
Buildplan Maven Plugin
A Maven 3.x plugin to inspect project lifecyle.
Stars: ✭ 57 (-37.36%)
Mutual labels:  lifecycle
Aacomponents
基于google Android Architecture Components 封装实现组件式MVP快速开发框架
Stars: ✭ 66 (-27.47%)
Mutual labels:  lifecycle
Mathjax Third Party Extensions
A collection of MathJax extensions provided by third-party contributors
Stars: ✭ 61 (-32.97%)
Mutual labels:  extensions
Rxjavajdk8interop
RxJava 2/3 interop library for supporting Java 8 features such as Optional, Stream and CompletableFuture [discontinued]
Stars: ✭ 70 (-23.08%)
Mutual labels:  extensions
Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (-39.56%)
Mutual labels:  lifecycle
Lambcycle
🐑🛵 A declarative lambda middleware with life cycle hooks 🐑🛵
Stars: ✭ 88 (-3.3%)
Mutual labels:  lifecycle
Retrofitlifecycle
Manage retrofit call's lifecycle with proxy class which generated by annotation
Stars: ✭ 81 (-10.99%)
Mutual labels:  lifecycle
Easychatandroidclient
EasyChat是一个开源的社交类的App。主要包含消息、好友、群组等相关的IM核心功能。部分界面参照了QQ、微信等相关社交APP。EasyChat APP整体采用MVVM模式,基于JetPack(Lifecycle,LiveData,ViewModel,Room)构建
Stars: ✭ 64 (-29.67%)
Mutual labels:  lifecycle
Canclearad
一款全自动清除网站广告的Chrome插件,支持通用广告,如百度推广或淘宝天猫等广告;也支持特定网站的广告清除。
Stars: ✭ 65 (-28.57%)
Mutual labels:  extensions
Vscode Powershell
Provides PowerShell language and debugging support for Visual Studio Code
Stars: ✭ 1,117 (+1127.47%)
Mutual labels:  extensions
Iter
Simple iterator abstract datatype, intended to iterate efficiently on collections while performing some transformations.
Stars: ✭ 71 (-21.98%)
Mutual labels:  lazy
Jetpackmvvm
🐔🏀一个Jetpack结合MVVM的快速开发框架,基于MVVM模式集成谷歌官方推荐的JetPack组件库:LiveData、ViewModel、Lifecycle、Navigation组件 使用Kotlin语言,添加大量拓展函数,简化代码 加入Retrofit网络请求,协程,帮你简化各种操作,让你快速开发项目
Stars: ✭ 1,100 (+1108.79%)
Mutual labels:  lifecycle
Postinstall Build
Helper for conditionally building your npm package on postinstall
Stars: ✭ 87 (-4.4%)
Mutual labels:  lifecycle
S Mvp
🔥🔥优化版MVP,使用注解泛型简化代码编写,使用模块化协议方便维护,APT过程使用注解解析器利用JavaPoet🌝完成重复模块的编写,利用ASpect+GradlePlugin 完成横向AOP编程+Javassist动态字节码注入+Tinker实现热修复+Retrofit实现优雅网络操作+RxJava轻松玩转数据处理
Stars: ✭ 1,095 (+1103.3%)
Mutual labels:  lifecycle
Formula Parser
Parsing and evaluating mathematical formulas given as strings.
Stars: ✭ 62 (-31.87%)
Mutual labels:  expression
Annotationkit
The annotation implementation using Objective-C
Stars: ✭ 68 (-25.27%)
Mutual labels:  lifecycle
Imlazy
😴 Functional programming with lazy immutable iterables
Stars: ✭ 89 (-2.2%)
Mutual labels:  lazy
Reason Loadable
🔥 Suspense/Lazy for ReasonReact.
Stars: ✭ 88 (-3.3%)
Mutual labels:  lazy

Lazybones

License API Build Status Javadoc

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

Ah... I'm a super lazy person.
I just want to initialize and declare of disposing it at the same time.

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) { }
  }
  ...

Find this library useful? ❤️

Support it by joining stargazers for this repository. ⭐️
And follow me for my next creations! 🤩

License

Copyright 2020 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the L
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].