git-touch-android-ios-app/lib/models/code.dart

106 lines
2.8 KiB
Dart
Raw Normal View History

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