Files
food-ai/client/lib/features/profile/profile_provider.dart
dbastrikin 0567d90784 feat: implement client-side localization infrastructure
- Add languageProvider (StateProvider<String>, default 'ru') with
  supportedLanguages map matching backend locale.Supported
- Wire Accept-Language header into AuthInterceptor via languageGetter
  callback; all API requests now carry the current language
- Sync language from user profile preferences into languageProvider
  on every ProfileNotifier load/update
- Add language field to UpdateProfileRequest, serialized as
  preferences.language in PUT /profile
- Profile screen: НАСТРОЙКИ section displays current language;
  edit sheet adds DropdownButtonFormField for language selection

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-27 23:34:51 +02:00

49 lines
1.3 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/locale/language_provider.dart';
import '../../shared/models/user.dart';
import 'profile_service.dart';
class ProfileNotifier extends StateNotifier<AsyncValue<User>> {
final ProfileService _service;
final void Function(String) _setLanguage;
ProfileNotifier(this._service, this._setLanguage)
: super(const AsyncValue.loading()) {
load();
}
Future<void> load() async {
state = const AsyncValue.loading();
state = await AsyncValue.guard(() => _service.getProfile());
_syncLanguage();
}
Future<bool> update(UpdateProfileRequest req) async {
try {
final updated = await _service.updateProfile(req);
state = AsyncValue.data(updated);
_syncLanguage();
return true;
} catch (_) {
return false;
}
}
// Propagates the user's preferred language to the global languageProvider.
void _syncLanguage() {
final lang = state.valueOrNull?.preferences['language'] as String?;
if (lang != null && supportedLanguages.containsKey(lang)) {
_setLanguage(lang);
}
}
}
final profileProvider =
StateNotifierProvider<ProfileNotifier, AsyncValue<User>>(
(ref) => ProfileNotifier(
ref.read(profileServiceProvider),
(lang) => ref.read(languageProvider.notifier).state = lang,
),
);