import 'dart:convert'; import 'dart:ui'; import 'package:shared_preferences/shared_preferences.dart'; class PreferencesService { static const _keyColor1 = 'color1'; static const _keyColor2 = 'color2'; static const _keyDiaryEntries = 'diaryEntries'; // Load color preferences Future loadColors() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String? color1 = prefs.getString(_keyColor1); final String? color2 = prefs.getString(_keyColor2); return ColorPreference( color1: color1 != null ? Color(int.parse(color1)) : null, color2: color2 != null ? Color(int.parse(color2)) : null, ); } // Save color preferences Future saveColors(Color color1, Color color2) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString(_keyColor1, color1.value.toString()); await prefs.setString(_keyColor2, color2.value.toString()); } // 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(); } } // 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, }); }