Files
CT/src/ViewModel/Chat/chatViewModel.ts
T

115 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-07-20 04:17:10 +02:00
import * as React from "bloatless-react";
2024-07-22 19:52:08 +02:00
import ChatModel, { ChatMessage } from "../../Model/Chat/chatModel";
import StorageModel, { filePaths } from "../../Model/Global/storageModel";
2024-07-20 16:34:16 +02:00
2024-07-20 14:16:26 +02:00
import ChatListViewModel from "./chatListViewModel";
2024-07-23 02:50:22 +02:00
import { Color } from "../../colors";
2024-07-22 21:07:44 +02:00
import MessagePageViewModel from "../Pages/messagePageViewModel";
import SettingsPageViewModel from "../Pages/settingsPageViewModel";
2024-07-22 19:52:08 +02:00
import SettingsViewModel from "../Global/settingsViewModel";
2024-07-22 21:07:44 +02:00
import TaskPageViewModel from "../Pages/taskPageViewModel";
2024-07-20 04:17:10 +02:00
export default class ChatViewModel {
2024-07-20 14:16:26 +02:00
chatModel: ChatModel;
2024-07-20 16:34:16 +02:00
storageModel: StorageModel;
2024-07-20 21:04:41 +02:00
settingsViewModel: SettingsViewModel;
2024-07-20 20:42:43 +02:00
chatListViewModel: ChatListViewModel;
2024-07-20 04:17:10 +02:00
2024-07-22 21:07:44 +02:00
taskPageViewModel: TaskPageViewModel;
messagePageViewModel: MessagePageViewModel;
settingsPageViewModel: SettingsPageViewModel;
2024-07-20 14:16:26 +02:00
// state
2024-07-23 02:50:22 +02:00
displayedColor: React.State<Color> = new React.State<Color>(Color.Standard);
2024-07-20 16:28:00 +02:00
selectedPage: React.State<ChatPageType> = new React.State<ChatPageType>(
ChatPageType.Messages
);
2024-07-23 02:50:22 +02:00
index: React.State<number> = new React.State(0);
2024-07-20 20:42:43 +02:00
// view
2024-07-20 16:34:16 +02:00
open = (): void => {
2024-07-20 14:16:26 +02:00
this.chatListViewModel.openChat(this);
2024-07-20 16:28:00 +02:00
};
2024-07-20 14:16:26 +02:00
2024-07-20 16:34:16 +02:00
close = (): void => {
this.chatListViewModel.closeChat();
2024-07-20 16:28:00 +02:00
};
2024-07-23 02:50:22 +02:00
closeSubPages = (): void => {
this.taskPageViewModel.closeBoard();
2024-07-23 13:48:17 +02:00
};
2024-07-23 02:50:22 +02:00
setColor = (color: Color): void => {
2024-07-23 13:48:17 +02:00
this.setDisplayedColor(color);
this.chatModel.setColor(color);
2024-07-23 13:48:17 +02:00
};
setDisplayedColor = (color: Color): void => {
this.displayedColor.value = color;
};
2024-07-23 02:50:22 +02:00
resetColor = (): void => {
this.displayedColor.value = this.settingsPageViewModel.color.value;
};
2024-07-21 21:41:19 +02:00
// load
loadPageSelection = (): void => {
2024-07-22 03:01:04 +02:00
const path: string[] = StorageModel.getPath(
"chat",
filePaths.chat.lastUsedPage(this.chatModel.id)
);
2024-07-21 21:41:19 +02:00
const lastUsedPage: string | null = this.storageModel.read(path);
2024-07-20 16:34:16 +02:00
if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage as any;
}
this.selectedPage.subscribeSilent((newPage) => {
2024-07-21 21:41:19 +02:00
this.storageModel.write(path, newPage);
2024-07-20 16:34:16 +02:00
});
};
2024-07-20 14:16:26 +02:00
// init
2024-07-20 20:42:43 +02:00
constructor(
chatModel: ChatModel,
storageModel: StorageModel,
2024-07-20 21:04:41 +02:00
settingsViewModel: SettingsViewModel,
2024-07-20 20:42:43 +02:00
chatListViewModel: ChatListViewModel
) {
2024-07-21 01:12:35 +02:00
// models
2024-07-20 14:16:26 +02:00
this.chatModel = chatModel;
2024-07-20 20:42:43 +02:00
this.storageModel = storageModel;
2024-07-20 21:04:41 +02:00
this.settingsViewModel = settingsViewModel;
2024-07-20 14:16:26 +02:00
this.chatListViewModel = chatListViewModel;
2024-07-22 21:07:44 +02:00
// page viewModels
this.taskPageViewModel = new TaskPageViewModel(
this.chatModel.fileModel.taskModel,
2024-07-23 02:50:22 +02:00
this.storageModel,
this
2024-07-22 21:07:44 +02:00
);
this.messagePageViewModel = new MessagePageViewModel(this);
this.settingsPageViewModel = new SettingsPageViewModel(this);
2024-07-21 01:12:35 +02:00
// handlers
2024-07-23 02:50:22 +02:00
chatModel.chatMessageHandlerManager.addHandler(
(chatMessage: ChatMessage) => {
this.messagePageViewModel.showChatMessage(chatMessage);
}
);
2024-07-21 01:12:35 +02:00
2024-07-21 21:41:19 +02:00
// load
this.loadPageSelection();
2024-07-23 02:50:22 +02:00
this.resetColor();
2024-07-20 14:16:26 +02:00
}
}
2024-07-20 16:28:00 +02:00
// types
export enum ChatPageType {
2024-07-20 16:34:16 +02:00
Settings = "settings",
Messages = "messages",
2024-07-22 13:17:21 +02:00
Tasks = "tasks",
2024-07-20 16:34:16 +02:00
Calendar = "calendar",
2024-07-20 16:28:00 +02:00
}