@@ -21,8 +21,10 @@ import com.linecorp.linesdk.unitywrapper.model.BotFriendshipStatus
2121import com.linecorp.linesdk.unitywrapper.model.LoginResultForFlutter
2222import com.linecorp.linesdk.unitywrapper.model.VerifyAccessTokenResult
2323import io.flutter.plugin.common.MethodChannel.Result
24- import kotlinx.coroutines.GlobalScope
24+ import kotlinx.coroutines.CoroutineScope
25+ import kotlinx.coroutines.Dispatchers
2526import kotlinx.coroutines.launch
27+ import kotlinx.coroutines.withContext
2628
2729
2830class LineSdkWrapper (
@@ -33,6 +35,7 @@ class LineSdkWrapper(
3335 private val gson = Gson ()
3436 private var loginRequestCode: Int = 0
3537 private var betaConfig: BetaConfig ? = null
38+ private val uiCoroutineScope: CoroutineScope = CoroutineScope (Dispatchers .Main )
3639
3740 fun setupSdk (channelId : String ) {
3841 runIfDebugBuild { Log .d(TAG , " setupSdk" ) }
@@ -64,9 +67,7 @@ class LineSdkWrapper(
6467 val lineAuthenticationParams = LineAuthenticationParams .Builder ()
6568 .scopes(Scope .convertToScopeList(scopes))
6669 .apply {
67- botPromptString?.let {
68- botPrompt(LineAuthenticationParams .BotPrompt .valueOf(botPromptString))
69- }
70+ botPrompt(LineAuthenticationParams .BotPrompt .valueOf(botPromptString))
7071 }
7172 .build()
7273
@@ -92,8 +93,8 @@ class LineSdkWrapper(
9293 fun getProfile (result : Result ) {
9394 runIfDebugBuild { Log .d(TAG , " getProfile" ) }
9495
95- GlobalScope .launch {
96- val lineApiResponse = lineApiClient.profile
96+ uiCoroutineScope .launch {
97+ val lineApiResponse = withContext( Dispatchers . IO ) { lineApiClient.profile }
9798 if (! lineApiResponse.isSuccess) {
9899 result.error(
99100 lineApiResponse.responseCode.name,
@@ -154,8 +155,8 @@ class LineSdkWrapper(
154155 fun logout (result : Result ) {
155156 runIfDebugBuild { Log .d(TAG , " logout" ) }
156157
157- GlobalScope .launch {
158- val lineApiResponse = lineApiClient.logout()
158+ uiCoroutineScope .launch {
159+ val lineApiResponse = withContext( Dispatchers . IO ) { lineApiClient.logout() }
159160 if (! lineApiResponse.isSuccess) {
160161 result.error(
161162 lineApiResponse.responseCode.name,
@@ -169,7 +170,7 @@ class LineSdkWrapper(
169170 }
170171
171172 fun getCurrentAccessToken (result : Result ) {
172- GlobalScope .launch {
173+ uiCoroutineScope .launch {
173174 val lineApiResponse = lineApiClient.currentAccessToken
174175 if (lineApiResponse.isSuccess) {
175176 result.success(
@@ -193,8 +194,8 @@ class LineSdkWrapper(
193194 fun getBotFriendshipStatus (result : Result ) {
194195 runIfDebugBuild { Log .d(TAG , " getBotFriendshipStatus" ) }
195196
196- GlobalScope .launch {
197- val lineApiResponse = lineApiClient.friendshipStatus
197+ uiCoroutineScope .launch {
198+ val lineApiResponse = withContext( Dispatchers . IO ) { lineApiClient.friendshipStatus }
198199 if (lineApiResponse.isSuccess) {
199200 result.success(
200201 gson.toJson(BotFriendshipStatus (lineApiResponse.responseData.isFriend))
@@ -212,8 +213,8 @@ class LineSdkWrapper(
212213 fun refreshToken (result : Result ) {
213214 runIfDebugBuild { Log .d(TAG , " refreshToken" ) }
214215
215- GlobalScope .launch {
216- val lineApiResponse = lineApiClient.refreshAccessToken()
216+ uiCoroutineScope .launch {
217+ val lineApiResponse = withContext( Dispatchers . IO ) { lineApiClient.refreshAccessToken() }
217218 if (lineApiResponse.isSuccess) {
218219 result.success(
219220 gson.toJson(
@@ -236,8 +237,8 @@ class LineSdkWrapper(
236237 fun verifyAccessToken (result : Result ) {
237238 runIfDebugBuild { Log .d(TAG , " verifyAccessToken" ) }
238239
239- GlobalScope .launch {
240- val lineApiResponse = lineApiClient.verifyToken()
240+ uiCoroutineScope .launch {
241+ val lineApiResponse = withContext( Dispatchers . IO ) { lineApiClient.verifyToken() }
241242 if (lineApiResponse.isSuccess) {
242243 result.success(
243244 gson.toJson(
0 commit comments