mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-10-23 06:53:23 +00:00
89 lines
2.8 KiB
Dart
89 lines
2.8 KiB
Dart
|
import 'package:comunic/helpers/preferences_helper.dart';
|
||
|
import 'package:comunic/models/api_request.dart';
|
||
|
|
||
|
/// Push notifications helper
|
||
|
///
|
||
|
/// @author Pierre Hubert
|
||
|
|
||
|
enum PushNotificationsStatus { UNDEFINED, DISABLED, FIREBASE, INDEPENDENT }
|
||
|
|
||
|
const _PushNotificationsAPIMap = {
|
||
|
"undefined": PushNotificationsStatus.UNDEFINED,
|
||
|
"disabled": PushNotificationsStatus.DISABLED,
|
||
|
"firebase": PushNotificationsStatus.FIREBASE,
|
||
|
"independent": PushNotificationsStatus.INDEPENDENT
|
||
|
};
|
||
|
|
||
|
class PushNotificationsHelper {
|
||
|
/// Get cached status of push notifications
|
||
|
static Future<PushNotificationsStatus> getLocalStatus() async {
|
||
|
final pref = await PreferencesHelper.getInstance();
|
||
|
|
||
|
if (!pref.containsKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS))
|
||
|
return PushNotificationsStatus.UNDEFINED;
|
||
|
|
||
|
return _PushNotificationsAPIMap[
|
||
|
pref.getString(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS)];
|
||
|
}
|
||
|
|
||
|
/// Refresh local status with information from server
|
||
|
///
|
||
|
/// Throws in case of failure
|
||
|
static Future<void> refreshLocalStatus() async {
|
||
|
final pref = await PreferencesHelper.getInstance();
|
||
|
|
||
|
final response = await APIRequest.withLogin("push_notifications/status")
|
||
|
.execWithThrowGetObject();
|
||
|
|
||
|
switch (response["status"]) {
|
||
|
case "undefined":
|
||
|
await pref.removeKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS);
|
||
|
break;
|
||
|
|
||
|
case "disabled":
|
||
|
await pref.setString(
|
||
|
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "disabled");
|
||
|
break;
|
||
|
|
||
|
case "firebase":
|
||
|
await pref.setString(
|
||
|
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "firebase");
|
||
|
break;
|
||
|
|
||
|
case "independent":
|
||
|
await pref.setString(
|
||
|
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "independent");
|
||
|
// TODO : Invoke plugin to apply new WebSocket URL
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
print(
|
||
|
"Push notifications status ${response["status"]} is unknown, defaulting to disabled!");
|
||
|
await pref.setString(
|
||
|
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "disabled");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Clear local push notifications status
|
||
|
static Future<void> clearLocalStatus() async {
|
||
|
await (await PreferencesHelper.getInstance())
|
||
|
.removeKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS);
|
||
|
|
||
|
// TODO : stop local refresh notification refresh
|
||
|
}
|
||
|
|
||
|
/// Set new push notification status on the server
|
||
|
static Future<void> setNewStatus(
|
||
|
PushNotificationsStatus newStatus, {
|
||
|
String firebaseToken = "",
|
||
|
}) async =>
|
||
|
await APIRequest.withLogin("push_notifications/configure")
|
||
|
.addString(
|
||
|
"status",
|
||
|
_PushNotificationsAPIMap.entries
|
||
|
.firstWhere((e) => e.value == newStatus)
|
||
|
.key)
|
||
|
.addString("firebase_token", firebaseToken)
|
||
|
.execWithThrow();
|
||
|
}
|