cleanup, add fileContentHandler for ViewModel

This commit is contained in:
marlon-erler
2024-07-23 00:25:28 +02:00
parent 224c00c8d8
commit 39b50e9254
6 changed files with 109 additions and 49 deletions
+44 -20
View File
@@ -696,6 +696,8 @@
chatModel; chatModel;
storageModel; storageModel;
taskModel; taskModel;
fileContentHandler = () => {
};
// paths // paths
getBasePath = () => { getBasePath = () => {
return StorageModel.getPath( return StorageModel.getPath(
@@ -729,6 +731,7 @@
const didStore = this.storeFileContent(fileContent); const didStore = this.storeFileContent(fileContent);
if (didStore == false) return; if (didStore == false) return;
this.taskModel.handleFileContent(fileContent); this.taskModel.handleFileContent(fileContent);
this.fileContentHandler(fileContent);
}; };
// methods // methods
addFileContent = (fileContent) => { addFileContent = (fileContent) => {
@@ -776,6 +779,10 @@
); );
return fileContent; return fileContent;
}; };
// other
setFileContentHandler = (handler) => {
this.fileContentHandler = handler;
};
// init // init
constructor(chatModel, storageModel2) { constructor(chatModel, storageModel2) {
this.chatModel = chatModel; this.chatModel = chatModel;
@@ -996,9 +1003,6 @@
chatMessage.body = decryptedBody; chatMessage.body = decryptedBody;
chatMessage.stringifiedFile = decryptedFile; chatMessage.stringifiedFile = decryptedFile;
}; };
setMessageHandler = (handler) => {
this.chatMessageHandler = handler;
};
subscribe = () => { subscribe = () => {
this.connectionModel.addChannel(this.info.primaryChannel); this.connectionModel.addChannel(this.info.primaryChannel);
}; };
@@ -1014,6 +1018,10 @@
const dirPath = this.getBasePath(); const dirPath = this.getBasePath();
this.storageModel.removeRecursively(dirPath); this.storageModel.removeRecursively(dirPath);
}; };
// other
setMessageHandler = (handler) => {
this.chatMessageHandler = handler;
};
// load // load
loadInfo = () => { loadInfo = () => {
const info = this.storageModel.readStringifiable( const info = this.storageModel.readStringifiable(
@@ -1246,20 +1254,6 @@
// guards // guards
cannotSendMessage; cannotSendMessage;
// methods // methods
addChatMessage = (chatMessage) => {
const chatMessageModel = new ChatMessageViewModel(
this,
chatMessage,
chatMessage.sender == this.chatViewModel.settingsViewModel.username.value
);
const existingChatMessageViewModel = this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != void 0) {
existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
}
};
sendMessage = () => { sendMessage = () => {
if (this.cannotSendMessage.value == true) return; if (this.cannotSendMessage.value == true) return;
this.sendMessageFromBody(this.composingMessage.value); this.sendMessageFromBody(this.composingMessage.value);
@@ -1274,10 +1268,25 @@
this.chatViewModel.chatModel.addMessage(chatMessage); this.chatViewModel.chatModel.addMessage(chatMessage);
messageViewModel.loadData(); messageViewModel.loadData();
}; };
// view
showChatMessage = (chatMessage) => {
const chatMessageModel = new ChatMessageViewModel(
this,
chatMessage,
chatMessage.sender == this.chatViewModel.settingsViewModel.username.value
);
const existingChatMessageViewModel = this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != void 0) {
existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
}
};
// load // load
loadData = () => { loadData = () => {
for (const chatMessage of this.chatViewModel.chatModel.messages) { for (const chatMessage of this.chatViewModel.chatModel.messages) {
this.addChatMessage(chatMessage); this.showChatMessage(chatMessage);
} }
}; };
// init // init
@@ -1391,6 +1400,12 @@
[this.newBoardNameInput], [this.newBoardNameInput],
() => this.newBoardNameInput.value == "" () => this.newBoardNameInput.value == ""
); );
// handlers
handleFileContent = (fileContent) => {
if (checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) == false)
return;
this.showBoard(fileContent);
};
// methods // methods
createBoard = () => { createBoard = () => {
if (this.cannotCreateBoard.value == true) return; if (this.cannotCreateBoard.value == true) return;
@@ -1398,6 +1413,10 @@
this.newBoardNameInput.value = ""; this.newBoardNameInput.value = "";
this.loadData(); this.loadData();
}; };
// view
showBoard = (boardInfo) => {
this.boards.add(boardInfo);
};
// load // load
loadData = () => { loadData = () => {
this.boards.clear(); this.boards.clear();
@@ -1405,7 +1424,7 @@
for (const boardId of boardIds) { for (const boardId of boardIds) {
const boardInfo = this.taskModel.getBoardInfo(boardId); const boardInfo = this.taskModel.getBoardInfo(boardId);
if (boardInfo == null) continue; if (boardInfo == null) continue;
this.boards.add(boardInfo); this.showBoard(boardInfo);
} }
}; };
// init // init
@@ -1466,8 +1485,13 @@
this.messagePageViewModel = new MessagePageViewModel(this); this.messagePageViewModel = new MessagePageViewModel(this);
this.settingsPageViewModel = new SettingsPageViewModel(this); this.settingsPageViewModel = new SettingsPageViewModel(this);
chatModel.setMessageHandler((chatMessage) => { chatModel.setMessageHandler((chatMessage) => {
this.messagePageViewModel.addChatMessage(chatMessage); this.messagePageViewModel.showChatMessage(chatMessage);
}); });
chatModel.fileModel.setFileContentHandler(
(fileContent) => {
this.taskPageViewModel.handleFileContent(fileContent);
}
);
this.loadPageSelection(); this.loadPageSelection();
this.updateIndex(); this.updateIndex();
} }
+5 -4
View File
@@ -159,10 +159,6 @@ export default class ChatModel {
chatMessage.stringifiedFile = decryptedFile; chatMessage.stringifiedFile = decryptedFile;
}; };
setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => {
this.chatMessageHandler = handler;
};
subscribe = (): void => { subscribe = (): void => {
this.connectionModel.addChannel(this.info.primaryChannel); this.connectionModel.addChannel(this.info.primaryChannel);
}; };
@@ -185,6 +181,11 @@ export default class ChatModel {
this.storageModel.removeRecursively(dirPath); this.storageModel.removeRecursively(dirPath);
}; };
// other
setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => {
this.chatMessageHandler = handler;
};
// load // load
loadInfo = (): void => { loadInfo = (): void => {
const info: ChatInfo | null = this.storageModel.readStringifiable( const info: ChatInfo | null = this.storageModel.readStringifiable(
+9 -1
View File
@@ -9,7 +9,6 @@ import {
} from "../Utility/utility"; } from "../Utility/utility";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
import { Color } from "../../colors";
import TaskModel from "./taskModel"; import TaskModel from "./taskModel";
import { v4 } from "uuid"; import { v4 } from "uuid";
@@ -19,6 +18,9 @@ export default class FileModel {
taskModel: TaskModel; taskModel: TaskModel;
fileContentHandler: (fileContent: FileContent<string>) => void = () => {};
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
return StorageModel.getPath( return StorageModel.getPath(
@@ -62,6 +64,7 @@ export default class FileModel {
if (didStore == false) return; if (didStore == false) return;
this.taskModel.handleFileContent(fileContent); this.taskModel.handleFileContent(fileContent);
this.fileContentHandler(fileContent);
}; };
// methods // methods
@@ -132,6 +135,11 @@ export default class FileModel {
return fileContent; return fileContent;
}; };
// other
setFileContentHandler = (handler: (fileContent: FileContent<string>) => void): void => {
this.fileContentHandler = handler;
};
// init // init
constructor(chatModel: ChatModel, storageModel: StorageModel) { constructor(chatModel: ChatModel, storageModel: StorageModel) {
this.chatModel = chatModel; this.chatModel = chatModel;
+8 -2
View File
@@ -4,6 +4,7 @@ import ChatModel, { ChatMessage } from "../../Model/Chat/chatModel";
import StorageModel, { filePaths } from "../../Model/Global/storageModel"; import StorageModel, { filePaths } from "../../Model/Global/storageModel";
import ChatListViewModel from "./chatListViewModel"; import ChatListViewModel from "./chatListViewModel";
import { FileContent } from "../../Model/Files/fileModel";
import MessagePageViewModel from "../Pages/messagePageViewModel"; import MessagePageViewModel from "../Pages/messagePageViewModel";
import SettingsPageViewModel from "../Pages/settingsPageViewModel"; import SettingsPageViewModel from "../Pages/settingsPageViewModel";
import SettingsViewModel from "../Global/settingsViewModel"; import SettingsViewModel from "../Global/settingsViewModel";
@@ -78,9 +79,14 @@ export default class ChatViewModel {
this.settingsPageViewModel = new SettingsPageViewModel(this); this.settingsPageViewModel = new SettingsPageViewModel(this);
// handlers // handlers
chatModel.setMessageHandler((chatMessage) => { chatModel.setMessageHandler((chatMessage: ChatMessage) => {
this.messagePageViewModel.addChatMessage(chatMessage); this.messagePageViewModel.showChatMessage(chatMessage);
}); });
chatModel.fileModel.setFileContentHandler(
(fileContent: FileContent<string>) => {
this.taskPageViewModel.handleFileContent(fileContent);
}
);
// load // load
this.loadPageSelection(); this.loadPageSelection();
+19 -18
View File
@@ -16,23 +16,6 @@ export default class MessagePageViewModel {
cannotSendMessage: React.State<boolean>; cannotSendMessage: React.State<boolean>;
// methods // methods
addChatMessage = (chatMessage: ChatMessage): void => {
const chatMessageModel = new ChatMessageViewModel(
this,
chatMessage,
chatMessage.sender == this.chatViewModel.settingsViewModel.username.value
);
const existingChatMessageViewModel: ChatMessageViewModel | undefined =
this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != undefined) {
existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
}
};
sendMessage = (): void => { sendMessage = (): void => {
if (this.cannotSendMessage.value == true) return; if (this.cannotSendMessage.value == true) return;
@@ -53,10 +36,28 @@ export default class MessagePageViewModel {
messageViewModel.loadData(); messageViewModel.loadData();
}; };
// view
showChatMessage = (chatMessage: ChatMessage): void => {
const chatMessageModel = new ChatMessageViewModel(
this,
chatMessage,
chatMessage.sender == this.chatViewModel.settingsViewModel.username.value
);
const existingChatMessageViewModel: ChatMessageViewModel | undefined =
this.chatMessageViewModels.value.get(chatMessage.id);
if (existingChatMessageViewModel != undefined) {
existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else {
this.chatMessageViewModels.set(chatMessage.id, chatMessageModel);
}
};
// load // load
loadData = (): void => { loadData = (): void => {
for (const chatMessage of this.chatViewModel.chatModel.messages) { for (const chatMessage of this.chatViewModel.chatModel.messages) {
this.addChatMessage(chatMessage); this.showChatMessage(chatMessage);
} }
}; };
+22 -2
View File
@@ -1,8 +1,13 @@
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import TaskModel, { BoardInfoFileContent } from "../../Model/Files/taskModel"; import TaskModel, {
BoardInfoFileContent,
BoardInfoFileContentReference,
} from "../../Model/Files/taskModel";
import { FileContent } from "../../Model/Files/fileModel";
import StorageModel from "../../Model/Global/storageModel"; import StorageModel from "../../Model/Global/storageModel";
import { checkMatchesObjectStructure } from "../../Model/Utility/typeSafety";
export default class TaskPageViewModel { export default class TaskPageViewModel {
taskModel: TaskModel; taskModel: TaskModel;
@@ -18,6 +23,16 @@ export default class TaskPageViewModel {
() => this.newBoardNameInput.value == "" () => this.newBoardNameInput.value == ""
); );
// handlers
handleFileContent = (fileContent: FileContent<string>): void => {
if (
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) ==
false
)
return;
this.showBoard(fileContent as BoardInfoFileContent);
};
// methods // methods
createBoard = (): void => { createBoard = (): void => {
if (this.cannotCreateBoard.value == true) return; if (this.cannotCreateBoard.value == true) return;
@@ -28,6 +43,11 @@ export default class TaskPageViewModel {
this.loadData(); this.loadData();
}; };
// view
showBoard = (boardInfo: BoardInfoFileContent): void => {
this.boards.add(boardInfo);
};
// load // load
loadData = (): void => { loadData = (): void => {
this.boards.clear(); this.boards.clear();
@@ -38,7 +58,7 @@ export default class TaskPageViewModel {
this.taskModel.getBoardInfo(boardId); this.taskModel.getBoardInfo(boardId);
if (boardInfo == null) continue; if (boardInfo == null) continue;
this.boards.add(boardInfo); this.showBoard(boardInfo);
} }
}; };