Coder Social home page Coder Social logo

loco's Introduction

loco - launch on ๐Ÿš€ cancel on ๐Ÿ’ฅ

Deprecated

You can now use the native Lifecycle.repeatOnLifecycle





version license

last commit code quality build

installation

repositories {
    jcenter()
}

dependencies {
    implementation("at.florianschuster.loco:loco-core:$version")
}

contains

extensions to launch on a specific lifecycle event & cancel on a specific lifecycle event

example:

val lifecycle: Lifecycle
lifecycle.launchOnCreateCancelOnDestroy {
    /**
     * these will be launched once the lifecycle is created and cancelled once the 
     * lifecycle is destroyed. it is also relaunched on each create.
     */
    someFlow().launchIn(this)
    launch { someSuspendingFunction() }
}

example:

class MainActivity : Activity(R.layout.activity_main) {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        launchOnLifecycleCreateCancelOnLifecycleDestroy {
            /**
             * this will be launched once the Activity is created and cancelled once the 
             * activity is destroyed. it is also relaunched on each create.
             */
            button.clicks().onEach {
                // do something
            }.launchIn(this)
        }
    }
}

example:

class SomeFragment : Frgment(R.layout.fragment_some) {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        launchOnViewLifecycleStartCancelOnViewLifecycleStop {
            /**
             * this will be launched once the fragment view lifecycle is started and cancelled  
             * once the fragment view lifecycle is stopped. it is also relaunched on each start.
             */
            button.clicks().onEach {
                // do something
            }.launchIn(this)
        }
    }
}

example view attach/detach:

class CustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
 
    init {
        launchOnViewAttachCancelOnViewDetach {
            /**
             * this will be launched once the view is attached to a window and cancelled
             * once the view is detached from the window. it is also relaunched in each
             * attach to window.
             */
            someFlow().launchIn(this)
            launch { suspendingFunction() }
        }
    }
}

example view-tree-lifecycle:

class CustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
 
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        launchOnLifecycleResumeCancelOnLifecyclePause {
            /**
             * this will be launched once the view tree lifecycle is resumed and cancelled  
             * once the view tree lifecycle is paused. it is also relaunched on each resume.
             */
            someFlow().launchIn(this)
            launch { suspendingFunction() }
        }
    }
}

author

visit my website.

loco's People

Contributors

floschu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.