Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
package com.madlonkay.flutter_charset_detector

import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.StandardMethodCodec
import org.mozilla.universalchardet.UniversalDetector
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.nio.charset.IllegalCharsetNameException
import java.nio.charset.UnsupportedCharsetException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/** FlutterCharsetDetectorPlugin */
class FlutterCharsetDetectorPlugin : FlutterPlugin, MethodCallHandler {
class FlutterCharsetDetectorPlugin : FlutterPlugin, MethodCallHandler, CoroutineScope by MainScope() {
private lateinit var channel: MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_charset_detector")
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val taskQueue = flutterPluginBinding.binaryMessenger.makeBackgroundTaskQueue()
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_charset_detector", StandardMethodCodec.INSTANCE, taskQueue)
channel.setMethodCallHandler(this)
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
"autoDecode" -> handleAutoDecode(call, result)
"detect" -> handleDetect(call, result)
else -> result.notImplemented()
override fun onMethodCall(call: MethodCall, result: Result) {
val mainThreadResult = object : Result {
override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
launch(Dispatchers.Main) { result.error(errorCode, errorMessage, errorDetails) }
}
override fun success(resultArg: Any?) {
launch(Dispatchers.Main) { result.success(resultArg) }
}
override fun notImplemented() {
launch(Dispatchers.Main) { result.notImplemented() }
}
}
launch(Dispatchers.Default) {
when (call.method) {
"autoDecode" -> handleAutoDecode(call, mainThreadResult)
"detect" -> handleDetect(call, mainThreadResult)
else -> mainThreadResult.notImplemented()
}
}
}

Expand Down Expand Up @@ -75,7 +93,7 @@ class FlutterCharsetDetectorPlugin : FlutterPlugin, MethodCallHandler {
result.success(charsetName)
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ public class SwiftFlutterCharsetDetectorPlugin: NSObject, FlutterPlugin {
#else
let messenger = registrar.messenger
#endif
let channel = FlutterMethodChannel(name: "flutter_charset_detector", binaryMessenger: messenger)
let taskQueue = registrar.messenger().makeBackgroundTaskQueue?()
let channel = FlutterMethodChannel(name: "flutter_charset_detector", binaryMessenger: messenger, codec: FlutterStandardMethodCodec.sharedInstance(), taskQueue: taskQueue)
let instance = SwiftFlutterCharsetDetectorPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "autoDecode":
handleAutoDecode(call, result)
case "detect":
handleDetect(call, result)
default:
result(FlutterError(code: "UnsupportedMethod", message: "\(call.method) is not supported", details: nil))
let result = { (val: Any?) in
DispatchQueue.main.async { result(val) }
}
DispatchQueue.global(qos: .userInitiated).async { [self] in
switch call.method {
case "autoDecode":
handleAutoDecode(call, result)
case "detect":
handleDetect(call, result)
default:
result(FlutterError(code: "UnsupportedMethod", message: "\(call.method) is not supported", details: nil))
}
}
}

Expand Down