import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:uuid/uuid.dart'; import '../core/constants/storage_keys.dart'; import '../data/models/server_model.dart'; /// 存储服务 class StorageService { static StorageService? _instance; SharedPreferences? _prefs; StorageService._(); /// 获取单例 static StorageService get instance { _instance ??= StorageService._(); return _instance!; } /// 初始化 Future init() async { _prefs ??= await SharedPreferences.getInstance(); } /// 获取值 Future getString(String key) async { await init(); return _prefs!.getString(key); } /// 设置值 Future setString(String key, String? value) async { await init(); if (value == null) { return _prefs!.remove(key); } return _prefs!.setString(key, value); } /// 获取整数值 Future getInt(String key) async { await init(); return _prefs!.getInt(key); } /// 设置整数值 Future setInt(String key, int? value) async { await init(); if (value == null) { return _prefs!.remove(key); } return _prefs!.setInt(key, value); } /// 获取布尔值 Future getBool(String key) async { await init(); return _prefs!.getBool(key); } /// 设置布尔值 Future setBool(String key, bool? value) async { await init(); if (value == null) { return _prefs!.remove(key); } return _prefs!.setBool(key, value); } /// 删除值 Future remove(String key) async { await init(); return _prefs!.remove(key); } /// 清空所有数据 Future clear() async { await init(); return _prefs!.clear(); } /// 设置 Future get themeMode => getString(StorageKeys.themeMode); Future setThemeMode(String value) => setString(StorageKeys.themeMode, value); /// 服务器地址配置 Future get customBaseUrl => getString(StorageKeys.customBaseUrl); Future setCustomBaseUrl(String? value) => setString(StorageKeys.customBaseUrl, value); Future removeCustomBaseUrl() => remove(StorageKeys.customBaseUrl); /// 服务器列表 Future> get servers async { final serversJson = await getString(StorageKeys.servers); if (serversJson == null || serversJson.isEmpty) { return []; } try { final serversList = jsonDecode(serversJson) as List; return serversList .map((e) => ServerModel.fromJson(e as Map)) .toList(); } catch (e) { return []; } } Future setServers(List servers) async { try { final serversJson = jsonEncode(servers.map((s) => s.toJson()).toList()); return await setString(StorageKeys.servers, serversJson); } catch (e) { return false; } } /// 上次选中的服务器 label Future get lastSelectedServerLabel => getString(StorageKeys.lastSelectedServer); Future setLastSelectedServerLabel(String? value) => setString(StorageKeys.lastSelectedServer, value); /// 搜索历史(最新在前,最多 20 条) Future> getSearchHistory() async { final json = await getString(StorageKeys.searchHistory); if (json == null || json.isEmpty) return []; try { final list = jsonDecode(json) as List; return list.cast(); } catch (_) { return []; } } Future setSearchHistory(List history) async { try { final json = jsonEncode(history); return await setString(StorageKeys.searchHistory, json); } catch (_) { return false; } } Future addToSearchHistory(String query) async { final history = await getSearchHistory(); history.remove(query); history.insert(0, query); if (history.length > 20) history.removeRange(20, history.length); await setSearchHistory(history); } Future clearSearchHistory() async { await remove(StorageKeys.searchHistory); } // ===== 同步配置持久化 ===== /// 保存同步配置 Future setSyncConfig(Map config) async { try { final json = jsonEncode(config); return await setString(StorageKeys.syncConfig, json); } catch (_) { return false; } } /// 读取同步配置 Future?> getSyncConfig() async { final json = await getString(StorageKeys.syncConfig); if (json == null || json.isEmpty) return null; try { return jsonDecode(json) as Map; } catch (_) { return null; } } /// 保存同步状态 Future setSyncState(String state) async { return await setString(StorageKeys.syncState, state); } /// 读取同步状态 Future getSyncState() async { return await getString(StorageKeys.syncState); } /// 清除同步配置和状态 Future clearSyncData() async { await remove(StorageKeys.syncConfig); await remove(StorageKeys.syncState); await remove(StorageKeys.syncCumStats); } /// 保存同步累积统计 Future setSyncCumStats(Map stats) async { final json = jsonEncode(stats); return await setString(StorageKeys.syncCumStats, json); } /// 读取同步累积统计 Future?> getSyncCumStats() async { final json = await getString(StorageKeys.syncCumStats); if (json == null) return null; try { final map = jsonDecode(json) as Map; return map.map((k, v) => MapEntry(k, v as int)); } catch (_) { return null; } } // ===== client_id 持久化 ===== /// 获取 client_id,首次调用自动生成并持久化 Future getOrCreateClientId() async { var id = await getString(StorageKeys.clientId); if (id == null || id.isEmpty) { id = const Uuid().v4(); await setString(StorageKeys.clientId, id); } return id; } }