What are the changes?
3 minute read
Get instances of AtClient and other services.
- To initialize AtClient Instance call setCurrentAtSign method on
AtClientManager.getInstance().
setCurrentAtSignaccepts the following arguments: currentAtSign, namespace and the preferences.AtClientManager.getInstance().setCurrentAtSign('@alice', 'wavi', <preferences>);
The
AtClientMangerInstance has getteratClientwhich returns an instance ofAtClient.AtClient atClient = atClientManager.atClient;The
AtClientManagerinstance has a late initialized variablenotificationServicewhich is for accessing notification service methods.NotificationService notificationService = atClientManager.notificationService;The
AtClientManagerinstance has a late initialized variablesyncServicewhich is for invoking the sync.SyncService syncService = atClientManager.syncService;
Note: Above code should be executed every time when the @sign is switched to get the right instances representing the new @sign.
Sending and receiving notifications.
Get the
AtClientManagerinstance initially.Access the
notificationServicevariable using atClientManager instance.Listen to notifications via callback and no filter. Ideally you don’t want to do this.
/// AtClientManager instance. AtClientManager atClientManager = AtClientManager.getInstance(); /// NotificationService variable. NotificationService notificationService = atClientManager.notificationService; /// Listen to notifications. notificationService.subscribe().listen((notification) { _notificationCallback(notification); });Listen to notifications via callback and filter notification key by regex. You can also come up with regexes that match other types of keys. Ex: ‘wavi | buzz’ or alternatively multiple listeners can also be registered.
notificationService.subscribe(regex: '.wavi').listen((notification) { _notificationCallback(notification); });Send notification. Await variant.
/// AtKey AtKey phoneKey = AtKey() ..key = 'phone' ..sharedWith = '@bob🛠' ..sharedBy = '@alice🛠'; /// AtKey's Value (String) String atValue = '+1 100 200 300'; /// Update the value and capture the notification result. NotificationResult notificationResponse = await notificationService .notify(NotificationParams.forUpdate(phoneKey, value: atValue));Validating if a notification failed in the await variant.
if(notificationResponse.notificationStatusEnum == NotificationStatusEnum.undelivered) { // Do something on notification error. print(notificationResponse.atClientException); } else{ // Do something on successful delivery response. print('Successfully delivered notification.'); }The other notifications are as follows -
/// Delete notification NotificationResult notificationResponse = await notificationService.notify(NotificationParams.forDelete(phoneKey)); /// Text notify NotificationResult notificationResponse = await notificationService.notify(NotificationParams.forText('phone', '@bob🛠'));Send notification using Callback.
notificationService.notify( NotificationParams.forUpdate(atKey, value: atValue), onDone: _onSuccessCallback, onError: _onErrorCallback, ); void _onSuccessCallback(notificationResult){ // Do something on successful delivery response. print(notificationResult); } void _onErrorCallback(notificationResult){ // Do something on notification error print(notificationResponse.atClientException); }
Syncing the data.
The
atClientMangerinstance has getteratClientwhich returns an instance ofAtClient.final AtClient atClient = atClientManager.atClient;CRUD operations
atClient.put(<params>) atClient.delete(<params>)Syncing the data. Apps no longer have to use
SyncStrategyorisDedicatedflag or manually callsync. All sync requests will be internally kept in a queue and synced to the server at periodic time interval (approx. 15 seconds). If remote server is updated from some other device, then those changes will be also synced at periodic intervals.syncService = atClientManager.syncService;- Optionally, Register to onDone callbacks to get SyncResult when run asynchronously.
syncService.sync(onDone: _onSuccessCallback); void _onSuccessCallback(syncResult){ print(syncResult); }Optionally, call
setOnDonefor global onDone callback. Call this method to set the Global onDone callback. This method will be called when a sync is completed. When a specific onDone function is passed to the sync function, then the specific onDone is called.syncService.setOnDone(onDone: _onSuccessCallback);