mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-06-01 08:38:18 +08:00
feat: max cache size
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -95,7 +95,7 @@
|
||||
- [x] 合集图片
|
||||
- [x] 删除/置顶/撤回私信
|
||||
- [x] 举报用户/评论/视频/动态
|
||||
- [x] 删除/发布文本/图片动态
|
||||
- [x] 删除/发布/置顶文本/图片动态
|
||||
- [x] 其他
|
||||
|
||||
## opt
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -46,7 +46,8 @@ class _AboutPageState extends State<AboutPage> {
|
||||
}
|
||||
|
||||
Future getCacheSize() async {
|
||||
cacheSize.value = await CacheManage().loadApplicationCache();
|
||||
cacheSize.value =
|
||||
CacheManage.formatSize(await CacheManage().loadApplicationCache());
|
||||
}
|
||||
|
||||
Future getCurrentApp() async {
|
||||
|
||||
@@ -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<SettingsModel> 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,
|
||||
|
||||
@@ -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<String> loadApplicationCache() async {
|
||||
Future<double> 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<String> unitArr = ['B', 'K', 'M', 'G'];
|
||||
static String formatSize(num value) {
|
||||
List<String> 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
|
||||
|
||||
@@ -45,6 +45,10 @@ extension ListExt<T> on List<T>? {
|
||||
return this![index];
|
||||
}
|
||||
|
||||
T getOrElse(int index, {required T Function() orElse}) {
|
||||
return getOrNull(index) ?? orElse();
|
||||
}
|
||||
|
||||
bool eq(List<T>? other) {
|
||||
if (this == null) {
|
||||
return other == null;
|
||||
|
||||
@@ -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<double> get dynamicDetailRatio => List<double>.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',
|
||||
|
||||
Reference in New Issue
Block a user