You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
Dart
78 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
|
|
import '../global.dart';
|
|
|
|
part 'settings.g.dart';
|
|
|
|
@JsonSerializable()
|
|
class AppSettings {
|
|
bool? betaPageRender;
|
|
|
|
AppSettings({this.betaPageRender});
|
|
|
|
factory AppSettings.fromJson(Map<String, dynamic> json) =>
|
|
_$AppSettingsFromJson(json);
|
|
|
|
Map<String, dynamic> toJson() => _$AppSettingsToJson(this);
|
|
}
|
|
|
|
class AppSettingsController extends GetxController {
|
|
bool _ignoreSave = false;
|
|
|
|
var betaPageRender = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
loadFromStorage();
|
|
|
|
everAll([betaPageRender], (_) {
|
|
saveToStorage();
|
|
});
|
|
}
|
|
|
|
/// 从本地存储读取
|
|
void loadFromStorage() {
|
|
try {
|
|
final storage = GetStorage();
|
|
var settingsJson = storage.read<String>("settings");
|
|
if (settingsJson == null) return;
|
|
|
|
var settingsObject = jsonDecode(settingsJson);
|
|
if (settingsObject == null) return;
|
|
|
|
var settingsData = AppSettings.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 storage = GetStorage();
|
|
|
|
var settingsData = AppSettings(betaPageRender: betaPageRender.value);
|
|
|
|
var settingsJson = jsonEncode(settingsData.toJson());
|
|
|
|
storage.write("settings", settingsJson);
|
|
}
|
|
}
|