1- package com.xmartlabs.rnline
1+ package com.xmartlabs.line
22
33import android.app.Activity
44import android.content.Context
55import android.content.Intent
6+
67import com.facebook.react.bridge.*
8+ import com.facebook.react.module.annotations.ReactModule
79
10+ import com.linecorp.linesdk.*
811import com.linecorp.linesdk.api.LineApiClient
912import com.linecorp.linesdk.api.LineApiClientBuilder
1013import com.linecorp.linesdk.auth.LineAuthenticationConfig
1114import com.linecorp.linesdk.auth.LineAuthenticationParams
1215import com.linecorp.linesdk.auth.LineLoginApi
1316import com.linecorp.linesdk.auth.LineLoginResult
17+ import com.linecorp.linesdk.LineProfile
1418
1519import kotlinx.coroutines.CoroutineScope
1620import kotlinx.coroutines.Dispatchers
1721import kotlinx.coroutines.launch
1822import kotlinx.coroutines.withContext
19- import com.facebook.react.bridge.WritableMap
20- import com.linecorp.linesdk.*
21- import com.linecorp.linesdk.LineProfile
23+
24+ private var LOGIN_REQUEST_CODE : Int = 0
2225
2326enum class LoginArguments (val key : String ) {
2427 BOT_PROMPT (" botPrompt" ),
2528 ONLY_WEB_LOGIN (" onlyWebLogin" ),
2629 SCOPES (" scopes" )
2730}
2831
29- class LineLogin (private val reactContext : ReactApplicationContext ) :
30- ReactContextBaseJavaModule (reactContext) {
32+ class LineLoginModule (private val reactContext : ReactApplicationContext ) :
33+ NativeLineLoginSpec (reactContext) {
34+
3135 companion object {
32- private const val MODULE_NAME : String = " LineLogin "
36+ const val NAME = NativeLineLoginSpec . NAME
3337 }
3438
39+ private val coroutineScope: CoroutineScope = CoroutineScope (Dispatchers .Main )
40+
3541 private lateinit var channelId: String
3642 private lateinit var lineApiClient: LineApiClient
37- private var LOGIN_REQUEST_CODE : Int = 0
38- private val uiCoroutineScope: CoroutineScope = CoroutineScope (Dispatchers .Main )
39-
4043 private var loginResult: Promise ? = null
4144
42- override fun getName () = MODULE_NAME
43-
44- @ReactMethod
45- fun setup (args : ReadableMap , promise : Promise ) {
46- val context: Context = reactContext.applicationContext
45+ override fun setup (args : ReadableMap , promise : Promise ) {
4746 channelId = args.getString(" channelId" )!!
48- lineApiClient = LineApiClientBuilder (context , channelId).build()
47+ lineApiClient = LineApiClientBuilder (reactContext.applicationContext , channelId).build()
4948 reactContext.addActivityEventListener(object : ActivityEventListener {
5049 override fun onNewIntent (intent : Intent ? ) {}
5150 override fun onActivityResult (
@@ -58,8 +57,7 @@ class LineLogin(private val reactContext: ReactApplicationContext) :
5857 })
5958 }
6059
61- @ReactMethod
62- fun login (args : ReadableMap , promise : Promise ) {
60+ override fun login (args : ReadableMap , promise : Promise ) {
6361 val scopes =
6462 if (args.hasKey(LoginArguments .SCOPES .key)) args.getArray(LoginArguments .SCOPES .key)!!
6563 .toArrayList() as List <String > else listOf (" profile" )
@@ -113,9 +111,8 @@ class LineLogin(private val reactContext: ReactApplicationContext) :
113111 this .loginResult = promise
114112 }
115113
116- @ReactMethod
117- fun getProfile (promise : Promise ) {
118- uiCoroutineScope.launch {
114+ override fun getProfile (promise : Promise ) {
115+ coroutineScope.launch {
119116 val lineApiResponse = withContext(Dispatchers .IO ) { lineApiClient.getProfile() }
120117 if (! lineApiResponse.isSuccess) {
121118 promise.reject(
@@ -168,9 +165,8 @@ class LineLogin(private val reactContext: ReactApplicationContext) :
168165 loginResult = null
169166 }
170167
171- @ReactMethod
172- fun logout (promise : Promise ) {
173- uiCoroutineScope.launch {
168+ override fun logout (promise : Promise ) {
169+ coroutineScope.launch {
174170 val lineApiResponse = withContext(Dispatchers .IO ) { lineApiClient.logout() }
175171 if (lineApiResponse.isSuccess) {
176172 promise.resolve(null )
@@ -184,29 +180,25 @@ class LineLogin(private val reactContext: ReactApplicationContext) :
184180 }
185181 }
186182
187- @ReactMethod
188- fun getCurrentAccessToken (promise : Promise ) = invokeLineServiceMethod(
183+ override fun getCurrentAccessToken (promise : Promise ) = invokeLineServiceMethod(
189184 promise = promise,
190185 serviceCallable = { lineApiClient.getCurrentAccessToken() },
191186 parser = { parseAccessToken(it, lineIdToken = null ) }
192187 )
193188
194- @ReactMethod
195- fun getFriendshipStatus (promise : Promise ) = invokeLineServiceMethod(
189+ override fun getFriendshipStatus (promise : Promise ) = invokeLineServiceMethod(
196190 promise = promise,
197191 serviceCallable = { lineApiClient.getFriendshipStatus() },
198192 parser = { parseFriendshipStatus(it) }
199193 )
200194
201- @ReactMethod
202- fun refreshAccessToken (promise : Promise ) = invokeLineServiceMethod(
195+ override fun refreshAccessToken (promise : Promise ) = invokeLineServiceMethod(
203196 promise = promise,
204197 serviceCallable = { lineApiClient.refreshAccessToken() },
205198 parser = { parseAccessToken(it, lineIdToken = null ) }
206199 )
207200
208- @ReactMethod
209- fun verifyAccessToken (promise : Promise ) = invokeLineServiceMethod(
201+ override fun verifyAccessToken (promise : Promise ) = invokeLineServiceMethod(
210202 promise = promise,
211203 serviceCallable = { lineApiClient.verifyToken() },
212204 parser = { parseVerifyAccessToken(it) }
@@ -217,7 +209,7 @@ class LineLogin(private val reactContext: ReactApplicationContext) :
217209 serviceCallable : () -> LineApiResponse <T >,
218210 parser : (T ) -> WritableMap
219211 ) {
220- uiCoroutineScope .launch {
212+ coroutineScope .launch {
221213 val lineApiResponse = withContext(Dispatchers .IO ) { serviceCallable.invoke() }
222214 if (lineApiResponse.isSuccess) {
223215 promise.resolve(parser.invoke(lineApiResponse.responseData))
0 commit comments