pikapika/lib/basic/config/AutoClean.dart

70 lines
1.7 KiB
Dart
Raw Normal View History

2021-11-15 08:26:53 +00:00
/// 自动清理
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Method.dart';
2021-09-29 23:57:09 +00:00
2021-11-04 05:56:25 +00:00
const _autoCleanMap = {
"一个月前": "${3600 * 24 * 30}",
"一周前": "${3600 * 24 * 7}",
"一天前": "${3600 * 24 * 1}",
"不自动清理": "${0}",
};
2021-09-29 23:57:09 +00:00
late String _autoCleanSec;
2021-11-04 05:56:25 +00:00
Future<dynamic> initAutoClean() async {
2021-10-13 13:57:35 +00:00
_autoCleanSec =
await method.loadProperty("autoCleanSec", "${3600 * 24 * 30}");
if ("0" != _autoCleanSec) {
await method.autoClean(_autoCleanSec);
}
2021-09-29 23:57:09 +00:00
}
2021-11-04 05:56:25 +00:00
String _currentAutoCleanSec() {
2021-09-29 23:57:09 +00:00
for (var value in _autoCleanMap.entries) {
if (value.value == _autoCleanSec) {
return value.key;
}
}
return "";
}
2021-11-04 05:56:25 +00:00
Future<void> _chooseAutoCleanSec(BuildContext context) async {
2021-09-29 23:57:09 +00:00
String? choose = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
2022-03-19 04:12:27 +00:00
title: const Text('选择自动清理周期'),
2021-09-29 23:57:09 +00:00
children: <Widget>[
..._autoCleanMap.entries.map(
(e) => SimpleDialogOption(
child: Text(e.key),
onPressed: () {
Navigator.of(context).pop(e.value);
},
),
),
],
);
},
);
if (choose != null) {
await method.saveProperty("autoCleanSec", choose);
_autoCleanSec = choose;
}
}
2021-11-04 03:45:22 +00:00
Widget autoCleanSecSetting() {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
2022-03-19 04:12:27 +00:00
title: const Text("自动清理缓存"),
2021-11-04 05:56:25 +00:00
subtitle: Text(_currentAutoCleanSec()),
2021-11-04 03:45:22 +00:00
onTap: () async {
2021-11-04 05:56:25 +00:00
await _chooseAutoCleanSec(context);
2021-11-04 03:45:22 +00:00
setState(() {});
},
);
},
);
}