@@ -47,51 +47,52 @@ class _LocationSelectionScreenState extends State<LocationSelectionScreen>
47
47
UserPreferencesModel ? userPreferences;
48
48
49
49
@override
50
- void initState () {
51
- super .initState ();
52
- loggy.info ('initState called' );
50
+ void initState () {
51
+ super .initState ();
52
+ loggy.info ('initState called' );
53
53
54
- _initializeUserData ();
54
+ _initializeUserData ();
55
55
56
- googlePlacesBloc = context.read <GooglePlacesBloc >()
57
- ..add (ResetGooglePlaces ());
56
+ googlePlacesBloc = context.read <GooglePlacesBloc >()
57
+ ..add (ResetGooglePlaces ());
58
58
59
- loggy.info ('Checking dashboard state' );
60
- final dashboardBloc = context.read <DashboardBloc >();
61
- final currentState = dashboardBloc.state;
62
- loggy.info ('Current dashboard state: ${currentState .runtimeType }' );
59
+ loggy.info ('Checking dashboard state' );
60
+ final dashboardBloc = context.read <DashboardBloc >();
61
+ final currentState = dashboardBloc.state;
62
+ loggy.info ('Current dashboard state: ${currentState .runtimeType }' );
63
+
64
+ if (currentState is DashboardLoaded ) {
65
+ loggy.info ('Dashboard already loaded, populating measurements' );
66
+ if (currentState.response.measurements != null ) {
67
+ loggy.info (
68
+ 'Found ${currentState .response .measurements !.length } measurements in loaded state' );
69
+ _populateMeasurements (currentState.response.measurements! );
70
+
71
+ // IMPORTANT ADDITION: Pre-select existing locations from the current DashboardState
72
+ if (currentState.userPreferences != null &&
73
+ currentState.userPreferences! .selectedSites.isNotEmpty) {
74
+ final existingIds = currentState.userPreferences! .selectedSites
75
+ .map ((site) => site.id)
76
+ .toSet ();
63
77
64
- if (currentState is DashboardLoaded ) {
65
- loggy.info ('Dashboard already loaded, populating measurements' );
66
- if (currentState.response.measurements != null ) {
67
- loggy.info (
68
- 'Found ${currentState .response .measurements !.length } measurements in loaded state' );
69
- _populateMeasurements (currentState.response.measurements! );
70
-
71
- // IMPORTANT ADDITION: Pre-select existing locations from the current DashboardState
72
- if (currentState.userPreferences != null &&
73
- currentState.userPreferences! .selectedSites.isNotEmpty) {
74
- final existingIds = currentState.userPreferences! .selectedSites
75
- .map ((site) => site.id)
76
- .toSet ();
77
-
78
- loggy.info ('Pre-selecting ${existingIds .length } existing locations from dashboard state' );
78
+ loggy.info (
79
+ 'Pre-selecting ${existingIds .length } existing locations from dashboard state' );
80
+ setState (() {
81
+ selectedLocations = existingIds;
82
+ });
83
+ }
84
+ } else {
85
+ loggy.warning ('No measurements in loaded state' );
79
86
setState (() {
80
- selectedLocations = existingIds;
87
+ isLoading = false ;
88
+ errorMessage = "No measurements available in loaded state" ;
81
89
});
82
90
}
83
91
} else {
84
- loggy.warning ('No measurements in loaded state' );
85
- setState (() {
86
- isLoading = false ;
87
- errorMessage = "No measurements available in loaded state" ;
88
- });
92
+ loggy.info ('Dispatching LoadDashboard event' );
93
+ dashboardBloc.add (LoadDashboard ());
89
94
}
90
- } else {
91
- loggy.info ('Dispatching LoadDashboard event' );
92
- dashboardBloc.add (LoadDashboard ());
93
95
}
94
- }
95
96
96
97
Future <void > _initializeUserData () async {
97
98
try {
@@ -174,100 +175,99 @@ void initState() {
174
175
// File: src/mobile-v3/lib/src/app/dashboard/pages/location_selection/location_selection_screen.dart
175
176
176
177
// Update the _saveSelectedLocations method in the LocationSelectionScreen
177
- Future <void > _saveSelectedLocations () async {
178
- loggy.info (
179
- 'Save button pressed with ${selectedLocations .length } selected locations' );
178
+ Future <void > _saveSelectedLocations () async {
179
+ loggy.info (
180
+ 'Save button pressed with ${selectedLocations .length } selected locations' );
181
+
182
+ // Debug token
183
+ await AuthHelper .debugToken ();
184
+
185
+ // Check auth state from the bloc
186
+ final authState = context.read <AuthBloc >().state;
187
+ final isLoggedIn = authState is AuthLoaded ;
188
+
189
+ loggy.info ('Current auth state: ${authState .runtimeType }' );
190
+ loggy.info ('Is user logged in? $isLoggedIn ' );
191
+
192
+ if (! isLoggedIn) {
193
+ loggy.warning ('❌ User not logged in, cannot save' );
194
+ ScaffoldMessenger .of (context).showSnackBar (
195
+ const SnackBar (content: Text ('Please log in to save your locations' )),
196
+ );
197
+ return ;
198
+ }
180
199
181
- // Debug token
182
- await AuthHelper .debugToken ();
200
+ // Use enhanced token checker
201
+ final isExpired = await TokenDebugger .checkTokenExpiration ();
202
+
203
+ if (isExpired) {
204
+ loggy.warning ('❌ Token is expired, cannot save' );
205
+
206
+ ScaffoldMessenger .of (context).showSnackBar (
207
+ SnackBar (
208
+ content: const Text ('Your session has expired. Please log in again.' ),
209
+ duration: const Duration (seconds: 8 ),
210
+ action: SnackBarAction (
211
+ label: 'Log In' ,
212
+ onPressed: () {
213
+ // Navigate directly to login screen
214
+ Navigator .of (context).pushAndRemoveUntil (
215
+ MaterialPageRoute (
216
+ builder: (context) => const LoginPage (),
217
+ ),
218
+ (route) => false ,
219
+ );
220
+ },
221
+ ),
222
+ ),
223
+ );
224
+ return ;
225
+ }
183
226
184
- // Check auth state from the bloc
185
- final authState = context. read < AuthBloc >().state ;
186
- final isLoggedIn = authState is AuthLoaded ;
227
+ setState (() {
228
+ isSaving = true ;
229
+ }) ;
187
230
188
- loggy.info ('Current auth state: ${authState .runtimeType }' );
189
- loggy.info ('Is user logged in? $isLoggedIn ' );
231
+ try {
232
+ // IMPORTANT CHANGE: Instead of creating a new preference, we dispatch
233
+ // the UpdateSelectedLocations event to the DashboardBloc, which will
234
+ // merge these with existing locations
190
235
191
- if (! isLoggedIn) {
192
- loggy.warning ('❌ User not logged in, cannot save' );
193
- ScaffoldMessenger .of (context).showSnackBar (
194
- const SnackBar (content: Text ('Please log in to save your locations' )),
195
- );
196
- return ;
197
- }
236
+ final dashboardBloc = context.read <DashboardBloc >();
198
237
199
- // Use enhanced token checker
200
- final isExpired = await TokenDebugger .checkTokenExpiration ();
201
-
202
- if (isExpired) {
203
- loggy.warning ('❌ Token is expired, cannot save' );
204
-
205
- ScaffoldMessenger .of (context).showSnackBar (
206
- SnackBar (
207
- content: const Text ('Your session has expired. Please log in again.' ),
208
- duration: const Duration (seconds: 8 ),
209
- action: SnackBarAction (
210
- label: 'Log In' ,
211
- onPressed: () {
212
- // Navigate directly to login screen
213
- Navigator .of (context).pushAndRemoveUntil (
214
- MaterialPageRoute (
215
- builder: (context) => const LoginPage (),
216
- ),
217
- (route) => false ,
218
- );
219
- },
220
- ),
221
- ),
222
- );
223
- return ;
224
- }
225
-
226
- setState (() {
227
- isSaving = true ;
228
- });
229
-
230
- try {
231
- // IMPORTANT CHANGE: Instead of creating a new preference, we dispatch
232
- // the UpdateSelectedLocations event to the DashboardBloc, which will
233
- // merge these with existing locations
234
-
235
- final dashboardBloc = context.read <DashboardBloc >();
236
-
237
- // Convert the Set to a List
238
- final locationIdsList = selectedLocations.toList ();
239
-
240
- loggy.info ('Dispatching UpdateSelectedLocations with ${locationIdsList .length } locations' );
241
-
242
- // Dispatch the event
243
- dashboardBloc.add (UpdateSelectedLocations (locationIdsList));
244
-
245
- // Show success message
246
- loggy.info ('✅ Successfully dispatched update event' );
247
- ScaffoldMessenger .of (context).showSnackBar (
248
- const SnackBar (content: Text ('Locations saved successfully' )),
249
- );
250
-
251
- // Return to previous screen with the selected locations
252
- Navigator .pop (context, locationIdsList);
253
- } catch (e) {
254
- loggy.error ('❌ Error saving locations: $e ' );
255
- loggy.error ('Stack trace: ${StackTrace .current }' );
256
- ScaffoldMessenger .of (context).showSnackBar (
257
- SnackBar (
258
- content: Text (
259
- 'An error occurred while saving locations: ${e .toString ()}' )),
260
- );
261
- } finally {
262
- if (mounted) {
263
- setState (() {
264
- isSaving = false ;
265
- });
266
- }
267
- }
268
- }
238
+ // Convert the Set to a List
239
+ final locationIdsList = selectedLocations.toList ();
240
+
241
+ loggy.info (
242
+ 'Dispatching UpdateSelectedLocations with ${locationIdsList .length } locations' );
269
243
244
+ // Dispatch the event
245
+ dashboardBloc.add (UpdateSelectedLocations (locationIdsList));
270
246
247
+ // Show success message
248
+ loggy.info ('✅ Successfully dispatched update event' );
249
+ ScaffoldMessenger .of (context).showSnackBar (
250
+ const SnackBar (content: Text ('Locations saved successfully' )),
251
+ );
252
+
253
+ // Return to previous screen with the selected locations
254
+ Navigator .pop (context, locationIdsList);
255
+ } catch (e) {
256
+ loggy.error ('❌ Error saving locations: $e ' );
257
+ loggy.error ('Stack trace: ${StackTrace .current }' );
258
+ ScaffoldMessenger .of (context).showSnackBar (
259
+ SnackBar (
260
+ content: Text (
261
+ 'An error occurred while saving locations: ${e .toString ()}' )),
262
+ );
263
+ } finally {
264
+ if (mounted) {
265
+ setState (() {
266
+ isSaving = false ;
267
+ });
268
+ }
269
+ }
270
+ }
271
271
272
272
Future <void > _loadUserPreferences (String userId) async {
273
273
try {
0 commit comments