uaestats_fe/lib/config/toggle_lang_service.dart
2026-04-09 12:55:10 +05:30

52 lines
1.5 KiB
Dart
Executable File

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pocketbase/pocketbase.dart';
import 'package:uae_stat/config/api_config.dart';
import 'package:uae_stat/infrastructure/services/pocketbase_service.dart';
class UserService {
final _pb = PocketBase(apiUrl);
Future<void> updateLanguage(String localeCode, WidgetRef ref) async {
try {
final prefs = await SharedPreferences.getInstance();
final userId = prefs.getString('userId');
if (userId == null) {
throw Exception('User ID not found');
}
// Authenticate as admin
// final adminAuth = await _pb.admins
// .authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
// final auth = ref.watch(authProvider);
// final adminAuth = await pb.admins.authWithPassword(
// auth.email,
// auth.password,
// );
// final token = adminAuth.token;
// final themeService = ThemeBaseService();
// final token = await themeService.getAdminToken(
// email: auth.email,
// password: auth.password,
// );
final headers = {
'Authorization': 'Bearer ${PocketBaseService.authStore.token}',
};
await _pb.collection('users').update(
userId,
body: {
'language': localeCode,
},
headers: headers,
);
} catch (e) {
// Keep UI flow resilient if the remote language update fails.
}
}
}