Giter VIP home page Giter VIP logo

task's Introduction

Task

Download

Task is a thread scheduling framework.

feature

  1. Support priority;
  2. Support cancel task when Activity/Fragment destroy;
  3. Support auto change priority when Activity/Fragment switch visible/invisible.
  4. Filter duplicate tasks.
  5. More control of task schedule.

Download

repositories {
    jcenter()
}

dependencies {
    implementation 'com.horizon.task:task:1.0.2'
}

At first, init logger

LogProxy.init(object : TaskLogger {
    override val isDebug: Boolean
        get() = BuildConfig.DEBUG

    override fun e(tag: String, e: Throwable) {
        Log.e(tag, e.message, e)
    }
})

If you need lifecycle support, notify events when Activity/Fragment lifecycle change.

abstract class BaseActivity : Activity() {
    override fun onDestroy() {
        super.onDestroy()
        LifecycleManager.notify(this, Event.DESTROY)
    }

    override fun onPause() {
        super.onPause()
        LifecycleManager.notify(this, Event.HIDE)
    }

    override fun onResume() {
        super.onResume()
        LifecycleManager.notify(this, Event.SHOW)
    }
}

There's several ways to use:

1、Just run jobs with executor

TaskCenter.io.execute{
    // do something
}

2、 Use like AsyncTask, but more contral

class TestActivity : BaseActivity() {
    private lateinit var mCountingTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_counting_test)

        mCountingTv = findViewById(R.id.counting_tv)

        CountingTask()
                .host(this)
                .priority(Priority.IMMEDIATE)
                .execute(20 as Integer)
    }

    private inner class CountingTask : IOTask<Integer, String, String>(){
        override fun doInBackground(vararg params: Integer): String? {
            val n = params[0] as Int
            return try {
                for (count in n downTo 1) {
                    publishProgress(Integer.toString(count))
                    Thread.sleep(1000)
                }
                "done"
            } catch (e: InterruptedException) {
                Log.w(tag, e.javaClass.simpleName + " occurred")
                "cancel"
            }
        }

        override fun onProgressUpdate(vararg values: String) {
            mCountingTv.text = values[0]
        }

        override fun onCancelled(result: String?) {
            Log.i(tag, "task result:  " + result!!)
        }

        override fun onPostExecute(result: String?) {
            mCountingTv.text = result
        }
    }
}

3、Use to RxJava

object TaskSchedulers {
    val io: Scheduler by lazy { Schedulers.from(TaskCenter.io) }
    val computation: Scheduler by lazy { Schedulers.from(TaskCenter.computation) }
    val single by lazy { Schedulers.from(PipeExecutor(1)) }
}
Observable.range(1, 8)
       .subscribeOn(TaskSchedulers.computation)
       .subscribe { Log.d(tag, "number:$it") }

License

See the LICENSE file for license rights and limitations.

task's People

Contributors

billywei01 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.