2019-10-13 10:01:29 +07:00
|
|
|
import 'package:fimber/fimber.dart';
|
2022-09-17 20:35:45 +08:00
|
|
|
import 'package:flutter/widgets.dart';
|
2019-09-15 17:36:09 +08:00
|
|
|
import 'package:flutter_highlight/theme_map.dart';
|
2019-09-15 18:27:46 +08:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2021-07-20 14:33:34 +08:00
|
|
|
import 'package:google_fonts/google_fonts.dart';
|
2019-09-15 17:36:09 +08:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
class CodeModel with ChangeNotifier {
|
|
|
|
static var themes = themeMap.keys.toList();
|
|
|
|
static const fontSizes = [12, 13, 14, 15, 16, 17, 18, 19, 20];
|
2019-09-15 18:27:46 +08:00
|
|
|
static const fontFamilies = [
|
|
|
|
'System',
|
2020-01-27 14:57:30 +08:00
|
|
|
'JetBrains Mono',
|
2019-09-15 18:27:46 +08:00
|
|
|
'Fira Code',
|
|
|
|
'Inconsolata',
|
|
|
|
'PT Mono',
|
|
|
|
'Source Code Pro',
|
2020-01-19 21:43:09 +08:00
|
|
|
'Ubuntu Mono',
|
2021-07-20 14:33:34 +08:00
|
|
|
// 'Cascadia Code', // TODO: https://github.com/google/fonts/issues/2179
|
2019-09-15 18:27:46 +08:00
|
|
|
];
|
2019-09-15 17:36:09 +08:00
|
|
|
|
2021-02-14 22:17:22 +08:00
|
|
|
String _theme = 'vs';
|
|
|
|
String _themeDark = 'vs2015';
|
2020-01-12 17:13:48 +08:00
|
|
|
int _fontSize = 14;
|
2020-01-27 14:57:30 +08:00
|
|
|
String _fontFamily = 'JetBrains Mono';
|
2019-09-15 17:36:09 +08:00
|
|
|
|
|
|
|
String get theme => _theme;
|
2020-01-12 17:13:48 +08:00
|
|
|
String get themeDark => _themeDark;
|
2021-07-20 14:33:34 +08:00
|
|
|
|
2019-09-15 17:36:09 +08:00
|
|
|
int get fontSize => _fontSize;
|
|
|
|
String get fontFamily => _fontFamily;
|
2021-07-20 14:33:34 +08:00
|
|
|
TextStyle get fontStyle {
|
|
|
|
if (_fontFamily == 'System') {
|
|
|
|
return TextStyle(
|
|
|
|
fontFamily: CommonStyle.monospace, fontSize: fontSize.toDouble());
|
|
|
|
} else {
|
|
|
|
return GoogleFonts.getFont(_fontFamily, fontSize: fontSize.toDouble());
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 17:36:09 +08:00
|
|
|
|
2019-11-05 15:09:54 +08:00
|
|
|
Future<void> init() async {
|
2019-09-15 17:36:09 +08:00
|
|
|
var prefs = await SharedPreferences.getInstance();
|
2020-01-16 12:52:47 +08:00
|
|
|
var vh = prefs.getString(StorageKeys.codeTheme);
|
|
|
|
var vdh = prefs.getString(StorageKeys.codeThemeDark);
|
|
|
|
var vs = prefs.getInt(StorageKeys.iCodeFontSize);
|
|
|
|
var vf = prefs.getString(StorageKeys.codeFontFamily);
|
2019-09-15 17:36:09 +08:00
|
|
|
|
2019-10-13 10:01:29 +07:00
|
|
|
Fimber.d('read code: $vh, $vs, $vf');
|
2019-09-15 17:36:09 +08:00
|
|
|
if (themeMap.keys.contains(vh)) {
|
2021-05-16 15:16:35 +08:00
|
|
|
_theme = vh!;
|
2019-09-15 17:36:09 +08:00
|
|
|
}
|
2020-01-12 17:13:48 +08:00
|
|
|
if (themeMap.keys.contains(vdh)) {
|
2021-05-16 15:16:35 +08:00
|
|
|
_themeDark = vdh!;
|
2020-01-12 17:13:48 +08:00
|
|
|
}
|
2019-09-15 17:36:09 +08:00
|
|
|
if (fontSizes.contains(vs)) {
|
2021-05-16 15:16:35 +08:00
|
|
|
_fontSize = vs!;
|
2019-09-15 17:36:09 +08:00
|
|
|
}
|
|
|
|
if (fontFamilies.contains(vf)) {
|
2021-05-16 15:16:35 +08:00
|
|
|
_fontFamily = vf!;
|
2019-09-15 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTheme(String v) async {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2020-01-16 12:52:47 +08:00
|
|
|
await prefs.setString(StorageKeys.codeTheme, v);
|
2019-10-13 10:01:29 +07:00
|
|
|
Fimber.d('write code theme: $v');
|
2019-09-15 17:36:09 +08:00
|
|
|
|
|
|
|
_theme = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:13:48 +08:00
|
|
|
setThemeDark(String v) async {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2020-01-16 12:52:47 +08:00
|
|
|
await prefs.setString(StorageKeys.codeThemeDark, v);
|
2020-01-12 17:13:48 +08:00
|
|
|
Fimber.d('write code theme dark: $v');
|
|
|
|
|
|
|
|
_themeDark = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2019-09-15 17:36:09 +08:00
|
|
|
setFontSize(int v) async {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2020-01-16 12:52:47 +08:00
|
|
|
await prefs.setInt(StorageKeys.iCodeFontSize, v);
|
2019-10-13 10:01:29 +07:00
|
|
|
Fimber.d('write code font size: $v');
|
2019-09-15 17:36:09 +08:00
|
|
|
|
|
|
|
_fontSize = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setFontFamily(String v) async {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2020-01-16 12:52:47 +08:00
|
|
|
await prefs.setString(StorageKeys.codeFontFamily, v);
|
2019-10-13 10:01:29 +07:00
|
|
|
Fimber.d('write code font family: $v');
|
2019-09-15 17:36:09 +08:00
|
|
|
|
|
|
|
_fontFamily = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|