2024-07-27 19:13:59 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-28 00:25:51 +02:00
|
|
|
import FileTransferModel, {
|
2026-04-01 08:29:39 +00:00
|
|
|
TransferData,
|
2024-07-28 00:25:51 +02:00
|
|
|
} from "../../Model/Global/fileTransferModel";
|
2024-07-27 19:13:59 +02:00
|
|
|
import StorageModel, {
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths,
|
2026-04-01 08:29:39 +00:00
|
|
|
filePaths,
|
2024-07-27 19:13:59 +02:00
|
|
|
} from "../../Model/Global/storageModel";
|
|
|
|
|
|
|
|
|
|
import ChatModel from "../../Model/Chat/chatModel";
|
2026-04-12 19:20:37 +00:00
|
|
|
import CoreViewModel, { Context } from "./coreViewModel";
|
|
|
|
|
import { CommonKeys } from "../../View/keystrokes";
|
|
|
|
|
|
|
|
|
|
export default class FileTransferViewModel extends Context {
|
2026-04-01 08:29:39 +00:00
|
|
|
// state
|
2026-04-08 14:02:12 +00:00
|
|
|
presentedModal: React.State<FileTransferModals | undefined> =
|
2026-04-01 08:29:39 +00:00
|
|
|
new React.State<any>(undefined);
|
|
|
|
|
|
|
|
|
|
generalFileOptions: React.ListState<FileTransferOption> =
|
|
|
|
|
new React.ListState();
|
|
|
|
|
chatFileOptions: React.ListState<FileTransferOption> =
|
|
|
|
|
new React.ListState();
|
|
|
|
|
selectedPaths: React.ListState<string[]> = new React.ListState();
|
|
|
|
|
|
|
|
|
|
transferChannel: React.State<string> = new React.State("");
|
|
|
|
|
transferKey: React.State<string> = new React.State("");
|
|
|
|
|
|
|
|
|
|
receivingTransferChannel: React.State<string> = new React.State("");
|
|
|
|
|
receivingTransferKey: React.State<string> = new React.State("");
|
|
|
|
|
|
|
|
|
|
filePathsSent: React.ListState<string> = new React.ListState();
|
|
|
|
|
filesSentCount: React.State<number> = React.createProxyState(
|
|
|
|
|
[this.filePathsSent],
|
|
|
|
|
() => this.filePathsSent.value.size,
|
|
|
|
|
);
|
|
|
|
|
filesSentText: React.State<string> = React.createProxyState(
|
|
|
|
|
[this.filesSentCount],
|
|
|
|
|
() =>
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.translations.dataTransferModal.filesSentCount(
|
2026-04-01 08:29:39 +00:00
|
|
|
this.filesSentCount.value,
|
|
|
|
|
),
|
2024-07-27 19:13:59 +02:00
|
|
|
);
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
filePathsReceived: React.ListState<string> = new React.ListState();
|
|
|
|
|
filesReceivedCount: React.State<number> = React.createProxyState(
|
|
|
|
|
[this.filePathsReceived],
|
|
|
|
|
() => this.filePathsReceived.value.size,
|
|
|
|
|
);
|
|
|
|
|
filesReceivedText: React.State<string> = React.createProxyState(
|
|
|
|
|
[this.filesReceivedCount],
|
|
|
|
|
() =>
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.translations.dataTransferModal.filesReceivedCount(
|
2026-04-01 08:29:39 +00:00
|
|
|
this.filesReceivedCount.value,
|
|
|
|
|
),
|
2024-07-28 02:27:09 +02:00
|
|
|
);
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// guards
|
|
|
|
|
hasNoPathsSelected: React.State<boolean> = React.createProxyState(
|
|
|
|
|
[this.selectedPaths],
|
|
|
|
|
() => this.selectedPaths.value.size == 0,
|
|
|
|
|
);
|
|
|
|
|
didNotFinishSending: React.State<boolean> = new React.State(true);
|
|
|
|
|
cannotPrepareToReceive: React.State<boolean> = React.createProxyState(
|
|
|
|
|
[this.receivingTransferChannel, this.receivingTransferKey],
|
|
|
|
|
() =>
|
|
|
|
|
this.receivingTransferChannel.value == "" ||
|
|
|
|
|
this.receivingTransferKey.value == "",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// handlers
|
|
|
|
|
handleReceivedFile = (path: string): void => {
|
|
|
|
|
this.filePathsReceived.add(path);
|
|
|
|
|
};
|
2024-07-28 02:27:09 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// methods
|
|
|
|
|
getOptions = (): void => {
|
|
|
|
|
this.generalFileOptions.clear();
|
|
|
|
|
this.chatFileOptions.clear();
|
|
|
|
|
this.selectedPaths.clear();
|
|
|
|
|
|
|
|
|
|
this.generalFileOptions.add(
|
|
|
|
|
{
|
2026-04-09 16:48:22 +00:00
|
|
|
label: this.coreViewModel.translations.dataTransferModal
|
|
|
|
|
.connectionData,
|
2026-04-01 08:29:39 +00:00
|
|
|
path: StorageModel.getPath(
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths.ConnectionModel,
|
2026-04-01 08:29:39 +00:00
|
|
|
filePaths.connectionModel.previousAddresses,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-04-09 16:48:22 +00:00
|
|
|
label: this.coreViewModel.translations.dataTransferModal
|
|
|
|
|
.settingsData,
|
2026-04-01 08:29:39 +00:00
|
|
|
path: StorageModel.getPath(
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths.SettingsModel,
|
2026-04-01 08:29:39 +00:00
|
|
|
[],
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-09 16:48:22 +00:00
|
|
|
const chatModels: Set<ChatModel> =
|
|
|
|
|
this.coreViewModel.chatListModel.chatModels;
|
2026-04-01 08:29:39 +00:00
|
|
|
for (const chatModel of chatModels) {
|
|
|
|
|
this.chatFileOptions.add({
|
|
|
|
|
label: chatModel.info.primaryChannel,
|
|
|
|
|
path: chatModel.getBasePath(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-28 03:46:08 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
getTransferData = (): void => {
|
|
|
|
|
const transferData: TransferData =
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.generateTransferData();
|
2026-04-01 08:29:39 +00:00
|
|
|
this.transferChannel.value = transferData.channel;
|
|
|
|
|
this.transferKey.value = transferData.key;
|
|
|
|
|
};
|
2024-07-28 03:46:08 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// view
|
|
|
|
|
showDirectionSelectionModal = (): void => {
|
2026-04-12 19:20:37 +00:00
|
|
|
this.coreViewModel.context = this;
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.DirectionSelection;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.getOptions();
|
2024-07-28 03:46:08 +02:00
|
|
|
};
|
2026-04-01 08:29:39 +00:00
|
|
|
|
|
|
|
|
showFileSelectionModal = (): void => {
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.FileSelection;
|
2026-04-01 08:29:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showTransferDataModal = (): void => {
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.TransferDataDisplay;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.getTransferData();
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.prepareToSend();
|
2026-04-01 08:29:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
initiateTransfer = (): void => {
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.TransferDisplay;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.didNotFinishSending.value = true;
|
|
|
|
|
this.filePathsSent.clear();
|
|
|
|
|
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.sendFiles(
|
2026-04-01 08:29:39 +00:00
|
|
|
this.selectedPaths.value.values(),
|
|
|
|
|
(path: string) => {
|
|
|
|
|
console.log(path);
|
|
|
|
|
this.filePathsSent.add(path);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.didNotFinishSending.value = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showTransferDataInputModal = (): void => {
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.TransferDataInput;
|
2026-04-01 08:29:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
prepareReceivingData = (): void => {
|
2026-04-13 10:26:03 +00:00
|
|
|
if (this.cannotPrepareToReceive.value == true) return;
|
2026-04-08 14:02:12 +00:00
|
|
|
this.presentedModal.value = FileTransferModals.ReceptionDisplay;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.filePathsReceived.clear();
|
|
|
|
|
|
|
|
|
|
const transferData: TransferData = {
|
|
|
|
|
channel: this.receivingTransferChannel.value,
|
|
|
|
|
key: this.receivingTransferKey.value,
|
|
|
|
|
};
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.prepareToReceive(transferData);
|
2026-04-01 08:29:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hideModal = (): void => {
|
2026-04-12 19:20:37 +00:00
|
|
|
this.coreViewModel.closeContext(this.contextId);
|
2026-04-01 08:29:39 +00:00
|
|
|
this.presentedModal.value = undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
2026-04-13 10:58:21 +00:00
|
|
|
constructor(public readonly coreViewModel: CoreViewModel) {
|
2026-04-13 19:45:52 +00:00
|
|
|
super("file-transfer");
|
2026-04-12 19:20:37 +00:00
|
|
|
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.fileHandlerManager.addHandler(
|
2026-04-01 08:29:39 +00:00
|
|
|
this.handleReceivedFile,
|
|
|
|
|
);
|
2026-04-06 15:50:13 +00:00
|
|
|
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.fileTransferModel.readyToSendHandlerManager.addHandler(
|
|
|
|
|
() => this.initiateTransfer(),
|
2026-04-06 15:50:13 +00:00
|
|
|
);
|
2026-04-12 19:20:37 +00:00
|
|
|
|
|
|
|
|
// keystrokes
|
|
|
|
|
this.registerKeyStroke(CommonKeys.CloseOrCancel, this.hideModal);
|
2026-04-01 08:29:39 +00:00
|
|
|
}
|
2024-07-27 19:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface FileTransferOption {
|
2026-04-01 08:29:39 +00:00
|
|
|
label: string;
|
|
|
|
|
path: string[];
|
2024-07-27 19:13:59 +02:00
|
|
|
}
|
2024-07-27 23:45:36 +02:00
|
|
|
|
2026-04-08 14:02:12 +00:00
|
|
|
export enum FileTransferModals {
|
2026-04-01 08:29:39 +00:00
|
|
|
DirectionSelection,
|
2024-07-27 23:45:36 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// sending
|
|
|
|
|
FileSelection,
|
|
|
|
|
TransferDataDisplay,
|
|
|
|
|
TransferDisplay,
|
2024-07-27 23:45:36 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// receiving
|
|
|
|
|
TransferDataInput,
|
|
|
|
|
ReceptionDisplay,
|
2024-07-28 00:02:17 +02:00
|
|
|
}
|