-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy paththeme_manager.dart
197 lines (174 loc) Β· 6.22 KB
/
theme_manager.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'dart:convert';
import 'dart:ui';
import 'package:byr_mobile_app/customizations/theme_controller.dart';
import 'package:byr_mobile_app/helper/helper.dart';
import 'package:byr_mobile_app/local_objects/local_storage.dart';
import 'package:byr_mobile_app/networking/http_request.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'theme.dart';
export 'theme.dart';
class BYRThemeManager {
static BYRThemeManager _singleton;
static BYRThemeManager instance() {
if (BYRThemeManager._singleton == null) {
_singleton = BYRThemeManager();
return _singleton;
} else {
return _singleton;
}
}
BYRTheme currentTheme;
Map<String, BYRTheme> themeMap;
void mapAll() {
themeMap = Map<String, BYRTheme>();
BYRTheme aLightTheme = BYRTheme.generate(
themeName: 'Light Theme',
themeDisplayName: 'π',
);
aLightTheme.fillBy(BYRTheme.originLightTheme);
themeMap[aLightTheme.themeName] = aLightTheme;
BYRTheme aDarkTheme = BYRTheme.generate(
themeName: 'Dark Theme',
themeDisplayName: 'π',
);
aDarkTheme.fillBy(BYRTheme.originDarkTheme);
themeMap[aDarkTheme.themeName] = aDarkTheme;
currentTheme = themeMap["Light Theme"];
}
Future<void> mapLocal() async {
Map<String, String> tss = LocalStorage.getLocalThemes();
if (tss != null) {
for (final ts in tss.entries) {
await importLocalTheme(ts.value, null, BYRTheme.originLightTheme);
}
}
await BYRThemeManager.instance().turnTheme(LocalStorage.getCurrentTheme());
}
Future<void> removeTheme(themeName) async {
if (themeName == currentTheme.themeName) {
BYRThemeManager.instance().setAutoSwitchDarkModeOn();
BYRThemeManager.instance().turnTheme('Light Theme');
}
Map<String, String> tss = LocalStorage.getLocalThemes();
if (tss[themeName] != null) {
final file = await Helper.getLocalFile(tss[themeName]);
file.delete();
tss.remove(themeName);
}
await LocalStorage.setLocalThemes(tss);
BYRThemeManager.instance().themeMap.remove(themeName);
}
Future<bool> importOnlineTheme(themeUrl, themeParams, baseTheme) {
return Request.httpGet(themeUrl, themeParams).then((response) async {
Map resultMap = jsonDecode(utf8.decode(response.bodyBytes));
BYRTheme anOnlineTheme = BYRTheme.generate(
themeName: themeUrl + resultMap['themeName'],
themeDisplayName: resultMap['themeDisplayName'],
);
BYRTheme jsonTheme = BYRTheme.inputJson(resultMap);
if (jsonTheme.themeName == 'Dark Theme' ||
jsonTheme.themeName == 'Light Theme' ||
jsonTheme.themeDisplayName == 'π' ||
jsonTheme.themeDisplayName == 'π') {
return null;
}
anOnlineTheme.fillBy(baseTheme);
anOnlineTheme.replaceBy(jsonTheme);
if (themeMap[anOnlineTheme.themeName] != null) {
await removeTheme(anOnlineTheme.themeName);
}
themeMap[anOnlineTheme.themeName] = anOnlineTheme;
resultMap["themeName"] = anOnlineTheme.themeName;
resultMap["themeDisplayName"] = anOnlineTheme.themeDisplayName;
return resultMap;
}).then((v) async {
if (v != null) {
String ts = "theme" + DateTime.now().millisecondsSinceEpoch.toString() + ".json";
Map<String, String> tss = LocalStorage.getLocalThemes();
if (tss[v["themeName"]] != null) {
final file = await Helper.getLocalFile(tss[v["themeName"]]);
file.delete();
}
tss[v["themeName"]] = ts;
await LocalStorage.setLocalThemes(tss);
return exportLocalTheme(ts, jsonEncode(v));
} else {
return false;
}
});
}
Future<String> readTheme(themeDir) async {
try {
final file = await Helper.getLocalFile(themeDir);
String contents = await file.readAsString();
return contents;
} catch (e) {
return "";
}
}
Future<bool> importLocalTheme(themeDir, themeParams, baseTheme) {
return readTheme(themeDir).then((jsonFile) {
if (jsonFile.length > 0) {
Map resultMap = jsonDecode(jsonFile);
BYRTheme anOnlineTheme = BYRTheme.generate(
themeName: resultMap['themeName'],
themeDisplayName: resultMap['themeDisplayName'],
);
BYRTheme jsonTheme = BYRTheme.inputJson(resultMap);
if (jsonTheme.themeName == 'Dark Theme' ||
jsonTheme.themeName == 'Light Theme' ||
jsonTheme.themeDisplayName == 'π' ||
jsonTheme.themeDisplayName == 'π') {
return false;
}
anOnlineTheme.fillBy(baseTheme);
anOnlineTheme.replaceBy(jsonTheme);
themeMap[anOnlineTheme.themeName] = anOnlineTheme;
return true;
} else {
return false;
}
});
}
Future<bool> exportLocalTheme(themeDir, content) async {
final file = await Helper.getLocalFile(themeDir);
return file.writeAsString(content).then((v) {
return true;
});
}
Future<void> autoSwitchDarkMode(Brightness brightness) async {
if (brightness == Brightness.light && currentTheme.themeName != "Light Theme") {
await turnTheme('Light Theme');
} else if (brightness == Brightness.dark && currentTheme.themeName != "Dark Theme") {
await turnTheme('Dark Theme');
}
setAutoSwitchDarkModeOn();
}
bool getIsAutoSwitchDarkModel() {
return LocalStorage.getIsAutoTheme() ?? false;
}
Future<void> setAutoSwitchDarkModeOn() async {
await LocalStorage.setIsAutoTheme(true);
}
Future<void> setAutoSwitchDarkModeOff() async {
await LocalStorage.setIsAutoTheme(false);
}
Future<void> turnTheme(String themeName) async {
if (themeMap[themeName] != null) {
currentTheme = themeMap[themeName];
await LocalStorage.setCurrentTheme(themeName);
ThemeController.turnTheme(themeMap[themeName]);
updateNonWidgetParts();
}
}
void updateNonWidgetParts() {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: currentTheme.statusBarColor,
systemNavigationBarColor: currentTheme.systemBottomNavigationBarColor,
systemNavigationBarIconBrightness: !currentTheme.isThemeDarkStyle ? Brightness.dark : Brightness.light,
),
);
}
}