Created
August 19, 2020 18:03
-
-
Save huxaiphaer/5bf040c222cc0d8568f1057902c2cfac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'package:streaming_shared_preferences/streaming_shared_preferences.dart'; | |
class LocalStorage { | |
static const String NOTIFICATIONS = "notifications"; | |
static const String LOAD_NOTIFICATION = "load_notification"; | |
static const String LOAD_NOTIFICATION_TYPE = "load_notification_type"; | |
static const String LOAD_NOTIFICATION_TYPE_TITLE = | |
"load_notification_type_title"; | |
static const String ENABLE_PUSH_NOTIFICATIONS = "enable_push_notifications"; | |
static const String ENABLE_EMAIL_NOTIFICATIONS = "enable_email_notifications"; | |
Future<SharedPreferences> _prefs = SharedPreferences.getInstance(); | |
Future<String> getNotifications() async { | |
final SharedPreferences prefs = await _prefs; | |
String notifications; | |
if (prefs.containsKey(NOTIFICATIONS)) { | |
notifications = prefs.getString(NOTIFICATIONS); | |
} | |
return notifications; | |
} | |
Future<bool> deleteNotification(int index) async { | |
final SharedPreferences prefs = await _prefs; | |
bool deleted = false; | |
if (prefs.containsKey(NOTIFICATIONS)) { | |
String data = prefs.getString(NOTIFICATIONS); | |
var notificationArr = jsonDecode(data); | |
notificationArr.removeAt(index); | |
prefs.setString(NOTIFICATIONS, jsonEncode(notificationArr)); | |
deleted = true; | |
} | |
return deleted; | |
} | |
Future<bool> deleteAllNotification() async { | |
final SharedPreferences prefs = await _prefs; | |
bool deleted = false; | |
if (prefs.containsKey(NOTIFICATIONS)) { | |
var notificationArr = []; | |
prefs.setString(NOTIFICATIONS, jsonEncode(notificationArr)); | |
deleted = true; | |
} | |
return deleted; | |
} | |
bool isNumeric(String s) { | |
if (s == null) { | |
return false; | |
} | |
return double.parse(s, (e) => null) != null; | |
} | |
Future<void> addNotification(var msg, bool loadMsg) async { | |
var newMsg = jsonDecode(jsonEncode(msg)); | |
final preferences = await StreamingSharedPreferences.instance; | |
if (loadMsg) { | |
print(msg); | |
var data; | |
var notificationType = newMsg['data']['notification_type']; | |
var notificationTitle = newMsg['data']['title']; | |
if(notificationType != null){ | |
if(notificationType == 'sports'){ | |
data = newMsg['data']['matchId']; | |
}else{ | |
data = newMsg['data']['article_id']; | |
} | |
}else { | |
notificationType = "article"; | |
data = newMsg['data']['article_id']; | |
} | |
if (isNumeric(data)) { | |
int articleId = int.parse(data); | |
preferences.setInt(LOAD_NOTIFICATION, articleId); | |
preferences.setString(LOAD_NOTIFICATION_TYPE, notificationType); | |
preferences.setString(LOAD_NOTIFICATION_TYPE_TITLE, notificationTitle); | |
} | |
newMsg['data']['isNew'] = false; | |
} else { | |
newMsg['data']['isNew'] = true; | |
} | |
String oldNotifications = await getNotifications(); | |
var newNotifications = []; | |
if (oldNotifications != null) { | |
newNotifications = jsonDecode(oldNotifications); | |
} | |
newNotifications.toSet().toList(); | |
newNotifications.add(jsonEncode(newMsg)); | |
preferences.setString(NOTIFICATIONS, jsonEncode(newNotifications)); | |
} | |
Future<void> setOpenedAllNotifications() async { | |
final preferences = await StreamingSharedPreferences.instance; | |
String oldNotifications = await getNotifications(); | |
var newNotifications = []; | |
newNotifications.toSet().toList(); | |
if (oldNotifications != null) { | |
var data = jsonDecode(oldNotifications); | |
data.forEach((val) { | |
var newMsg = jsonDecode(val); | |
newMsg['data']['isNew'] = false; | |
newNotifications.add(jsonEncode(newMsg)); | |
}); | |
} | |
preferences.setString(NOTIFICATIONS, jsonEncode(newNotifications)); | |
} | |
Future togglePushNotification(bool status) async { | |
final SharedPreferences prefs = await _prefs; | |
prefs.setBool(ENABLE_PUSH_NOTIFICATIONS, status); //turn on flag | |
} | |
Future getPushNotificationStatus() async { | |
final SharedPreferences prefs = await _prefs; | |
bool status = false; | |
if (!prefs.containsKey(ENABLE_PUSH_NOTIFICATIONS)) { | |
await togglePushNotification(true); | |
} | |
status = prefs.getBool(ENABLE_PUSH_NOTIFICATIONS); | |
return status; | |
} | |
Future toggleEmailNotification(bool status) async { | |
final SharedPreferences prefs = await _prefs; | |
prefs.setBool(ENABLE_EMAIL_NOTIFICATIONS, status); //turn on flag | |
} | |
Future getEmailNotificationStatus() async { | |
final SharedPreferences prefs = await _prefs; | |
bool status = false; | |
if (!prefs.containsKey(ENABLE_EMAIL_NOTIFICATIONS)) { | |
await toggleEmailNotification(true); | |
} | |
status = prefs.getBool(ENABLE_EMAIL_NOTIFICATIONS); | |
return status; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment