项目作者: mcxiaoke

项目描述 :
Koi, a lightweight kotlin library for Android Development.
高级语言: Kotlin
项目地址: git://github.com/mcxiaoke/kotlin-koi.git
创建时间: 2016-01-26T10:19:24Z
项目社区:https://github.com/mcxiaoke/kotlin-koi

开源协议:Apache License 2.0

下载


Koi - A lightweight Kotlin library for Android

Koi include many useful extensions and functions, they can help reducing the boilerplate code in Android applications. Specifically, Koi include a powerful extension function named asyncSafe.

Gradle

Latest Version: :core:0.5.5-brightgreen.svg" alt="Maven Central"> Compiled with Kotlin 1.1.4.

  1. compile 'com.mcxiaoke.koi:core:0.5.5' // useful extensions (only ~100k)
  2. compile 'com.mcxiaoke.koi:async:0.5.5' // async functions (only ~70k)

Usage

Context Extensions

Activity Functions

  1. // available for Activity
  2. fun activityExtensions() {
  3. val act = getActivity() // Activity
  4. act.restart() // restart Activity
  5. val app = act.getApp() // Application
  6. val app2 = act.application // Application
  7. // Activity.find()
  8. // Fragment.find()
  9. // View.find()
  10. val textView = act.find<TextView>(android.R.id.text1)
  11. }

Fragment Functions

  1. // available for Fragment
  2. fun fragmentExtensions() {
  3. val act = activity // Activity
  4. val app = getApp() // Application
  5. val textView = find<TextView>(android.R.id.text1) // view.findViewById
  6. val imageView = find<TextView>(android.R.id.icon1) // view.findViewById
  7. }

Easy to use Toast

  1. // available for Context
  2. fun toastExtensions() {
  3. // available in Activity/Fragment/Service/Context
  4. toast(R.string.app_name)
  5. toast("this is a toast")
  6. longToast(R.string.app_name)
  7. longToast("this is a long toast")
  8. }

Easy to Inflate Layout

  1. // available for Context
  2. fun inflateLayout() {
  3. val view1 = inflate(R.layout.activity_main)
  4. val viewGroup = view1 as ViewGroup
  5. val view2 = inflate(android.R.layout.activity_list_item, viewGroup, false)
  6. }

Useful Functions

  1. // available for Context
  2. fun miscExtensions() {
  3. val hasCamera = hasCamera()
  4. mediaScan(Uri.parse("file:///sdcard/Pictures/koi/cat.png"))
  5. addToMediaStore(File("/sdcard/Pictures/koi/cat.png"))
  6. val batteryStatusIntent = getBatteryStatus()
  7. val colorValue = getResourceValue(android.R.color.darker_gray)
  8. }

Easy to create Intent

  1. // available for Context
  2. fun intentExtensions() {
  3. val extras = Bundle { putString("key", "value") }
  4. val intent1 = newIntent<MainActivity>()
  5. val intent2 = newIntent<MainActivity>(Intent.FLAG_ACTIVITY_NEW_TASK, extras)
  6. }

Easy to Start Activity

  1. // available for Activity
  2. fun startActivityExtensions() {
  3. startActivity<MainActivity>()
  4. // equal to
  5. startActivity(Intent(this, MainActivity::class.java))
  6. startActivity<MainActivity>(Intent.FLAG_ACTIVITY_SINGLE_TOP, Bundle())
  7. startActivity<MainActivity>(Bundle())
  8. startActivityForResult<MainActivity>(100)
  9. startActivityForResult<MainActivity>(Bundle(), 100)
  10. startActivityForResult<MainActivity>(200, Intent.FLAG_ACTIVITY_CLEAR_TOP)
  11. }

Easy to Start Service

  1. // available for Context
  2. fun startServiceExtensions() {
  3. startService<BackgroundService>()
  4. startService<BackgroundService>(Bundle())
  5. }

Network State

  1. // available for Context
  2. fun networkExtensions() {
  3. val name = networkTypeName()
  4. val operator = networkOperator()
  5. val type = networkType()
  6. val wifi = isWifi()
  7. val mobile = isMobile()
  8. val connected = isConnected()
  9. }

