fix: theming preference

This commit is contained in:
omkar 2025-01-31 20:27:38 +05:30
parent 2fab0e7745
commit 2a55da488c
2 changed files with 30 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import 'package:universal_platform/universal_platform.dart';
import 'package:window_manager/window_manager.dart';
import '../../pocketbase/service/pocketbase.service.dart';
import '../../theme/theme/app_theme.dart';
import '../../widgetter/plugin_base.dart';
import '../../widgetter/plugins/stremio/stremio_plugin.dart';
@ -17,7 +18,7 @@ Future startupApp() async {
MediaKit.ensureInitialized();
await AppPocketBaseService.ensureInitialized();
await AppTheme().ensureInitialized();
await SelectedProfileService.instance.initialize();
if (UniversalPlatform.isDesktop) {
@ -27,6 +28,7 @@ Future startupApp() async {
if (kDebugMode) {
PluginRegistry.instance.reset();
}
PluginRegistry.instance.registerPlugin(
StremioCatalogPlugin(),
);

View file

@ -1,10 +1,13 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../provider/theme_provider.dart';
import '../utils/color_utils.dart';
class AppTheme {
static final AppTheme _instance = AppTheme._internal();
static const String _primaryColorKey = 'primary_color';
static const String _isDarkModeKey = 'is_dark_mode';
factory AppTheme() {
return _instance;
@ -18,13 +21,33 @@ class AppTheme {
ThemeProvider get themeProvider => _themeProvider;
ColorUtils get colorUtils => _colorUtils;
void toggleTheme() => _themeProvider.toggleTheme();
Future<void> ensureInitialized() async {
final prefs = await SharedPreferences.getInstance();
void setPrimaryColor(Color color) => _themeProvider.setPrimaryColor(color);
final isDarkMode = prefs.getBool(_isDarkModeKey) ?? false;
if (isDarkMode) _themeProvider.toggleTheme();
void setPrimaryColorFromRGB(int r, int g, int b) {
Color color = _colorUtils.colorFromRGB(r, g, b);
final primaryColorValue = prefs.getInt(_primaryColorKey);
if (primaryColorValue != null) {
_themeProvider.setPrimaryColor(Color(primaryColorValue));
}
}
Future<void> toggleTheme() async {
_themeProvider.toggleTheme();
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_isDarkModeKey, _themeProvider.isDarkMode);
}
Future<void> setPrimaryColor(Color color) async {
_themeProvider.setPrimaryColor(color);
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_primaryColorKey, color.value);
}
Future<void> setPrimaryColorFromRGB(int r, int g, int b) async {
Color color = _colorUtils.colorFromRGB(r, g, b);
await setPrimaryColor(color);
}
ThemeData getCurrentTheme() => _themeProvider.getTheme();