diff --git a/README.md b/README.md index 87234a606..5cb6c8279 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ - [x] 合集图片 - [x] 删除/置顶/撤回私信 - [x] 举报用户/评论/视频/动态 -- [x] 删除/发布文本/图片动态 +- [x] 删除/发布/置顶文本/图片动态 - [x] 其他 ## opt diff --git a/lib/main.dart b/lib/main.dart index 651c31bc0..549cfb949 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -31,8 +31,16 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); MediaKit.ensureInitialized(); await GStorage.init(); - if (GStorage.setting.get(SettingBoxKey.autoClearCache, defaultValue: true)) { + if (GStorage.setting.get(SettingBoxKey.autoClearCache, defaultValue: false)) { await CacheManage.clearLibraryCache(); + } else { + final num maxCacheSize = GStorage.maxCacheSize; + if (maxCacheSize != 0) { + final double currCache = await CacheManage().loadApplicationCache(); + if (currCache >= maxCacheSize) { + await CacheManage.clearLibraryCache(); + } + } } if (GStorage.setting .get(SettingBoxKey.horizontalScreen, defaultValue: false)) { diff --git a/lib/pages/about/view.dart b/lib/pages/about/view.dart index e7d601929..a425c8574 100644 --- a/lib/pages/about/view.dart +++ b/lib/pages/about/view.dart @@ -46,7 +46,8 @@ class _AboutPageState extends State { } Future getCacheSize() async { - cacheSize.value = await CacheManage().loadApplicationCache(); + cacheSize.value = + CacheManage.formatSize(await CacheManage().loadApplicationCache()); } Future getCurrentApp() async { diff --git a/lib/pages/setting/widgets/model.dart b/lib/pages/setting/widgets/model.dart index 1e4a996b3..a81fddb90 100644 --- a/lib/pages/setting/widgets/model.dart +++ b/lib/pages/setting/widgets/model.dart @@ -35,6 +35,7 @@ import 'package:PiliPlus/plugin/pl_player/models/bottom_progress_behavior.dart'; import 'package:PiliPlus/plugin/pl_player/models/fullscreen_mode.dart'; import 'package:PiliPlus/plugin/pl_player/utils/fullscreen.dart'; import 'package:PiliPlus/utils/accounts/account_manager/account_mgr.dart'; +import 'package:PiliPlus/utils/cache_manage.dart'; import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/feed_back.dart'; import 'package:PiliPlus/utils/global_data.dart'; @@ -2464,7 +2465,56 @@ List get extraSettings => [ subtitle: '每次启动时清除缓存', leading: const Icon(Icons.auto_delete_outlined), setKey: SettingBoxKey.autoClearCache, - defaultVal: true, + defaultVal: false, + ), + SettingsModel( + settingsType: SettingsType.normal, + title: '最大缓存大小', + getSubtitle: () { + final num = GStorage.maxCacheSize; + return '当前最大缓存大小: 「${num == 0 ? '无限' : CacheManage.formatSize(GStorage.maxCacheSize)}」'; + }, + onTap: (setState) { + showDialog( + context: Get.context!, + builder: (context) { + String valueStr = ''; + return AlertDialog( + title: const Text('最大缓存大小'), + content: TextField( + autofocus: true, + onChanged: (value) => valueStr = value, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter.allow(RegExp(r'[\d\.]+')), + ], + decoration: InputDecoration(suffixText: 'MB'), + ), + actions: [ + TextButton( + onPressed: Get.back, + child: Text( + '取消', + style: TextStyle( + color: Theme.of(context).colorScheme.outline), + ), + ), + TextButton( + onPressed: () async { + Get.back(); + num value = num.tryParse(valueStr) ?? 0; + await GStorage.setting + .put(SettingBoxKey.maxCacheSize, value * 1024 * 1024); + setState(); + }, + child: const Text('确定'), + ), + ], + ); + }, + ); + }, + leading: const Icon(Icons.delete_outlined), ), SettingsModel( settingsType: SettingsType.sw1tch, diff --git a/lib/utils/cache_manage.dart b/lib/utils/cache_manage.dart index 75c932dbf..5de201fb6 100644 --- a/lib/utils/cache_manage.dart +++ b/lib/utils/cache_manage.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:io'; +import 'package:PiliPlus/utils/extension.dart'; import 'package:path_provider/path_provider.dart'; class CacheManage { @@ -10,7 +11,7 @@ class CacheManage { factory CacheManage() => cacheManage; // 获取缓存目录 - Future loadApplicationCache() async { + Future loadApplicationCache() async { /// clear all of image in memory // clearMemoryImageCache(); /// get ImageCache @@ -41,7 +42,7 @@ class CacheManage { cacheSize += value; } - return formatSize(cacheSize); + return cacheSize; } // 循环计算文件的大小(递归) @@ -62,15 +63,15 @@ class CacheManage { } // 缓存大小格式转换 - static String formatSize(double value) { - List unitArr = ['B', 'K', 'M', 'G']; + static String formatSize(num value) { + List unitArr = const ['B', 'K', 'M', 'G', 'T', 'P']; int index = 0; - while (value > 1024) { + while (value >= 1024) { index++; value = value / 1024; } String size = value.toStringAsFixed(2); - return size + unitArr[index]; + return size + unitArr.getOrElse(index, orElse: () => ''); } /// 清除 Documents 目录下的 DioCache.db diff --git a/lib/utils/extension.dart b/lib/utils/extension.dart index 86d5882e8..6a634a1bb 100644 --- a/lib/utils/extension.dart +++ b/lib/utils/extension.dart @@ -45,6 +45,10 @@ extension ListExt on List? { return this![index]; } + T getOrElse(int index, {required T Function() orElse}) { + return getOrNull(index) ?? orElse(); + } + bool eq(List? other) { if (this == null) { return other == null; diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index bb51dd201..1c26481a6 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -468,6 +468,9 @@ class GStorage { SettingBoxKey.pageTransition, defaultValue: Transition.native.index)]; + static num get maxCacheSize => GStorage.setting + .get(SettingBoxKey.maxCacheSize, defaultValue: pow(1024, 3)); + static List get dynamicDetailRatio => List.from(setting .get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0])); @@ -669,6 +672,7 @@ class SettingBoxKey { /// 其他 autoUpdate = 'autoUpdate', autoClearCache = 'autoClearCache', + maxCacheSize = 'maxCacheSize', defaultShowComment = 'defaultShowComment', replySortType = 'replySortType', defaultDynamicType = 'defaultDynamicType',