152 lines
4.9 KiB
Dart
Executable File
152 lines
4.9 KiB
Dart
Executable File
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:star_lock/main/lockDetail/videoLog/videoLog/videoLog_entity.dart';
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
import 'videoLog_state.dart';
|
|
|
|
class VideoLogLogic extends BaseGetXController {
|
|
VideoLogState state = VideoLogState();
|
|
|
|
void getLockCloudStorageList() async {
|
|
var entity = await ApiRepository.to.getLockCloudStorageList(
|
|
lockId: state.getLockId.value,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.videoLogList.value = entity.data!;
|
|
state.videoLogList.value.forEach((element) {
|
|
// 过滤掉 imagesUrl 和 videoUrl 都为 null 或空字符串的项
|
|
element.recordList = element.recordList!
|
|
.where((record) =>
|
|
(record.imagesUrl != null && record.imagesUrl!.isNotEmpty) ||
|
|
(record.videoUrl != null && record.videoUrl!.isNotEmpty))
|
|
.toList();
|
|
});
|
|
|
|
state.videoLogList.refresh();
|
|
}
|
|
}
|
|
|
|
// 列出已下载的视频文件
|
|
Future<void> listDownloadedVideos() async {
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
Directory downloadsDir = Directory('${appDocDir.path}/downloads');
|
|
|
|
if (await downloadsDir.exists()) {
|
|
List<FileSystemEntity> files = downloadsDir.listSync();
|
|
final list =
|
|
files.where((file) => file is File).map((file) => file.path).toList();
|
|
list.forEach((element) async {
|
|
final downloadTime = await getDownloadTime(element);
|
|
// 获取每一个文件夹对应的下载时间
|
|
});
|
|
}
|
|
}
|
|
|
|
// 按天分组统计下载文件
|
|
Future<List<CloudStorageData>> groupDownloadsByDay() async {
|
|
final appDocDir = await getApplicationDocumentsDirectory();
|
|
final logFilePath = '${appDocDir.path}/download_log.json';
|
|
|
|
// 初始化结果映射
|
|
Map<String, List<RecordListData>> groupedDownloads = {};
|
|
|
|
if (await File(logFilePath).exists()) {
|
|
final content = await File(logFilePath).readAsString();
|
|
final logData = Map<String, int>.from(json.decode(content));
|
|
|
|
// 遍历所有记录
|
|
logData.forEach((filePath, timestamp) {
|
|
final downloadDateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
|
|
final dateKey =
|
|
'${downloadDateTime.year}-${downloadDateTime.month.toString().padLeft(2, '0')}-${downloadDateTime.day.toString().padLeft(2, '0')}';
|
|
|
|
// 如果日期不存在于映射中,则初始化为空列表
|
|
if (!groupedDownloads.containsKey(dateKey)) {
|
|
groupedDownloads[dateKey] = [];
|
|
}
|
|
|
|
// 将文件记录添加到对应日期的列表中
|
|
if (filePath.endsWith('.jpg')) {
|
|
groupedDownloads[dateKey]?.add(
|
|
RecordListData(operateDate: timestamp, imagesUrl: filePath),
|
|
);
|
|
} else if (filePath.endsWith('.mp4')) {
|
|
groupedDownloads[dateKey]?.add(
|
|
RecordListData(operateDate: timestamp, videoUrl: filePath),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 将分组结果转换为 CloudStorageData 列表
|
|
List<CloudStorageData> cloudStorageData = [];
|
|
groupedDownloads.forEach((dateKey, recordList) {
|
|
cloudStorageData.add(
|
|
CloudStorageData(date: dateKey, recordList: recordList),
|
|
);
|
|
});
|
|
state.lockVideoList.value = cloudStorageData;
|
|
return cloudStorageData;
|
|
}
|
|
|
|
// 获取文件的下载时间
|
|
Future<int?> getDownloadTime(String filePath) async {
|
|
final appDocDir = await getApplicationDocumentsDirectory();
|
|
final logFilePath = '${appDocDir.path}/download_log.json';
|
|
|
|
if (await File(logFilePath).exists()) {
|
|
final content = await File(logFilePath).readAsString();
|
|
final logData = Map<String, int>.from(json.decode(content));
|
|
return logData[filePath];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 清空下载目录和日志文件
|
|
Future<bool> clearDownloads() async {
|
|
try {
|
|
// 获取应用专属目录
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
|
|
// 删除下载目录中的所有文件
|
|
final downloadsDir = Directory('${appDocDir.path}/downloads');
|
|
if (await downloadsDir.exists()) {
|
|
await downloadsDir.list().forEach((entity) {
|
|
if (entity is File) {
|
|
entity.delete(recursive: true);
|
|
}
|
|
});
|
|
print('下载目录已清空');
|
|
} else {
|
|
print('下载目录不存在');
|
|
}
|
|
|
|
// 删除日志文件
|
|
final logFilePath = '${appDocDir.path}/download_log.json';
|
|
if (await File(logFilePath).exists()) {
|
|
await File(logFilePath).delete();
|
|
print('日志文件已删除');
|
|
} else {
|
|
print('日志文件不存在');
|
|
}
|
|
|
|
return true;
|
|
} catch (e) {
|
|
print('清空失败:$e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
onReady() {
|
|
super.onReady();
|
|
|
|
getLockCloudStorageList();
|
|
}
|
|
}
|