@@ -14,79 +14,97 @@ class SelectionProvider extends ChangeNotifier {
1414 _loadSelection ();
1515 }
1616
17- void _loadSelection () async {
18- final prefs = await SharedPreferences . getInstance ();
19- _selectedIndex = prefs. getInt ( 'selectedIndex' ) ?? 0 ;
20-
21- final firstStoredValue = prefs. getStringList ( 'firstSelectedValues' ) ?? [];
22- for ( final entry in firstStoredValue) {
23- final split = entry. split ( ':' );
24- if ( split.length == 2 ) {
25- final index = int . tryParse (split[ 0 ]);
26- if (index != null ) {
27- _firstSelectedValues[index ] = split[1 ];
17+ Future < void > _loadSelection () async {
18+ try {
19+ final prefs = await SharedPreferences . getInstance () ;
20+ _selectedIndex = prefs. getInt ( 'selectedIndex' ) ?? 0 ;
21+
22+ final firstStoredValue = prefs. getStringList ( 'firstSelectedValues' ) ?? [];
23+ for ( final entry in firstStoredValue) {
24+ final split = entry. split ( ':' );
25+ if (split.length == 2 ) {
26+ final idx = int . tryParse (split[ 0 ]);
27+ if (idx != null ) _firstSelectedValues[idx ] = split[1 ];
2828 }
2929 }
30- }
3130
32- final secondStoredValue = prefs.getStringList ('secondSelectedValues' ) ?? [];
33- for (final entry in secondStoredValue) {
34- final split = entry.split (':' );
35- if (split.length == 2 ) {
36- final index = int .tryParse (split[0 ]);
37- if (index != null ) {
38- _secondSelectedValues[index] = split[1 ];
31+ final secondStoredValue = prefs.getStringList ('secondSelectedValues' ) ?? [];
32+ for (final entry in secondStoredValue) {
33+ final split = entry.split (':' );
34+ if (split.length == 2 ) {
35+ final idx = int .tryParse (split[0 ]);
36+ if (idx != null ) _secondSelectedValues[idx] = split[1 ];
3937 }
4038 }
39+
40+ // Ensure keys exist for categories (adjust `5` if you have different count)
41+ for (int i = 0 ; i < 5 ; i++ ) {
42+ _firstSelectedValues.putIfAbsent (i, () => null );
43+ _secondSelectedValues.putIfAbsent (i, () => null );
44+ }
45+
46+ notifyListeners ();
47+ } catch (e) {
48+ debugPrint ('Error loading selections: $e ' );
4149 }
42- notifyListeners ();
4350 }
4451
4552 Future <void > _saveSelection () async {
46- final prefs = await SharedPreferences .getInstance ();
47-
48- final List <String > firstStoredMap = _firstSelectedValues.entries
49- .where ((e) => e.value != null )
50- .map ((e) => '${e .key }:${e .value }' )
51- .toList ();
52- await prefs.setStringList ('firstSelectedValues' , firstStoredMap);
53-
54- final List <String > secondStoredMap = _secondSelectedValues.entries
55- .where ((e) => e.value != null )
56- .map ((e) => '${e .key }:${e .value }' )
57- .toList ();
58- await prefs.setStringList ('secondSelectedValues' , secondStoredMap);
53+ try {
54+ final prefs = await SharedPreferences .getInstance ();
55+
56+ final List <String > firstStoredMap = _firstSelectedValues.entries
57+ .where ((e) => e.value != null )
58+ .map ((e) => '${e .key }:${e .value }' )
59+ .toList ();
60+ await prefs.setStringList ('firstSelectedValues' , firstStoredMap);
61+
62+ final List <String > secondStoredMap = _secondSelectedValues.entries
63+ .where ((e) => e.value != null )
64+ .map ((e) => '${e .key }:${e .value }' )
65+ .toList ();
66+ await prefs.setStringList ('secondSelectedValues' , secondStoredMap);
67+
68+ await prefs.setInt ('selectedIndex' , _selectedIndex);
69+ } catch (e) {
70+ debugPrint ('Error saving selections: $e ' );
71+ }
5972 }
6073
61- void selectIndex (int index) async {
74+ void selectIndex (int index) {
75+ if (_selectedIndex == index) return ;
6276 _selectedIndex = index;
63- final prefs = await SharedPreferences .getInstance ();
64- await prefs.setInt ('selectedIndex' , index);
6577 notifyListeners ();
6678 }
6779
68- void selectFirstValue (String value) async {
80+ Future < void > selectFirstValue (String value) async {
6981 _firstSelectedValues[_selectedIndex] = value;
7082 await _saveSelection ();
7183 notifyListeners ();
7284 }
7385
74- void selectSecondValue (String value) async {
86+ Future < void > selectSecondValue (String value) async {
7587 _secondSelectedValues[_selectedIndex] = value;
7688 await _saveSelection ();
7789 notifyListeners ();
7890 }
7991
80- void reset () {
81- _selectedIndex = 0 ;
92+ Future <void > resetCurrentCategory () async {
93+ _firstSelectedValues[_selectedIndex] = null ;
94+ _secondSelectedValues[_selectedIndex] = null ;
95+ await _saveSelection ();
96+ notifyListeners ();
97+ }
98+
99+ Future <void > resetAllCategories () async {
82100 _firstSelectedValues.clear ();
83101 _secondSelectedValues.clear ();
102+ for (int i = 0 ; i < 5 ; i++ ) {
103+ _firstSelectedValues[i] = null ;
104+ _secondSelectedValues[i] = null ;
105+ }
106+ _selectedIndex = 0 ;
107+ await _saveSelection ();
84108 notifyListeners ();
85-
86- SharedPreferences .getInstance ().then ((prefs) {
87- prefs.remove ('selectedIndex' );
88- prefs.remove ('firstSelectedValues' );
89- prefs.remove ('secondSelectedValues' );
90- });
91109 }
92110}
0 commit comments