import 'dart:convert'; import 'dart:ui'; import 'package:shared_preferences/shared_preferences.dart'; import '../definitions/ColorPairs.dart'; class PreferencesService { static const _keyColor1 = 'color1'; static const _keyColor2 = 'color2'; static const _keyDiaryEntries = 'diaryEntries'; static const _keyPin = 'userPin'; static const _keyPinEnabled = 'pinEnabled'; // Helper method to save a ColorPair preference Future saveColorPair(ColorPair colorPair) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString( _keyColor1, colorPair.backgroundColor.value.toString()); await prefs.setString(_keyColor2, colorPair.textColor.value.toString()); } // Helper method to load the ColorPair preference Future loadColorPair() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String? color1 = prefs.getString(_keyColor1); final String? color2 = prefs.getString(_keyColor2); if (color1 != null && color2 != null) { return ColorPair( name: "Custom", // You might want to handle naming differently textColor: Color(int.parse(color2)), backgroundColor: Color(int.parse(color1)), ); } return colorPairs[0]; // Return null if no color pair is set } // Get the diary entry for the current date Future getDiaryEntryByCurrentDate() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String diaryEntriesJson = prefs.getString(_keyDiaryEntries) ?? '{}'; final Map diaryEntriesMap = json.decode(diaryEntriesJson); String todayKey = DateTime.now().toIso8601String().split('T')[0]; if (diaryEntriesMap.containsKey(todayKey)) { final Map entryData = diaryEntriesMap[todayKey]; final List texts = List.from(entryData['texts']); return DiaryEntry( date: DateTime.parse(todayKey), percentValue: entryData['percentValue'], texts: texts, ); } return null; } // Get the diary entry by a given date Future getDiaryEntryByDate(DateTime date) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String diaryEntriesJson = prefs.getString(_keyDiaryEntries) ?? '{}'; final Map diaryEntriesMap = json.decode(diaryEntriesJson); String dateKey = date.toIso8601String().split('T')[0]; if (diaryEntriesMap.containsKey(dateKey)) { final Map entryData = diaryEntriesMap[dateKey]; final List texts = List.from(entryData['texts']); return DiaryEntry( date: DateTime.parse(dateKey), percentValue: entryData['percentValue'], texts: texts, ); } return null; } // Save a diary entry Future saveDiaryEntry(DiaryEntry entry) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final Map diaryEntriesMap = json.decode(prefs.getString(_keyDiaryEntries) ?? '{}'); // Use the date as the ID for the diary entry final String dateKey = entry.date.toIso8601String().split('T')[0]; // If the entry is new or the date is older than two days, add or update the text if (!diaryEntriesMap.containsKey(dateKey) || DateTime.now().difference(entry.date).inDays > 2) { final List texts = diaryEntriesMap[dateKey] != null ? List.from(diaryEntriesMap[dateKey]['texts']) : []; texts.addAll(entry.texts); diaryEntriesMap[dateKey] = { 'percentValue': entry.percentValue, 'texts': texts, }; } else { // For an existing entry less than two days old, update the percent value and overwrite the text diaryEntriesMap[dateKey] = { 'percentValue': entry.percentValue, 'texts': entry.texts, }; } await prefs.setString(_keyDiaryEntries, json.encode(diaryEntriesMap)); } // Load diary entries Future> loadDiaryEntries() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String diaryEntriesJson = prefs.getString(_keyDiaryEntries) ?? '{}'; final Map diaryEntriesMap = json.decode(diaryEntriesJson); return diaryEntriesMap.entries.map((entry) { final List texts = List.from(entry.value['texts']); return DiaryEntry( date: DateTime.parse(entry.key), percentValue: entry.value['percentValue'], texts: texts, ); }).toList(); } // Set PIN method Future setPin(int pin) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setInt(_keyPin, pin); await prefs.setBool( _keyPinEnabled, true); // Automatically enable PIN on setting } // Check PIN method Future checkPin(int pin) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); int savedPin = prefs.getInt(_keyPin) ?? -1; // Default to -1 if not set return savedPin == pin; } // Check if PIN is enabled Future isPinEnabled() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.getBool(_keyPinEnabled) ?? false; // Return false if not set } // Enable PIN Future enablePin() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); if (prefs.getInt(_keyPin) != null) { // Ensure PIN exists before enabling await prefs.setBool(_keyPinEnabled, true); } } // Disable PIN Future disablePin() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setBool(_keyPinEnabled, false); } } // Helper class to store color preferences class ColorPreference { final Color? color1; final Color? color2; ColorPreference({this.color1, this.color2}); } // Diary entry data model class DiaryEntry { final DateTime date; final int percentValue; final List texts; DiaryEntry({ required this.date, required this.percentValue, required this.texts, }); }