Git Product home page Git Product logo

whitemagic's Introduction

Whitemagic

An easy way to do lifecycle aware async task style thing on Android with Kotlin

About

Ever wanted to write code like this?

async {
    loadingIndicatorShown = true
    val success = inBackground { Repository.longRunningOperation() }
    loadingIndicatorShown = false
    if(!success){
        showError = true
    }
}

Now you can!

How to

The library defines a couple of extension functions for LifecycleOwner:

Function Description
async wrap your async code inside the async lambda - it's important to not reference the outer scope from here. Therefore you get "target" to reference it.
inBackground inside "async" you can use this to execute the given lambda on a background thread, the lambda can return a result
call the lambda needs to return a tag (String) which you can refer to with findCallbackFor to return the actual result any time later

The best way to learn about the usage might be to look into the sample code - code is worth a thousand words, you know

There is also a LifecycleAwareAndroidViewModel which extends AndroidViewModel but a Lifecycle can be attached.

It's easy to use this library with it and also LiveData is easy to use.

To make use of LifecycleAwareAndroidViewModel you need to attach an actual Lifecycle

vm.attach(this)

Actual code in the ViewModel could look like this (code from the weirdo sample app :) )

async<MainViewModel> {
            // we certainly don't need the permission but just for demonstration we request it
            val locationPermissionGranted = call<Boolean> { requestPermission(target.actionCallbacks.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, "We need Location Permission", "Not really but this is a sample app.") }

            Toast.makeText(target.getApplication<App>(), "Value is $locationPermissionGranted", Toast.LENGTH_SHORT).show()

            if (locationPermissionGranted) {
                val websiteUrl = call<String> { target.actionCallbacks.showInputDialog() }

                val shouldAddToList = call<Boolean> { target.actionCallbacks.showWebsiteStats(websiteUrl) }

                if (shouldAddToList) {
                    inBackground {
                        App.component().getDatabase().webSiteDao().insert(WebSite(websiteUrl))
                    }
                } else {
                    println("Error/Canceled")
                }
            }

        }

Things missing

  • better documentation (obviously)
  • a better sample app (the current is really weird, I know)
  • unit tests at least for the core functionality

Versioning and how to get it

For now it's just available via JitPack.

Versions below 1.0.0 are considered "early access" and things might change. Starting with 1.0.0 the API should be stable and the versioning scheme will follow the rules of semantic versioning.

whitemagic's People

Contributors

bjoernq avatar

Watchers

 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.