Skip to content

Commit 41aee89

Browse files
authored
[MOB 2950] [MOB 2966] Flutter API: getUserAttributes getUserAttributeForKey (#37)
* ✨ Add android map for getUserAttributeForKey * ✨ add ios map for getUserAttributeForKey * ✨ add dart API for getUserAttributeForKey * ✨ Add getUserAttributeFromKey to example app * ✨ Add getUserAttributeForKey test * ✨ add android map for getUserAttributes * ✨ add ios map for getUserAttributes * ✨ add dart API for getUserAttributes * ✨ add the API getUserAttributes to the example app * ✨ add getUserAttributes test and move the unit tests file from the example app to the library directory
1 parent bf96aae commit 41aee89

File tree

7 files changed

+91
-11
lines changed

7 files changed

+91
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ The table below contains a list of APIs we're planning to implement for our 1.0
3636
|`InstabugFlutter.resetTags()`| `Instabug.resetTags()`<br>`+ [Instabug resetTags]` |
3737
|`InstabugFlutter.getTags()`| `Instabug.getTags()`<br>`+ [Instabug getTags]` |
3838
| | `Instabug.setCustomTextPlaceHolders(InstabugCustomTextPlaceHolder placeholder)`<br>`+ [Instabug setValue:forStringWithKey:]` |
39-
| | `Instabug.setUserAttribute(String key, String value)`<br>`+ [Instabug setUserAttribute:withKey:]` |
40-
|`InstabugFlutter.setUserAttributeWithKey(String value, String key)`| `Instabug.getUserAttribute(String key)`<br>`+ [Instabug userAttributeForKey:]` |
39+
|`InstabugFlutter.setUserAttributeWithKey(String value, String key)`| `Instabug.setUserAttribute(String key, String value)`<br>`+ [Instabug setUserAttribute:withKey:]` |
40+
|`Instabugflutter.getUserAttributeForKey(Sring Key)`| `Instabug.getUserAttribute(String key)`<br>`+ [Instabug userAttributeForKey:]` |
4141
|`InstabugFlutter.removeUserAttributeForKey(String key)`| `Instabug.removeUserAttribute(String key)`<br>`+ [Instabug removeUserAttributeForKey:]` |
42-
| | `Instabug.getAllUserAttributes()`<br>`+ [Instabug userAttributes:]` |
42+
|`InstabugFlutter.getUserAttributes()`| `Instabug.getAllUserAttributes()`<br>`+ [Instabug userAttributes:]` |
4343
|`logUserEventWithName(String name)`| `Instabug.logUserEvent(String name)`<br>`+ [Instabug logUserEventWithName:]` |
4444
|`show()`| `Instabug.show()`<br>`+ [Instabug show]` |
4545
|`invokeWithMode(InvocationMode invocationMode, [List<InvocationOption> invocationOptions])`| `BugReporting.invoke(InvocationMode mode, @InvocationOption int... options)`<br>`+ [IBGBugReporting invokeWithMode:options:]` |

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public void onMethodCall(MethodCall call, Result result) {
6060
if (callMethod.equals(method.getName())) {
6161
isImplemented = true;
6262
ArrayList<Object> tempParamValues = new ArrayList<>();
63-
Log.e("Method Name", callMethod);
64-
Log.e("Method Args", call.arguments().toString());
6563
if (call.arguments != null) {
6664
tempParamValues = (ArrayList<Object>) call.arguments;
6765
}
@@ -266,6 +264,23 @@ public void removeUserAttributeForKey(String key) {
266264
Instabug.removeUserAttribute(key);
267265
}
268266

267+
/**
268+
* Returns the user attribute associated with a given key.
269+
* @param key The key for which to return the corresponding value.
270+
* @return The value associated with aKey, or null if no value is associated with aKey.
271+
*/
272+
public String getUserAttributeForKey(String key) {
273+
return Instabug.getUserAttribute(key);
274+
}
275+
276+
/**
277+
* Returns all user attributes.
278+
* @return A new HashMap containing all the currently set user attributes, or an empty HashMap if no user attributes have been set.
279+
*/
280+
public HashMap<String, String> getUserAttributes() {
281+
return Instabug.getAllUserAttributes();
282+
}
283+
269284
/**
270285
* invoke sdk manually
271286
*/

example/lib/main.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ class _MyAppState extends State<MyApp> {
4040
InstabugFlutter.setLocale(Locale.German);
4141
//InstabugFlutter.setLocale(Locale.German);
4242
InstabugFlutter.setColorTheme(ColorTheme.dark);
43-
InstabugFlutter.appendTags(['tag1', 'tag2']);
43+
InstabugFlutter.appendTags(<String>['tag1', 'tag2']);
4444
InstabugFlutter.setUserAttributeWithKey('19', 'Age');
4545
InstabugFlutter.setUserAttributeWithKey('female', 'gender');
4646
InstabugFlutter.removeUserAttributeForKey('gender');
47+
final String value = await InstabugFlutter.getUserAttributeForKey('Age');
48+
print('User Attribute ' + value);
49+
final Map<String, String> userAttributes = await InstabugFlutter.getUserAttributes();
50+
print(userAttributes.toString());
4751
InstabugFlutter.logUserEventWithName('Aly Event');
4852

4953
} on PlatformException {

ios/Classes/InstabugFlutterPlugin.m

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
4949

5050
/**
5151
* starts the SDK
52-
* @param {token} token The token that identifies the app
53-
* @param {invocationEvents} invocationEvents The events that invoke
52+
* @param token token The token that identifies the app
53+
* @param invocationEvents invocationEvents The events that invoke
5454
* the SDK's UI.
5555
*/
5656
+ (void)startWithToken:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray {
@@ -219,6 +219,23 @@ + (void) removeUserAttributeForKey:(NSString*) key {
219219
[Instabug removeUserAttributeForKey:key];
220220
}
221221

222+
/**
223+
* Returns the user attribute associated with a given key.
224+
* @param key The key for which to return the corresponding value.
225+
* @return The value associated with aKey, or nil if no value is associated with aKey.
226+
*/
227+
+ (NSString*) getUserAttributeForKey:(NSString*) key {
228+
return [Instabug userAttributeForKey:key];
229+
}
230+
231+
/**
232+
* Returns all user attributes.
233+
* @return A new dictionary containing all the currently set user attributes, or an empty dictionary if no user attributes have been set.
234+
*/
235+
+ (NSDictionary*) getUserAttributes {
236+
return Instabug.userAttributes;
237+
}
238+
222239
/**
223240
* invoke sdk manually
224241
*/

lib/instabug_flutter.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ class InstabugFlutter {
199199
await _channel.invokeMethod<Object>('removeUserAttributeForKey:', params);
200200
}
201201

202+
/// Returns the user attribute associated with a given [key].
203+
static Future<String> getUserAttributeForKey(String key) async {
204+
final List<dynamic> params = <dynamic>[key];
205+
return await _channel.invokeMethod<Object>('getUserAttributeForKey:', params);
206+
}
207+
208+
/// A new Map containing all the currently set user attributes, or an empty Map if no user attributes have been set.
209+
static Future<Map<String, String>> getUserAttributes() async {
210+
final Object userAttributes = await _channel.invokeMethod<Object>('getUserAttributes');
211+
return userAttributes != null ? Map<String, String>.from(userAttributes) : <String, String>{};
212+
}
213+
202214
/// invoke sdk manually
203215
static void show() async {
204216
await _channel.invokeMethod<Object>('show');

pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ dependencies:
1818
flutter:
1919
sdk: flutter
2020

21+
dev_dependencies:
22+
flutter_test:
23+
sdk: flutter
24+
2125
# For information on the generic Dart part of this file, see the
2226
# following page: https://www.dartlang.org/tools/pub/pubspec
2327

example/test/main.dart renamed to test/instabug_flutter.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ void main() {
88

99
final List<MethodCall> log = <MethodCall>[];
1010
final appToken = '068ba9a8c3615035e163dc5f829c73be';
11-
final invocationEvents = [InvocationEvent.floatingButton];
11+
final List<InvocationEvent> invocationEvents = <InvocationEvent>[InvocationEvent.floatingButton];
1212
final email = "[email protected]";
1313
final name = "santa";
1414
String message = "Test Message";
15+
const String userAttribute = '19';
16+
const Map<String, String> userAttributePair = <String, String>{'gender': 'female'};
1517

1618
setUpAll(() async {
1719
MethodChannel('instabug_flutter')
1820
.setMockMethodCallHandler((MethodCall methodCall) async {
1921
log.add(methodCall);
2022
switch (methodCall.method) {
2123
case 'getTags':
22-
return ['tag1', 'tag2'];
24+
return <String>['tag1', 'tag2'];
25+
case 'getUserAttributeForKey:':
26+
return userAttribute;
27+
case 'getUserAttributes':
28+
return userAttributePair;
2329
default:
2430
return null;
2531
}
@@ -123,7 +129,8 @@ test('startWithToken:invocationEvents: Test', () async {
123129
InstabugFlutter.clearAllLogs();
124130
expect(log, <Matcher>[
125131
isMethodCall('clearAllLogs',
126-
arguments: null)
132+
arguments: null
133+
)
127134
]);
128135
});
129136

@@ -211,6 +218,27 @@ test('startWithToken:invocationEvents: Test', () async {
211218
]);
212219
});
213220

221+
test('test getUserAttributeForKey should be called with a string argument and return a string', () async {
222+
const String key = 'Age';
223+
final String value = await InstabugFlutter.getUserAttributeForKey(key);
224+
expect(log, <Matcher>[
225+
isMethodCall('getUserAttributeForKey:',
226+
arguments: <dynamic>[key]
227+
)
228+
]);
229+
expect(value, userAttribute);
230+
});
231+
232+
test('test getuserAttributes should be called with no arguments and returns a Map', () async {
233+
final Map<String, String> result = await InstabugFlutter.getUserAttributes();
234+
expect(log, <Matcher>[
235+
isMethodCall('getUserAttributes',
236+
arguments: null
237+
)
238+
]);
239+
expect(result, userAttributePair);
240+
});
241+
214242
test('show Test', () async {
215243
InstabugFlutter.show();
216244
expect(log, <Matcher>[

0 commit comments

Comments
 (0)