Notification Builder

  1. // available for Context
  2. fun notificationExtensions() {
  3. // easy way using Notification.Builder
  4. val notification = newNotification() {
  5. this.setColor(0x0099cc)
  6. .setAutoCancel(true)
  7. .setContentTitle("Notification Title")
  8. .setContentText("Notification Message Text")
  9. .setDefaults(0)
  10. .setGroup("koi")
  11. .setVibrate(longArrayOf(1, 0, 0, 1))
  12. .setSubText("this is a sub title")
  13. .setSmallIcon(android.R.drawable.ic_dialog_info)
  14. .setLargeIcon(null)
  15. }
  16. }

Package Functions

  1. // available for Context
  2. fun packageExtensions() {
  3. val isYoutubeInstalled = isAppInstalled("com.google.android.youtube")
  4. val isMainProcess = isMainProcess()
  5. val disabled = isComponentDisabled(MainActivity::class.java)
  6. enableComponent(MainActivity::class.java)
  7. val sig = getPackageSignature()
  8. val sigString = getSignature()
  9. println(dumpSignature())
  10. }

System Service

  1. // available for Context
  2. // easy way to get system service, no cast
  3. fun systemServices() {
  4. val wm = getWindowService()
  5. val tm = getTelephonyManager()
  6. val nm = getNotificationManager()
  7. val cm = getConnectivityManager()
  8. val am = getAccountManager()
  9. val acm = getActivityManager()
  10. val alm = getAlarmManager()
  11. val imm = getInputMethodManager()
  12. val inflater = getLayoutService()
  13. val lm = getLocationManager()
  14. val wifi = getWifiManager()
  15. }

Easy to Log

  1. // available for Context
  2. fun logExtensions() {
  3. KoiConfig.logEnabled = true //default is false
  4. // true == Log.VERBOSE
  5. // false == Log.ASSERT
  6. // optional
  7. KoiConfig.logLevel = Log.VERBOSE // default is Log.ASSERT
  8. //
  9. logv("log functions available in Context") //Log.v
  10. logd("log functions available in Context") //Log.d
  11. loge("log functions available in Context") //Log.e
  12. // support lazy evaluated message
  13. logv { "lazy eval message lambda" } //Log.v
  14. logw { "lazy eval message lambda" } //Log.w
  15. }

View Extensions

View Listeners 1

  1. fun viewListeners1() {
  2. val view = View(this)
  3. // View.OnClickListener
  4. view.onClick { print("view clicked") }
  5. // View.OnLongClickListener
  6. view.onLongClick { print("view long clicked");false }
  7. // View.OnKeyListener
  8. view.onKeyEvent { view, keyCode, event ->
  9. print("keyEvent: action:${event.action} code:$keyCode")
  10. false
  11. }
  12. // View.OnTouchListener
  13. view.onTouchEvent { view, event ->
  14. when (event.actionMasked) {
  15. MotionEvent.ACTION_DOWN -> print("touch down")
  16. MotionEvent.ACTION_UP -> print("touch up")
  17. MotionEvent.ACTION_MOVE -> print("touch move")
  18. }
  19. false
  20. }
  21. // View.OnFocusChangeListener
  22. view.onFocusChange { view, hasFocus ->
  23. print("focus changed = $hasFocus")
  24. }
  25. }

