Last active
August 22, 2020 15:02
-
-
Save CaiJingLong/2c67438f7a1c4b28690b141dcb2d5bb8 to your computer and use it in GitHub Desktop.
ResultHandler in kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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