diff --git a/src/Model/Chat/chatModel.ts b/src/Model/Chat/chatModel.ts index 6c85dc21..4b2ea0bd 100644 --- a/src/Model/Chat/chatModel.ts +++ b/src/Model/Chat/chatModel.ts @@ -37,7 +37,7 @@ export default class ChatModel { info: ChatInfoFileContent; /* load function called in constructor */ get secondaryChannels(): string[] { - return this.info.secondaryChannels.sort(localeCompare); + return this.info.secondaryChannels.sort(localeCompare); } get color(): Colors { @@ -47,53 +47,54 @@ export default class ChatModel { } // handler managers - chatMessageHandlerManager: HandlerManager = - new HandlerManager(); + readonly chatMessageHandlerManager: HandlerManager = + new HandlerManager(); readonly reactionHandlerManager: HandlerManager = - new HandlerManager(); + new HandlerManager(); + readonly changeHandlerManager: HandlerManager = new HandlerManager(); // paths readonly getBasePath = (): string[] => { - return StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.chatBase(this.id), - ); + return StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.chatBase(this.id), + ); }; readonly getInfoPath = (): string[] => { - return StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.info(this.id), - ); + return StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.info(this.id), + ); }; readonly getColorPath = (): string[] => { - return StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.color(this.id), - ); + return StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.color(this.id), + ); }; readonly getMessageDirPath = (): string[] => { - return StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.messages(this.id), - ); + return StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.messages(this.id), + ); }; readonly getMessagePath = (id: string): string[] => { - return [...this.getMessageDirPath(), id]; + return [...this.getMessageDirPath(), id]; }; readonly getReactionDirPath = (): string[] => { - return StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.reactions(this.id), - ); + return StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.reactions(this.id), + ); }; readonly getReactionPath = (id: string): string[] => { - return [...this.getReactionDirPath(), id]; + return [...this.getReactionDirPath(), id]; }; readonly getPreviousFilterPath = (): string[] => { @@ -102,168 +103,174 @@ export default class ChatModel { // handlers readonly handleMessage = (body: string): void => { - const chatMessage: ChatMessage | null = parseValidObject( - body, - ChatMessageReference, - ); - if (chatMessage == null) return; + const chatMessage: ChatMessage | null = parseValidObject( + body, + ChatMessageReference, + ); + if (chatMessage == null) return; - chatMessage.status = ChatMessageStatuses.Received; + chatMessage.status = ChatMessageStatuses.Received; - this.addMessage(chatMessage); + this.addMessage(chatMessage); - if (chatMessage.stringifiedFile) return; - this.setReadStatus(true); + if (chatMessage.stringifiedFile) return; + this.setReadStatus(true); }; readonly handleReaction = (reaction: ChatMessageReaction | any): void => { - if ( - !checkMatchesObjectStructure(reaction, ChatMessageReactionReference) - ) - return; - const reactionPath: string[] = this.getReactionPath(reaction.fileId); + if ( + !checkMatchesObjectStructure(reaction, ChatMessageReactionReference) + ) + return; + const reactionPath: string[] = this.getReactionPath(reaction.fileId); - if (reaction.isDeleting == true) { - this.storageModel.remove(reactionPath); - } else { - this.storageModel.writeStringifiable(reactionPath, reaction); - } - this.reactionHandlerManager.trigger(reaction); + if (reaction.isDeleting == true) { + this.storageModel.remove(reactionPath); + } else { + this.storageModel.writeStringifiable(reactionPath, reaction); + } + this.reactionHandlerManager.trigger(reaction); }; readonly handleMessageSent = (chatMessage: ChatMessage): void => { - chatMessage.status = ChatMessageStatuses.Sent; - this.addMessage(chatMessage); + chatMessage.status = ChatMessageStatuses.Sent; + this.addMessage(chatMessage); }; // settings readonly setName = (name: string): void => { - this.info.name = name; - this.storeInfo(); - this.subscribe(); + this.info.name = name; + this.storeInfo(); + this.syncInfo(); + this.subscribe(); }; readonly setSecondaryChannels = (secondaryChannels: string[]): void => { - this.info.secondaryChannels = secondaryChannels; - this.storeInfo(); + this.info.secondaryChannels = secondaryChannels; + this.storeInfo(); }; readonly setEncryptionKey = (key: string): void => { - this.info.encryptionKey = key; - this.storeInfo(); + this.info.encryptionKey = key; + this.storeInfo(); }; readonly setColor = (color: Colors): void => { - this.info.color = color; - this.storeInfo(); + this.info.color = color; + this.syncInfo(); + this.storeInfo(); }; // messaging readonly addMessage = async (chatMessage: ChatMessage): Promise => { - await this.decryptMessage(chatMessage); + await this.decryptMessage(chatMessage); - // message - if (chatMessage.body != "") { - const messagePath: string[] = this.getMessagePath(chatMessage.id); - this.storageModel.writeStringifiable(messagePath, chatMessage); - this.chatMessageHandlerManager.trigger(chatMessage); - } + // message + if (chatMessage.body != "") { + const messagePath: string[] = this.getMessagePath(chatMessage.id); + this.storageModel.writeStringifiable(messagePath, chatMessage); + this.chatMessageHandlerManager.trigger(chatMessage); + } - // file - this.fileModel.handleStringifiedFileContent( - chatMessage.stringifiedFile, - ); + // file + this.fileModel.handleStringifiedFileContent( + chatMessage.stringifiedFile, + ); }; readonly getNameAndChannel = (): [string, string] | false => { - const senderName: string = this.settingsModel.username; - if (senderName == "") return false; + const senderName: string = this.settingsModel.username; + if (senderName == "") return false; - const allChannels: string[] = [this.id]; - for (const secondaryChannel of this.info.secondaryChannels) { - allChannels.push(secondaryChannel); - } + const allChannels: string[] = [this.id]; + for (const secondaryChannel of this.info.secondaryChannels) { + allChannels.push(secondaryChannel); + } - const combinedChannel: string = allChannels.join("/"); + const combinedChannel: string = allChannels.join("/"); - return [senderName, combinedChannel]; + return [senderName, combinedChannel]; }; readonly sendMessage = async ( - body: string, - inlineReplyId?: string, - fileContent?: FileContent, + body: string, + inlineReplyId?: string, + fileContent?: FileContent, ): Promise => { - const nameAndChannel = this.getNameAndChannel(); - if (nameAndChannel == false) return false; - const [senderName, combinedChannel] = nameAndChannel; + const nameAndChannel = this.getNameAndChannel(); + if (nameAndChannel == false) return false; + const [senderName, combinedChannel] = nameAndChannel; - const chatMessage: ChatMessage = await ChatModel.createChatMessage( - combinedChannel, - senderName, - this.info.encryptionKey, - body, - inlineReplyId, - fileContent, - ); + const chatMessage: ChatMessage = await ChatModel.createChatMessage( + combinedChannel, + senderName, + this.info.encryptionKey, + body, + inlineReplyId, + fileContent, + ); - this.addMessage(chatMessage); - this.connectionModel.sendMessageOrStore(chatMessage); - return chatMessage.id; + this.addMessage(chatMessage); + this.connectionModel.sendMessageOrStore(chatMessage); + return chatMessage.id; }; readonly decryptMessage = async ( - chatMessage: ChatMessage, + chatMessage: ChatMessage, ): Promise => { - const decryptedBody: string = await decryptString( - chatMessage.body, - this.info.encryptionKey, - ); - const decryptedFile: string = await decryptString( - chatMessage.stringifiedFile ?? "", - this.info.encryptionKey, - ); - chatMessage.body = decryptedBody; - chatMessage.stringifiedFile = decryptedFile; + const decryptedBody: string = await decryptString( + chatMessage.body, + this.info.encryptionKey, + ); + const decryptedFile: string = await decryptString( + chatMessage.stringifiedFile ?? "", + this.info.encryptionKey, + ); + chatMessage.body = decryptedBody; + chatMessage.stringifiedFile = decryptedFile; }; readonly sendReaction = async ( - messageId: string, - content: ReactionSymbols, - isDeleting: boolean = false, + messageId: string, + content: ReactionSymbols, + isDeleting: boolean = false, ): Promise => { - const nameAndChannel = this.getNameAndChannel(); - if (nameAndChannel == false) return; - const [senderName] = nameAndChannel; + const nameAndChannel = this.getNameAndChannel(); + if (nameAndChannel == false) return; + const [senderName] = nameAndChannel; - const reaction = ChatModel.createMessageReaction( - messageId, - senderName, - content, - isDeleting, - ); - this.sendMessage("", undefined, reaction); - this.handleReaction(reaction); + const reaction = ChatModel.createMessageReaction( + messageId, + senderName, + content, + isDeleting, + ); + this.sendMessage("", undefined, reaction); + this.handleReaction(reaction); }; readonly subscribe = (): void => { - this.connectionModel.addChannel(this.id); + this.connectionModel.addChannel(this.id); }; readonly setReadStatus = (hasUnreadMessages: boolean): void => { - this.info.hasUnreadMessages = hasUnreadMessages; - this.storeInfo(); + this.info.hasUnreadMessages = hasUnreadMessages; + this.storeInfo(); }; // storage readonly storeInfo = (): void => { - this.storageModel.writeStringifiable(this.getInfoPath(), this.info); + this.storageModel.writeStringifiable(this.getInfoPath(), this.info); }; + readonly syncInfo = (): void => { + this.sendMessage("", undefined, this.info); + } + readonly storeFilter = (filter: string): void => { this.storageModel.write(this.getPreviousFilterPath(), filter); } - + readonly getFilter = (): string=> { return this.storageModel.read(this.getPreviousFilterPath()) || ""; } @@ -283,13 +290,15 @@ export default class ChatModel { this.getInfoPath(), ChatInfoReference, ); - if (info != null) { - this.info = info; - } else { - this.info = ChatModel.generateChatInfo("0", "0", Colors.Standard ); - } + this.info = (info || ChatModel.generateChatInfo("0", "0", Colors.Standard)); }; + readonly handleInfo = (info: ChatInfoFileContent): void => { + this.info = info; + this.changeHandlerManager.trigger(null); + this.storeInfo(); + } + get messages(): ChatMessage[] { const messageIds: string[] = this.storageModel.list( this.getMessageDirPath(), @@ -499,7 +508,7 @@ extends ValidObject, FileContent<"reaction"> { // references export const ChatInfoReference: ChatInfoFileContent = { dataVersion: DATA_VERSION, - + fileId: "", fileContentId: "", creationDate: "", diff --git a/src/Model/Files/fileModel.ts b/src/Model/Files/fileModel.ts index 4b895b9b..fd41a47f 100644 --- a/src/Model/Files/fileModel.ts +++ b/src/Model/Files/fileModel.ts @@ -8,13 +8,13 @@ import { parseValidObject, stringify, } from "../Utility/utility"; -import { DATA_VERSION, ValidObject } from "../Utility/typeSafety"; +import { checkIsValidObject, checkMatchesObjectStructure, DATA_VERSION, ValidObject } from "../Utility/typeSafety"; import StorageModel, { StorageModelSubPaths, filePaths, } from "../Global/storageModel"; import SettingsModel from "../Global/settingsModel"; -import ChatModel from "../Chat/chatModel"; +import ChatModel, {ChatInfoFileContent, ChatInfoReference} from "../Chat/chatModel"; export default class FileModel { readonly storageModel: StorageModel; @@ -67,154 +67,160 @@ export default class FileModel { }; readonly handleFileContent = (fileContent: FileContent): void => { - const didStore: boolean = this.storeFileContent(fileContent); - if (didStore == false) return; + if (fileContent.type == "chat-info") { + if (checkMatchesObjectStructure(fileContent, ChatInfoReference) == false) return; + console.log("CHANGING"); + this.chatModel.handleInfo(fileContent as ChatInfoFileContent); + } - switch (fileContent.type) { - case "board-info": - case "task": - this.boardsAndTasksModel.handleFileContent(fileContent); - break; - case "reaction": - this.chatModel.handleReaction(fileContent); - break; - } + const didStore: boolean = this.storeFileContent(fileContent); + if (didStore == false) return; + + switch (fileContent.type) { + case "board-info": + case "task": + this.boardsAndTasksModel.handleFileContent(fileContent); + break; + case "reaction": + this.chatModel.handleReaction(fileContent); + break; + } }; // methods readonly addFileContentAndSend = ( - fileContent: FileContent, + fileContent: FileContent, ): void => { - this.handleFileContent(fileContent); - this.chatModel.sendMessage("", undefined, fileContent); + this.handleFileContent(fileContent); + this.chatModel.sendMessage("", undefined, fileContent); }; // storage readonly storeFileContent = (fileContent: FileContent): boolean => { - const fileContentPath: string[] = this.getFileContentPath( - fileContent.fileId, - fileContent.fileContentId, - ); + const fileContentPath: string[] = this.getFileContentPath( + fileContent.fileId, + fileContent.fileContentId, + ); - // check if fileContent already exists - const existingFileContent: string | null = - this.storageModel.read(fileContentPath); - if (existingFileContent != null) return false; + // check if fileContent already exists + const existingFileContent: string | null = + this.storageModel.read(fileContentPath); + if (existingFileContent != null) return false; - const stringifiedContent: string = stringify(fileContent); - this.storageModel.write(fileContentPath, stringifiedContent); - return true; + const stringifiedContent: string = stringify(fileContent); + this.storageModel.write(fileContentPath, stringifiedContent); + return true; }; readonly listFileIds = (): string[] => { - return this.storageModel.list(this.basePath); + return this.storageModel.list(this.basePath); }; readonly listFileContentIds = (fileId: string): string[] => { - const filePath: string[] = this.getFilePath(fileId); - return this.storageModel.list(filePath); + const filePath: string[] = this.getFilePath(fileId); + return this.storageModel.list(filePath); }; readonly selectLatestFileContentId = ( - fileContentIds: string[], + fileContentIds: string[], ): string | undefined => { - return fileContentIds[fileContentIds.length - 1]; + return fileContentIds[fileContentIds.length - 1]; }; readonly getFileContent = >( - fileId: string, - fileContentName: string, - reference: T, + fileId: string, + fileContentName: string, + reference: T, ): T | null => { - const filePath: string[] = this.getFileContentPath( - fileId, - fileContentName, - ); - const fileContentOrNull: T | null = this.storageModel.readStringifiable( - filePath, - reference, - ); - return fileContentOrNull; + const filePath: string[] = this.getFileContentPath( + fileId, + fileContentName, + ); + const fileContentOrNull: T | null = this.storageModel.readStringifiable( + filePath, + reference, + ); + return fileContentOrNull; }; readonly getLatestFileContent = >( - fileId: string, - reference: T, + fileId: string, + reference: T, ): T | null => { - const fileContentsIds: string[] = this.listFileContentIds(fileId); - const latestFileContentId: string | undefined = - this.selectLatestFileContentId(fileContentsIds); - if (latestFileContentId == undefined) return null; + const fileContentsIds: string[] = this.listFileContentIds(fileId); + const latestFileContentId: string | undefined = + this.selectLatestFileContentId(fileContentsIds); + if (latestFileContentId == undefined) return null; - const fileContent: T | null = this.getFileContent( - fileId, - latestFileContentId, - reference, - ); - return fileContent; + const fileContent: T | null = this.getFileContent( + fileId, + latestFileContentId, + reference, + ); + return fileContent; }; // init constructor( - storageModel: StorageModel, - settingsModel: SettingsModel, - chatModel: ChatModel, + storageModel: StorageModel, + settingsModel: SettingsModel, + chatModel: ChatModel, ) { - this.chatModel = chatModel; - this.settingsModel = settingsModel; - this.storageModel = storageModel; + this.chatModel = chatModel; + this.settingsModel = settingsModel; + this.storageModel = storageModel; - this.boardsAndTasksModel = new BoardsAndTasksModel( - this.storageModel, - this.settingsModel, - chatModel, - this, - ); + this.boardsAndTasksModel = new BoardsAndTasksModel( + this.storageModel, + this.settingsModel, + chatModel, + this, + ); } // utility static generateFileContentId = (creationDate: string): string => { - return creationDate + v4(); + return creationDate + v4(); }; static createFileContent = ( - fileId: string, - type: T, + fileId: string, + type: T, ): FileContent => { - const creationDate: string = createTimestamp(); - const fileContentId: string = - FileModel.generateFileContentId(creationDate); + const creationDate: string = createTimestamp(); + const fileContentId: string = + FileModel.generateFileContentId(creationDate); - return { - dataVersion: DATA_VERSION, + return { + dataVersion: DATA_VERSION, - fileId, - fileContentId, - creationDate, - type, - }; + fileId, + fileContentId, + creationDate, + type, + }; }; } // paths export enum FileModelSubPath { Data = "data", - Model = "model", - Chat = "chat", - ModelView = "view", - ModelTask = "tasks", - ModelCalendar = "calendar", + Model = "model", + Chat = "chat", + ModelView = "view", + ModelTask = "tasks", + ModelCalendar = "calendar", } // types export interface FileContent extends ValidObject, StringEntryObject { - readonly fileId: string; - readonly fileContentId: string; - readonly creationDate: string; + readonly fileId: string; + readonly fileContentId: string; + readonly creationDate: string; - readonly type: T; -} + readonly type: T; + } // references export const FileContentReference: FileContent = { diff --git a/src/View/ChatPages/settingsPage.tsx b/src/View/ChatPages/settingsPage.tsx index 87e869af..96dd2e36 100644 --- a/src/View/ChatPages/settingsPage.tsx +++ b/src/View/ChatPages/settingsPage.tsx @@ -177,7 +177,24 @@ export function SettingsPage(
- {ColorPicker(settingsPageViewModel.color)} + {ColorPicker(settingsPageViewModel.color)} + +
+ +
+
diff --git a/src/ViewModel/Chat/chatViewModel.ts b/src/ViewModel/Chat/chatViewModel.ts index 97876f6b..2dd177cc 100644 --- a/src/ViewModel/Chat/chatViewModel.ts +++ b/src/ViewModel/Chat/chatViewModel.ts @@ -31,7 +31,7 @@ export default class ChatViewModel extends ContextHost { // state displayedColor: React.State = new React.State(Colors.Standard); selectedPage: React.State = new React.State( - ChatPageTypes.Messages, + ChatPageTypes.Messages, ); pageContexts = new React.MapState(); @@ -42,185 +42,186 @@ export default class ChatViewModel extends ContextHost { // context get isOpen(): boolean { - return this.chatListViewModel.selectedChat.value == this; + return this.chatListViewModel.selectedChat.value == this; } get contextSelection(): ChatPageTypes { - return this.selectedPage.value; + return this.selectedPage.value; } // view open = (): void => { - this.coreViewModel.context = this; - this.chatListViewModel.openChat(this); - //this.selectedPage.callSubscriptions(); + this.coreViewModel.context = this; + this.chatListViewModel.openChat(this); + //this.selectedPage.callSubscriptions(); }; openPage = (page: ChatPageTypes): void => { - this.closeCurrentContext(); - this.selectedPage.value = page; + this.closeCurrentContext(); + this.selectedPage.value = page; }; setColor = (color: Colors): void => { - this.setDisplayedColor(color); - this.chatModel.setColor(color); + this.setDisplayedColor(color); + this.chatModel.setColor(color); }; setDisplayedColor = (color: Colors): void => { - this.displayedColor.value = color; + this.displayedColor.value = color; }; resetColor = (): void => { - this.displayedColor.value = this.settingsPageViewModel.color.value; + this.displayedColor.value = this.settingsPageViewModel.color.value; }; updateIndex = (): void => { - const index: number = - this.chatListViewModel.chatIndexManager.getIndex(this); - this.index.value = index; + const index: number = + this.chatListViewModel.chatIndexManager.getIndex(this); + this.index.value = index; }; // load loadPageSelection = (): void => { - const path: string[] = StorageModel.getPath( - StorageModelSubPaths.Chat, - filePaths.chat.lastUsedPage(this.chatModel.id), - ); - const lastUsedPage: string | null = - this.coreViewModel.storageModel.read(path); - if (lastUsedPage != null) { - this.openPage(lastUsedPage as ChatPageTypes); - } + const path: string[] = StorageModel.getPath( + StorageModelSubPaths.Chat, + filePaths.chat.lastUsedPage(this.chatModel.id), + ); + const lastUsedPage: string | null = + this.coreViewModel.storageModel.read(path); + if (lastUsedPage != null) { + this.openPage(lastUsedPage as ChatPageTypes); + } - this.selectedPage.subscribeSilent((newPage) => { - this.coreViewModel.storageModel.write(path, newPage); - this.resetColor(); - }); + this.selectedPage.subscribeSilent((newPage) => { + this.coreViewModel.storageModel.write(path, newPage); + this.resetColor(); + }); }; loadInfo = (): void => { - this.updateReadStatus(); - this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [ - CALENDAR_EVENT_BOARD_ID, - this.coreViewModel.translations.chatPage.calendar.eventsBoard, - ]); + this.updateReadStatus(); + this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [ + CALENDAR_EVENT_BOARD_ID, + this.coreViewModel.translations.chatPage.calendar.eventsBoard, + ]); }; updateReadStatus = (): void => { - if ( - this.chatListViewModel.selectedChat.value == this && - this.selectedPage.value == ChatPageTypes.Messages - ) { - this.chatModel.setReadStatus(false); - } - this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages; + if ( + this.chatListViewModel.selectedChat.value == this && + this.selectedPage.value == ChatPageTypes.Messages + ) { + this.chatModel.setReadStatus(false); + } + this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages; }; setReadStatus = (hasUnreadMessages: boolean): void => { - this.chatModel.setReadStatus(hasUnreadMessages); - this.hasUnreadMessages.value = hasUnreadMessages; + this.chatModel.setReadStatus(hasUnreadMessages); + this.hasUnreadMessages.value = hasUnreadMessages; }; subscribeReadStatus = (): void => { - React.createProxyState( - [this.selectedPage, this.chatListViewModel.selectedChat], - () => { - if (this.chatListViewModel.selectedChat.value != this) return; - if (this.selectedPage.value != ChatPageTypes.Messages) return; - this.setReadStatus(false); - }, - ); + React.createProxyState( + [this.selectedPage, this.chatListViewModel.selectedChat], + () => { + if (this.chatListViewModel.selectedChat.value != this) return; + if (this.selectedPage.value != ChatPageTypes.Messages) return; + this.setReadStatus(false); + }, + ); }; // exit close = (): void => { - this.coreViewModel.closeContext(this.contextId); + this.coreViewModel.closeContext(this.contextId); }; handleContextClose = (): void => { - this.chatListViewModel.closeChat(); + this.chatListViewModel.closeChat(); }; // init constructor( - readonly coreViewModel: CoreViewModel, - public readonly chatModel: ChatModel, - public readonly settingsViewModel: SettingsViewModel, - public readonly notificationViewModel: NotificationViewModel, - public readonly connectionViewModel: ConnectionViewModel, - public readonly chatListViewModel: ChatListViewModel, + readonly coreViewModel: CoreViewModel, + public readonly chatModel: ChatModel, + public readonly settingsViewModel: SettingsViewModel, + public readonly notificationViewModel: NotificationViewModel, + public readonly connectionViewModel: ConnectionViewModel, + public readonly chatListViewModel: ChatListViewModel, ) { - super("chat", coreViewModel); + super("chat", coreViewModel); - // page viewModels - this.calendarViewModel = new CalendarPageViewModel( - coreViewModel, - this, - this.chatModel.fileModel.boardsAndTasksModel.calendarModel, - this.chatModel.fileModel.boardsAndTasksModel, - ); - this.taskPageViewModel = new TaskPageViewModel( - this.coreViewModel, - this, - this.chatModel.fileModel.boardsAndTasksModel, - ); - this.messagePageViewModel = new MessagePageViewModel( - this.coreViewModel, - this, - ); - this.settingsPageViewModel = new SettingsPageViewModel( - this.coreViewModel, - this, - ); + // page viewModels + this.calendarViewModel = new CalendarPageViewModel( + coreViewModel, + this, + this.chatModel.fileModel.boardsAndTasksModel.calendarModel, + this.chatModel.fileModel.boardsAndTasksModel, + ); + this.taskPageViewModel = new TaskPageViewModel( + this.coreViewModel, + this, + this.chatModel.fileModel.boardsAndTasksModel, + ); + this.messagePageViewModel = new MessagePageViewModel( + this.coreViewModel, + this, + ); + this.settingsPageViewModel = new SettingsPageViewModel( + this.coreViewModel, + this, + ); - // handlers - chatModel.chatMessageHandlerManager.setHandler( - this.chatModel.id, - (chatMessage: ChatMessage) => { - this.messagePageViewModel.showChatMessage(chatMessage); - this.updateReadStatus(); + // handlers + chatModel.chatMessageHandlerManager.setHandler( + this.chatModel.id, + (chatMessage: ChatMessage) => { + this.messagePageViewModel.showChatMessage(chatMessage); + this.updateReadStatus(); - this.notificationViewModel.showNotification(chatMessage); - }, - ); - chatModel.reactionHandlerManager.setHandler( - this.chatModel.id, - (reaction: ChatMessageReaction) => { - this.messagePageViewModel.handleReaction(reaction); - }, - ); + this.notificationViewModel.showNotification(chatMessage); + }, + ); + chatModel.reactionHandlerManager.setHandler( + this.chatModel.id, + (reaction: ChatMessageReaction) => { + this.messagePageViewModel.handleReaction(reaction); + }, + ); + chatModel.changeHandlerManager.setHandler(this.chatModel.id, this.settingsPageViewModel.preloadData); - // load - this.loadPageSelection(); - this.resetColor(); - this.loadInfo(); - this.subscribeReadStatus(); + // load + this.loadPageSelection(); + this.resetColor(); + this.loadInfo(); + this.subscribeReadStatus(); - // keystrokes - this.registerKeyStroke(CommonKeys.Home, this.close); - this.registerKeyStroke("u", () => - this.openPage(ChatPageTypes.Messages), - ); - this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks)); - this.registerKeyStroke("o", () => - this.openPage(ChatPageTypes.Calendar), - ); - this.registerKeyStroke(CommonKeys.Settings, () => - this.openPage(ChatPageTypes.Settings), - ); + // keystrokes + this.registerKeyStroke(CommonKeys.Home, this.close); + this.registerKeyStroke("u", () => + this.openPage(ChatPageTypes.Messages), + ); + this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks)); + this.registerKeyStroke("o", () => + this.openPage(ChatPageTypes.Calendar), + ); + this.registerKeyStroke(CommonKeys.Settings, () => + this.openPage(ChatPageTypes.Settings), + ); - // context - this.chatListViewModel.selectedChat.subscribeSilent( - this.updateContexts, - ); - this.selectedPage.subscribeSilent(this.updateContexts); + // context + this.chatListViewModel.selectedChat.subscribeSilent( + this.updateContexts, + ); + this.selectedPage.subscribeSilent(this.updateContexts); } } // types export enum ChatPageTypes { Settings = "settings", - Messages = "messages", - Tasks = "tasks", - Calendar = "calendar", + Messages = "messages", + Tasks = "tasks", + Calendar = "calendar", } diff --git a/src/ViewModel/Pages/settingsPageViewModel.ts b/src/ViewModel/Pages/settingsPageViewModel.ts index 60d9c7e6..c2c90392 100644 --- a/src/ViewModel/Pages/settingsPageViewModel.ts +++ b/src/ViewModel/Pages/settingsPageViewModel.ts @@ -71,8 +71,8 @@ export default class SettingsPageViewModel extends Context { this.encryptionKeyInput.callSubscriptions(); }; - applyColor = (newColor: Colors): void => { - this.chatViewModel.setColor(newColor); + applyColor = (): void => { + this.chatViewModel.setColor(this.color.value); }; remove = (): void => { @@ -122,10 +122,6 @@ export default class SettingsPageViewModel extends Context { this.chatViewModel.chatModel.info.encryptionKey, ); - this.color.subscribe((newColor) => { - this.applyColor(newColor); - }); - this.chatViewModel.registerContext(ChatPageTypes.Settings, this); } }