From 7aca00abd113f6067ea566d7a97eea025415778e Mon Sep 17 00:00:00 2001 From: JoshKiff Date: Wed, 8 Oct 2025 10:32:46 +0100 Subject: [PATCH] feat: add changeWorkspace method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../io/maido/intercom/IntercomFlutterPlugin.kt | 10 ++++++++++ .../ios/Classes/IntercomFlutterPlugin.m | 14 ++++++++++++++ intercom_flutter/lib/intercom_flutter.dart | 15 +++++++++++++++ .../lib/intercom_flutter_platform_interface.dart | 15 +++++++++++++++ .../lib/method_channel_intercom_flutter.dart | 5 +++++ .../lib/intercom_flutter_web.dart | 12 ++++++++++++ 6 files changed, 71 insertions(+) diff --git a/intercom_flutter/android/src/main/kotlin/io/maido/intercom/IntercomFlutterPlugin.kt b/intercom_flutter/android/src/main/kotlin/io/maido/intercom/IntercomFlutterPlugin.kt index c952c17..e553ef2 100644 --- a/intercom_flutter/android/src/main/kotlin/io/maido/intercom/IntercomFlutterPlugin.kt +++ b/intercom_flutter/android/src/main/kotlin/io/maido/intercom/IntercomFlutterPlugin.kt @@ -298,6 +298,16 @@ class IntercomFlutterPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Str }) } } + "changeWorkspace" -> { + val appId = call.argument("appId") + val androidApiKey = call.argument("androidApiKey") + if (appId != null && androidApiKey != null) { + Intercom.client().changeWorkspace(androidApiKey, appId) + result.success("Workspace changed") + } else { + result.error("INVALID_ARGUMENTS", "appId and androidApiKey are required", null) + } + } else -> result.notImplemented() } } diff --git a/intercom_flutter/ios/Classes/IntercomFlutterPlugin.m b/intercom_flutter/ios/Classes/IntercomFlutterPlugin.m index e3e36af..d6abdbe 100644 --- a/intercom_flutter/ios/Classes/IntercomFlutterPlugin.m +++ b/intercom_flutter/ios/Classes/IntercomFlutterPlugin.m @@ -285,6 +285,20 @@ - (void) handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result details: [self getIntercomError:errorCode:errorMsg]]); }]; } + } else if([@"changeWorkspace" isEqualToString:call.method]) { + NSString *iosApiKey = call.arguments[@"iosApiKey"]; + NSString *appId = call.arguments[@"appId"]; + if (iosApiKey != nil && iosApiKey != (id)[NSNull null] && + appId != nil && appId != (id)[NSNull null]) { + // iOS doesn't have native changeWorkspace, so logout and re-initialize + [Intercom logout]; + [Intercom setApiKey:iosApiKey forAppId:appId]; + result(@"Workspace changed"); + } else { + result([FlutterError errorWithCode:@"INVALID_ARGUMENTS" + message:@"appId and iosApiKey are required" + details:nil]); + } } else { result(FlutterMethodNotImplemented); diff --git a/intercom_flutter/lib/intercom_flutter.dart b/intercom_flutter/lib/intercom_flutter.dart index b4b794b..d5e82ee 100755 --- a/intercom_flutter/lib/intercom_flutter.dart +++ b/intercom_flutter/lib/intercom_flutter.dart @@ -304,4 +304,19 @@ class Intercom { Future setAuthTokens(Map tokens) { return IntercomFlutterPlatform.instance.setAuthTokens(tokens); } + + /// Changes the Intercom workspace. + /// + /// On Android: Uses native changeWorkspace API (SDK 16.1.0+) + /// On iOS: Logs out and re-initializes with new credentials + /// + /// This will logout the current user and clear all SDK data. + /// You must call login again after changing workspace. + /// + /// [appId] is required for both platforms. + /// [androidApiKey] is required for Android. + /// [iosApiKey] is required for iOS. + Future changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) { + return IntercomFlutterPlatform.instance.changeWorkspace(appId, androidApiKey: androidApiKey, iosApiKey: iosApiKey); + } } diff --git a/intercom_flutter_platform_interface/lib/intercom_flutter_platform_interface.dart b/intercom_flutter_platform_interface/lib/intercom_flutter_platform_interface.dart index 4f5b749..d7fb8e7 100644 --- a/intercom_flutter_platform_interface/lib/intercom_flutter_platform_interface.dart +++ b/intercom_flutter_platform_interface/lib/intercom_flutter_platform_interface.dart @@ -295,4 +295,19 @@ abstract class IntercomFlutterPlatform extends PlatformInterface { Future setAuthTokens(Map tokens) { throw UnimplementedError('setAuthTokens() has not been implemented.'); } + + /// Changes the Intercom workspace. + /// + /// On Android: Uses native changeWorkspace API (SDK 16.1.0+) + /// On iOS: Logs out and re-initializes with new credentials + /// + /// This will logout the current user and clear all SDK data. + /// You must call login again after changing workspace. + /// + /// [appId] is required for both platforms. + /// [androidApiKey] is required for Android. + /// [iosApiKey] is required for iOS. + Future changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) { + throw UnimplementedError('changeWorkspace() has not been implemented.'); + } } diff --git a/intercom_flutter_platform_interface/lib/method_channel_intercom_flutter.dart b/intercom_flutter_platform_interface/lib/method_channel_intercom_flutter.dart index 949a8c5..a6e1977 100644 --- a/intercom_flutter_platform_interface/lib/method_channel_intercom_flutter.dart +++ b/intercom_flutter_platform_interface/lib/method_channel_intercom_flutter.dart @@ -265,6 +265,11 @@ class MethodChannelIntercomFlutter extends IntercomFlutterPlatform { await _channel.invokeMethod('setAuthTokens', {'tokens': tokens}); } + @override + Future changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) async { + await _channel.invokeMethod('changeWorkspace', {'appId': appId, 'androidApiKey': androidApiKey, 'iosApiKey': iosApiKey}); + } + /// Convert the [PlatformException] details to [IntercomError]. /// From the Platform side if the intercom operation failed then error details /// will be sent as details in [PlatformException]. diff --git a/intercom_flutter_web/lib/intercom_flutter_web.dart b/intercom_flutter_web/lib/intercom_flutter_web.dart index c1181f3..96d59e0 100644 --- a/intercom_flutter_web/lib/intercom_flutter_web.dart +++ b/intercom_flutter_web/lib/intercom_flutter_web.dart @@ -352,6 +352,18 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { print("Auth tokens added"); } + @override + Future changeWorkspace(String appId, {String? androidApiKey, String? iosApiKey}) async { + // For web, we shutdown the current workspace and boot with the new appId + // Clear user data first + removeIntercomSettings(['user_hash', 'intercom_user_jwt', 'user_id', 'email', 'auth_tokens']); + // Shutdown current workspace + globalContext.callMethod('Intercom'.toJS, 'shutdown'.toJS); + // Boot with new workspace + globalContext.callMethod('Intercom'.toJS, 'boot'.toJS, updateIntercomSettings('app_id', appId).jsify()); + print("Workspace changed"); + } + /// get the [window.intercomSettings] Map getIntercomSettings() { if (globalContext.hasProperty('intercomSettings'.toJS).toDart) {