- Clear button (✕) now resets _cityMismatch and _detectedCity so banner and CTA unblock - _citiesMatch: replace bidirectional contains with equality + word-prefix check to prevent 'Cali' matching 'Calima' - ProfessionalsProvider: track _currentUserId to reset state on user change (cross-user leak) - ProfessionalsProvider: store _lastSearch so setLocationContext preserves active search term Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
68 lines
2.3 KiB
Dart
68 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:prosapp_web_app/models/usuario_profesional.dart';
|
|
import 'package:prosapp_web_app/services/api_service.dart';
|
|
|
|
class ProfessionalsProvider extends ChangeNotifier {
|
|
List<UsuarioProfesional> professionals = [];
|
|
bool isLoading = true;
|
|
bool _initialized = false;
|
|
String? _currentUserId;
|
|
String? _locationCity;
|
|
double? _locationLat;
|
|
double? _locationLng;
|
|
String? _lastSearch;
|
|
final _api = ApiService.instance;
|
|
|
|
void updateCity(String? city, {String? userId}) {
|
|
// Reset state when a different user logs in (prevents cross-user data leak)
|
|
if (_currentUserId != userId) {
|
|
_currentUserId = userId;
|
|
_initialized = false;
|
|
_locationCity = null;
|
|
_locationLat = null;
|
|
_locationLng = null;
|
|
_lastSearch = null;
|
|
professionals = [];
|
|
}
|
|
if (!_initialized) {
|
|
_initialized = true;
|
|
_locationCity = city;
|
|
getProfessionals();
|
|
}
|
|
}
|
|
|
|
void setLocationContext({required String? city, required double lat, required double lng}) {
|
|
if (city != null && city.isNotEmpty) _locationCity = city;
|
|
_locationLat = lat;
|
|
_locationLng = lng;
|
|
getProfessionals(search: _lastSearch);
|
|
}
|
|
|
|
Future<void> getProfessionals({String? search}) async {
|
|
if (search != null) _lastSearch = search.trim().isEmpty ? null : search.trim();
|
|
isLoading = true;
|
|
notifyListeners();
|
|
try {
|
|
final params = <String, String>{};
|
|
if (_lastSearch != null && _lastSearch!.isNotEmpty) params['search'] = _lastSearch!;
|
|
if (_locationCity != null && _locationCity!.isNotEmpty) params['city'] = _locationCity!.trim();
|
|
if (_locationLat != null) params['lat'] = _locationLat.toString();
|
|
if (_locationLng != null) params['lng'] = _locationLng.toString();
|
|
|
|
final query = params.isEmpty
|
|
? ''
|
|
: '?${params.entries.map((e) => '${e.key}=${Uri.encodeComponent(e.value)}').join('&')}';
|
|
final res = await _api.get('/professionals$query');
|
|
final list = (res is Map ? res['data'] : res) as List;
|
|
professionals = list
|
|
.map((e) => UsuarioProfesional.fromDocument(e as Map<String, dynamic>))
|
|
.toList();
|
|
} catch (e) {
|
|
print('Error obteniendo profesionales: $e');
|
|
} finally {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|