marigold_mobile_flutter_sdk 1.0.1
marigold_mobile_flutter_sdk: ^1.0.1 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_mobile_flutter_sdk.dart';
import 'dart:convert';
void main() => runApp(MaterialApp(home: const MyApp()));
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> 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();
},
child: const Text('Show Device ID'))),
Center(
child: TextButton(
onPressed: () {
updateLocation();
},
child: const Text('Update Location'))),
Center(
child: TextButton(
onPressed: () {
toggleGeoIpLocation();
},
child: const Text('Toggle Geo IP Location'))),
Center(
child: TextButton(
onPressed: () {
toggleInAppNotificationsEnabled();
},
child: const Text('Toggle In-App Notifications'))),
Center(
child: TextButton(
onPressed: () {
requestNotificationPermissions();
},
child: const Text('Request Notification Permissions'))),
Center(
child: TextButton(
onPressed: () {
syncNotificationSettings();
},
child: const Text('Sync Notification Settings'))),
Center(
child: TextButton(
onPressed: () {
showLatestMessage();
},
child: const Text('Show Latest Message'))),
Center(
child: TextButton(
onPressed: () {
registerMessageImpression();
},
child: const Text('Register Message Impression'))),
Center(
child: TextButton(
onPressed: () {
removeMessage();
},
child: const Text('Remove Message'))),
Center(
child: TextButton(
onPressed: () {
clearMessages();
},
child: const Text('Clear Messages'))),
Center(
child: TextButton(
onPressed: () {
getUnreadCount();
},
child: const Text('Get Unread Message Count'))),
Center(
child: TextField(
onSubmitted: (String value) async {
setUserId(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set User ID',
)),
),
Center(
child: TextField(
onSubmitted: (String value) async {
setUserEmail(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set User Email',
)),
),
Center(
child: TextButton(
onPressed: () {
setAttributes();
},
child: const Text('Set Attributes'))),
Center(
child: TextField(
onSubmitted: (String value) async {
removeAttribute(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Remove Attribute',
)),
),
Center(
child: TextButton(
onPressed: () {
clearAttributes();
},
child: const Text('Clear Attributes'))),
Center(
child: TextField(
onSubmitted: (String value) async {
logEvent(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Log Event',
)),
),
Center(
child: TextButton(
onPressed: () {
clearEvents();
},
child: const Text('Clear Events'))),
Center(
child: TextButton(
onPressed: () {
getProfileVars();
},
child: const Text('Get Profile Vars'))),
Center(
child: TextField(
onSubmitted: (String value) async {
setProfileVars(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Set Profile Vars',
)),
),
Center(
child: TextButton(
onPressed: () {
logPurchase();
},
child: const Text('Log Purchase'))),
Center(
child: TextButton(
onPressed: () {
logAbandonedCart();
},
child: const Text('Log Abandoned Cart'))),
Center(
child: TextField(
onSubmitted: (String value) async {
logRegistrationEvent(value);
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Login Registration Event',
)),
),
Center(
child: TextButton(
onPressed: () {
logRegistrationEvent(null);
},
child: const Text('Logout Registration Event'))),
],
),
));
}
void showDialog(String message) {
if (!context.mounted) {
return;
}
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() async {
try {
String deviceId = await Marigold.getDeviceID() ?? "null";
showDialog(deviceId);
} on Exception catch (e) {
showDialog('Error syncing: $e');
}
}
void updateLocation() async {
try {
await Marigold.updateLocation(55.9533, -3.1883);
} on Exception catch (e) {
showDialog('Error setting location: $e');
}
}
void toggleGeoIpLocation() async {
try {
geoIpEnabled = !geoIpEnabled;
await Marigold.setGeoIPTrackingEnabled(geoIpEnabled);
} on Exception catch (e) {
showDialog('Error setting geo IP enabled: $e');
}
}
void toggleInAppNotificationsEnabled() async {
try {
inAppNotificationsEnabled = !inAppNotificationsEnabled;
await Marigold.setInAppNotificationsEnabled(inAppNotificationsEnabled);
} on Exception catch (e) {
showDialog('Error setting in-app notifications enabled: $e');
}
}
void requestNotificationPermissions() async {
try {
await Marigold.registerForPushNotifications();
} on Exception catch (e) {
showDialog('Error registering: $e');
}
}
void syncNotificationSettings() async {
try {
await Marigold.syncNotificationSettings();
} on Exception catch (e) {
showDialog('Error syncing: $e');
}
}
void showLatestMessage() async {
try {
Message message = await getLatestMessage();
await MessageStream.presentMessageDetail(message);
} on Exception catch (e) {
showDialog('Error displaying message: $e');
}
}
void registerMessageImpression() async {
try {
Message message = await getLatestMessage();
await MessageStream.registerMessageImpression(message, 1);
} on Exception catch (e) {
showDialog('Error registering message impression: $e');
}
}
void removeMessage() async {
try {
Message message = await getLatestMessage();
await MessageStream.removeMessage(message);
} on Exception catch (e) {
showDialog('Error removing message: $e');
}
}
void clearMessages() async {
try {
await MessageStream.clearMessages();
} on Exception catch (e) {
showDialog('Error clearing messages: $e');
}
}
void getUnreadCount() async {
try {
int unreadCount = await MessageStream.getUnreadCount();
showDialog('Unread message count: $unreadCount');
} on Exception catch (e) {
showDialog('Error clearing messages: $e');
}
}
void setUserId(String userId) async {
try {
await Sailthru.setUserId(userId);
} on Exception catch (e) {
showDialog('Error setting attributes: $e');
}
}
void setUserEmail(String userEmail) async {
try {
await Sailthru.setUserEmail(userEmail);
} on Exception catch (e) {
showDialog('Error setting attributes: $e');
}
}
void setAttributes() 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(const Duration(days: 1)),
DateTime.now().add(const Duration(days: 2, hours: 1))
]);
try {
await Sailthru.setAttributes(attributes);
} on Exception catch (e) {
showDialog('Error setting attributes: $e');
}
}
void removeAttribute(String key) async {
try {
await Sailthru.removeAttribute(key);
} on Exception catch (e) {
showDialog('Error removing attribute: $e');
}
}
void clearAttributes() async {
try {
await Sailthru.clearAttributes();
} on Exception catch (e) {
showDialog('Error clearing attributes: $e');
}
}
void logEvent(String eventName) async {
final Map<String, dynamic> vars = <String, dynamic>{'test_var': 'yo'};
try {
await Sailthru.logEvent(eventName, vars);
} on Exception catch (e) {
showDialog('Error logging event: $e');
}
}
void clearEvents() async {
try {
await Sailthru.clearEvents();
} on Exception catch (e) {
showDialog('Error clearing event: $e');
}
}
void getProfileVars() async {
try {
Map<String, dynamic>? vars = await Sailthru.getProfileVars();
showDialog('$vars');
} on Exception catch (e) {
showDialog('Error getting profile vars: $e');
}
}
void setProfileVars(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('Error setting profile vars: $e');
}
}
void logPurchase() async {
try {
Purchase purchase = createPurchase();
await Sailthru.logPurchase(purchase);
} on Exception catch (e) {
showDialog('Error logging purchase: $e');
}
}
void logAbandonedCart() async {
try {
Purchase purchase = createPurchase();
await Sailthru.logAbandonedCart(purchase);
} on Exception catch (e) {
showDialog('Error logging abandoned cart: $e');
}
}
void logRegistrationEvent(String? userId) async {
try {
await Cheetah.logRegistrationEvent(userId);
} on Exception catch (e) {
showDialog('Error logging registration event: $e');
}
}
// HELPERS
Future<Message> getLatestMessage() 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;
}
}