Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the registry interface and improve plugin interface code quality #119

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
Expand Up @@ -6,13 +6,12 @@ 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.PluginRegistry
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.BinaryMessenger

class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware {
class FlutterLineSdkPlugin : MethodCallHandler, FlutterPlugin, ActivityAware {

private var methodChannel: MethodChannel? = null
private val lineSdkWrapper = LineSdkWrapper()
Expand All @@ -23,57 +22,43 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"toBeta" -> run {
val channelId: String = call.argument("channelId") ?: ""
val openDiscoveryIdDocumentUrl: String = call.argument("openDiscoveryIdDocumentUrl") ?: ""
val apiServerBaseUrl: String = call.argument("apiServerBaseUrl") ?: ""
val webLoginPageUrl: String = call.argument("webLoginPageUrl") ?: ""
val channelId = call.argument<String>("channelId").orEmpty()
val openDiscoveryIdDocumentUrl = call.argument<String>("openDiscoveryIdDocumentUrl").orEmpty()
val apiServerBaseUrl = call.argument<String>("apiServerBaseUrl").orEmpty()
val webLoginPageUrl = call.argument<String>("webLoginPageUrl").orEmpty()
lineSdkWrapper.setupBetaConfig(
channelId,
openDiscoveryIdDocumentUrl,
apiServerBaseUrl,
webLoginPageUrl
channelId,
openDiscoveryIdDocumentUrl,
apiServerBaseUrl,
webLoginPageUrl
)
result.success(null)
}
"setup" -> {
val channelId: String = call.argument<String?>("channelId").orEmpty()
val activity = activity
if (activity == null) {
result.error(
"no_activity_found",
"There is no valid Activity found to present LINE SDK Login screen.",
null
)
return
withActivity(result) { activity ->
val channelId = call.argument<String>("channelId").orEmpty()
lineSdkWrapper.setupSdk(activity, channelId)
result.success(null)
}
lineSdkWrapper.setupSdk(activity, channelId)
result.success(null)
}
"login" -> {
val activity = this.activity
if (activity == null) {
result.error(
"no_activity_found",
"There is no valid Activity found to present LINE SDK Login screen.",
null
)
return
}

val scopes = call.argument("scopes") ?: emptyList<String>()
val isWebLogin = call.argument("onlyWebLogin") ?: false
val botPrompt = call.argument("botPrompt") ?: "normal"
val idTokenNonce: String? = call.argument("idTokenNonce")
val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE
lineSdkWrapper.login(
withActivity(result) { activity ->
val scopes = call.argument<List<String>>("scopes").orEmpty()
val isWebLogin = call.argument<Boolean>("onlyWebLogin") ?: false
val botPrompt = call.argument<String>("botPrompt") ?: "normal"
val idTokenNonce = call.argument<String>("idTokenNonce")
val loginRequestCode = call.argument<Int>("loginRequestCode")
?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE
lineSdkWrapper.login(
loginRequestCode,
activity,
scopes = scopes,
onlyWebLogin = isWebLogin,
botPromptString = botPrompt,
idTokenNonce = idTokenNonce,
result = result
)
)
}
}
"getProfile" -> lineSdkWrapper.getProfile(result)
"currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result)
Expand All @@ -85,12 +70,26 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
}
}

private fun withActivity(result: Result, block: (Activity) -> Unit) {
val activity = this.activity
if (activity == null) {
result.error(
"no_activity_found",
"There is no valid Activity found to present LINE SDK Login screen.",
null
)
return
}
block(activity)
}

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
onAttachedToEngine(binding.binaryMessenger)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
methodChannel = null;
methodChannel?.setMethodCallHandler(null)
methodChannel = null
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
Expand All @@ -109,45 +108,25 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
unbindActivityBinding()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean =
lineSdkWrapper.handleActivityResult(requestCode, resultCode, intent)

private fun bindActivityBinding(binding: ActivityPluginBinding) {
this.activity = binding.activity
this.activityBinding = binding
addActivityResultListener(binding)
binding.addActivityResultListener(lineSdkWrapper::handleActivityResult)
}

private fun unbindActivityBinding() {
activityBinding?.removeActivityResultListener(this)
this.activity = null;
activityBinding?.removeActivityResultListener(lineSdkWrapper::handleActivityResult)
this.activity = null
this.activityBinding = null
}

private fun onAttachedToEngine(messenger: BinaryMessenger) {
methodChannel = MethodChannel(messenger, CHANNEL_NAME)
methodChannel!!.setMethodCallHandler(this)
}

private fun addActivityResultListener(activityBinding: ActivityPluginBinding) {
activityBinding.addActivityResultListener(this)
}

private fun addActivityResultListener(registrar: PluginRegistry.Registrar) {
registrar.addActivityResultListener(this)
methodChannel?.setMethodCallHandler(this)
}

companion object {
private const val CHANNEL_NAME = "com.linecorp/flutter_line_sdk"
private const val DEFAULT_ACTIVITY_RESULT_REQUEST_CODE = 8192

@JvmStatic
fun registerWith(registrar: PluginRegistry.Registrar) {
FlutterLineSdkPlugin().apply {
onAttachedToEngine(registrar.messenger())
activity = registrar.activity()
addActivityResultListener(registrar)
}
}
}
}