cm file delete/create log

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-11-05 15:55:09 +08:00
parent 27112e3480
commit 663d355a48
9 changed files with 345 additions and 46 deletions

View File

@@ -10,8 +10,8 @@ import 'file_model.dart';
class CmFileModel {
final WeakReference<FFI> parent;
final currentJobTable = RxList<JobProgress>();
final _jobTables = HashMap<int, RxList<JobProgress>>.fromEntries([]);
final currentJobTable = RxList<CmFileLog>();
final _jobTables = HashMap<int, RxList<CmFileLog>>.fromEntries([]);
Stopwatch stopwatch = Stopwatch();
int _lastElapsed = 0;
@@ -19,14 +19,24 @@ class CmFileModel {
void updateCurrentClientId(int id) {
if (_jobTables[id] == null) {
_jobTables[id] = RxList<JobProgress>();
_jobTables[id] = RxList<CmFileLog>();
}
Future.delayed(Duration.zero, () {
currentJobTable.value = _jobTables[id]!;
});
}
onFileTransferLog(dynamic log) {
onFileTransferLog(Map<String, dynamic> evt) {
if (evt['transfer'] != null) {
_onFileTransfer(evt['transfer']);
} else if (evt['remove'] != null) {
_onFileRemove(evt['remove']);
} else if (evt['create_dir'] != null) {
_onDirCreate(evt['create_dir']);
}
}
_onFileTransfer(dynamic log) {
try {
dynamic d = jsonDecode(log);
if (!stopwatch.isRunning) stopwatch.start();
@@ -56,9 +66,9 @@ class CmFileModel {
debugPrint("jobTable should not be null");
return;
}
JobProgress? job = jobTable.firstWhereOrNull((e) => e.id == data.id);
CmFileLog? job = jobTable.firstWhereOrNull((e) => e.id == data.id);
if (job == null) {
job = JobProgress();
job = CmFileLog();
jobTable.add(job);
final currentSelectedTab =
gFFI.serverModel.tabController.state.value.selectedTabInfo;
@@ -68,14 +78,14 @@ class CmFileModel {
}
}
job.id = data.id;
job.isRemoteToLocal = data.isRemote;
job.action =
data.isRemote ? CmFileAction.remoteToLocal : CmFileAction.localToRemote;
job.fileName = data.path;
job.totalSize = data.totalSize;
job.finishedSize = data.finishedSize;
if (job.finishedSize > data.totalSize) {
job.finishedSize = data.totalSize;
}
job.isRemoteToLocal = data.isRemote;
if (job.finishedSize > 0) {
if (job.finishedSize < job.totalSize) {
@@ -99,6 +109,112 @@ class CmFileModel {
}
jobTable.refresh();
}
_onFileRemove(dynamic log) {
try {
dynamic d = jsonDecode(log);
FileActionLog data = FileActionLog.fromJson(d);
Client? client =
gFFI.serverModel.clients.firstWhereOrNull((e) => e.id == data.connId);
var jobTable = _jobTables[data.connId];
if (jobTable == null) {
debugPrint("jobTable should not be null");
return;
}
int removeUnreadCount = 0;
if (data.dir) {
removeUnreadCount = jobTable
.where((e) =>
e.action == CmFileAction.remove &&
e.fileName.startsWith(data.path))
.length;
jobTable.removeWhere((e) =>
e.action == CmFileAction.remove &&
e.fileName.startsWith(data.path));
}
jobTable.add(CmFileLog()
..id = data.id
..fileName = data.path
..action = CmFileAction.remove
..state = JobState.done);
final currentSelectedTab =
gFFI.serverModel.tabController.state.value.selectedTabInfo;
if (!(gFFI.chatModel.isShowCMSidePage &&
currentSelectedTab.key == data.connId.toString())) {
// Wrong number if unreadCount changes during deletion, which rarely happens
RxInt? rx = client?.unreadChatMessageCount;
if (rx != null) {
if (rx.value >= removeUnreadCount) {
rx.value -= removeUnreadCount;
}
rx.value += 1;
}
}
jobTable.refresh();
} catch (e) {
debugPrint('$e');
}
}
_onDirCreate(dynamic log) {
try {
dynamic d = jsonDecode(log);
FileActionLog data = FileActionLog.fromJson(d);
Client? client =
gFFI.serverModel.clients.firstWhereOrNull((e) => e.id == data.connId);
var jobTable = _jobTables[data.connId];
if (jobTable == null) {
debugPrint("jobTable should not be null");
return;
}
jobTable.add(CmFileLog()
..id = data.id
..fileName = data.path
..action = CmFileAction.createDir
..state = JobState.done);
final currentSelectedTab =
gFFI.serverModel.tabController.state.value.selectedTabInfo;
if (!(gFFI.chatModel.isShowCMSidePage &&
currentSelectedTab.key == data.connId.toString())) {
client?.unreadChatMessageCount.value += 1;
}
jobTable.refresh();
} catch (e) {
debugPrint('$e');
}
}
}
enum CmFileAction {
none,
remoteToLocal,
localToRemote,
remove,
createDir,
}
class CmFileLog {
JobState state = JobState.none;
var id = 0;
var speed = 0.0;
var finishedSize = 0;
var totalSize = 0;
CmFileAction action = CmFileAction.none;
var fileName = "";
var err = "";
int lastTransferredSize = 0;
String display() {
if (state == JobState.done && err == "skipped") {
return translate("Skipped");
}
return state.display();
}
bool isTransfer() {
return action == CmFileAction.remoteToLocal ||
action == CmFileAction.localToRemote;
}
}
class TransferJobSerdeData {
@@ -140,3 +256,25 @@ class TransferJobSerdeData {
error: d['error'] ?? '',
);
}
class FileActionLog {
int id = 0;
int connId = 0;
String path = '';
bool dir = false;
FileActionLog({
required this.connId,
required this.id,
required this.path,
required this.dir,
});
FileActionLog.fromJson(dynamic d)
: this(
connId: d['connId'] ?? 0,
id: d['id'] ?? 0,
path: d['path'] ?? '',
dir: d['dir'] ?? false,
);
}