// lib/remote/remote_settings.dart import 'package:flutter/foundation.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; class RemoteSettings { static const _storage = FlutterSecureStorage(); // Keys static const _kEnabled = 'remote_enabled'; static const _kBaseUrl = 'remote_base_url'; static const _kIndexPath = 'remote_index_path'; static const _kEmail = 'remote_email'; static const _kPassword = 'remote_password'; // Default values: // In DEBUG vogliamo valori pre-compilati; in RELEASE lasciamo vuoti/false. static final bool defaultEnabled = kDebugMode ? true : false; static final String defaultBaseUrl = kDebugMode ? 'https://prova.patachina.it' : ''; static final String defaultIndexPath = kDebugMode ? 'photos/' : ''; static final String defaultEmail = kDebugMode ? 'fabio@gmail.com' : ''; static final String defaultPassword = kDebugMode ? 'master66' : ''; bool enabled; String baseUrl; String indexPath; String email; String password; RemoteSettings({ required this.enabled, required this.baseUrl, required this.indexPath, required this.email, required this.password, }); /// Carica i setting dal secure storage. /// Se un valore non esiste, usa i default (in debug: quelli precompilati). static Future load() async { final enabledStr = await _storage.read(key: _kEnabled); final baseUrl = await _storage.read(key: _kBaseUrl) ?? defaultBaseUrl; final indexPath = await _storage.read(key: _kIndexPath) ?? defaultIndexPath; final email = await _storage.read(key: _kEmail) ?? defaultEmail; final password = await _storage.read(key: _kPassword) ?? defaultPassword; final enabled = (enabledStr ?? (defaultEnabled ? 'true' : 'false')) == 'true'; return RemoteSettings( enabled: enabled, baseUrl: baseUrl, indexPath: indexPath, email: email, password: password, ); } /// Scrive i setting nel secure storage. Future save() async { await _storage.write(key: _kEnabled, value: enabled ? 'true' : 'false'); await _storage.write(key: _kBaseUrl, value: baseUrl); await _storage.write(key: _kIndexPath, value: indexPath); await _storage.write(key: _kEmail, value: email); await _storage.write(key: _kPassword, value: password); } /// In DEBUG: se un valore non è ancora impostato, inizializzalo con i default. /// NON sovrascrive valori già presenti (quindi puoi sempre entrare in Settings e cambiare). static Future debugSeedIfEmpty() async { if (!kDebugMode) return; Future _seed(String key, String value) async { final existing = await _storage.read(key: key); if (existing == null) { await _storage.write(key: key, value: value); } } await _seed(_kEnabled, defaultEnabled ? 'true' : 'false'); await _seed(_kBaseUrl, defaultBaseUrl); await _seed(_kIndexPath, defaultIndexPath); await _seed(_kEmail, defaultEmail); await _seed(_kPassword, defaultPassword); } }