import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:chepuhagram/domain/services/api_service.dart'; class PrivacySettingsScreen extends StatefulWidget { const PrivacySettingsScreen({super.key}); @override State createState() => _PrivacySettingsScreenState(); } class _PrivacySettingsScreenState extends State { static const _showEmailKey = 'privacy_show_email'; static const _showPhoneKey = 'privacy_show_phone'; static const _showAvatarKey = 'privacy_show_avatar'; static const _showAboutKey = 'privacy_show_about'; static const _showLastOnlineKey = 'privacy_show_last_online'; bool _showEmail = true; bool _showPhone = true; bool _showAvatar = true; bool _showAbout = true; bool _showLastOnline = true; bool _isSaving = false; @override void initState() { super.initState(); _loadPreferences(); _loadServerSettings(); } Future _loadPreferences() async { final prefs = await SharedPreferences.getInstance(); setState(() { _showEmail = prefs.getBool(_showEmailKey) ?? true; _showPhone = prefs.getBool(_showPhoneKey) ?? true; _showAvatar = prefs.getBool(_showAvatarKey) ?? true; _showAbout = prefs.getBool(_showAboutKey) ?? true; _showLastOnline = prefs.getBool(_showLastOnlineKey) ?? true; }); } Future _loadServerSettings() async { try { final api = ApiService(); final data = await api.getPrivacySettings(); setState(() { _showEmail = data['show_email'] ?? true; _showPhone = data['show_phone'] ?? true; _showAvatar = data['show_avatar'] ?? true; _showAbout = data['show_about'] ?? true; _showLastOnline = data['show_last_online'] ?? true; }); // Сохраняем локально для быстрого доступа await _savePreference(_showEmailKey, _showEmail); await _savePreference(_showPhoneKey, _showPhone); await _savePreference(_showAvatarKey, _showAvatar); await _savePreference(_showAboutKey, _showAbout); await _savePreference(_showLastOnlineKey, _showLastOnline); } catch (e) { // Если не удалось загрузить с сервера, используем локальные настройки print('Ошибка загрузки настроек с сервера: $e'); } } Future _savePreference(String key, bool value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(key, value); } Future _saveToServer() async { if (_isSaving) return; setState(() => _isSaving = true); try { final api = ApiService(); final success = await api.updatePrivacySettings( showEmail: _showEmail, showPhone: _showPhone, showAvatar: _showAvatar, showAbout: _showAbout, showLastOnline: _showLastOnline, ); if (success) { // Сохраняем локально только после успешного сохранения на сервере await _savePreference(_showEmailKey, _showEmail); await _savePreference(_showPhoneKey, _showPhone); await _savePreference(_showAvatarKey, _showAvatar); await _savePreference(_showAboutKey, _showAbout); await _savePreference(_showLastOnlineKey, _showLastOnline); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Настройки сохранены')), ); } } else { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Не удалось сохранить настройки')), ); } } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Ошибка: ${e.toString().replaceAll('Exception: ', '')}')), ); } } finally { if (mounted) { setState(() => _isSaving = false); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Конфиденциальность'), actions: [ TextButton( onPressed: _isSaving ? null : _saveToServer, child: _isSaving ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white), ) : const Text( 'Сохранить', style: TextStyle(color: Colors.white), ), ), ], ), body: ListView( padding: const EdgeInsets.all(16), children: [ const Text('Настройки видимости', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), const SizedBox(height: 12), SwitchListTile( title: const Text('Показывать почту другим'), value: _showEmail, onChanged: (value) { setState(() => _showEmail = value); }, ), SwitchListTile( title: const Text('Показывать телефон другим'), value: _showPhone, onChanged: (value) { setState(() => _showPhone = value); }, ), SwitchListTile( title: const Text('Показывать аватар другим'), value: _showAvatar, onChanged: (value) { setState(() => _showAvatar = value); }, ), SwitchListTile( title: const Text('Показывать информацию «О себе»'), value: _showAbout, onChanged: (value) { setState(() => _showAbout = value); }, ), SwitchListTile( title: const Text('Показывать последний онлайн'), value: _showLastOnline, onChanged: (value) { setState(() => _showLastOnline = value); }, ), const SizedBox(height: 24), const Text( 'Эти настройки влияют на то, какую информацию о вас видят другие пользователи приложения.', style: TextStyle(color: Colors.grey), ), ], ), ); } }