@@ -39,7 +39,16 @@ class FocusViewModel extends ChangeNotifier {
3939 final prefs = await SharedPreferences .getInstance ();
4040 List <String >? noteStrings = prefs.getStringList ('saved_notes' );
4141 if (noteStrings != null ) {
42- notes = noteStrings.map ((s) => NoteModel .fromJson (s)).toList ();
42+ List <NoteModel > loadedNotes = [];
43+ for (String noteString in noteStrings) {
44+ try {
45+ loadedNotes.add (NoteModel .fromJson (noteString));
46+ } catch (e) {
47+ debugPrint ("Error parsing note JSON: $e " );
48+ // Skip the malformed entry and continue
49+ }
50+ }
51+ notes = loadedNotes;
4352 notifyListeners ();
4453 }
4554 }
@@ -66,7 +75,7 @@ class FocusViewModel extends ChangeNotifier {
6675 }
6776
6877 // Add note (optionally with an image) instantly to the UI and save to disk
69- void addNote () {
78+ Future < void > addNote () async {
7079 final text = noteController.text.trim ();
7180 if (text.isEmpty && selectedImagePath == null ) return ; // Skip if both text and image are empty
7281
@@ -78,21 +87,21 @@ class FocusViewModel extends ChangeNotifier {
7887 ));
7988
8089 _sortNotes ();
81- saveNotesToDisk (); // Persist data
90+ await saveNotesToDisk (); // Persist data
8291 noteController.clear ();
8392 selectedImagePath = null ; // Clear temporary image after saving
8493 notifyListeners ();
8594 }
8695
8796 // Remove note instantly and update storage
88- void removeNote (String id) {
97+ Future < void > removeNote (String id) async {
8998 notes.removeWhere ((note) => note.id == id);
90- saveNotesToDisk (); // Persist data
99+ await saveNotesToDisk (); // Persist data
91100 notifyListeners ();
92101 }
93102
94103 // Pin/unpin note and update storage
95- void togglePin (String id) {
104+ Future < void > togglePin (String id) async {
96105 final index = notes.indexWhere ((n) => n.id == id);
97106 if (index != - 1 ) {
98107 notes[index] = NoteModel (
@@ -102,7 +111,7 @@ class FocusViewModel extends ChangeNotifier {
102111 imagePath: notes[index].imagePath // Keep image when pinning
103112 );
104113 _sortNotes ();
105- saveNotesToDisk (); // Persist data
114+ await saveNotesToDisk (); // Persist data
106115 notifyListeners ();
107116 }
108117 }
@@ -142,7 +151,7 @@ class FocusViewModel extends ChangeNotifier {
142151 }
143152
144153 // Calculate progress for the circular indicator
145- double get progress => totalTime <= 0 ? 0.0 : ( timeRemaining / totalTime). clamp ( 0.0 , 1.0 ) ;
154+ double get progress => timeRemaining / totalTime;
146155
147156 // --- TIMER OPERATIONS ---
148157
@@ -212,11 +221,6 @@ class FocusViewModel extends ChangeNotifier {
212221
213222 // Update preferences from the settings dialog
214223 void updateSettings ({required int newPomodoroMinutes, required int newBreakMinutes, required bool vibrate, required int ringtone}) {
215- if (newPomodoroMinutes <= 0 || newBreakMinutes <= 0 ) {
216- debugPrint ('Lỗi: Thời gian cài đặt phải lớn hơn 0 phút. Đã tự động set về 1.' );
217- newPomodoroMinutes = newPomodoroMinutes <= 0 ? 1 : newPomodoroMinutes;
218- newBreakMinutes = newBreakMinutes <= 0 ? 1 : newBreakMinutes;
219- }
220224 stopAlarm (); // Stop alarm if opening settings
221225 pomodoroTime = newPomodoroMinutes * 60 ;
222226 shortBreakTime = newBreakMinutes * 60 ;
0 commit comments