marigold_mobile_flutter_sdk 0.0.3
marigold_mobile_flutter_sdk: ^0.0.3 copied to clipboard
A wrapper for the Marigold mobile SDKs using Flutter
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:marigold_mobile_flutter_sdk/marigold.dart';
import 'package:marigold_mobile_flutter_sdk/message_stream.dart';
import 'package:marigold_mobile_flutter_sdk/sailthru.dart';
import 'package:marigold_mobile_flutter_sdk/model/attributes.dart';
import 'package:marigold_mobile_flutter_sdk/model/message.dart';
import 'package:marigold_mobile_flutter_sdk/model/purchase.dart';
import 'dart:convert';
void main() => runApp(new MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
bool geoIpEnabled = true;
bool inAppNotificationsEnabled = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Marigold Flutter SDK Example App'),
),
body: SingleChildScrollView(
child: Column(
verticalDirection: VerticalDirection.down,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: TextButton(
onPressed: () {
getDeviceId(context);
},
child: Text('Show Device ID'))),
Center(
child: TextButton(
onPressed: () {
updateLocation(context);
},
child: Text('Update Location'))),
Center(
child: TextButton(
onPressed: () {
toggleGeoIpLocation(context);
},
child: Text('Toggle Geo IP Location'))),
Center(
child: TextButton(
onPressed: () {
toggleInAppNotificationsEnabled(context);
},
child: Text('Toggle In-App Notifications'))),
Center(
child: TextButton(
onPressed: () {
requestNotificationPermissions(context);
},
child: Text('Request Notification Permissions'))),
Center(
child: TextButton(
onPressed: () {
syncNotificationSettings(context);
},
child: Text('Sync Notification Settings'))),
Center(
child: TextButton(
onPressed: () {
showLatestMessage(context);
},
child: Text('Show Latest Message'))),
Center(
child: TextButton(
onPressed: () {
registerMessageImpression(context);
},
child: Text('Register Message Impression'))),
Center(
child: TextButton(
onPressed: () {
removeMessage(context);
},
child: Text('Remove Message'))),
Center(
child: TextButton(
onPressed: () {
clearMessages(context);
},
child: Text('Clear Messages'))),
Center(
child: TextButton(
onPressed: () {
getUnreadCount(context);
},
child: Text('Get Unread Message Count'))),
Center(
child: TextField(
onSubmitted: (String value) async {
setUserId(context, value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set User ID',
)),
),
Center(
child: TextField(
onSubmitted: (String value) async {
setUserEmail(context, value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set User Email',
)),
),
Center(
child: TextButton(
onPressed: () {
setAttributes(context);
},
child: Text('Set Attributes'))),
Center(
child: TextField(
onSubmitted: (String value) async {
removeAttribute(context, value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Remove Attribute',
)),
),
Center(
child: TextButton(
onPressed: () {
clearAttributes(context);
},
child: Text('Clear Attributes'))),
Center(
child: TextField(
onSubmitted: (String value) async {
logEvent(context, value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Log Event',
)),
),
Center(
child: TextButton(
onPressed: () {
clearEvents(context);
},
child: Text('Clear Events'))),
Center(
child: TextButton(
onPressed: () {
getProfileVars(context);
}, child: Text('Get Profile Vars'))),
Center(
child: TextField(
onSubmitted: (String value) async {
setProfileVars(context, value);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set Profile Vars',
)),
),
Center(
child: TextButton(
onPressed: () {
logPurchase(context);
}, child: Text('Log Purchase'))),
Center(
child: TextButton(
onPressed: () {
logAbandonedCart(context);
}, child: Text('Log Abandoned Cart'))),
],
),
));
}
void showDialog(BuildContext buildContext, String message) {
showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation<double> animation,
Animation<double> secondaryAnimation) {
return Wrap(
children: [Text(message)],
);
},
barrierDismissible: true,
barrierLabel:
MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: Colors.black,
transitionDuration: const Duration(milliseconds: 200));
}
void getDeviceId(BuildContext context) async {
try {
String deviceId = await Marigold.getDeviceID() ?? "null";
showDialog(context, deviceId);
} on Exception catch (e) {
showDialog(context, 'Error syncing: $e');
}
}
void updateLocation(BuildContext context) async {
try {
await Marigold.updateLocation(55.9533, -3.1883);
} on Exception catch (e) {
showDialog(context, 'Error setting location: $e');
}
}
void toggleGeoIpLocation(BuildContext context) async {
try {
geoIpEnabled = !geoIpEnabled;
await Marigold.setGeoIPTrackingEnabled(geoIpEnabled);
} on Exception catch (e) {
showDialog(context, 'Error setting geo IP enabled: $e');
}
}
void toggleInAppNotificationsEnabled(BuildContext context) async {
try {
inAppNotificationsEnabled = !inAppNotificationsEnabled;
await Marigold.setInAppNotificationsEnabled(inAppNotificationsEnabled);
} on Exception catch (e) {
showDialog(context, 'Error setting in-app notifications enabled: $e');
}
}
void requestNotificationPermissions(BuildContext context) async {
try {
await Marigold.registerForPushNotifications();
} on Exception catch (e) {
showDialog(context, 'Error registering: $e');
}
}
void syncNotificationSettings(BuildContext context) async {
try {
await Marigold.syncNotificationSettings();
} on Exception catch (e) {
showDialog(context, 'Error syncing: $e');
}
}
void showLatestMessage(BuildContext context) async {
try {
Message message = await getLatestMessage(context);
await MessageStream.presentMessageDetail(message);
} on Exception catch (e) {
showDialog(context, 'Error displaying message: $e');
}
}
void registerMessageImpression(BuildContext context) async {
try {
Message message = await getLatestMessage(context);
await MessageStream.registerMessageImpression(message, 1);
} on Exception catch (e) {
showDialog(context, 'Error registering message impression: $e');
}
}
void removeMessage(BuildContext context) async {
try {
Message message = await getLatestMessage(context);
await MessageStream.removeMessage(message);
} on Exception catch (e) {
showDialog(context, 'Error removing message: $e');
}
}
void clearMessages(BuildContext context) async {
try {
await MessageStream.clearMessages();
} on Exception catch (e) {
showDialog(context, 'Error clearing messages: $e');
}
}
void getUnreadCount(BuildContext context) async {
try {
int unreadCount = await MessageStream.getUnreadCount();
showDialog(context, 'Unread message count: $unreadCount');
} on Exception catch (e) {
showDialog(context, 'Error clearing messages: $e');
}
}
void setUserId(BuildContext buildContext, String userId) async {
try {
await Sailthru.setUserId(userId);
} on Exception catch (e) {
showDialog(context, 'Error setting attributes: $e');
}
}
void setUserEmail(BuildContext buildContext, String userEmail) async {
try {
await Sailthru.setUserEmail(userEmail);
} on Exception catch (e) {
showDialog(context, 'Error setting attributes: $e');
}
}
void setAttributes(BuildContext context) async {
Attributes attributes = Attributes();
attributes.setString('stringKey', 'hi');
attributes.setStringArray('stringArrayKey', ['hi', 'there', 'buddy']);
attributes.setInteger('intKey', 123);
attributes.setIntegerArray('intArrayKey', [1, 2, 3]);
attributes.setBoolean('boolKey', true);
attributes.setFloat('floatKey', 1.23);
attributes.setFloatArray('floatArrayKey', [1.23, 2.34, 3.45]);
attributes.setDate('dateKey', DateTime.now());
attributes.setDateArray('dateArrayKey', [
DateTime.now(),
DateTime.now().add(Duration(days: 1)),
DateTime.now().add(Duration(days: 2, hours: 1))
]);
try {
await Sailthru.setAttributes(attributes);
} on Exception catch (e) {
showDialog(context, 'Error setting attributes: $e');
}
}
void removeAttribute(BuildContext context, String key) async {
try {
await Sailthru.removeAttribute(key);
} on Exception catch (e) {
showDialog(context, 'Error removing attribute: $e');
}
}
void clearAttributes(BuildContext context) async {
try {
await Sailthru.clearAttributes();
} on Exception catch (e) {
showDialog(context, 'Error clearing attributes: $e');
}
}
void logEvent(BuildContext context, String eventName) async {
final Map<String, dynamic> vars = <String, dynamic>{'test_var': 'yo'};
try {
await Sailthru.logEvent(eventName, vars);
} on Exception catch (e) {
showDialog(context, 'Error logging event: $e');
}
}
void clearEvents(BuildContext context) async {
try {
await Sailthru.clearEvents();
} on Exception catch (e) {
showDialog(context, 'Error clearing event: $e');
}
;
}
void getProfileVars(BuildContext context) async {
try {
Map<String, dynamic>? vars = await Sailthru.getProfileVars();
showDialog(context, '$vars');
} on Exception catch (e) {
showDialog(context, 'Error getting profile vars: $e');
}
}
void setProfileVars(BuildContext context, String varsString) async {
try {
Map<String, dynamic> vars = json.decode(varsString) as Map<String, dynamic>;
await Sailthru.setProfileVars(vars.cast());
} on Exception catch (e) {
showDialog(context, 'Error setting profile vars: $e');
}
}
void logPurchase(BuildContext context) async {
try {
Purchase purchase = createPurchase();
await Sailthru.logPurchase(purchase);
} on Exception catch (e) {
showDialog(context, 'Error logging purchase: $e');
}
}
void logAbandonedCart(BuildContext context) async {
try {
Purchase purchase = createPurchase();
await Sailthru.logAbandonedCart(purchase);
} on Exception catch (e) {
showDialog(context, 'Error logging abandoned cart: $e');
}
}
// HELPERS
Future<Message> getLatestMessage(BuildContext context) async {
List<Message> messages = await MessageStream.getMessages();
if (messages.isNotEmpty) {
return messages.elementAt(0);
} else {
throw Exception("Messages Empty");
}
}
Purchase createPurchase() {
PurchaseItem purchaseItem = PurchaseItem(2, 'item name', 1234, '23456', 'https://www.sailthru.com/not-real');
purchaseItem.vars = {'item':'var'};
PurchaseAdjustment purchaseAdjustment = PurchaseAdjustment('tax', 123);
Purchase purchase = Purchase([purchaseItem]);
purchase.purchaseAdjustments = [purchaseAdjustment];
purchase.vars = {'test':'me'};
return purchase;
}
}