import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../global.dart'; part 'site_config.g.dart'; @JsonSerializable() class SiteConfig { List moduleStyles; List moduleScripts; String renderTheme; SiteConfig({ this.moduleStyles = const [], this.moduleScripts = const [], this.renderTheme = Global.renderThemeFallback, }); factory SiteConfig.fromJson(Map json) => _$SiteConfigFromJson(json); Map toJson() => _$SiteConfigToJson(this); } class AppSettingsController extends GetxController { bool _ignoreSave = false; var betaPageRender = false.obs; @override void onInit() { super.onInit(); loadFromStorage(); everAll([betaPageRender], (_) { saveToStorage(); }); } /// 从本地存储读取 Future loadFromStorage() async { try { final prefs = await SharedPreferences.getInstance(); var settingsJson = prefs.getString("siteConfigCache"); if (settingsJson == null) return; var settingsObject = jsonDecode(settingsJson); if (settingsObject == null) return; var settingsData = SiteConfig.fromJson(settingsObject); _ignoreSave = true; // betaPageRender.value = settingsData.betaPageRender ?? betaPageRender.value; _ignoreSave = false; } catch (ex) { if (kDebugMode) { print(ex); } } finally { _ignoreSave = false; } } /// 保存到本地存储 void saveToStorage() { if (_ignoreSave) return; final prefs = Global.sharedPreferences!; var settingsData = SiteConfig(); var settingsJson = jsonEncode(settingsData.toJson()); prefs.setString("siteConfigCache", settingsJson); } }