2024-07-27 19:13:59 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-28 00:25:51 +02:00
|
|
|
import FileTransferModel, {
|
|
|
|
|
TransferData,
|
|
|
|
|
} from "../../Model/Global/fileTransferModel";
|
2024-07-27 19:13:59 +02:00
|
|
|
import StorageModel, {
|
|
|
|
|
StorageModelSubPath,
|
2024-07-28 03:46:08 +02:00
|
|
|
filePaths,
|
2024-07-27 19:13:59 +02:00
|
|
|
} from "../../Model/Global/storageModel";
|
|
|
|
|
|
|
|
|
|
import ChatListModel from "../../Model/Chat/chatListModel";
|
|
|
|
|
import ChatModel from "../../Model/Chat/chatModel";
|
|
|
|
|
import { translations } from "../../View/translations";
|
|
|
|
|
|
|
|
|
|
export default class FileTransferViewModel {
|
|
|
|
|
fileTransferModel: FileTransferModel;
|
|
|
|
|
chatListModel: ChatListModel;
|
|
|
|
|
|
|
|
|
|
// state
|
2024-07-28 00:02:17 +02:00
|
|
|
presentedModal: React.State<FileTransferModal | undefined> =
|
|
|
|
|
new React.State<any>(undefined);
|
2024-07-27 19:13:59 +02:00
|
|
|
|
|
|
|
|
generalFileOptions: React.ListState<FileTransferOption> =
|
|
|
|
|
new React.ListState();
|
|
|
|
|
chatFileOptions: React.ListState<FileTransferOption> = new React.ListState();
|
2024-07-27 19:40:16 +02:00
|
|
|
selectedPaths: React.ListState<string[]> = new React.ListState();
|
2024-07-27 19:13:59 +02:00
|
|
|
|
2024-07-28 00:25:51 +02:00
|
|
|
transferChannel: React.State<string> = new React.State("");
|
|
|
|
|
transferKey: React.State<string> = new React.State("");
|
|
|
|
|
|
2024-07-28 03:46:08 +02:00
|
|
|
receivingTransferChannel: React.State<string> = new React.State("");
|
|
|
|
|
receivingTransferKey: React.State<string> = new React.State("");
|
|
|
|
|
|
2024-07-28 02:27:09 +02:00
|
|
|
filePathsSent: React.ListState<string> = new React.ListState();
|
2024-07-28 03:46:08 +02:00
|
|
|
filesSentCount: React.State<number> = React.createProxyState(
|
|
|
|
|
[this.filePathsSent],
|
|
|
|
|
() => this.filePathsSent.value.size
|
|
|
|
|
);
|
2024-07-28 02:27:09 +02:00
|
|
|
filesSentText: React.State<string> = React.createProxyState(
|
|
|
|
|
[this.filesSentCount],
|
|
|
|
|
() =>
|
|
|
|
|
translations.dataTransferModal.filesSentCount(this.filesSentCount.value)
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-28 03:46:08 +02: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],
|
|
|
|
|
() =>
|
|
|
|
|
translations.dataTransferModal.filesReceivedCount(
|
|
|
|
|
this.filesReceivedCount.value
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-28 00:02:17 +02:00
|
|
|
// guards
|
|
|
|
|
hasNoPathsSelected: React.State<boolean> = React.createProxyState(
|
|
|
|
|
[this.selectedPaths],
|
|
|
|
|
() => this.selectedPaths.value.size == 0
|
|
|
|
|
);
|
2024-07-28 03:46:08 +02:00
|
|
|
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 00:02:17 +02:00
|
|
|
|
2024-07-27 19:13:59 +02:00
|
|
|
// methods
|
|
|
|
|
getOptions = (): void => {
|
|
|
|
|
this.generalFileOptions.clear();
|
|
|
|
|
this.chatFileOptions.clear();
|
2024-07-28 03:46:08 +02:00
|
|
|
this.selectedPaths.clear();
|
2024-07-27 19:13:59 +02:00
|
|
|
|
|
|
|
|
this.generalFileOptions.add(
|
|
|
|
|
{
|
2024-07-27 23:45:36 +02:00
|
|
|
label: translations.dataTransferModal.connectionData,
|
2024-07-28 03:46:08 +02:00
|
|
|
path: StorageModel.getPath(
|
|
|
|
|
StorageModelSubPath.ConnectionModel,
|
|
|
|
|
filePaths.connectionModel.previousAddresses
|
|
|
|
|
),
|
2024-07-27 19:13:59 +02:00
|
|
|
},
|
|
|
|
|
{
|
2024-07-27 23:45:36 +02:00
|
|
|
label: translations.dataTransferModal.settingsData,
|
2024-07-27 19:13:59 +02:00
|
|
|
path: StorageModel.getPath(StorageModelSubPath.SettingsModel, []),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const chatModels: Set<ChatModel> = this.chatListModel.chatModels;
|
|
|
|
|
for (const chatModel of chatModels) {
|
|
|
|
|
this.chatFileOptions.add({
|
|
|
|
|
label: chatModel.info.primaryChannel,
|
|
|
|
|
path: chatModel.getBasePath(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-28 00:25:51 +02:00
|
|
|
getTransferData = (): void => {
|
|
|
|
|
const transferData: TransferData =
|
|
|
|
|
this.fileTransferModel.generateTransferData();
|
|
|
|
|
this.transferChannel.value = transferData.channel;
|
|
|
|
|
this.transferKey.value = transferData.key;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-27 19:13:59 +02:00
|
|
|
// view
|
2024-07-28 00:02:17 +02:00
|
|
|
showDirectionSelectionModal = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.DirectionSelection;
|
2024-07-28 00:25:51 +02:00
|
|
|
this.getOptions();
|
2024-07-28 00:02:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showFileSelectionModal = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.FileSelection;
|
2024-07-28 00:25:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showTransferDataModal = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.TransferDataDisplay;
|
|
|
|
|
this.getTransferData();
|
2024-07-27 19:13:59 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-28 02:27:09 +02:00
|
|
|
initiateTransfer = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.TransferDisplay;
|
|
|
|
|
this.didNotFinishSending.value = true;
|
|
|
|
|
this.filePathsSent.clear();
|
|
|
|
|
|
|
|
|
|
this.fileTransferModel.sendFiles(
|
|
|
|
|
this.selectedPaths.value.values(),
|
|
|
|
|
(path: string) => {
|
|
|
|
|
console.log(path);
|
|
|
|
|
this.filePathsSent.add(path);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.didNotFinishSending.value = false;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-28 03:46:08 +02:00
|
|
|
showTransferDataInputModal = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.TransferDataInput;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
prepareReceivingData = (): void => {
|
|
|
|
|
this.presentedModal.value = FileTransferModal.ReceptionDisplay;
|
|
|
|
|
this.filePathsReceived.clear();
|
|
|
|
|
|
|
|
|
|
const transferData: TransferData = {
|
|
|
|
|
channel: this.receivingTransferChannel.value,
|
|
|
|
|
key: this.receivingTransferKey.value,
|
|
|
|
|
};
|
|
|
|
|
this.fileTransferModel.prepareToReceive(transferData);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-28 00:02:17 +02:00
|
|
|
hideModal = (): void => {
|
|
|
|
|
this.presentedModal.value = undefined;
|
2024-07-27 19:13:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor(
|
|
|
|
|
fileTransferModel: FileTransferModel,
|
|
|
|
|
chatListModel: ChatListModel
|
|
|
|
|
) {
|
|
|
|
|
this.fileTransferModel = fileTransferModel;
|
|
|
|
|
this.chatListModel = chatListModel;
|
2024-07-28 03:46:08 +02:00
|
|
|
|
|
|
|
|
this.fileTransferModel.fileHandlerManager.addHandler(
|
|
|
|
|
this.handleReceivedFile
|
|
|
|
|
);
|
2024-07-27 19:13:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface FileTransferOption {
|
|
|
|
|
label: string;
|
|
|
|
|
path: string[];
|
|
|
|
|
}
|
2024-07-27 23:45:36 +02:00
|
|
|
|
2024-07-28 00:02:17 +02:00
|
|
|
export enum FileTransferModal {
|
2024-07-27 23:45:36 +02:00
|
|
|
DirectionSelection,
|
|
|
|
|
|
|
|
|
|
// sending
|
|
|
|
|
FileSelection,
|
|
|
|
|
TransferDataDisplay,
|
|
|
|
|
TransferDisplay,
|
|
|
|
|
|
|
|
|
|
// receiving
|
|
|
|
|
TransferDataInput,
|
2024-07-28 00:02:17 +02:00
|
|
|
ReceptionDisplay,
|
|
|
|
|
}
|