Skip to content

Commit 4788fff

Browse files
authored
Use Return Values Instead of Callbacks (#287)
Affected APIs: 1. Replies.getUnreadRepliesCount 2. Replies.hasChats 3. Surveys.hasRespondedToSurvey 4. Surveys.getAvailableSurveys
1 parent c48d111 commit 4788fff

File tree

5 files changed

+66
-34
lines changed

5 files changed

+66
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* Fixes main thread violation on Android
44
* Fixes an issue with request and response headers parameters type causing network requests not getting logged on iOS
55
* Uses pigeon for internal communication between Flutter and the host platform
6+
* Uses return values instead of callbacks in the following APIs:
7+
1. Replies.getUnreadRepliesCount
8+
2. Replies.hasChats
9+
3. Surveys.hasRespondedToSurvey
10+
4. Surveys.getAvailableSurveys
611

712
## 11.3.0 (2022-09-30)
813

lib/src/modules/replies.dart

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import 'package:instabug_flutter/generated/replies.api.g.dart';
66
import 'package:instabug_flutter/src/utils/ibg_build_info.dart';
77
import 'package:meta/meta.dart';
88

9+
@Deprecated(
10+
"Use [Replies.hasChats] return value instead of callback.",
11+
)
912
typedef HasChatsCallback = void Function(bool);
13+
1014
typedef OnNewReplyReceivedCallback = void Function();
15+
16+
@Deprecated(
17+
"Use [Replies.getUnreadRepliesCount] return value instead of callback.",
18+
)
1119
typedef UnreadRepliesCountCallback = void Function(int);
1220

1321
class Replies implements RepliesFlutterApi {
@@ -45,10 +53,16 @@ class Replies implements RepliesFlutterApi {
4553

4654
/// Tells whether the user has chats already or not.
4755
/// [callback] - callback that is invoked if chats exist
48-
static Future<void> hasChats(HasChatsCallback callback) async {
49-
// TODO: return directly without callback
56+
static Future<bool> hasChats([
57+
@Deprecated(
58+
'Use return value instead of callback: `final count = await Replies.hasChats();`',
59+
)
60+
// ignore: deprecated_member_use_from_same_package
61+
HasChatsCallback? callback,
62+
]) async {
5063
final hasChats = await _host.hasChats();
51-
callback(hasChats);
64+
callback?.call(hasChats);
65+
return hasChats;
5266
}
5367

5468
/// Sets a block of code that gets executed when a new message is received.
@@ -65,12 +79,16 @@ class Replies implements RepliesFlutterApi {
6579
/// has, then possibly notify them about it with your own UI.
6680
/// [function] callback with argument
6781
/// Notifications count, or -1 in case the SDK has not been initialized.
68-
static Future<void> getUnreadRepliesCount(
69-
UnreadRepliesCountCallback callback,
70-
) async {
71-
// TODO: return directly without callback
82+
static Future<int> getUnreadRepliesCount([
83+
@Deprecated(
84+
'Use return value instead of callback: `final count = await Replies.getUnreadRepliesCount();`',
85+
)
86+
// ignore: deprecated_member_use_from_same_package
87+
UnreadRepliesCountCallback? callback,
88+
]) async {
7289
final count = await _host.getUnreadRepliesCount();
73-
callback(count);
90+
callback?.call(count);
91+
return count;
7492
}
7593

7694
/// Enables/disables showing in-app notifications when the user receives a new message.

lib/src/modules/surveys.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import 'package:meta/meta.dart';
88

99
typedef OnShowSurveyCallback = void Function();
1010
typedef OnDismissSurveyCallback = void Function();
11+
12+
@Deprecated(
13+
"Use [Surveys.getAvailableSurveys] return value instead of callback.",
14+
)
1115
typedef AvailableSurveysCallback = void Function(List<String>);
16+
17+
@Deprecated(
18+
"Use [Surveys.hasRespondedToSurvey] return value instead of callback.",
19+
)
1220
typedef HasRespondedToSurveyCallback = void Function(bool);
1321

1422
class Surveys implements SurveysFlutterApi {
@@ -60,12 +68,16 @@ class Surveys implements SurveysFlutterApi {
6068
/// Returns an array containing the available surveys.
6169
/// [callback] availableSurveysCallback callback with
6270
/// argument available surveys
63-
static Future<void> getAvailableSurveys(
64-
AvailableSurveysCallback callback,
65-
) async {
66-
// TODO: return directly without callback
71+
static Future<List<String>> getAvailableSurveys([
72+
@Deprecated(
73+
'Use return value instead of callback: `final surveys = await Surveys.getAvailableSurveys();`',
74+
)
75+
// ignore: deprecated_member_use_from_same_package
76+
AvailableSurveysCallback? callback,
77+
]) async {
6778
final titles = await _host.getAvailableSurveys();
68-
callback(titles.cast<String>());
79+
callback?.call(titles.cast<String>());
80+
return titles.cast<String>();
6981
}
7082

7183
/// Sets a block of code to be executed just before the SDK's UI is presented.
@@ -118,13 +130,17 @@ class Surveys implements SurveysFlutterApi {
118130
/// This block is executed on the UI thread. Could be used for performing any
119131
/// UI changes after the survey's UI is dismissed.
120132
/// [callback] A callback that gets executed after the survey's UI is dismissed.
121-
static Future<void> hasRespondedToSurvey(
122-
String surveyToken,
123-
HasRespondedToSurveyCallback callback,
124-
) async {
125-
// TODO: return directly without callback
133+
static Future<bool> hasRespondedToSurvey(
134+
String surveyToken, [
135+
@Deprecated(
136+
'Use return value instead of callback: `final responded = await Surveys.hasRespondedToSurvey("<TOKEN>");`',
137+
)
138+
// ignore: deprecated_member_use_from_same_package
139+
HasRespondedToSurveyCallback? callback,
140+
]) async {
126141
final hasResponded = await _host.hasRespondedToSurvey(surveyToken);
127-
callback(hasResponded);
142+
callback?.call(hasResponded);
143+
return hasResponded;
128144
}
129145

130146
/// iOS Only

test/replies_test.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ void main() {
6767
const count = 10;
6868
when(mHost.getUnreadRepliesCount()).thenAnswer((_) async => count);
6969

70-
await Replies.getUnreadRepliesCount((result) {
71-
expect(result, count);
72-
});
70+
final result = await Replies.getUnreadRepliesCount();
7371

72+
expect(result, count);
7473
verify(
7574
mHost.getUnreadRepliesCount(),
7675
).called(1);
@@ -80,10 +79,9 @@ void main() {
8079
const hasChats = true;
8180
when(mHost.hasChats()).thenAnswer((_) async => hasChats);
8281

83-
await Replies.hasChats((result) {
84-
expect(result, hasChats);
85-
});
82+
final result = await Replies.hasChats();
8683

84+
expect(result, hasChats);
8785
verify(
8886
mHost.hasChats(),
8987
).called(1);

test/surveys_test.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ void main() {
8888
const responded = true;
8989
when(mHost.hasRespondedToSurvey(token)).thenAnswer((_) async => responded);
9090

91-
await Surveys.hasRespondedToSurvey(
92-
token,
93-
(result) {
94-
expect(result, responded);
95-
},
96-
);
91+
final result = await Surveys.hasRespondedToSurvey(token);
9792

93+
expect(result, responded);
9894
verify(
9995
mHost.hasRespondedToSurvey(token),
10096
).called(1);
@@ -104,10 +100,9 @@ void main() {
104100
const surveys = ["survey-1", "survey-2"];
105101
when(mHost.getAvailableSurveys()).thenAnswer((_) async => surveys);
106102

107-
await Surveys.getAvailableSurveys((result) {
108-
expect(result, surveys);
109-
});
103+
final result = await Surveys.getAvailableSurveys();
110104

105+
expect(result, surveys);
111106
verify(
112107
mHost.getAvailableSurveys(),
113108
).called(1);

0 commit comments

Comments
 (0)