Skip to content

Commit

Permalink
Use Instance approach (#226)
Browse files Browse the repository at this point in the history
make all the methods instance level instead of static
  • Loading branch information
deepak786 authored Apr 11, 2022
1 parent 232308d commit eebecbd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 53 deletions.
6 changes: 3 additions & 3 deletions intercom_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized();
// initialize the Intercom.
// make sure to add keys from your Intercom workspace.
await Intercom.initialize('appIdHere', iosApiKey: 'iosKeyHere', androidApiKey: 'androidKeyHere');
await Intercom.instance.initialize('appIdHere', iosApiKey: 'iosKeyHere', androidApiKey: 'androidKeyHere');
runApp(App());
}
Expand All @@ -38,7 +38,7 @@ class App extends StatelessWidget {
onPressed: () async {
// messenger will load the messages only if the user is registered in Intercom.
// either identified or unidentified.
await Intercom.displayMessenger();
await Intercom.instance.displayMessenger();
},
);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ This plugin works in combination with the [`firebase_messaging`](https://pub.dev
final firebaseMessaging = FirebaseMessaging.instance;
final intercomToken = Platform.isIOS ? await firebaseMessaging.getAPNSToken() : await firebaseMessaging.getToken();
Intercom.sendTokenToIntercom(intercomToken);
Intercom.instance.sendTokenToIntercom(intercomToken);
```

Now, if either Firebase direct (e.g. by your own backend server) or Intercom sends you a message, it will be delivered to your app.
Expand Down
4 changes: 2 additions & 2 deletions intercom_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:intercom_flutter/intercom_flutter.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Intercom.initialize(
await Intercom.instance.initialize(
'appId',
androidApiKey: 'androidApiKey',
iosApiKey: 'iosApiKey',
Expand All @@ -22,7 +22,7 @@ class SampleApp extends StatelessWidget {
body: Center(
child: TextButton(
onPressed: () {
Intercom.displayMessenger();
Intercom.instance.displayMessenger();
},
child: Text('Show messenger'),
),
Expand Down
51 changes: 28 additions & 23 deletions intercom_flutter/lib/intercom_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ export 'package:intercom_flutter_platform_interface/intercom_flutter_platform_in
show IntercomVisibility;

class Intercom {
/// private constructor to not allow the object creation from outside.
Intercom._();

static final Intercom _instance = Intercom._();

/// get the instance of the [Intercom].
static Intercom get instance => _instance;

/// Function to initialize the Intercom SDK.
///
/// First, you'll need to get your Intercom [appId].
Expand All @@ -22,7 +28,7 @@ class Intercom {
/// * [iOS](https://app.intercom.com/a/apps/_/settings/ios)
///
/// Then, initialize Intercom in main method.
static Future<void> initialize(
Future<void> initialize(
String appId, {
String? androidApiKey,
String? iosApiKey,
Expand All @@ -35,15 +41,15 @@ class Intercom {
/// even if a user dismisses a notification.
///
/// You can listen for unread conversation count with this method.
static Stream<dynamic> getUnreadStream() {
Stream<dynamic> getUnreadStream() {
return IntercomFlutterPlatform.instance.getUnreadStream();
}

/// This method allows you to set a fixed bottom padding for in app messages and the launcher.
///
/// It is useful if your app has a tab bar or similar UI at the bottom of your window.
/// [padding] is the size of the bottom padding in points.
static Future<void> setBottomPadding(int padding) {
Future<void> setBottomPadding(int padding) {
return IntercomFlutterPlatform.instance.setBottomPadding(padding);
}

Expand All @@ -60,7 +66,7 @@ class Intercom {
/// <https://gist.github.com/thewheat/7342c76ade46e7322c3e>
///
/// Note: identity verification does not apply to unidentified users.
static Future<void> setUserHash(String userHash) {
Future<void> setUserHash(String userHash) {
return IntercomFlutterPlatform.instance.setUserHash(userHash);
}

Expand All @@ -70,15 +76,15 @@ class Intercom {
///
/// You can register a identified user either with [userId] or with [email],
/// but not with both.
static Future<void> registerIdentifiedUser({String? userId, String? email}) {
Future<void> registerIdentifiedUser({String? userId, String? email}) {
return IntercomFlutterPlatform.instance
.registerIdentifiedUser(userId: userId, email: email);
}

/// Function to create a unidentified user in Intercom.
/// You need to register your users before you can talk to them and
/// track their activity in your app.
static Future<void> registerUnidentifiedUser() {
Future<void> registerUnidentifiedUser() {
return IntercomFlutterPlatform.instance.registerUnidentifiedUser();
}

Expand All @@ -92,7 +98,7 @@ class Intercom {
///
/// See also:
/// * [Localize Intercom to work with multiple languages](https://www.intercom.com/help/en/articles/180-localize-intercom-to-work-with-multiple-languages)
static Future<void> updateUser({
Future<void> updateUser({
String? email,
String? name,
String? phone,
Expand All @@ -118,26 +124,25 @@ class Intercom {

/// To logout a user from Intercom.
/// This clears the Intercom SDK's cache of your user's identity.
static Future<void> logout() {
Future<void> logout() {
return IntercomFlutterPlatform.instance.logout();
}

/// To hide or show the standard launcher on the bottom right-hand side of the screen.
static Future<void> setLauncherVisibility(IntercomVisibility visibility) {
Future<void> setLauncherVisibility(IntercomVisibility visibility) {
return IntercomFlutterPlatform.instance.setLauncherVisibility(visibility);
}

/// You can check how many unread conversations a user has
/// even if a user dismisses a notification.
///
/// You can get the current unread conversation count with this method.
static Future<int> unreadConversationCount() {
Future<int> unreadConversationCount() {
return IntercomFlutterPlatform.instance.unreadConversationCount();
}

/// To allow or prevent in app messages from popping up in certain parts of your app.
static Future<void> setInAppMessagesVisibility(
IntercomVisibility visibility) {
Future<void> setInAppMessagesVisibility(IntercomVisibility visibility) {
return IntercomFlutterPlatform.instance
.setInAppMessagesVisibility(visibility);
}
Expand All @@ -146,14 +151,14 @@ class Intercom {
///
/// This is used when you manually want to launch Intercom messenger.
/// for e.g: from your custom launcher (Help & Support) or (Talk to us).
static Future<void> displayMessenger() {
Future<void> displayMessenger() {
return IntercomFlutterPlatform.instance.displayMessenger();
}

/// To close the Intercom messenger.
///
/// This is used when you manually want to close Intercom messenger.
static Future<void> hideMessenger() {
Future<void> hideMessenger() {
return IntercomFlutterPlatform.instance.hideMessenger();
}

Expand All @@ -162,49 +167,49 @@ class Intercom {
/// Make sure Help Center is turned on.
/// If you don't have Help Center enabled in your Intercom settings the method
/// displayHelpCenter will fail to load.
static Future<void> displayHelpCenter() {
Future<void> displayHelpCenter() {
return IntercomFlutterPlatform.instance.displayHelpCenter();
}

/// To log events in Intercom that record what users do in your app and when they do it.
/// For example, you can record when user opened a specific screen in your app.
/// You can also pass [metaData] about the event.
static Future<void> logEvent(String name, [Map<String, dynamic>? metaData]) {
Future<void> logEvent(String name, [Map<String, dynamic>? metaData]) {
return IntercomFlutterPlatform.instance.logEvent(name, metaData);
}

/// The [token] to send to the Intercom to receive the notifications.
///
/// For the Android, this [token] must be a FCM (Firebase cloud messaging) token.
/// For the iOS, this [token] must be a APNS token.
static Future<void> sendTokenToIntercom(String token) {
Future<void> sendTokenToIntercom(String token) {
return IntercomFlutterPlatform.instance.sendTokenToIntercom(token);
}

/// When a user taps on a push notification Intercom hold onto data
/// such as the URI in your message or the conversation to open.
///
/// When you want Intercom to act on that data, use this method.
static Future<void> handlePushMessage() {
Future<void> handlePushMessage() {
return IntercomFlutterPlatform.instance.handlePushMessage();
}

/// To open the Intercom messenger to the composer screen with [message]
/// field pre-populated.
static Future<void> displayMessageComposer(String message) {
Future<void> displayMessageComposer(String message) {
return IntercomFlutterPlatform.instance.displayMessageComposer(message);
}

/// To check if the push [message] is for Intercom or not.
/// This is useful when your app is also configured to receive push messages
/// from third parties.
static Future<bool> isIntercomPush(Map<String, dynamic> message) async {
Future<bool> isIntercomPush(Map<String, dynamic> message) async {
return IntercomFlutterPlatform.instance.isIntercomPush(message);
}

/// If the push [message] is for Intercom then use this method to let
/// Intercom handle that push.
static Future<void> handlePush(Map<String, dynamic> message) async {
Future<void> handlePush(Map<String, dynamic> message) async {
return IntercomFlutterPlatform.instance.handlePush(message);
}

Expand All @@ -213,7 +218,7 @@ class Intercom {
/// An article must be ‘live’ to be used in this feature.
/// If it is in a draft or paused state,
/// end-users will see an error if the app tries to open the content.
static Future<void> displayArticle(String articleId) async {
Future<void> displayArticle(String articleId) async {
return IntercomFlutterPlatform.instance.displayArticle(articleId);
}

Expand All @@ -222,7 +227,7 @@ class Intercom {
/// A carousel must be ‘live’ to be used in this feature.
/// If it is in a draft or paused state,
/// end-users will see an error if the app tries to open the content.
static Future<void> displayCarousel(String carouselId) async {
Future<void> displayCarousel(String carouselId) async {
return IntercomFlutterPlatform.instance.displayCarousel(carouselId);
}
}
Loading

0 comments on commit eebecbd

Please sign in to comment.