mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-05-02 14:26:12 -04:00
refactor some more
This commit is contained in:
parent
2adc958128
commit
c516323e7d
91 changed files with 1237 additions and 748 deletions
|
@ -1,108 +0,0 @@
|
|||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../entities/preferences.dart';
|
||||
import 'radix_generator.dart';
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class ThemeService {
|
||||
ThemeService._();
|
||||
static late SharedPreferences prefs;
|
||||
static ThemeService? _instance;
|
||||
|
||||
static Future<ThemeService> get instance async {
|
||||
if (_instance == null) {
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
_instance = ThemeService._();
|
||||
}
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
static bool get isPlatformDark =>
|
||||
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
|
||||
Brightness.dark;
|
||||
|
||||
ThemeData get initial {
|
||||
final themePreferences = load();
|
||||
return get(themePreferences);
|
||||
}
|
||||
|
||||
ThemePreferences load() {
|
||||
final themePreferencesJson = prefs.getString('themePreferences');
|
||||
ThemePreferences? themePreferences;
|
||||
if (themePreferencesJson != null) {
|
||||
try {
|
||||
themePreferences =
|
||||
ThemePreferences.fromJson(jsonDecode(themePreferencesJson));
|
||||
// ignore: avoid_catches_without_on_clauses
|
||||
} catch (_) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return themePreferences ??
|
||||
const ThemePreferences(
|
||||
colorPreference: ColorPreference.vapor,
|
||||
brightnessPreference: BrightnessPreference.system,
|
||||
displayScale: 1,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> save(ThemePreferences themePreferences) async {
|
||||
await prefs.setString(
|
||||
'themePreferences', jsonEncode(themePreferences.toJson()));
|
||||
}
|
||||
|
||||
ThemeData get(ThemePreferences themePreferences) {
|
||||
late final Brightness brightness;
|
||||
switch (themePreferences.brightnessPreference) {
|
||||
case BrightnessPreference.system:
|
||||
if (isPlatformDark) {
|
||||
brightness = Brightness.dark;
|
||||
} else {
|
||||
brightness = Brightness.light;
|
||||
}
|
||||
case BrightnessPreference.light:
|
||||
brightness = Brightness.light;
|
||||
case BrightnessPreference.dark:
|
||||
brightness = Brightness.dark;
|
||||
}
|
||||
|
||||
late final ThemeData themeData;
|
||||
switch (themePreferences.colorPreference) {
|
||||
// Special cases
|
||||
case ColorPreference.contrast:
|
||||
// xxx do contrastGenerator
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.grim);
|
||||
// Generate from Radix
|
||||
case ColorPreference.scarlet:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.scarlet);
|
||||
case ColorPreference.babydoll:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.babydoll);
|
||||
case ColorPreference.vapor:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.vapor);
|
||||
case ColorPreference.gold:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.gold);
|
||||
case ColorPreference.garden:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.garden);
|
||||
case ColorPreference.forest:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.forest);
|
||||
case ColorPreference.arctic:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.arctic);
|
||||
case ColorPreference.lapis:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.lapis);
|
||||
case ColorPreference.eggplant:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.eggplant);
|
||||
case ColorPreference.lime:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.lime);
|
||||
case ColorPreference.grim:
|
||||
themeData = radixGenerator(brightness, RadixThemeColor.grim);
|
||||
}
|
||||
|
||||
return themeData;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue