Skip to content

Commit 194a5ab

Browse files
committed
feat: add changeWorkspace method
1 parent b77e32e commit 194a5ab

6 files changed

Lines changed: 218 additions & 56 deletions

File tree

intercom_flutter/android/src/main/kotlin/io/maido/intercom/IntercomFlutterPlugin.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ class IntercomFlutterPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Str
298298
})
299299
}
300300
}
301+
"changeWorkspace" -> {
302+
val appId = call.argument<String>("appId")
303+
val androidApiKey = call.argument<String>("androidApiKey")
304+
if (appId != null && androidApiKey != null) {
305+
Intercom.client().changeWorkspace(androidApiKey, appId)
306+
result.success("Workspace changed")
307+
} else {
308+
result.error("INVALID_ARGUMENTS", "appId and androidApiKey are required", null)
309+
}
310+
}
301311
else -> result.notImplemented()
302312
}
303313
}

intercom_flutter/ios/Classes/IntercomFlutterPlugin.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,26 @@ - (void) handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
279279
// Handle failure
280280
NSInteger errorCode = error.code;
281281
NSString *errorMsg = error.localizedDescription;
282-
282+
283283
result([FlutterError errorWithCode:[@(errorCode) stringValue]
284284
message:errorMsg
285285
details: [self getIntercomError:errorCode:errorMsg]]);
286286
}];
287287
}
288+
} else if([@"changeWorkspace" isEqualToString:call.method]) {
289+
NSString *iosApiKey = call.arguments[@"iosApiKey"];
290+
NSString *appId = call.arguments[@"appId"];
291+
if (iosApiKey != nil && iosApiKey != (id)[NSNull null] &&
292+
appId != nil && appId != (id)[NSNull null]) {
293+
// iOS doesn't have native changeWorkspace, so logout and re-initialize
294+
[Intercom logout];
295+
[Intercom setApiKey:iosApiKey forAppId:appId];
296+
result(@"Workspace changed");
297+
} else {
298+
result([FlutterError errorWithCode:@"INVALID_ARGUMENTS"
299+
message:@"appId and iosApiKey are required"
300+
details:nil]);
301+
}
288302
}
289303
else {
290304
result(FlutterMethodNotImplemented);

intercom_flutter/lib/intercom_flutter.dart

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ class Intercom {
3636
String? androidApiKey,
3737
String? iosApiKey,
3838
}) {
39-
return IntercomFlutterPlatform.instance
40-
.initialize(appId, androidApiKey: androidApiKey, iosApiKey: iosApiKey);
39+
return IntercomFlutterPlatform.instance.initialize(
40+
appId,
41+
androidApiKey: androidApiKey,
42+
iosApiKey: iosApiKey,
43+
);
4144
}
4245

4346
/// You can check how many unread conversations a user has
@@ -79,18 +82,25 @@ class Intercom {
7982
///
8083
/// You can register a identified user either with [userId] or with [email],
8184
/// but not with both.
82-
Future<void> loginIdentifiedUser(
83-
{String? userId, String? email, IntercomStatusCallback? statusCallback}) {
85+
Future<void> loginIdentifiedUser({
86+
String? userId,
87+
String? email,
88+
IntercomStatusCallback? statusCallback,
89+
}) {
8490
return IntercomFlutterPlatform.instance.loginIdentifiedUser(
85-
userId: userId, email: email, statusCallback: statusCallback);
91+
userId: userId,
92+
email: email,
93+
statusCallback: statusCallback,
94+
);
8695
}
8796

8897
/// Function to create a unidentified user in Intercom.
8998
/// You need to register your users before you can talk to them and
9099
/// track their activity in your app.
91100
Future<void> loginUnidentifiedUser({IntercomStatusCallback? statusCallback}) {
92-
return IntercomFlutterPlatform.instance
93-
.loginUnidentifiedUser(statusCallback: statusCallback);
101+
return IntercomFlutterPlatform.instance.loginUnidentifiedUser(
102+
statusCallback: statusCallback,
103+
);
94104
}
95105

96106
/// Updates the attributes of the current Intercom user.
@@ -150,8 +160,9 @@ class Intercom {
150160

151161
/// To allow or prevent in app messages from popping up in certain parts of your app.
152162
Future<void> setInAppMessagesVisibility(IntercomVisibility visibility) {
153-
return IntercomFlutterPlatform.instance
154-
.setInAppMessagesVisibility(visibility);
163+
return IntercomFlutterPlatform.instance.setInAppMessagesVisibility(
164+
visibility,
165+
);
155166
}
156167

157168
/// To open the Intercom messenger.
@@ -185,8 +196,9 @@ class Intercom {
185196
/// displayHelpCenterCollections will fail to load.
186197
/// The [collectionIds] you want to display.
187198
Future<void> displayHelpCenterCollections(List<String> collectionIds) {
188-
return IntercomFlutterPlatform.instance
189-
.displayHelpCenterCollections(collectionIds);
199+
return IntercomFlutterPlatform.instance.displayHelpCenterCollections(
200+
collectionIds,
201+
);
190202
}
191203

192204
/// To display an Activity with your Messages content.
@@ -214,7 +226,8 @@ class Intercom {
214226
///
215227
/// When you want Intercom to act on that data, use this method.
216228
@Deprecated(
217-
"Calling this API is no longer required. Intercom will directly open the chat screen when a push notification is clicked.")
229+
"Calling this API is no longer required. Intercom will directly open the chat screen when a push notification is clicked.",
230+
)
218231
Future<void> handlePushMessage() {
219232
return IntercomFlutterPlatform.instance.handlePushMessage();
220233
}
@@ -304,4 +317,27 @@ class Intercom {
304317
Future<void> setAuthTokens(Map<String, String> tokens) {
305318
return IntercomFlutterPlatform.instance.setAuthTokens(tokens);
306319
}
320+
321+
/// Changes the Intercom workspace.
322+
///
323+
/// On Android: Uses native changeWorkspace API (SDK 16.1.0+)
324+
/// On iOS: Logs out and re-initializes with new credentials
325+
///
326+
/// This will logout the current user and clear all SDK data.
327+
/// You must call login again after changing workspace.
328+
///
329+
/// [appId] is required for both platforms.
330+
/// [androidApiKey] is required for Android.
331+
/// [iosApiKey] is required for iOS.
332+
Future<void> changeWorkspace(
333+
String appId, {
334+
String? androidApiKey,
335+
String? iosApiKey,
336+
}) {
337+
return IntercomFlutterPlatform.instance.changeWorkspace(
338+
appId,
339+
androidApiKey: androidApiKey,
340+
iosApiKey: iosApiKey,
341+
);
342+
}
307343
}

intercom_flutter_platform_interface/lib/intercom_flutter_platform_interface.dart

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
7070
///
7171
/// You can register a identified user either with [userId] or with [email],
7272
/// but not with both.
73-
Future<void> loginIdentifiedUser(
74-
{String? userId, String? email, IntercomStatusCallback? statusCallback}) {
73+
Future<void> loginIdentifiedUser({
74+
String? userId,
75+
String? email,
76+
IntercomStatusCallback? statusCallback,
77+
}) {
7578
throw UnimplementedError('loginIdentifiedUser() has not been implemented.');
7679
}
7780

@@ -80,7 +83,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
8083
/// track their activity in your app.
8184
Future<void> loginUnidentifiedUser({IntercomStatusCallback? statusCallback}) {
8285
throw UnimplementedError(
83-
'loginUnidentifiedUser() has not been implemented.');
86+
'loginUnidentifiedUser() has not been implemented.',
87+
);
8488
}
8589

8690
/// Updates the attributes of the current Intercom user.
@@ -117,7 +121,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
117121
/// To hide or show the standard launcher on the bottom right-hand side of the screen.
118122
Future<void> setLauncherVisibility(IntercomVisibility visibility) {
119123
throw UnimplementedError(
120-
'setLauncherVisibility() has not been implemented.');
124+
'setLauncherVisibility() has not been implemented.',
125+
);
121126
}
122127

123128
/// You can check how many unread conversations a user has
@@ -126,13 +131,15 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
126131
/// You can get the current unread conversation count with this method.
127132
Future<int> unreadConversationCount() {
128133
throw UnimplementedError(
129-
'unreadConversationCount() has not been implemented.');
134+
'unreadConversationCount() has not been implemented.',
135+
);
130136
}
131137

132138
/// To allow or prevent in app messages from popping up in certain parts of your app.
133139
Future<void> setInAppMessagesVisibility(IntercomVisibility visibility) {
134140
throw UnimplementedError(
135-
'setInAppMessagesVisibility() has not been implemented.');
141+
'setInAppMessagesVisibility() has not been implemented.',
142+
);
136143
}
137144

