Merge branch 'fix'

This commit is contained in:
guozhigq
2023-08-15 13:06:18 +08:00
2 changed files with 44 additions and 4 deletions

View File

@ -93,6 +93,12 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
} }
} }
@override
void dispose() async {
await GStrorage.close();
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Box localCache = GStrorage.localCache; Box localCache = GStrorage.localCache;

View File

@ -17,14 +17,19 @@ class GStrorage {
static late final Box video; static late final Box video;
static Future<void> init() async { static Future<void> init() async {
final dir = await getApplicationDocumentsDirectory(); final dir = await getApplicationSupportDirectory();
final path = dir.path; final path = dir.path;
await Hive.initFlutter('$path/hive'); await Hive.initFlutter('$path/hive');
regAdapter(); regAdapter();
// 用户信息 // 用户信息
user = await Hive.openBox('user'); user = await Hive.openBox('user');
// 首页推荐视频 // 首页推荐视频
recVideo = await Hive.openBox('recVideo'); recVideo = await Hive.openBox(
'recVideo',
compactionStrategy: (entries, deletedEntries) {
return deletedEntries > 20;
},
);
// 登录用户信息 // 登录用户信息
userInfo = await Hive.openBox('userInfo'); userInfo = await Hive.openBox('userInfo');
// 本地缓存 // 本地缓存
@ -32,9 +37,19 @@ class GStrorage {
// 设置 // 设置
setting = await Hive.openBox('setting'); setting = await Hive.openBox('setting');
// 热搜关键词 // 热搜关键词
hotKeyword = await Hive.openBox('hotKeyword'); hotKeyword = await Hive.openBox(
'hotKeyword',
compactionStrategy: (entries, deletedEntries) {
return deletedEntries > 10;
},
);
// 搜索历史 // 搜索历史
historyword = await Hive.openBox('historyWord'); historyword = await Hive.openBox(
'historyWord',
compactionStrategy: (entries, deletedEntries) {
return deletedEntries > 10;
},
);
} }
static regAdapter() { static regAdapter() {
@ -53,6 +68,25 @@ class GStrorage {
// 视频设置 // 视频设置
video = await Hive.openBox('video'); video = await Hive.openBox('video');
} }
static Future<void> close() async {
user.compact();
user.close();
recVideo.compact();
recVideo.close();
userInfo.compact();
userInfo.close();
hotKeyword.compact();
hotKeyword.close();
historyword.compact();
historyword.close();
localCache.compact();
localCache.close();
setting.compact();
setting.close();
video.compact();
video.close();
}
} }
// 约定 key // 约定 key