@@ -6,13 +6,12 @@ import io.flutter.plugin.common.MethodCall
6
6
import io.flutter.plugin.common.MethodChannel
7
7
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
8
8
import io.flutter.plugin.common.MethodChannel.Result
9
- import io.flutter.plugin.common.PluginRegistry
10
9
import io.flutter.embedding.engine.plugins.FlutterPlugin
11
10
import io.flutter.embedding.engine.plugins.activity.ActivityAware
12
11
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
13
12
import io.flutter.plugin.common.BinaryMessenger
14
13
15
- class FlutterLineSdkPlugin : MethodCallHandler , PluginRegistry . ActivityResultListener , FlutterPlugin , ActivityAware {
14
+ class FlutterLineSdkPlugin : MethodCallHandler , FlutterPlugin , ActivityAware {
16
15
17
16
private var methodChannel: MethodChannel ? = null
18
17
private val lineSdkWrapper = LineSdkWrapper ()
@@ -23,57 +22,43 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
23
22
override fun onMethodCall (call : MethodCall , result : Result ) {
24
23
when (call.method) {
25
24
" 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()
30
29
lineSdkWrapper.setupBetaConfig(
31
- channelId,
32
- openDiscoveryIdDocumentUrl,
33
- apiServerBaseUrl,
34
- webLoginPageUrl
30
+ channelId,
31
+ openDiscoveryIdDocumentUrl,
32
+ apiServerBaseUrl,
33
+ webLoginPageUrl
35
34
)
36
35
result.success(null )
37
36
}
38
37
" 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 )
48
42
}
49
- lineSdkWrapper.setupSdk(activity, channelId)
50
- result.success(null )
51
43
}
52
44
" 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(
69
53
loginRequestCode,
70
54
activity,
71
55
scopes = scopes,
72
56
onlyWebLogin = isWebLogin,
73
57
botPromptString = botPrompt,
74
58
idTokenNonce = idTokenNonce,
75
59
result = result
76
- )
60
+ )
61
+ }
77
62
}
78
63
" getProfile" -> lineSdkWrapper.getProfile(result)
79
64
" currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result)
@@ -85,12 +70,26 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
85
70
}
86
71
}
87
72
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
+
88
86
override fun onAttachedToEngine (binding : FlutterPlugin .FlutterPluginBinding ) {
89
87
onAttachedToEngine(binding.binaryMessenger)
90
88
}
91
89
92
90
override fun onDetachedFromEngine (binding : FlutterPlugin .FlutterPluginBinding ) {
93
- methodChannel = null ;
91
+ methodChannel?.setMethodCallHandler(null )
92
+ methodChannel = null
94
93
}
95
94
96
95
override fun onAttachedToActivity (binding : ActivityPluginBinding ) {
@@ -109,45 +108,25 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis
109
108
unbindActivityBinding()
110
109
}
111
110
112
- override fun onActivityResult (requestCode : Int , resultCode : Int , intent : Intent ? ): Boolean =
113
- lineSdkWrapper.handleActivityResult(requestCode, resultCode, intent)
114
-
115
111
private fun bindActivityBinding (binding : ActivityPluginBinding ) {
116
112
this .activity = binding.activity
117
113
this .activityBinding = binding
118
- addActivityResultListener(binding )
114
+ binding. addActivityResultListener(lineSdkWrapper::handleActivityResult )
119
115
}
120
116
121
117
private fun unbindActivityBinding () {
122
- activityBinding?.removeActivityResultListener(this )
123
- this .activity = null ;
118
+ activityBinding?.removeActivityResultListener(lineSdkWrapper::handleActivityResult )
119
+ this .activity = null
124
120
this .activityBinding = null
125
121
}
126
122
127
123
private fun onAttachedToEngine (messenger : BinaryMessenger ) {
128
124
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 )
138
126
}
139
127
140
128
companion object {
141
129
private const val CHANNEL_NAME = " com.linecorp/flutter_line_sdk"
142
130
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
- }
152
131
}
153
132
}
0 commit comments