Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Last active August 22, 2020 15:02
Show Gist options
  • Save CaiJingLong/2c67438f7a1c4b28690b141dcb2d5bb8 to your computer and use it in GitHub Desktop.
Save CaiJingLong/2c67438f7a1c4b28690b141dcb2d5bb8 to your computer and use it in GitHub Desktop.
ResultHandler in kotlin
package top.kikt.imagescanner.util
import android.os.Handler
import android.os.Looper
import io.flutter.plugin.common.MethodChannel
class ResultHandler(var result: MethodChannel.Result?) {
companion object {
private val handler = Handler(Looper.getMainLooper())
}
private var isReply = false
fun reply(any: Any?) {
if(isReply){
return
}
isReply = true
val result = this.result
this.result = null
handler.post {
result?.success(any)
}
}
fun replyError(code: String, message: String? = null, obj: Any? = null) {
if(isReply){
return
}
isReply = true
val result = this.result
this.result = null
handler.post {
result?.error(code, message, obj)
}
}
fun notImplemented() {
if(isReply){
return
}
isReply = true
val result = this.result
this.result = null
handler.post {
result?.notImplemented()
}
}
fun success(any: Any?) {
reply(any)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment