-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstorage_handler.dart
41 lines (33 loc) · 1.17 KB
/
storage_handler.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// ignore_for_file: avoid_print
import 'dart:ui';
import 'package:cssapp/state_handlers/user/user_model.dart';
import 'package:shared_preferences/shared_preferences.dart';
class StorageHandler {
static SharedPreferences? _preferences;
// ----------------------------- Getter Methods -----------------------------
bool isDarkTheme() {
return _preferences?.getBool('isDarkTheme') ??
PlatformDispatcher.instance.platformBrightness == Brightness.dark;
}
Future<UserModel?> loadUserFromLocalStorage() async {
final jsonString = _preferences?.getString('user');
if (jsonString != null) {
return UserModel.fromJson(jsonString);
} else {
return null;
}
}
// ----------------------------- Setter Methods -----------------------------
Future<void> toggleDarkTheme() async {
await _preferences?.setBool('isDarkTheme', !isDarkTheme());
}
Future<void> saveUserToLocalStorage(UserModel user) async {
await _preferences?.setString('user', user.toJson());
}
Future<void> clearAllData() async {
await _preferences?.clear();
}
Future<void> initPreferences() async {
_preferences = await SharedPreferences.getInstance();
}
}