diff --git a/intercom_flutter/CHANGELOG.md b/intercom_flutter/CHANGELOG.md index 1022611..823bea5 100755 --- a/intercom_flutter/CHANGELOG.md +++ b/intercom_flutter/CHANGELOG.md @@ -6,6 +6,8 @@ * Bump Intercom iOS SDK version to 18.2.0 * Added API `isUserLoggedIn`. * Added API `fetchLoggedInUserAttributes`. +* Fixed [#479](https://github.com/v3rm0n/intercom_flutter/issues/479). +* Fixed [#481](https://github.com/v3rm0n/intercom_flutter/issues/481). ## 9.1.1 diff --git a/intercom_flutter/README.md b/intercom_flutter/README.md index 7ba554c..b3425ec 100755 --- a/intercom_flutter/README.md +++ b/intercom_flutter/README.md @@ -149,8 +149,6 @@ But you can pre-define some Intercom settings, if you want (optional). - [ ] handlePush - [ ] displayCarousel - [ ] displayHelpCenterCollections -- [ ] isUserLoggedIn -- [ ] fetchLoggedInUserAttributes ## Using Intercom keys with `--dart-define` diff --git a/intercom_flutter/pubspec.yaml b/intercom_flutter/pubspec.yaml index 8d9fede..5eddce5 100644 --- a/intercom_flutter/pubspec.yaml +++ b/intercom_flutter/pubspec.yaml @@ -10,7 +10,7 @@ dependencies: flutter_web_plugins: sdk: flutter intercom_flutter_platform_interface: ^2.0.2 - intercom_flutter_web: ^1.1.4 + intercom_flutter_web: ^1.1.5 dev_dependencies: flutter_test: diff --git a/intercom_flutter_web/CHANGELOG.md b/intercom_flutter_web/CHANGELOG.md index 7807dc3..39290fc 100755 --- a/intercom_flutter_web/CHANGELOG.md +++ b/intercom_flutter_web/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.1.5 + +* Set user_hash, user_id, email in intercomSettings. +* Implemented methods `isUserLoggedIn` and `fetchLoggedInUserAttributes`. + ## 1.1.4 * Updated dependency `intercom_flutter_platform_interface: ^2.0.2`. diff --git a/intercom_flutter_web/README.md b/intercom_flutter_web/README.md index c7a9dec..c8e14ac 100755 --- a/intercom_flutter_web/README.md +++ b/intercom_flutter_web/README.md @@ -26,8 +26,6 @@ But you can pre-define some Intercom settings, if you want (optional). - handlePush - displayCarousel - displayHelpCenterCollections -- isUserLoggedIn -- fetchLoggedInUserAttributes [1]: ../intercom_flutter diff --git a/intercom_flutter_web/example/integration_test/intercom_flutter_web_test.dart b/intercom_flutter_web/example/integration_test/intercom_flutter_web_test.dart index 48e3069..9a79398 100755 --- a/intercom_flutter_web/example/integration_test/intercom_flutter_web_test.dart +++ b/intercom_flutter_web/example/integration_test/intercom_flutter_web_test.dart @@ -138,5 +138,13 @@ void main() { testWidgets('displayHome', (WidgetTester _) async { expect(plugin.displayHome(), completes); }); + + testWidgets('isUserLoggedIn', (WidgetTester _) async { + expect(plugin.isUserLoggedIn(), completes); + }); + + testWidgets('fetchLoggedInUserAttributes', (WidgetTester _) async { + expect(plugin.fetchLoggedInUserAttributes(), completes); + }); }); } diff --git a/intercom_flutter_web/lib/intercom_flutter_web.dart b/intercom_flutter_web/lib/intercom_flutter_web.dart index 8d8abae..7900330 100644 --- a/intercom_flutter_web/lib/intercom_flutter_web.dart +++ b/intercom_flutter_web/lib/intercom_flutter_web.dart @@ -73,7 +73,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { globalContext.callMethod( 'Intercom'.toJS, 'update'.toJS, - {'user_hash': userHash}.jsify(), + updateIntercomSettings('user_hash', userHash).jsify(), ); print("user hash added"); } @@ -93,7 +93,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { globalContext.callMethod( 'Intercom'.toJS, 'update'.toJS, - {'user_id': userId}.jsify(), + updateIntercomSettings('user_id', userId).jsify(), ); // send the success callback only as web does not support the statusCallback. statusCallback?.onSuccess?.call(); @@ -102,7 +102,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { globalContext.callMethod( 'Intercom'.toJS, 'update'.toJS, - {'email': email}.jsify(), + updateIntercomSettings('email', email).jsify(), ); // send the success callback only as web does not support the statusCallback. statusCallback?.onSuccess?.call(); @@ -120,7 +120,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { globalContext.callMethod( 'Intercom'.toJS, 'update'.toJS, - {'user_id': userId}.jsify(), + updateIntercomSettings('user_id', userId).jsify(), ); // send the success callback only as web does not support the statusCallback. statusCallback?.onSuccess?.call(); @@ -213,6 +213,11 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { @override Future logout() async { + // shutdown will effectively clear out any user data that you have been passing through the JS API. + // but not from intercomSettings + // so manually clear some intercom settings + removeIntercomSettings(['user_hash', 'user_id', 'email']); + // shutdown globalContext.callMethod('Intercom'.toJS, 'shutdown'.toJS); print("logout"); } @@ -292,6 +297,35 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { print("Launched Home space"); } + @override + Future isUserLoggedIn() async { + // There is no direct JS API available + // Here we check if intercomSettings has user_id or email then user is + // logged in + var settings = getIntercomSettings(); + var user_id = settings['user_id'] as String? ?? ""; + var email = settings['email'] as String? ?? ""; + + return user_id.isNotEmpty || email.isNotEmpty; + } + + @override + Future> fetchLoggedInUserAttributes() async { + // There is no direct JS API available + // Just return the user_id or email from intercomSettings + var settings = getIntercomSettings(); + var user_id = settings['user_id'] as String? ?? ""; + var email = settings['email'] as String? ?? ""; + + if (user_id.isNotEmpty) { + return {'user_id': user_id}; + } else if (email.isNotEmpty) { + return {'email': email}; + } + + return {}; + } + /// get the [window.intercomSettings] Map getIntercomSettings() { if (globalContext.hasProperty('intercomSettings'.toJS).toDart) { @@ -316,4 +350,20 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { return intercomSettings; } + + /// Remove properties from [window.intercomSettings] + Map removeIntercomSettings(List keys) { + var intercomSettings = getIntercomSettings(); + + // remove the keys + for (var key in keys) { + intercomSettings.remove(key); + } + + // Update the [window.intercomSettings] + globalContext.setProperty( + "intercomSettings".toJS, intercomSettings.jsify()); + + return intercomSettings; + } } diff --git a/intercom_flutter_web/pubspec.yaml b/intercom_flutter_web/pubspec.yaml index a649ce7..45e0bf4 100644 --- a/intercom_flutter_web/pubspec.yaml +++ b/intercom_flutter_web/pubspec.yaml @@ -1,6 +1,6 @@ name: intercom_flutter_web description: Web platform implementation of intercom_flutter -version: 1.1.4 +version: 1.1.5 homepage: https://github.com/v3rm0n/intercom_flutter flutter: