Skip to content

Commit 2b91928

Browse files
authored
Merge pull request #3 from plateaukao/fix_coroutine_issue
Fix coroutine issue
2 parents 6b8231c + 1e26c9e commit 2b91928

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ android {
4040
}
4141

4242
dependencies {
43-
implementation 'com.linecorp:linesdk:5.0.1'
43+
implementation 'com.linecorp:linesdk:5.1'
4444
implementation 'com.google.code.gson:gson:2.8.5'
4545
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4646
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1"

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import com.linecorp.linesdk.unitywrapper.model.BotFriendshipStatus
2121
import com.linecorp.linesdk.unitywrapper.model.LoginResultForFlutter
2222
import com.linecorp.linesdk.unitywrapper.model.VerifyAccessTokenResult
2323
import io.flutter.plugin.common.MethodChannel.Result
24-
import kotlinx.coroutines.GlobalScope
24+
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.Dispatchers
2526
import kotlinx.coroutines.launch
27+
import kotlinx.coroutines.withContext
2628

2729

2830
class 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

Comments
 (0)