Files
Finlytic/FinlyticApp/lib/core/theme/theme_cubit.dart
T

50 lines
1.4 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import '../network/api_client.dart';
import 'app_theme.dart';
class ThemeState {
final ThemePreset preset;
final bool isLoading;
const ThemeState({required this.preset, this.isLoading = false});
}
class ThemeCubit extends Cubit<ThemeState> {
final ApiClient? apiClient;
ThemeCubit({this.apiClient}) : super(ThemeState(preset: AppTheme.activePreset));
Future<void> fetchUserThemePreference() async {
if (apiClient == null) return;
try {
final res = await apiClient!.get('/api/v1/user/preferences');
if (res.statusCode == 200 && res.data is Map) {
final themeId = res.data['themePreference']?.toString();
if (themeId != null && themeId.isNotEmpty) {
final preset = AppTheme.getPresetById(themeId);
AppTheme.activePreset = preset;
emit(ThemeState(preset: preset));
}
}
} catch (_) {
// Keep active preset on network failure
}
}
Future<void> setTheme(ThemePreset newPreset) async {
AppTheme.activePreset = newPreset;
emit(ThemeState(preset: newPreset));
if (apiClient != null) {
try {
await apiClient!.put(
'/api/v1/user/preferences/theme',
data: {'themeId': newPreset.id},
);
} catch (_) {
// Silently handle offline preference update
}
}
}
}