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";
|
2024-07-25 19:32:46 +02:00
|
|
|
import StorageModel, {
|
|
|
|
|
StorageModelSubPath,
|
|
|
|
|
filePaths,
|
|
|
|
|
} from "../../Model/Global/storageModel";
|
2024-07-20 16:34:16 +02:00
|
|
|
|
2024-07-25 19:32:46 +02:00
|
|
|
import CalendarPageViewModel from "../Pages/calendarPageViewModel";
|
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-25 16:02:49 +02:00
|
|
|
import CoreViewModel from "../Global/coreViewModel";
|
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-25 19:32:46 +02:00
|
|
|
calendarViewModel: CalendarPageViewModel;
|
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-26 03:14:55 +02:00
|
|
|
displayedColor: React.State<Color> = new React.State<any>(Color.Standard);
|
|
|
|
|
selectedPage: React.State<ChatPageType> = new React.State<any>(
|
2024-07-20 16:28:00 +02:00
|
|
|
ChatPageType.Messages
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-23 02:50:22 +02:00
|
|
|
index: React.State<number> = new React.State(0);
|
2024-07-27 15:24:50 +02:00
|
|
|
hasUnreadMessages: React.State<boolean> = new React.State(false);
|
2024-07-23 02:50:22 +02:00
|
|
|
|
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-27 15:24:50 +02:00
|
|
|
this.markUnread();
|
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 => {
|
2024-07-20 15:49:58 +02:00
|
|
|
this.chatListViewModel.closeChat();
|
2024-07-20 16:28:00 +02:00
|
|
|
};
|
2024-07-20 15:49:58 +02:00
|
|
|
|
2024-07-23 18:51:41 +02:00
|
|
|
closeSubPages = (): void => {};
|
2024-07-23 02:50:22 +02:00
|
|
|
|
2024-07-23 13:23:27 +02:00
|
|
|
setColor = (color: Color): void => {
|
2024-07-23 13:48:17 +02:00
|
|
|
this.setDisplayedColor(color);
|
2024-07-23 13:23:27 +02:00
|
|
|
this.chatModel.setColor(color);
|
2024-07-23 13:48:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setDisplayedColor = (color: Color): void => {
|
|
|
|
|
this.displayedColor.value = color;
|
|
|
|
|
};
|
2024-07-23 13:23:27 +02:00
|
|
|
|
2024-07-23 02:50:22 +02:00
|
|
|
resetColor = (): void => {
|
|
|
|
|
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-23 14:15:01 +02:00
|
|
|
updateIndex = (): void => {
|
2024-07-23 18:51:41 +02:00
|
|
|
const index: number =
|
|
|
|
|
this.chatListViewModel.chatIndexManager.getIndex(this);
|
2024-07-23 14:15:01 +02:00
|
|
|
this.index.value = index;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-27 15:24:50 +02:00
|
|
|
markUnread = (): void => {
|
|
|
|
|
this.hasUnreadMessages.value = false;
|
|
|
|
|
this.chatModel.markUnread();
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
// load
|
|
|
|
|
loadPageSelection = (): void => {
|
2024-07-22 03:01:04 +02:00
|
|
|
const path: string[] = StorageModel.getPath(
|
2024-07-25 16:44:08 +02:00
|
|
|
StorageModelSubPath.Chat,
|
2024-07-22 03:01:04 +02:00
|
|
|
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-23 18:51:41 +02:00
|
|
|
this.resetColor();
|
2024-07-20 16:34:16 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-27 15:24:50 +02:00
|
|
|
loadInfo = (): void => {
|
|
|
|
|
this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-20 14:16:26 +02:00
|
|
|
// init
|
2024-07-20 20:42:43 +02:00
|
|
|
constructor(
|
2024-07-25 16:02:49 +02:00
|
|
|
public coreViewModel: CoreViewModel,
|
2024-07-20 20:42:43 +02:00
|
|
|
storageModel: StorageModel,
|
2024-07-24 01:19:44 +02:00
|
|
|
chatModel: ChatModel,
|
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 20:42:43 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-24 01:19:44 +02:00
|
|
|
this.chatModel = chatModel;
|
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
|
2024-07-25 19:32:46 +02:00
|
|
|
this.calendarViewModel = new CalendarPageViewModel(
|
|
|
|
|
coreViewModel,
|
|
|
|
|
this.storageModel,
|
2024-07-26 03:14:55 +02:00
|
|
|
this.chatModel.fileModel.boardsAndTasksModel.calendarModel,
|
2024-07-26 05:43:49 +02:00
|
|
|
this.chatModel.fileModel.boardsAndTasksModel,
|
2024-07-27 15:24:50 +02:00
|
|
|
this
|
2024-07-25 19:32:46 +02:00
|
|
|
);
|
2024-07-22 21:07:44 +02:00
|
|
|
this.taskPageViewModel = new TaskPageViewModel(
|
2024-07-25 16:02:49 +02:00
|
|
|
this.coreViewModel,
|
2024-07-23 02:50:22 +02:00
|
|
|
this.storageModel,
|
2024-07-24 17:28:49 +02:00
|
|
|
this.chatModel.fileModel.boardsAndTasksModel,
|
2024-07-23 02:50:22 +02:00
|
|
|
this
|
2024-07-22 21:07:44 +02:00
|
|
|
);
|
2024-07-25 16:02:49 +02:00
|
|
|
this.messagePageViewModel = new MessagePageViewModel(
|
|
|
|
|
this.coreViewModel,
|
|
|
|
|
this
|
|
|
|
|
);
|
|
|
|
|
this.settingsPageViewModel = new SettingsPageViewModel(
|
|
|
|
|
this.coreViewModel,
|
|
|
|
|
this
|
|
|
|
|
);
|
2024-07-22 21:07:44 +02:00
|
|
|
|
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-28 13:24:32 +02:00
|
|
|
this.markUnread();
|
2024-07-23 02:50:22 +02:00
|
|
|
}
|
|
|
|
|
);
|
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-27 15:24:50 +02:00
|
|
|
this.loadInfo();
|
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
|
|
|
}
|