Skip to content

Commit ff3454c

Browse files
Merge pull request #632 from Kommunicate-io/CM-2453-Handle-Logout-Login-AppID-change-Android-SDK
[CM-2453] Handle Logout, Login, AppID change | Android SDK
2 parents 1057eaa + 9fe76ca commit ff3454c

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

kommunicate/src/main/java/io/kommunicate/Kommunicate.java

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.kommunicate.devkit.api.notification.MobiComPushReceiver;
2424
import io.kommunicate.devkit.api.people.ChannelInfo;
2525
import io.kommunicate.devkit.broadcast.BroadcastService;
26+
import io.kommunicate.devkit.channel.service.ChannelService;
2627
import io.kommunicate.devkit.contact.database.ContactDatabase;
2728
import io.kommunicate.devkit.exception.KommunicateException;
2829

@@ -290,6 +291,157 @@ public static void setInAppNotification(boolean isEnable) {
290291
KmAppSettingPreferences.setInAppNotificationEnable(isEnable);
291292
}
292293

294+
/**
295+
* Universal function to handle Kommunicate login, conversation building, and launching.
296+
*
297+
* @param context The context from which the chat screen will be launched.
298+
* @param appID Application ID. Required if kmUser is null or kmUser.getApplicationId() is not set.
299+
* @param kmUser KMUser containing user details. If null, a visitor user will be created.
300+
* @param kmConversationBuilder Builder used to create a conversation.
301+
* @param shouldMaintainSession Indicates whether to maintain the session when {@code kmUser} is not provided and a random (visitor) user is used for login.
302+
* @param callback Callback to return success (conversation ID) or failure (error message).
303+
* @throws KmException If an unexpected error occurs during initialization or conversation launch.
304+
*/
305+
public static void launchConversationWithUser(
306+
@NotNull Context context,
307+
final String appID,
308+
final KMUser kmUser,
309+
final KmConversationBuilder kmConversationBuilder,
310+
final Boolean shouldMaintainSession,
311+
final KmCallback callback
312+
) throws KmException {
313+
314+
if (callback == null) {
315+
throw new IllegalArgumentException("Callback cannot be null.");
316+
}
317+
318+
final boolean isVisitorUser = (kmUser == null);
319+
320+
// Resolve and validate application ID
321+
String resolvedAppId = resolveAppId(appID, kmUser);
322+
if (resolvedAppId == null || resolvedAppId.isEmpty()) {
323+
callback.onFailure("APP_ID_IS_MISSING");
324+
return;
325+
}
326+
327+
initializeKommunicate(context, resolvedAppId);
328+
329+
final KmConversationBuilder conversationBuilder =
330+
(kmConversationBuilder != null) ? kmConversationBuilder : new KmConversationBuilder(context);
331+
332+
// Define post-login action
333+
Runnable proceedAfterLogin = () ->
334+
conversationBuilder.setContext(context).createConversation(new KmCallback() {
335+
@Override
336+
public void onSuccess(Object message) {
337+
openConversationUI(context, message, callback);
338+
}
339+
340+
@Override
341+
public void onFailure(Object error) {
342+
callback.onFailure("CONVERSATION_BUILDING_FAILED: " + error);
343+
}
344+
});
345+
346+
// Login logic
347+
Runnable loginAndProceed = () -> loginUser(context, kmUser, isVisitorUser, proceedAfterLogin, callback);
348+
349+
// Check current login state
350+
try {
351+
String loggedInUserId = ChannelService.getInstance(context).getLoggedInUserId();
352+
String inputUserId = (kmUser != null) ? kmUser.getUserId() : null;
353+
354+
boolean isSameUser = inputUserId != null && inputUserId.equals(loggedInUserId);
355+
boolean shouldSkipLogin = KMUser.isLoggedIn(context) && (isSameUser || (isVisitorUser && shouldMaintainSession));
356+
357+
if (shouldSkipLogin) {
358+
proceedAfterLogin.run();
359+
} else {
360+
loginAndProceed.run();
361+
}
362+
} catch (Exception e) {
363+
callback.onFailure("USER_CHECK_FAILED: " + e.getMessage());
364+
}
365+
}
366+
367+
private static void loginUser(Context context, KMUser kmUser, boolean isVisitorUser, Runnable onSuccess, KmCallback callback) {
368+
if (isVisitorUser) {
369+
Kommunicate.loginAsVisitor(context, new KMLoginHandler() {
370+
@Override
371+
public void onSuccess(RegistrationResponse response, Context ctx) {
372+
onSuccess.run();
373+
}
374+
375+
@Override
376+
public void onFailure(RegistrationResponse response, Exception e) {
377+
callback.onFailure("LOGIN_USER_FAILED");
378+
}
379+
});
380+
} else {
381+
try {
382+
Kommunicate.login(context, kmUser, new KMLoginHandler() {
383+
@Override
384+
public void onSuccess(RegistrationResponse response, Context ctx) {
385+
onSuccess.run();
386+
}
387+
388+
@Override
389+
public void onFailure(RegistrationResponse response, Exception e) {
390+
callback.onFailure("LOGIN_USER_FAILED");
391+
}
392+
});
393+
} catch (Exception e) {
394+
callback.onFailure("LOGIN_EXCEPTION: " + e.getMessage());
395+
}
396+
}
397+
}
398+
399+
private static void openConversationUI(Context context, Object message, KmCallback callback) {
400+
try {
401+
int conversationId = Integer.parseInt(String.valueOf(message));
402+
403+
KmConversationHelper.openConversation(context, true, conversationId, new KmCallback() {
404+
@Override
405+
public void onSuccess(Object msg) {
406+
callback.onSuccess(msg);
407+
}
408+
409+
@Override
410+
public void onFailure(Object error) {
411+
callback.onFailure("OPEN_CONVERSATION_FAILED: " + error);
412+
}
413+
});
414+
} catch (NumberFormatException e) {
415+
callback.onFailure("INVALID_CONVERSATION_ID: " + message);
416+
} catch (KmException e) {
417+
callback.onFailure("OPEN_CONVERSATION_EXCEPTION: " + e.getMessage());
418+
}
419+
}
420+
421+
private static String resolveAppId(String appID, KMUser kmUser) {
422+
if (appID != null && !appID.trim().isEmpty()) {
423+
return appID.trim();
424+
}
425+
if (kmUser != null && kmUser.getApplicationId() != null) {
426+
return kmUser.getApplicationId().trim();
427+
}
428+
return null;
429+
}
430+
431+
private static void initializeKommunicate(Context context, String applicationID) {
432+
try {
433+
String currentAppKey = KommunicateSettings.getInstance(context).getApplicationKey();
434+
435+
if (TextUtils.isEmpty(currentAppKey) || PLACEHOLDER_APP_ID.equals(currentAppKey)) {
436+
PrefSettings.getInstance(context).setApplicationKey(applicationID);
437+
}
438+
439+
Kommunicate.init(context, applicationID);
440+
} catch (Exception e) {
441+
Utils.printLog(context, TAG, "Failed to initialize Kommunicate: " + e.getMessage());
442+
}
443+
}
444+
293445
/**
294446
* To Check the Login status & launch the Pre Chat Lead Collection Screen
295447
*

0 commit comments

Comments
 (0)