aboutsummaryrefslogtreecommitdiff
path: root/lib/data
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2020-06-03 14:39:52 +0530
committerAkshay Nair <phenax5@gmail.com>2020-06-03 14:39:52 +0530
commit871706b606c77eeac13bfcea67c3ce1d71203d2c (patch)
tree82d3ca8b51cc001606f5412a20730a813b7d9e2d /lib/data
parentc745b90dfdc97a2e04582051b01fde83883d8e3e (diff)
downloaddaft-launcher-871706b606c77eeac13bfcea67c3ce1d71203d2c.tar.gz
daft-launcher-871706b606c77eeac13bfcea67c3ce1d71203d2c.zip
Adds shared preferences for config/theme storage + refactors a streamstate
Diffstat (limited to 'lib/data')
-rw-r--r--lib/data/config.dart51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/data/config.dart b/lib/data/config.dart
new file mode 100644
index 0000000..0dd33cd
--- /dev/null
+++ b/lib/data/config.dart
@@ -0,0 +1,51 @@
+import 'package:shared_preferences/shared_preferences.dart';
+import 'dart:async';
+
+String defaultTheme = 'dark';
+
+class Config {
+ String theme = defaultTheme;
+ Config({ this.theme }) {}
+
+ bool isDarkMode() {
+ return theme == 'dark';
+ }
+}
+
+StreamController<Config> config_$ = StreamController<Config>.broadcast();
+
+Future<Config> getConfig() async {
+ final prefs = await SharedPreferences.getInstance();
+
+ return Config(
+ theme: prefs.getString('theme'),
+ );
+}
+
+void initConfig() async {
+ Config c = await getConfig();
+ config_$.add(c);
+}
+
+Stream<Config> getConfig$() {
+ return config_$.stream;
+}
+
+void setConfig(Config config) async {
+ final prefs = await SharedPreferences.getInstance();
+
+ prefs.setString('theme', config.theme);
+
+ config_$.add(await getConfig());
+}
+
+void toggleTheme() async {
+ final config = await getConfig();
+
+ if (config.theme == 'dark') {
+ setConfig(Config(theme: 'light'));
+ } else {
+ setConfig(Config(theme: 'dark'));
+ }
+}
+