Skip to content

Commit d8b89e6

Browse files
authored
Flutter API [Replies] (#62)
## Mapped APIs: * setEnabled * show * hasChats * setOnNewReplyReceivedCallback * getUnreadRepliesCount * setInAppNotificationsEnabled * setInAppNotificationSound
1 parent 5c5ca7e commit d8b89e6

File tree

12 files changed

+361
-55
lines changed

12 files changed

+361
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Master
22

3+
* Adds Replies Api mappings
34
* Adds Chats Api mappings
45
* Adds FeatureRequests Api mappings.
56

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ A Flutter plugin for [Instabug](https://instabug.com/).
1010
|:---------------------------------------------------------:|:-------:|
1111
| [Bug Reporting](https://instabug.com/bug-reporting) | ⚙️ |
1212
| [Crash Reporting](https://instabug.com/crash-reporting) ||
13-
| [In-App Chat](https://instabug.com/in-app-chat) | |
13+
| [In-App Chat](https://instabug.com/in-app-chat) | ⚙️ |
1414
| [In-App Surveys](https://instabug.com/in-app-surveys) | ⚙️ |
15-
| [Feature Requests](https://instabug.com/feature-requests) | |
15+
| [Feature Requests](https://instabug.com/feature-requests) | ⚙️ |
1616

1717
* ✅ Stable
1818
* ⚙️ Under active development
@@ -106,6 +106,19 @@ The section below contains the APIs we're planning to implement for our 1.0 rele
106106
| `show()` | `show()`<br>`+ show` |
107107
| `setEnabled(bool isEnabled)` | `setState(Feature.State state)`<br>`enabled` |
108108

109+
110+
#### `Replies`
111+
112+
| API Method | Native Equivalent (Android/iOS) |
113+
|-----------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|
114+
| `setEnabled(bool isEnabled)` | `setState(Feature.State state)`<br>`enabled` |
115+
| `show()` | `show()`<br>`+ show` |
116+
| `hasChats(Function function)` | `hasChats()`<br>`+ hasChats` |
117+
| `setOnNewReplyReceivedCallback(Function function)` | `setOnNewReplyReceivedCallback(Callback callback)`<br>`didReceiveReplyHandler`
118+
| `getUnreadRepliesCount(Function function)` | `getUnreadRepliesCount()`<br>`unreadRepliesCount` |
119+
| `setInAppNotificationsEnabled(bool isEnabled)`| `setInAppNotificationEnabled(Boolean isChatNotificationEnable)`<br>`inAppNotificationsEnabled` |
120+
| `setInAppNotificationSound(bool isEnabled)` | `setInAppNotificationSound(Boolean shouldPlaySound)` |
121+
109122
## Integration
110123

111124
Creating a Flutter app on the Instabug dashboard isn't possible yet. Create a React Native app instead.

android/src/main/java/com/instabug/instabugflutter/InstabugFlutterPlugin.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,5 +779,80 @@ public void run() {
779779
});
780780
}
781781

782+
/**
783+
* Enables and disables everything related to receiving replies.
784+
* @param {boolean} isEnabled
785+
*/
786+
public void setRepliesEnabled(final boolean isEnabled) {
787+
new Handler(Looper.getMainLooper()).post(new Runnable() {
788+
@Override
789+
public void run() {
790+
if (isEnabled) {
791+
Replies.setState(Feature.State.ENABLED);
792+
} else {
793+
Replies.setState(Feature.State.DISABLED);
794+
}
795+
}
796+
});
797+
}
798+
799+
/**
800+
* Manual invocation for replies.
801+
*/
802+
public void showReplies() {
803+
Replies.show();
804+
}
805+
806+
807+
/**
808+
* Tells whether the user has chats already or not.
809+
*/
810+
public void hasChats() {
811+
boolean hasChats = Replies.hasChats();
812+
channel.invokeMethod("hasChatsCallback", hasChats);
813+
}
814+
815+
/**
816+
* Sets a block of code that gets executed when a new message is received.
817+
*/
818+
public void setOnNewReplyReceivedCallback() {
819+
Runnable onNewMessageRunnable = new Runnable() {
820+
@Override
821+
public void run() {
822+
channel.invokeMethod("onNewReplyReceivedCallback", null);
823+
}
824+
};
825+
Replies.setOnNewReplyReceivedCallback(onNewMessageRunnable);
826+
}
827+
828+
/**
829+
* Get current unread count of messages for this user
830+
*
831+
* @return number of messages that are unread for this user
832+
*/
833+
public void getUnreadRepliesCount() {
834+
int unreadMessages = Replies.getUnreadRepliesCount();
835+
channel.invokeMethod("unreadRepliesCountCallback", unreadMessages);
836+
}
837+
838+
/**
839+
* Enabled/disable chat notification
840+
*
841+
* @param isChatNotificationEnable whether chat notification is reburied or not
842+
*/
843+
public void setChatNotificationEnabled(boolean isChatNotificationEnable) {
844+
Replies.setInAppNotificationEnabled(isChatNotificationEnable);
845+
}
846+
847+
/**
848+
* Set whether new in app notification received will play a small sound notification
849+
* or not (Default is {@code false})
850+
*
851+
* @param shouldPlaySound desired state of conversation sounds
852+
* @since 4.1.0
853+
*/
854+
public void setEnableInAppNotificationSound(boolean shouldPlaySound) {
855+
Replies.setInAppNotificationSound(shouldPlaySound);
856+
}
782857

783858
}

example/lib/main.dart

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:instabug_flutter/InstabugLog.dart';
99
import 'package:instabug_flutter/Surveys.dart';
1010
import 'package:instabug_flutter/FeatureRequests.dart';
1111
import 'package:instabug_flutter/Chats.dart';
12+
import 'package:instabug_flutter/Replies.dart';
1213

1314
void main() => runApp(MyApp());
1415

@@ -36,7 +37,7 @@ class _MyAppState extends State<MyApp> {
3637
}
3738
//Instabug.showWelcomeMessageWithMode(WelcomeMessageMode.beta);
3839
//Instabug.setWelcomeMessageMode(WelcomeMessageMode.beta);
39-
Instabug.identifyUserWithEmail('[email protected]', 'Aly Ezz');
40+
Instabug.identifyUser('[email protected]', 'Aly Ezz');
4041
InstabugLog.logInfo('Test Log Info Message from Flutter!');
4142
InstabugLog.logDebug('Test Debug Message from Flutter!');
4243
InstabugLog.logVerbose('Test Verbose Message from Flutter!');
@@ -47,14 +48,14 @@ class _MyAppState extends State<MyApp> {
4748
//Instabug.setLocale(Locale.German);
4849
Instabug.setColorTheme(ColorTheme.dark);
4950
Instabug.appendTags(<String>['tag1', 'tag2']);
50-
Instabug.setUserAttributeWithKey('19', 'Age');
51-
Instabug.setUserAttributeWithKey('female', 'gender');
52-
Instabug.removeUserAttributeForKey('gender');
51+
Instabug.setUserAttribute('19', 'Age');
52+
Instabug.setUserAttribute('female', 'gender');
53+
Instabug.removeUserAttribute('gender');
5354
final String value = await Instabug.getUserAttributeForKey('Age');
5455
print('User Attribute ' + value);
5556
final Map<String, String> userAttributes = await Instabug.getUserAttributes();
5657
print(userAttributes.toString());
57-
Instabug.logUserEventWithName('Aly Event');
58+
Instabug.logUserEvent('Aly Event');
5859
Instabug.setValueForStringWithKey('What\'s the problem', IBGCustomTextPlaceHolderKey.reportBug);
5960
Instabug.setValueForStringWithKey('Send some ideas', IBGCustomTextPlaceHolderKey.reportFeedback);
6061
Instabug.setSessionProfilerEnabled(false);
@@ -73,6 +74,10 @@ class _MyAppState extends State<MyApp> {
7374
Surveys.setAutoShowingEnabled(false);
7475
Surveys.setOnShowCallback(surveyShown);
7576
Surveys.setOnDismissCallback(surveyDismiss);
77+
//Replies.setInAppNotificationsEnabled(false);
78+
Replies.setEnabled(true);
79+
Replies.show();
80+
Replies.setOnNewReplyReceivedCallback(replies);
7681
//BugReporting.setEnabledAttachmentTypes(false, false, false, false);
7782
//BugReporting.setReportTypes(<ReportType>[ReportType.FEEDBACK,ReportType.BUG]);
7883
//BugReporting.setExtendedBugReportMode(ExtendedBugReportMode.ENABLED_WITH_REQUIRED_FIELDS);
@@ -120,6 +125,10 @@ class _MyAppState extends State<MyApp> {
120125
void hasResponded(bool hasResponded) {
121126
debugPrint(hasResponded.toString());
122127
}
128+
129+
void replies() {
130+
debugPrint("new Replyyy");
131+
}
123132
void show() {
124133
//Instabug.show();
125134
// Surveys.getAvailableSurveys(getSurveys);
@@ -129,9 +138,10 @@ class _MyAppState extends State<MyApp> {
129138
//BugReporting.showWithOptions(ReportType.bug, <InvocationOption>[InvocationOption.emailFieldHidden]);
130139
// FeatureRequests.setEmailFieldRequired(false, [ActionType.allActions]);
131140
// FeatureRequests.show();
132-
Chats.setEnabled(true);
133-
Chats.show();
134-
141+
// Replies.setEnabled(true);
142+
// Replies.show();
143+
//Replies.setInAppNotificationsEnabled(false);
144+
//Replies.getUnreadRepliesCount(replies);
135145
}
136146

137147
void invokeWithMode() {

ios/Classes/InstabugFlutterPlugin.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,59 @@ + (void)setChatsEnabled:(NSNumber *)isEnabled {
654654
IBGChats.enabled = boolValue;
655655
}
656656

657+
/**
658+
* Enables and disables everything related to receiving replies.
659+
* @param {boolean} isEnabled
660+
*/
661+
+ (void)setRepliesEnabled:(NSNumber *)isEnabled {
662+
BOOL boolValue = [isEnabled boolValue];
663+
IBGReplies.enabled = boolValue;
664+
}
665+
666+
/**
667+
* Manual invocation for replies.
668+
*/
669+
+ (void)showReplies {
670+
[IBGReplies show];
671+
}
672+
673+
/**
674+
* Tells whether the user has chats already or not.
675+
*/
676+
+ (void)hasChats {
677+
BOOL hasChats = IBGReplies.hasChats;
678+
NSNumber *boolNumber = [NSNumber numberWithBool:hasChats];
679+
[channel invokeMethod:@"hasChatsCallback" arguments:boolNumber];
680+
}
681+
682+
/**
683+
* Sets a block of code that gets executed when a new message is received.
684+
*/
685+
+ (void)setOnNewReplyReceivedCallback {
686+
IBGReplies.didReceiveReplyHandler = ^{
687+
[channel invokeMethod:@"onNewReplyReceivedCallback" arguments:nil];
688+
};
689+
}
690+
691+
/**
692+
* Get current unread count of messages for this user
693+
*
694+
* @return number of messages that are unread for this user
695+
*/
696+
+ (void)getUnreadRepliesCount {
697+
[channel invokeMethod:@"unreadRepliesCountCallback" arguments:@(IBGReplies.unreadRepliesCount)];
698+
}
699+
700+
/**
701+
* Enabled/disable chat notification
702+
*
703+
* @param isEnabled whether chat notification is reburied or not
704+
*/
705+
+ (void)setChatNotificationEnabled:(NSNumber *)isEnabled {
706+
BOOL boolValue = [isEnabled boolValue];
707+
IBGReplies.inAppNotificationsEnabled = boolValue;
708+
}
709+
657710

658711
+ (NSDictionary *)constants {
659712
return @{

lib/BugReporting.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'package:flutter/foundation.dart';
32
import 'package:flutter/services.dart';
43
import 'package:instabug_flutter/Instabug.dart';
54

@@ -30,8 +29,8 @@ enum ExtendedBugReportMode {
3029

3130
class BugReporting {
3231

33-
static Function onInvokeCallback;
34-
static Function onDismissCallback;
32+
static Function _onInvokeCallback;
33+
static Function _onDismissCallback;
3534
static const MethodChannel _channel = MethodChannel('instabug_flutter');
3635

3736
static Future<String> get platformVersion async {
@@ -42,10 +41,10 @@ class BugReporting {
4241
static Future<dynamic> _handleMethod(MethodCall call) async {
4342
switch(call.method) {
4443
case 'onInvokeCallback':
45-
onInvokeCallback();
44+
_onInvokeCallback();
4645
return ;
4746
case 'onDismissCallback':
48-
Map<dynamic, dynamic> map = call.arguments;
47+
final Map<dynamic, dynamic> map = call.arguments;
4948
DismissType dismissType;
5049
ReportType reportType;
5150
final String dismissTypeString = map['dismissType'].toUpperCase();
@@ -73,19 +72,19 @@ class BugReporting {
7372
break;
7473
}
7574
try {
76-
onDismissCallback(dismissType,reportType);
75+
_onDismissCallback(dismissType,reportType);
7776
}
7877
catch(exception) {
79-
onDismissCallback();
78+
_onDismissCallback();
8079
}
8180
return ;
8281
}
8382
}
8483
/// invoke sdk manually with desire invocation mode
8584
/// [invocationMode] the invocation mode
8685
/// [invocationOptions] the array of invocation options
87-
static void invokeWithMode(InvocationMode invocationMode, [List<InvocationOption> invocationOptions]) async {
88-
List<String> invocationOptionsStrings = <String>[];
86+
static void invoke(InvocationMode invocationMode, [List<InvocationOption> invocationOptions]) async {
87+
final List<String> invocationOptionsStrings = <String>[];
8988
if (invocationOptions != null) {
9089
invocationOptions.forEach((e) {
9190
invocationOptionsStrings.add(e.toString());
@@ -108,7 +107,7 @@ class BugReporting {
108107
/// [function] A callback that gets executed before invoking the SDK
109108
static void setOnInvokeCallback(Function function) async {
110109
_channel.setMethodCallHandler(_handleMethod);
111-
onInvokeCallback = function;
110+
_onInvokeCallback = function;
112111
await _channel.invokeMethod<Object>('setOnInvokeCallback');
113112
}
114113

@@ -118,15 +117,15 @@ class BugReporting {
118117
/// [function] A callback that gets executed before invoking the SDK
119118
static void setOnDismissCallback(Function function) async {
120119
_channel.setMethodCallHandler(_handleMethod);
121-
onDismissCallback = function;
120+
_onDismissCallback = function;
122121
await _channel.invokeMethod<Object>('setOnDismissCallback');
123122
}
124123

125124
/// Sets the events that invoke the feedback form.
126125
/// Default is set by `Instabug.startWithToken`.
127126
/// [invocationEvents] invocationEvent List of events that invokes the
128127
static void setInvocationEvents(List<InvocationEvent> invocationEvents) async {
129-
List<String> invocationEventsStrings = <String>[];
128+
final List<String> invocationEventsStrings = <String>[];
130129
if (invocationEvents != null) {
131130
invocationEvents.forEach((e) {
132131
invocationEventsStrings.add(e.toString());
@@ -151,7 +150,7 @@ class BugReporting {
151150
///Sets what type of reports, bug or feedback, should be invoked.
152151
/// [reportTypes] - List of reportTypes
153152
static void setReportTypes(List<ReportType> reportTypes) async {
154-
List<String> reportTypesStrings = <String>[];
153+
final List<String> reportTypesStrings = <String>[];
155154
if (reportTypes != null) {
156155
reportTypes.forEach((e) {
157156
reportTypesStrings.add(e.toString());
@@ -173,7 +172,7 @@ class BugReporting {
173172
/// Default is set by `Instabug.startWithToken`.
174173
/// [invocationOptions] List of invocation options
175174
static void setInvocationOptions(List<InvocationOption> invocationOptions) async {
176-
List<String> invocationOptionsStrings = <String>[];
175+
final List<String> invocationOptionsStrings = <String>[];
177176
if (invocationOptions != null) {
178177
invocationOptions.forEach((e) {
179178
invocationOptionsStrings.add(e.toString());
@@ -186,8 +185,8 @@ class BugReporting {
186185
/// Invoke bug reporting with report type and options.
187186
/// [reportType] type
188187
/// [invocationOptions] List of invocation options
189-
static void showWithOptions(ReportType reportType, List<InvocationOption> invocationOptions) async {
190-
List<String> invocationOptionsStrings = <String>[];
188+
static void show(ReportType reportType, List<InvocationOption> invocationOptions) async {
189+
final List<String> invocationOptionsStrings = <String>[];
191190
if (invocationOptions != null) {
192191
invocationOptions.forEach((e) {
193192
invocationOptionsStrings.add(e.toString());

lib/Chats.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import 'dart:async';
2-
import 'package:flutter/foundation.dart';
32
import 'package:flutter/services.dart';
4-
import 'package:instabug_flutter/Instabug.dart';
53

64

75
class Chats {

lib/FeatureRequests.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import 'dart:async';
2-
import 'package:flutter/foundation.dart';
32
import 'package:flutter/services.dart';
4-
import 'package:instabug_flutter/Instabug.dart';
53

64
enum ActionType {
75
allActions,
@@ -30,7 +28,7 @@ class FeatureRequests {
3028
/// field is required or not.
3129
/// [actionTypes] An enum that indicates which action types will have the isEmailFieldRequired
3230
static void setEmailFieldRequired(bool isEmailFieldRequired, List<ActionType> actionTypes) async {
33-
List<String> actionTypesStrings = <String>[];
31+
final List<String> actionTypesStrings = <String>[];
3432
if (actionTypes != null) {
3533
actionTypes.forEach((e) {
3634
actionTypesStrings.add(e.toString());

0 commit comments

Comments
 (0)