Files
CT/src/ViewModel/Global/fileTransferViewModel.ts
T

204 lines
6.2 KiB
TypeScript
Raw Normal View History

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 ChatListModel from "../../Model/Chat/chatListModel";
import ChatModel from "../../Model/Chat/chatModel";
import { translations } from "../../View/translations";
export default class FileTransferViewModel {
2026-04-01 08:29:39 +00:00
fileTransferModel: FileTransferModel;
chatListModel: ChatListModel;
// 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],
() =>
translations.dataTransferModal.filesSentCount(
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],
() =>
translations.dataTransferModal.filesReceivedCount(
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(
{
label: translations.dataTransferModal.connectionData,
path: StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.ConnectionModel,
2026-04-01 08:29:39 +00:00
filePaths.connectionModel.previousAddresses,
),
},
{
label: translations.dataTransferModal.settingsData,
path: StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.SettingsModel,
2026-04-01 08:29:39 +00:00
[],
),
},
);
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 03:46:08 +02:00
2026-04-01 08:29:39 +00:00
getTransferData = (): void => {
const transferData: TransferData =
this.fileTransferModel.generateTransferData();
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-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-06 15:50:13 +00:00
this.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();
this.fileTransferModel.sendFiles(
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-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,
};
this.fileTransferModel.prepareToReceive(transferData);
};
hideModal = (): void => {
this.presentedModal.value = undefined;
};
// init
constructor(
fileTransferModel: FileTransferModel,
chatListModel: ChatListModel,
) {
this.fileTransferModel = fileTransferModel;
this.chatListModel = chatListModel;
this.fileTransferModel.fileHandlerManager.addHandler(
this.handleReceivedFile,
);
2026-04-06 15:50:13 +00:00
this.fileTransferModel.readyToSendHandlerManager.addHandler(() =>
this.initiateTransfer(),
);
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,
}