56 lines
1.5 KiB
Dart
56 lines
1.5 KiB
Dart
import 'package:collection/collection.dart';
|
|
import 'package:flutter_translate/common/hive/hive.dart';
|
|
import 'package:flutter_translate/common/utils/date_utils.dart';
|
|
import 'package:flutter_translate/model/history_model.dart';
|
|
|
|
class HistoryData {
|
|
/// 私有构造函数
|
|
HistoryData._();
|
|
|
|
/// 静态常量用于保存类的唯一实例
|
|
static final HistoryData _instance = HistoryData._();
|
|
|
|
/// 工厂构造函数返回类的唯一实例
|
|
factory HistoryData() {
|
|
return _instance;
|
|
}
|
|
|
|
/// 声明盒子
|
|
/// 注意, main函数中这个盒子已经打开, 可以进行存储操作
|
|
final _box = getHistoryBox();
|
|
|
|
/// 返回所有数据
|
|
List<HistoryModel> findAll() {
|
|
return _box.values.toList();
|
|
}
|
|
|
|
/// 返回分组后的所有数据
|
|
Map<String, List<HistoryModel>> findGroup() {
|
|
// 使用 groupBy 函数进行分组
|
|
var groupedEvents = groupBy(
|
|
findAll().reversed.toList(),
|
|
(HistoryModel event) => DateUtils.formatDateMs(
|
|
event.translationTime ?? DateUtils.getNowTimestamp(),
|
|
format: DateFormats.yMoD));
|
|
return groupedEvents;
|
|
}
|
|
|
|
/// 添加数据
|
|
Future<int> add(HistoryModel entity) async {
|
|
entity.translationTime = DateUtils.getNowTimestamp();
|
|
return await _box.add(entity);
|
|
}
|
|
|
|
/// 删除
|
|
Future<void> remove(int index) async {
|
|
await _box.deleteAt(index);
|
|
await _box.flush();
|
|
}
|
|
|
|
/// 清空所有数据
|
|
Future<void> drop() async {
|
|
await _box.clear();
|
|
await _box.flush();
|
|
}
|
|
}
|