View Listeners 2

  1. fun viewListeners2() {
  2. // TextWatcher
  3. val editText = EditText(this)
  4. editText.onTextChange { text, start, before, count ->
  5. print("text changed: $text")
  6. }
  7. // OnCheckedChangeListener
  8. val checkBox = CheckBox(this)
  9. checkBox.onCheckedChanged { button, isChecked ->
  10. print("CheckBox value changed:$isChecked")
  11. }
  12. // OnSeekBarChangeListener
  13. val seekBar = SeekBar(this)
  14. seekBar.onProgressChanged { seekBar, progress, fromUser ->
  15. print("seekBar progress: $progress")
  16. }
  17. }
  18. `

ListView Listeners

  1. fun listViewListeners() {
  2. val listView = ListView(this)
  3. // OnItemClickListener
  4. listView.onItemClick { parent, view, position, id ->
  5. print("onItemClick: position=$position")
  6. }
  7. // OnScrollListener
  8. listView.onScrollChanged { view, scrollState ->
  9. print("scroll state changed")
  10. }
  11. }

View Utils

  1. // available for View
  2. fun viewSample() {
  3. val w = dm.widthPixels
  4. val h = dm.heightPixels
  5. val v1 = 32.5f
  6. val dp1 = v1.pxToDp()
  7. val v2 = 24f
  8. val px1 = v2.dpToPx()
  9. val dp2 = pxToDp(56)
  10. val px2 = dpToPx(32)
  11. val dp3 = 72.pxToDp()
  12. val px3 = 48.dpToPx()
  13. hideSoftKeyboard()
  14. val editText = EditText(context)
  15. editText.showSoftKeyboard()
  16. editText.toggleSoftKeyboard()
  17. }

Adapter Extensions

Easy to Create Adapter

  1. // easy way to create array adapter
  2. fun adapterFunctions() {
  3. listView.adapter = quickAdapterOf(
  4. android.R.layout.simple_list_item_2,
  5. (1..100).map { "List Item No.$it" })
  6. { binder, data ->
  7. binder.setText(android.R.id.text1, data)
  8. binder.setText(android.R.id.text2, "Index: ${binder.position}")
  9. }
  10. val adapter2 = quickAdapterOf<String>(android.R.layout.simple_list_item_1) {
  11. binder, data ->
  12. binder.setText(android.R.id.text1, data)
  13. }
  14. adapter2.addAll(listOf("Cat", "Dog", "Rabbit"))
  15. val adapter3 = quickAdapterOf<Int>(android.R.layout.simple_list_item_1,
  16. arrayOf(1, 2, 3, 4, 5, 6)) {
  17. binder, data ->
  18. binder.setText(android.R.id.text1, "Item Number: $data")
  19. }
  20. val adapter4 = quickAdapterOf<Int>(android.R.layout.simple_list_item_1,
  21. setOf(22, 33, 4, 5, 6, 8, 8, 8)) {
  22. binder, data ->
  23. binder.setText(android.R.id.text1, "Item Number: $data")
  24. }
  25. }

Bundle Extensions

Bundle Builder

  1. // available in any where
  2. fun bundleExtension() {
  3. // easy way to create bundle
  4. val bundle = Bundle {
  5. putString("key", "value")
  6. putInt("int", 12345)
  7. putBoolean("boolean", false)
  8. putIntArray("intArray", intArrayOf(1, 2, 3, 4, 5))
  9. putStringArrayList("strings", arrayListOf("Hello", "World", "Cat"))
  10. }
  11. // equal to using with
  12. val bundle2 = Bundle()
  13. with(bundle2) {
  14. putString("key", "value")
  15. putInt("int", 12345)
  16. putBoolean("boolean", false)
  17. putIntArray("intArray", intArrayOf(1, 2, 3, 4, 5))
  18. putStringArrayList("strings", arrayListOf("Hello", "World", "Cat"))
  19. }
  20. }

Parcelable Extensions

Easy to create Parcelable

  1. // easy way to create Android Parcelable class
  2. data class Person(val name: String, val age: Int) : Parcelable {
  3. override fun writeToParcel(dest: Parcel, flags: Int) {
  4. dest.writeString(name)
  5. dest.writeInt(age)
  6. }
  7. override fun describeContents(): Int = 0
  8. protected constructor(p: Parcel) : this(name = p.readString(), age = p.readInt()) {}
  9. companion object {
  10. // using createParcel
  11. @JvmField val CREATOR = createParcel { Person(it) }
  12. }
  13. }

Collection Extensions

Collection to String

  1. fun collectionToString() {
  2. val pets = listOf<String>("Cat", "Dog", "Rabbit", "Fish")
  3. // list to string, delimiter is space
  4. val string1 = pets.asString(delim = " ") // "Cat Dog Rabbit Fish"
  5. // default delimiter is comma
  6. val string2 = pets.asString() // "Cat,Dog,Rabbit,Fish"
  7. val numbers = arrayOf(2016, 2, 2, 20, 57, 40)
  8. // array to string, default delimiter is comma
  9. val string3 = numbers.asString() // "2016,2,2,20,57,40"
  10. // array to string, delimiter is -
  11. val string4 = numbers.asString(delim = "-") // 2016-2-2-20-57-40
  12. // using Kotlin stdlib
  13. val s1 = pets.joinToString()
  14. val s2 = numbers.joinToString(separator = "-", prefix = "<", postfix = ">")
  15. }

Map to String

  1. fun mapToString() {
  2. val map = mapOf<String, Int>(
  3. "John" to 30,
  4. "Smith" to 50,
  5. "Alice" to 22
  6. )
  7. // default delimiter is ,
  8. val string1 = map.asString() // "John=30,Smith=50,Alice=22"
  9. // using delimiter /
  10. val string2 = map.asString(delim = "/") // "John=30/Smith=50/Alice=22"
  11. // using stdlib
  12. map.asSequence().joinToString { "${it.key}=${it.value}" }
  13. }

List Append

  1. fun appendAndPrepend() {
  2. val numbers = (1..6).toArrayList()
  3. println(numbers.joinToString()) // "1, 2, 3, 4, 5, 6, 7"
  4. numbers.head() // .dropLast(1)
  5. numbers.tail() //.drop(1)
  6. val numbers2 = 100.appendTo(numbers) //
  7. val numbers3 = 2016.prependTo(numbers)
  8. }

Database Extensions

Easy to get Cursor Value

  1. // available for Cursor
  2. fun cursorValueExtensions() {
  3. val cursor = this.writableDatabase.query("table", null, null, null, null, null, null)
  4. cursor.moveToFirst()
  5. do {
  6. val intVal = cursor.intValue("column-a")
  7. val stringVal = cursor.stringValue("column-b")
  8. val longVal = cursor.longValue("column-c")
  9. val booleanValue = cursor.booleanValue("column-d")
  10. val doubleValue = cursor.doubleValue("column-e")
  11. val floatValue = cursor.floatValue("column-f")
  12. // no need to do like this, so verbose
  13. cursor.getInt(cursor.getColumnIndexOrThrow("column-a"))
  14. cursor.getString(cursor.getColumnIndexOrThrow("column-b"))
  15. } while (cursor.moveToNext())
  16. }

Easy to convert Cursor to Model

  1. // available for Cursor
  2. // transform cursor to model object
  3. fun cursorToModels() {
  4. val where = " age>? "
  5. val whereArgs = arrayOf("20")
  6. val cursor = this.readableDatabase.query("users", null, where, whereArgs, null, null, null)
  7. val users1 = cursor.map {
  8. UserInfo(
  9. stringValue("name"),
  10. intValue("age"),
  11. stringValue("bio"),
  12. booleanValue("pet_flag"))
  13. }
  14. // or using mapAndClose
  15. val users2 = cursor.mapAndClose {
  16. UserInfo(
  17. stringValue("name"),
  18. intValue("age"),
  19. stringValue("bio"),
  20. booleanValue("pet_flag"))
  21. }
  22. // or using Cursor?mapTo(collection, transform())
  23. }

Easy to use Transaction

  1. // available for SQLiteDatabase and SQLiteOpenHelper
  2. // auto apply transaction to db operations
  3. fun inTransaction() {
  4. val db = this.writableDatabase
  5. val values = ContentValues()
  6. // or db.transaction
  7. transaction {
  8. db.execSQL("insert into users (?,?,?) (1,2,3)")
  9. db.insert("users", null, values)
  10. }
  11. // equal to
  12. db.beginTransaction()
  13. try {
  14. db.execSQL("insert into users (?,?,?) (1,2,3)")
  15. db.insert("users", null, values)
  16. db.setTransactionSuccessful()
  17. } finally {
  18. db.endTransaction()
  19. }
  20. }

IO Extensions

Easy to close Stream

  1. // available for Closeable
  2. fun closeableSample() {
  3. val input = FileInputStream(File("readme.txt"))
  4. try {
  5. val string = input.readString("UTF-8")
  6. } catch(e: IOException) {
  7. e.printStackTrace()
  8. } finally {
  9. input.closeQuietly()
  10. }
  11. }

Stream doSafe Function

  1. // simple way, equal to closeableSample
  2. // InputStream.doSafe{}
  3. fun doSafeSample() {
  4. val input = FileInputStream(File("readme.txt"))
  5. input.doSafe {
  6. val string = readString("UTF-8")
  7. }
  8. }

readString/readList

  1. // available for InputStream/Reader
  2. fun readStringAndList1() {
  3. val input = FileInputStream(File("readme.txt"))
  4. try {
  5. val reader = input.reader(Encoding.CHARSET_UTF_8)
  6. val string1 = input.readString(Encoding.UTF_8)
  7. val string2 = input.readString(Encoding.CHARSET_UTF_8)
  8. val list1 = input.readList()
  9. val list2 = input.readList(Encoding.CHARSET_UTF_8)
  10. } catch(e: IOException) {
  11. } finally {
  12. input.closeQuietly()
  13. }
  14. }

readString/readList using doSafe

  1. // available for InputStream/Reader
  2. //equal to readStringAndList1
  3. fun readStringAndList2() {
  4. val input = FileInputStream(File("readme.txt"))
  5. input.doSafe {
  6. val reader = reader(Encoding.CHARSET_UTF_8)
  7. val string1 = readString(Encoding.UTF_8)
  8. val string2 = readString(Encoding.CHARSET_UTF_8)
  9. val list1 = readList()
  10. val list2 = readList(Encoding.CHARSET_UTF_8)
  11. }
  12. }

writeString/writeList using doSafe

  1. fun writeStringAndList() {
  2. val output = FileOutputStream("output.txt")
  3. output.doSafe {
  4. output.writeString("hello, world")
  5. output.writeString("Alic's Adventures in Wonderland", charset = Encoding.CHARSET_UTF_8)
  6. val list1 = listOf<Int>(1, 2, 3, 4, 5)
  7. val list2 = (1..8).map { "Item No.$it" }
  8. output.writeList(list1, charset = Encoding.CHARSET_UTF_8)
  9. output.writeList(list2)
  10. }
  11. }

File Read and Write

  1. fun fileReadWrite() {
  2. val directory = File("/Users/koi/workspace")
  3. val file = File("some.txt")
  4. val text1 = file.readText()
  5. val text2 = file.readString(Encoding.CHARSET_UTF_8)
  6. val list1 = file.readList()
  7. val list2 = file.readLines(Encoding.CHARSET_UTF_8)
  8. file.writeText("hello, world")
  9. file.writeList(list1)
  10. file.writeList(list2, Encoding.CHARSET_UTF_8)
  11. val v1 = file.relativeToOrNull(directory)
  12. val v2 = file.toRelativeString(directory)
  13. // clean files in directory
  14. directory.clean()
  15. val file1 = File("a.txt")
  16. val file2 = File("b.txt")
  17. file1.copyTo(file2, overwrite = false)
  18. }

Handler Extensions

Easy to use Handler

  1. // available for Handler
  2. // short name for functions
  3. fun handlerFunctions() {
  4. val handler = Handler()
  5. handler.atNow { print("perform action now") }
  6. // equal to
  7. handler.post { print("perform action now") }
  8. handler.atFront { print("perform action at first") }
  9. // equal to
  10. handler.postAtFrontOfQueue { print("perform action at first") }
  11. handler.atTime(timestamp() + 5000, { print("perform action after 5s") })
  12. // equal to
  13. handler.postAtTime({ print("perform action after 5s") }, 5000)
  14. handler.delayed(3000, { print("perform action after 5s") })
  15. // equal to
  16. handler.postDelayed({ print("perform action after 5s") }, 3000)
  17. }

Other Extensions

Date Functions

  1. // available in any where
  2. fun dateSample() {
  3. val nowString = dateNow()
  4. val date = dateParse("2016-02-02 20:30:45")
  5. val dateStr1 = date.asString()
  6. val dateStr2 = date.asString(SimpleDateFormat("yyyyMMdd.HHmmss"))
  7. val dateStr3 = date.asString("yyyy-MM-dd-HH-mm-ss")
  8. // easy way to get timestamp
  9. val timestamp1 = timestamp()
  10. // equal to
  11. val timestamp2 = System.currentTimeMillis()
  12. val dateStr4 = timestamp1.asDateString()
  13. }

Number Functions

  1. fun numberExtensions() {
  2. val number = 179325344324902187L
  3. println(number.readableByteCount())
  4. val bytes = byteArrayOf(1, 7, 0, 8, 9, 4, 125)
  5. println(bytes.hexString())
  6. }

String Functions

  1. // available for String
  2. fun stringExtensions() {
  3. val string = "hello, little cat!"
  4. val quotedString = string.quote()
  5. val isBlank = string.isBlank()
  6. val hexBytes = string.toHexBytes()
  7. val s1 = string.trimAllWhitespace()
  8. val c = string.containsWhitespace()
  9. val url = "https://github.com/mcxiaoke/kotlin-koi?year=2016&encoding=utf8&a=b#changelog"
  10. val urlNoQuery = url.withoutQuery()
  11. val isNameSafe = url.isNameSafe()
  12. val fileName = url.toSafeFileName()
  13. val queries = url.toQueries()
  14. val path = "/Users/koi/workspace/String.kt"
  15. val baseName = path.fileNameWithoutExtension()
  16. val extension = path.fileExtension()
  17. val name = path.fileName()
  18. }

Crypto Functions

  1. // available in any where
  2. fun cryptoFunctions() {
  3. val md5 = HASH.md5("hello, world")
  4. val sha1 = HASH.sha1("hello, world")
  5. val sha256 = HASH.sha256("hello, world")
  6. }

Check API Level

  1. // available in any where
  2. fun apiLevelFunctions() {
  3. // Build.VERSION.SDK_INT
  4. val v = currentVersion()
  5. val ics = icsOrNewer()
  6. val kk = kitkatOrNewer()
  7. val bkk = beforeKitkat()
  8. val lol = lollipopOrNewer()
  9. val mar = marshmallowOrNewer()
  10. }

Device Functions

  1. // available in any where
  2. fun deviceSample() {
  3. val a = isLargeHeap
  4. val b = noSdcard()
  5. val c = noFreeSpace(needSize = 10 * 1024 * 1024L)
  6. val d = freeSpace()
  7. }

Preconditions

  1. // available in any where
  2. // null and empty check
  3. fun preconditions() {
  4. throwIfEmpty(listOf(), "collection is null or empty")
  5. throwIfNull(null, "object is null")
  6. throwIfTrue(currentVersion() == 10, "result is true")
  7. throwIfFalse(currentVersion() < 4, "result is false")
  8. }

Thread Functions

Create Thread Pool

  1. // available in any where
  2. fun executorFunctions() {
  3. // global main handler
  4. val uiHandler1 = CoreExecutor.mainHandler
  5. // or using this function
  6. val uiHandler2 = koiHandler()
  7. // global executor service
  8. val executor = CoreExecutor.executor
  9. // or using this function
  10. val executor2 = koiExecutor()
  11. // create thread pool functions
  12. val pool1 = newCachedThreadPool("cached")
  13. val pool2 = newFixedThreadPool("fixed", 4)
  14. val pool3 = newSingleThreadExecutor("single")
  15. }

Main Thread Functions

  1. // available in any where
  2. fun mainThreadFunctions() {
  3. //check current thread
  4. // call from any where
  5. val isMain = isMainThread()
  6. // execute in main thread
  7. mainThread {
  8. print("${(1..8).asSequence().joinToString()}")
  9. }
  10. // delay execute in main thread
  11. mainThreadDelay(3000) {
  12. print("execute after 3000 ms")
  13. }
  14. }

Context Check

  1. // isContextAlive function impl
  2. fun <T> isContextAlive(context: T?): Boolean {
  3. return when (context) {
  4. null -> false
  5. is Activity -> !context.isFinishing
  6. is Fragment -> context.isAdded
  7. is android.support.v4.app.Fragment -> context.isAdded
  8. is Detachable -> !context.isDetached()
  9. else -> true
  10. }
  11. }

Safe Functions

  1. // available in any where
  2. fun safeFunctions() {
  3. val context = this
  4. // check Activity/Fragment lifecycle
  5. val alive = isContextAlive(context)
  6. fun func1() {
  7. print("func1")
  8. }
  9. // convert to safe function with context check
  10. // internal using isContextAlive
  11. val safeFun1 = safeFunction(::func1)
  12. // call function with context check
  13. // internal using isContextAlive
  14. safeExecute(::func1)
  15. // direct use
  16. safeExecute { print("func1") }
  17. }

Async Functions

  1. class AsyncFunctionsSample {
  2. private val intVal = 1000
  3. private var strVal: String? = null
  4. }

With Context Check 1

  1. // async functions with context check
  2. // internal using isContextAlive
  3. // context alive:
  4. // !Activity.isFinishing
  5. // Fragment.isAdded
  6. // !Detachable.isDetached
  7. //
  8. // available in any where
  9. // using in Activity/Fragment better
  10. fun asyncSafeFunction1() {
  11. // safe means context alive check
  12. // async
  13. asyncSafe {
  14. print("action executed only if context alive ")
  15. // if you want get caller context
  16. // maybe null
  17. val ctx = getCtx()
  18. // you can also using outside variables
  19. // not recommended
  20. // if context is Activity or Fragment
  21. // may cause memory leak
  22. print("outside value, $intVal $strVal")
  23. // you can using mainThreadSafe here
  24. // like a callback
  25. mainThreadSafe {
  26. // also with context alive check
  27. // if context dead, not executed
  28. print("code here executed in main thread")
  29. }
  30. // if you don't want context check, using mainThread{}
  31. mainThread {
  32. // no context check
  33. print("code here executed in main thread")
  34. }
  35. }
  36. // if your result or error is nullable
  37. // using asyncSafe2, just as asyncSafe
  38. // but type of result and error is T?, Throwable?
  39. }

With Context Check 2

  1. fun asyncSafeFunction2() {
  2. // async with callback
  3. asyncSafe(
  4. {
  5. print("action executed in async thread")
  6. listOf<Int>(1, 2, 3, 4, 5)
  7. },
  8. { result, error ->
  9. // in main thread
  10. print("callback executed in main thread")
  11. })
  12. }
  1. fun asyncSafeFunction3() {
  2. // async with success/failure callback
  3. asyncSafe(
  4. {
  5. print("action executed in async thread")
  6. "this string is result of the action"
  7. // throw RuntimeException("action error")
  8. },
  9. { result ->
  10. // if action success with no exception
  11. print("success callback in main thread result:$result")
  12. },
  13. { error ->
  14. // if action failed with exception
  15. print("failure callback in main thread, error:$error")
  16. })
  17. }

Without Context Check

  1. // if you don't want context check
  2. // using asyncUnsafe series functions
  3. // just replace asyncSafe with asyncUnsafe
  4. fun asyncUnsafeFunctions() {
  5. // async
  6. asyncUnsafe {
  7. print("action executed with no context check ")
  8. // may cause memory leak
  9. print("outside value, $intVal $strVal")
  10. mainThread {
  11. // no context check
  12. print("code here executed in main thread")
  13. }
  14. }
  15. }

Custom Executor

  1. val executor = Executors.newFixedThreadPool(4)
  2. asyncSafe(executor) {
  3. print("action executed in async thread")
  4. mainThreadSafe {
  5. print("code here executed in main thread")
  6. }
  7. }

With Delay

  1. // async functions with delay
  2. // with context check
  3. // if context died, not executed
  4. // others just like asyncSafe
  5. fun asyncDelayFunctions() {
  6. // usage see asyncSafe
  7. asyncDelay(5000) {
  8. print("action executed after 5000ms only if context alive ")
  9. // you can using mainThreadSafe here
  10. // like a callback
  11. mainThreadSafe {
  12. // also with context alive check
  13. // if context dead, not executed
  14. print("code here executed in main thread")
  15. }
  16. // if you don't want context check, using mainThread{}
  17. mainThread {
  18. // no context check
  19. print("code here executed in main thread")
  20. }
  21. }
  22. }

About Me

Contacts

Projects


License

  1. Copyright 2015, 2016 Xiaoke Zhang
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.