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.
149 lines
3.3 KiB
Dart
149 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:isekai_wiki/api/base_api.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
|
|
import '../global.dart';
|
|
|
|
part 'site_config.g.dart';
|
|
|
|
@JsonSerializable()
|
|
class SiteConfig {
|
|
List<String> moduleStyles;
|
|
List<String> moduleScripts;
|
|
String renderTheme;
|
|
String renderTemplateUrl;
|
|
|
|
String baseUrl;
|
|
String indexUrl;
|
|
String apiUrl;
|
|
String resourceLoaderUrl;
|
|
String restfulApiUrl;
|
|
String pageUrlTemplate;
|
|
|
|
bool enableFollowing;
|
|
|
|
SiteConfig({
|
|
this.moduleStyles = const [],
|
|
this.moduleScripts = const [],
|
|
this.renderTheme = Global.renderThemeFallback,
|
|
this.renderTemplateUrl = "",
|
|
this.baseUrl = "",
|
|
this.indexUrl = "",
|
|
this.apiUrl = "",
|
|
this.resourceLoaderUrl = "",
|
|
this.restfulApiUrl = "",
|
|
this.pageUrlTemplate = "",
|
|
this.enableFollowing = false,
|
|
});
|
|
|
|
factory SiteConfig.fromJson(Map<String, dynamic> json) => _$SiteConfigFromJson(json);
|
|
|
|
Map<String, dynamic> toJson() => _$SiteConfigToJson(this);
|
|
|
|
void fillUrl() {
|
|
if (indexUrl.isEmpty) {
|
|
indexUrl = "$baseUrl/index.php";
|
|
}
|
|
|
|
if (apiUrl.isEmpty) {
|
|
apiUrl = "$baseUrl/api.php";
|
|
}
|
|
|
|
if (resourceLoaderUrl.isEmpty) {
|
|
resourceLoaderUrl = "$baseUrl/load.php";
|
|
}
|
|
|
|
if (pageUrlTemplate.isEmpty) {
|
|
pageUrlTemplate = "$baseUrl/index.php?title={{title}}";
|
|
}
|
|
}
|
|
|
|
bool get restfulApiAvailable {
|
|
return restfulApiUrl.isNotEmpty;
|
|
}
|
|
}
|
|
|
|
class SiteConfigController extends GetxController {
|
|
bool _ignoreSave = false;
|
|
|
|
bool isAppActive = false;
|
|
|
|
var config = Rx<SiteConfig>(SiteConfig());
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
loadFromStorage();
|
|
|
|
if (isAppActive) {
|
|
// 尝试更新APP配置
|
|
loadFromRemote().catchError((err, stack) {
|
|
if (kDebugMode) {
|
|
print("Cannot update site config: $err");
|
|
stack.printError();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// 从本地存储读取
|
|
void loadFromStorage() {
|
|
try {
|
|
final storage = GetStorage();
|
|
|
|
isAppActive = storage.read<bool>("appActive") ?? false;
|
|
|
|
var siteConfigJson = storage.read<String>("siteConfigCache");
|
|
if (siteConfigJson == null) return;
|
|
|
|
var siteConfigObject = jsonDecode(siteConfigJson);
|
|
if (siteConfigObject == null) return;
|
|
|
|
var siteConfigData = SiteConfig.fromJson(siteConfigObject);
|
|
loadFromEntity(siteConfigData);
|
|
} catch (ex, stack) {
|
|
if (kDebugMode) {
|
|
print(ex);
|
|
stack.printError();
|
|
}
|
|
} finally {
|
|
_ignoreSave = false;
|
|
}
|
|
}
|
|
|
|
/// 缓存到本地存储
|
|
void saveToStorage() {
|
|
if (_ignoreSave) return;
|
|
|
|
final storage = GetStorage();
|
|
|
|
var siteConfigJson = jsonEncode(config.value);
|
|
|
|
storage.write("siteConfigCache", siteConfigJson);
|
|
|
|
storage.write("appActive", isAppActive);
|
|
}
|
|
|
|
void loadFromEntity(SiteConfig siteConfigData) {
|
|
config.value = siteConfigData;
|
|
Global.siteConfig = config.value;
|
|
}
|
|
|
|
Future<void> loadFromRemote() async {
|
|
Uri siteConfigUri = Uri.parse(Global.siteConfigUrl);
|
|
var resMap = await BaseApi.getJson(siteConfigUri);
|
|
|
|
var siteConfigData = SiteConfig.fromJson(resMap);
|
|
loadFromEntity(siteConfigData);
|
|
|
|
isAppActive = true;
|
|
Global.isAppActive = true;
|
|
saveToStorage();
|
|
}
|
|
}
|