Skip to content

Commit e252408

Browse files
authored
Merge pull request #119 from line/fix/remove-registry
Remove the registry interface and improve plugin interface code quality
2 parents 8299524 + 0abe8e6 commit e252408

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import io.flutter.plugin.common.MethodCall
66
import io.flutter.plugin.common.MethodChannel
77
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
88
import io.flutter.plugin.common.MethodChannel.Result
9-
import io.flutter.plugin.common.PluginRegistry
109
import io.flutter.embedding.engine.plugins.FlutterPlugin
1110
import io.flutter.embedding.engine.plugins.activity.ActivityAware
1211
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
1312
import io.flutter.plugin.common.BinaryMessenger
1413

15-
class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware {
14+
class FlutterLineSdkPlugin : MethodCallHandler, FlutterPlugin, ActivityAware {
1615

1716
private var methodChannel: MethodChannel? = null
1817
private val lineSdkWrapper = LineSdkWrapper()
@@ -23,57 +22,43 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
2322
override fun onMethodCall(call: MethodCall, result: Result) {
2423
when (call.method) {
2524
"toBeta" -> run {
26-
val channelId: String = call.argument("channelId") ?: ""
27-
val openDiscoveryIdDocumentUrl: String = call.argument("openDiscoveryIdDocumentUrl") ?: ""
28-
val apiServerBaseUrl: String = call.argument("apiServerBaseUrl") ?: ""
29-
val webLoginPageUrl: String = call.argument("webLoginPageUrl") ?: ""
25+
val channelId = call.argument<String>("channelId").orEmpty()
26+
val openDiscoveryIdDocumentUrl = call.argument<String>("openDiscoveryIdDocumentUrl").orEmpty()
27+
val apiServerBaseUrl = call.argument<String>("apiServerBaseUrl").orEmpty()
28+
val webLoginPageUrl = call.argument<String>("webLoginPageUrl").orEmpty()
3029
lineSdkWrapper.setupBetaConfig(
31-
channelId,
32-
openDiscoveryIdDocumentUrl,
33-
apiServerBaseUrl,
34-
webLoginPageUrl
30+
channelId,
31+
openDiscoveryIdDocumentUrl,
32+
apiServerBaseUrl,
33+
webLoginPageUrl
3534
)
3635
result.success(null)
3736
}
3837
"setup" -> {
39-
val channelId: String = call.argument<String?>("channelId").orEmpty()
40-
val activity = activity
41-
if (activity == null) {
42-
result.error(
43-
"no_activity_found",
44-
"There is no valid Activity found to present LINE SDK Login screen.",
45-
null
46-
)
47-
return
38+
withActivity(result) { activity ->
39+
val channelId = call.argument<String>("channelId").orEmpty()
40+
lineSdkWrapper.setupSdk(activity, channelId)
41+
result.success(null)
4842
}
49-
lineSdkWrapper.setupSdk(activity, channelId)
50-
result.success(null)
5143
}
5244
"login" -> {
53-
val activity = this.activity
54-
if (activity == null) {
55-
result.error(
56-
"no_activity_found",
57-
"There is no valid Activity found to present LINE SDK Login screen.",
58-
null
59-
)
60-
return
61-
}
62-
63-
val scopes = call.argument("scopes") ?: emptyList<String>()
64-
val isWebLogin = call.argument("onlyWebLogin") ?: false
65-
val botPrompt = call.argument("botPrompt") ?: "normal"
66-
val idTokenNonce: String? = call.argument("idTokenNonce")
67-
val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE
68-
lineSdkWrapper.login(
45+
withActivity(result) { activity ->
46+
val scopes = call.argument<List<String>>("scopes").orEmpty()
47+
val isWebLogin = call.argument<Boolean>("onlyWebLogin") ?: false
48+
val botPrompt = call.argument<String>("botPrompt") ?: "normal"
49+
val idTokenNonce = call.argument<String>("idTokenNonce")
50+
val loginRequestCode = call.argument<Int>("loginRequestCode")
51+
?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE
52+
lineSdkWrapper.login(
6953
loginRequestCode,
7054
activity,
7155
scopes = scopes,
7256
onlyWebLogin = isWebLogin,
7357
botPromptString = botPrompt,
7458
idTokenNonce = idTokenNonce,
7559
result = result
76-
)
60+
)
61+
}
7762
}
7863
"getProfile" -> lineSdkWrapper.getProfile(result)
7964
"currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result)
@@ -85,12 +70,26 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
8570
}
8671
}
8772

73+
private fun withActivity(result: Result, block: (Activity) -> Unit) {
74+
val activity = this.activity
75+
if (activity == null) {
76+
result.error(
77+
"no_activity_found",
78+
"There is no valid Activity found to present LINE SDK Login screen.",
79+
null
80+
)
81+
return
82+
}
83+
block(activity)
84+
}
85+
8886
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
8987
onAttachedToEngine(binding.binaryMessenger)
9088
}
9189

9290
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
93-
methodChannel = null;
91+
methodChannel?.setMethodCallHandler(null)
92+
methodChannel = null
9493
}
9594

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

112-
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean =
113-
lineSdkWrapper.handleActivityResult(requestCode, resultCode, intent)
114-
115111
private fun bindActivityBinding(binding: ActivityPluginBinding) {
116112
this.activity = binding.activity
117113
this.activityBinding = binding
118-
addActivityResultListener(binding)
114+
binding.addActivityResultListener(lineSdkWrapper::handleActivityResult)
119115
}
120116

121117
private fun unbindActivityBinding() {
122-
activityBinding?.removeActivityResultListener(this)
123-
this.activity = null;
118+
activityBinding?.removeActivityResultListener(lineSdkWrapper::handleActivityResult)
119+
this.activity = null
124120
this.activityBinding = null
125121
}
126122

127123
private fun onAttachedToEngine(messenger: BinaryMessenger) {
128124
methodChannel = MethodChannel(messenger, CHANNEL_NAME)
129-
methodChannel!!.setMethodCallHandler(this)
130-
}
131-
132-
private fun addActivityResultListener(activityBinding: ActivityPluginBinding) {
133-
activityBinding.addActivityResultListener(this)
134-
}
135-
136-
private fun addActivityResultListener(registrar: PluginRegistry.Registrar) {
137-
registrar.addActivityResultListener(this)
125+
methodChannel?.setMethodCallHandler(this)
138126
}
139127

140128
companion object {
141129
private const val CHANNEL_NAME = "com.linecorp/flutter_line_sdk"
142130
private const val DEFAULT_ACTIVITY_RESULT_REQUEST_CODE = 8192
143-
144-
@JvmStatic
145-
fun registerWith(registrar: PluginRegistry.Registrar) {
146-
FlutterLineSdkPlugin().apply {
147-
onAttachedToEngine(registrar.messenger())
148-
activity = registrar.activity()
149-
addActivityResultListener(registrar)
150-
}
151-
}
152131
}
153132
}

0 commit comments

Comments
 (0)