|
23 | 23 | import io.kommunicate.devkit.api.notification.MobiComPushReceiver; |
24 | 24 | import io.kommunicate.devkit.api.people.ChannelInfo; |
25 | 25 | import io.kommunicate.devkit.broadcast.BroadcastService; |
| 26 | +import io.kommunicate.devkit.channel.service.ChannelService; |
26 | 27 | import io.kommunicate.devkit.contact.database.ContactDatabase; |
27 | 28 | import io.kommunicate.devkit.exception.KommunicateException; |
28 | 29 |
|
@@ -290,6 +291,164 @@ public static void setInAppNotification(boolean isEnable) { |
290 | 291 | KmAppSettingPreferences.setInAppNotificationEnable(isEnable); |
291 | 292 | } |
292 | 293 |
|
| 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 | + boolean isAppIdSame = 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) && isAppIdSame && (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 boolean initializeKommunicate(Context context, String applicationID) { |
| 432 | + try { |
| 433 | + String currentAppKey = KommunicateSettings.getInstance(context).getApplicationKey(); |
| 434 | + |
| 435 | + boolean isAppIdChanged = !TextUtils.isEmpty(currentAppKey) |
| 436 | + && !PLACEHOLDER_APP_ID.equals(currentAppKey) |
| 437 | + && !applicationID.equals(currentAppKey); |
| 438 | + |
| 439 | + if (TextUtils.isEmpty(currentAppKey) || PLACEHOLDER_APP_ID.equals(currentAppKey)) { |
| 440 | + PrefSettings.getInstance(context).setApplicationKey(applicationID); |
| 441 | + } |
| 442 | + |
| 443 | + Kommunicate.init(context, applicationID); |
| 444 | + |
| 445 | + return !isAppIdChanged; |
| 446 | + } catch (Exception e) { |
| 447 | + Utils.printLog(context, TAG, "Failed to initialize Kommunicate: " + e.getMessage()); |
| 448 | + return true; // Don't block flow even if init fails |
| 449 | + } |
| 450 | + } |
| 451 | + |
293 | 452 | /** |
294 | 453 | * To Check the Login status & launch the Pre Chat Lead Collection Screen |
295 | 454 | * |
|
0 commit comments