Skip to content

Commit

Permalink
Fixed bug which opens the conversation with invalid conversation id. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek-kommunicate authored Oct 24, 2024
1 parent 61bd975 commit fbd0fd6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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"))
}
})
}
Expand Down
40 changes: 26 additions & 14 deletions kommunicate/src/main/java/io/kommunicate/KmConversationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbd0fd6

Please sign in to comment.