aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-30 16:19:41 -0500
committerAllan Wang <me@allanwang.ca>2018-12-30 16:19:41 -0500
commit3aa909f055c24d3700fa02f80c88e77a0c096f6e (patch)
tree8b700f0ccac3c526ec193002cf18f810d55257c3 /core
parentf931e55d534e6162748f9fa888e7313cc2f0b619 (diff)
downloadkau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.tar.gz
kau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.tar.bz2
kau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.zip
Build main dispatcher from handler, resolves #182
Diffstat (limited to 'core')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt3
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt6
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt19
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt2
4 files changed, 19 insertions, 11 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
index e192ff2..be23d45 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt
@@ -18,6 +18,7 @@ package ca.allanwang.kau.internal
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import ca.allanwang.kau.permissions.kauOnRequestPermissionsResult
+import ca.allanwang.kau.utils.ContextHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -41,7 +42,7 @@ abstract class KauBaseActivity : AppCompatActivity(), CoroutineScope {
open lateinit var job: Job
override val coroutineContext: CoroutineContext
- get() = Dispatchers.Main + job
+ get() = ContextHelper.dispatcher + job
open fun defaultJob(): Job = SupervisorJob()
diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
index d01859f..4562b00 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt
@@ -17,8 +17,8 @@
package ca.allanwang.kau.logging
-import android.os.Looper
import android.util.Log
+import ca.allanwang.kau.utils.kauIsMainThread
/**
* Created by Allan Wang on 2017-05-28.
@@ -82,7 +82,7 @@ open class KauLogger(
inline fun checkThread(id: Int) {
d {
val name = Thread.currentThread().name
- val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not"
+ val status = if (kauIsMainThread) "is" else "is not"
"$id $status in the main thread - thread name: $name"
}
}
@@ -120,7 +120,7 @@ class KauLoggerExtension(val tag: String, val logger: KauLogger) {
inline fun checkThread(id: Int) {
d {
val name = Thread.currentThread().name
- val status = if (Looper.myLooper() == Looper.getMainLooper()) "is" else "is not"
+ val status = if (kauIsMainThread) "is" else "is not"
"$id $status in the main thread - thread name: $name"
}
}
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index 6867b49..09ad4ea 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -48,18 +48,25 @@ import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
import com.afollestad.materialdialogs.MaterialDialog
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.android.asCoroutineDispatcher
import kotlin.coroutines.CoroutineContext
/**
* Created by Allan Wang on 2017-06-03.
*/
-private object ContextHelper : CoroutineScope {
+object ContextHelper : CoroutineScope {
- val handler = Handler(Looper.getMainLooper())
+ val looper = Looper.getMainLooper()
- override val coroutineContext: CoroutineContext
- get() = Dispatchers.Main
+ val handler = Handler(looper)
+
+ /**
+ * Creating dispatcher from main handler to avoid IO
+ * See https://github.com/Kotlin/kotlinx.coroutines/issues/878
+ */
+ val dispatcher = handler.asCoroutineDispatcher("kau-main")
+
+ override val coroutineContext: CoroutineContext get() = dispatcher
}
/**
@@ -71,7 +78,7 @@ internal inline val Context.ctxCoroutine: CoroutineScope
get() = this as? CoroutineScope ?: ContextHelper
fun Context.runOnUiThread(f: Context.() -> Unit) {
- if (Looper.getMainLooper() === Looper.myLooper()) f() else ContextHelper.handler.post { f() }
+ if (ContextHelper.looper === Looper.myLooper()) f() else ContextHelper.handler.post { f() }
}
/**
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
index da4feb8..48a2028 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
@@ -141,7 +141,7 @@ fun postDelayed(delay: Long, action: () -> Unit) {
}
inline val kauIsMainThread: Boolean
- get() = Looper.myLooper() == Looper.getMainLooper()
+ get() = Looper.myLooper() == ContextHelper.looper
class KauException(message: String) : RuntimeException(message)