|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_chatgpt_text_and_image_processing/configs/theme/app_theme.dart'; |
| 5 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 6 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 7 | +// import 'package:shared_preferences.dart'; |
| 8 | +import '../models/theme_settings.dart'; |
| 9 | + |
| 10 | +final themeSettingsProvider = StateNotifierProvider<ThemeSettingsNotifier, ThemeSettings>((ref) { |
| 11 | + return ThemeSettingsNotifier(); |
| 12 | +}); |
| 13 | + |
| 14 | +class ThemeSettingsNotifier extends StateNotifier<ThemeSettings> { |
| 15 | + static const String _storageKey = 'themeSettings'; |
| 16 | + |
| 17 | + ThemeSettingsNotifier() : super(_defaultSettings) { |
| 18 | + _initializeSettings(); |
| 19 | + } |
| 20 | + |
| 21 | + static const _defaultSettings = ThemeSettings( |
| 22 | + backgroundImage: 'assets/themeImages/aiBackground.jpeg', |
| 23 | + primaryColor: AppTheme.primaryColor, |
| 24 | + secondaryColor: Colors.teal, |
| 25 | + systemBubbleColor: Color(0xFF444654), |
| 26 | + userBubbleColor: Color(0xFF343541), |
| 27 | + ); |
| 28 | + |
| 29 | + Future<void> _initializeSettings() async { |
| 30 | + try { |
| 31 | + final prefs = await SharedPreferences.getInstance(); |
| 32 | + final settingsJson = prefs.getString(_storageKey); |
| 33 | + |
| 34 | + if (settingsJson != null) { |
| 35 | + final settings = ThemeSettings.fromJson(json.decode(settingsJson)); |
| 36 | + state = settings; |
| 37 | + } else { |
| 38 | + // Save default settings if none exist |
| 39 | + await updateSettings(_defaultSettings); |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + // Fallback to default settings on error |
| 43 | + state = _defaultSettings; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + Future<void> updateSettings(ThemeSettings settings) async { |
| 48 | + try { |
| 49 | + final prefs = await SharedPreferences.getInstance(); |
| 50 | + await prefs.setString(_storageKey, json.encode(settings.toJson())); |
| 51 | + state = settings; |
| 52 | + } catch (e) { |
| 53 | + // Handle error (could show an error message) |
| 54 | + print('Error saving theme settings: $e'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Helper method to update individual properties |
| 59 | + Future<void> updateThemeColors({ |
| 60 | + Color? primaryColor, |
| 61 | + Color? secondaryColor, |
| 62 | + Color? systemBubbleColor, |
| 63 | + Color? userBubbleColor, |
| 64 | + }) async { |
| 65 | + final newSettings = state.copyWith( |
| 66 | + primaryColor: primaryColor, |
| 67 | + secondaryColor: secondaryColor, |
| 68 | + systemBubbleColor: systemBubbleColor, |
| 69 | + userBubbleColor: userBubbleColor, |
| 70 | + ); |
| 71 | + await updateSettings(newSettings); |
| 72 | + } |
| 73 | + |
| 74 | + Future<void> updateBackgroundImage(String imagePath) async { |
| 75 | + final newSettings = state.copyWith(backgroundImage: imagePath); |
| 76 | + await updateSettings(newSettings); |
| 77 | + } |
| 78 | +} |
0 commit comments