import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../../../domain/models/server_config.dart'; typedef Credentials = ({String username, String password}); class CredentialStore { static const _usernameKey = 'localiserd_username'; static const _passwordKey = 'localiserd_password'; static const _hostKey = 'localiserd_host'; static const _portKey = 'localiserd_port'; final _storage = const FlutterSecureStorage(); Future save(Credentials credentials) => Future.wait([ _storage.write(key: _usernameKey, value: credentials.username), _storage.write(key: _passwordKey, value: credentials.password), ]).then((_) {}); Future load() async { final results = await Future.wait([ _storage.read(key: _usernameKey), _storage.read(key: _passwordKey), ]); final username = results[0]; final password = results[1]; if (username == null || password == null) return null; return (username: username, password: password); } Future saveServer(ServerConfig config) => Future.wait([ _storage.write(key: _hostKey, value: config.host), _storage.write(key: _portKey, value: config.port.toString()), ]).then((_) {}); Future loadServer() async { final results = await Future.wait([ _storage.read(key: _hostKey), _storage.read(key: _portKey), ]); final host = results[0]; final portStr = results[1]; if (host == null || portStr == null) return null; final port = int.tryParse(portStr); if (port == null) return null; return ServerConfig(host: host, port: port); } Future clear() => Future.wait([ _storage.delete(key: _usernameKey), _storage.delete(key: _passwordKey), _storage.delete(key: _hostKey), _storage.delete(key: _portKey), ]).then((_) {}); }