From fbd0fd6cba0269292d86cf5e2e34777fd692bacc Mon Sep 17 00:00:00 2001 From: Prateek Singh Date: Thu, 24 Oct 2024 11:36:16 +0530 Subject: [PATCH] Fixed bug which opens the conversation with invalid conversation id. (#566) --- .../io/sample/ConversationListTest.kt | 27 ++++++------- .../io/kommunicate/KmConversationHelper.java | 40 ++++++++++++------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/src/androidTest/java/kommunicate/io/sample/ConversationListTest.kt b/app/src/androidTest/java/kommunicate/io/sample/ConversationListTest.kt index 27414843..3505b709 100644 --- a/app/src/androidTest/java/kommunicate/io/sample/ConversationListTest.kt +++ b/app/src/androidTest/java/kommunicate/io/sample/ConversationListTest.kt @@ -19,21 +19,18 @@ import kommunicate.io.sample.network.RetrofitClient import kommunicate.io.sample.utils.getAuthToken import kommunicate.io.sample.utils.getRandomKmUser import kommunicate.io.sample.utils.getRandomString -import kommunicate.io.sample.utils.waitFor import kommunicate.io.sample.utils.waitForLatch import kommunicate.io.sample.utils.withItemCount import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.suspendCancellableCoroutine import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotSame -import org.junit.Assert.assertThrows import org.junit.Assert.fail import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import java.lang.Exception import java.util.concurrent.CountDownLatch +import kotlin.Exception import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException @@ -163,22 +160,22 @@ class ConversationListTest { val latch = CountDownLatch(1) val conversationId = Integer.parseInt(getRandomString(8, allNumbers = true)) - assertThrows("conversation is opened with invalid group id: $conversationId", IllegalStateException::class.java) { - mActivityRule.onActivity { - it.lifecycleScope.launch { + mActivityRule.onActivity { + it.lifecycleScope.launch { + try { // Login user loginUser(it, kmUser) - - // Launch conversation with group id - val resultMessage = openConversationList(it, conversationId) - assertNotSame(resultMessage, conversationId) - }.invokeOnCompletion { + openConversationList(it, conversationId) + } catch (e: IllegalStateException) { + assertEquals("Invalid Conversation id, Unable to find conversation with given ID.", e.message) + } finally { latch.countDown() } } } - onView(isRoot()) - .perform(waitForLatch(latch)) + + // Wait for the coroutine to complete + onView(isRoot()).perform(waitForLatch(latch)) } private suspend fun createConversation(context: Context) = @@ -221,7 +218,7 @@ class ConversationListTest { } override fun onFailure(error: Any) { - continuation.resumeWithException(IllegalStateException("unable to create conversation throw error: $error")) + continuation.resumeWithException(IllegalStateException("$error")) } }) } diff --git a/kommunicate/src/main/java/io/kommunicate/KmConversationHelper.java b/kommunicate/src/main/java/io/kommunicate/KmConversationHelper.java index 8bc031e3..985bf3e2 100644 --- a/kommunicate/src/main/java/io/kommunicate/KmConversationHelper.java +++ b/kommunicate/src/main/java/io/kommunicate/KmConversationHelper.java @@ -126,22 +126,34 @@ public void onFailure(Exception e, Context context) { } private static void openParticularConversation(Context context, boolean skipConversationList, Integer conversationId, String preFilledMessage, KmCallback callback) { - try { - Intent intent = new Intent(context, KmUtils.getClassFromName(KmConstants.CONVERSATION_ACTIVITY_NAME)); - intent.putExtra(KmConstants.GROUP_ID, conversationId); - intent.putExtra(KmConstants.TAKE_ORDER, skipConversationList); - if (!TextUtils.isEmpty(preFilledMessage)) { - intent.putExtra(KmConstants.KM_PREFILLED_MESSAGE, preFilledMessage); - } - context.startActivity(intent); - if (callback != null) { - callback.onSuccess(conversationId); + KmGetConversationInfoCallback callbackListener = new KmGetConversationInfoCallback() { + @Override + public void onSuccess(Channel channel, Context context) { + try { + Intent intent = new Intent(context, KmUtils.getClassFromName(KmConstants.CONVERSATION_ACTIVITY_NAME)); + intent.putExtra(KmConstants.GROUP_ID, conversationId); + intent.putExtra(KmConstants.TAKE_ORDER, skipConversationList); + if (!TextUtils.isEmpty(preFilledMessage)) { + intent.putExtra(KmConstants.KM_PREFILLED_MESSAGE, preFilledMessage); + } + context.startActivity(intent); + if (callback != null) { + callback.onSuccess(conversationId); + } + } catch (ClassNotFoundException e) { + if (callback != null) { + callback.onFailure(e.getMessage()); + } + } } - } catch (ClassNotFoundException e) { - if (callback != null) { - callback.onFailure(e.getMessage()); + + @Override + public void onFailure(Exception e, Context context) { + callback.onFailure("Invalid Conversation id, Unable to find conversation with given ID."); } - } + }; + + new KmConversationInfoTask(context, conversationId, callbackListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @Deprecated