138145
/// To open the Intercom messenger.
@@ -167,7 +174,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
167174
/// The [collectionIds] you want to display.
168175
Future<void> displayHelpCenterCollections(List<String> collectionIds) {
169176
throw UnimplementedError(
170-
'displayHelpCenterCollections() has not been implemented.');
177+
'displayHelpCenterCollections() has not been implemented.',
178+
);
171179
}
172180

173181
/// To display an Activity with your Messages content.
@@ -195,7 +203,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
195203
///
196204
/// When you want Intercom to act on that data, use this method.
197205
@Deprecated(
198-
"Calling this API is no longer required. Intercom will directly open the chat screen when a push notification is clicked.")
206+
"Calling this API is no longer required. Intercom will directly open the chat screen when a push notification is clicked.",
207+
)
199208
Future<void> handlePushMessage() {
200209
throw UnimplementedError('handlePushMessage() has not been implemented.');
201210
}
@@ -204,7 +213,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
204213
/// field pre-populated.
205214
Future<void> displayMessageComposer(String message) {
206215
throw UnimplementedError(
207-
'displayMessageComposer() has not been implemented.');
216+
'displayMessageComposer() has not been implemented.',
217+
);
208218
}
209219

210220
/// To check if the push [message] is for Intercom or not.
@@ -278,7 +288,8 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
278288
/// Retrieve the details of the currently logged in user.
279289
Future<Map<String, dynamic>> fetchLoggedInUserAttributes() {
280290
throw UnimplementedError(
281-
'fetchLoggedInUserAttributes() has not been implemented.');
291+
'fetchLoggedInUserAttributes() has not been implemented.',
292+
);
282293
}
283294

284295
/// JWT (JSON Web Token) is the recommended method to secure your Messenger.
@@ -295,4 +306,23 @@ abstract class IntercomFlutterPlatform extends PlatformInterface {
295306
Future<void> setAuthTokens(Map<String, String> tokens) {
296307
throw UnimplementedError('setAuthTokens() has not been implemented.');
297308
}
309+
310+
/// Changes the Intercom workspace.
311+
///
312+
/// On Android: Uses native changeWorkspace API (SDK 16.1.0+)
313+
/// On iOS: Logs out and re-initializes with new credentials
314+
///
315+
/// This will logout the current user and clear all SDK data.
316+
/// You must call login again after changing workspace.
317+
///
318+
/// [appId] is required for both platforms.
319+
/// [androidApiKey] is required for Android.
320+
/// [iosApiKey] is required for iOS.
321+
Future<void> changeWorkspace(
322+
String appId, {
323+
String? androidApiKey,
324+
String? iosApiKey,
325+
}) {
326+
throw UnimplementedError('changeWorkspace() has not been implemented.');
327+
}
298328
}

0 commit comments

Comments
 (0)