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

102 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-07-20 05:10:50 +02:00
import * as React from "bloatless-react";
2024-07-22 19:52:08 +02:00
import ChatListModel from "../../Model/Chat/chatListModel";
import ChatModel from "../../Model/Chat/chatModel";
2024-07-20 14:16:26 +02:00
import ChatViewModel from "./chatViewModel";
2024-07-23 14:15:01 +02:00
import { IndexManager } from "../../Model/Utility/utility";
2024-07-22 19:52:08 +02:00
import SettingsViewModel from "../Global/settingsViewModel";
import StorageModel from "../../Model/Global/storageModel";
2024-07-20 05:10:50 +02:00
export default class ChatListViewModel {
chatListModel: ChatListModel;
2024-07-20 20:42:43 +02:00
storageModel: StorageModel;
2024-07-20 21:04:41 +02:00
settingsViewModel: SettingsViewModel;
2024-07-20 05:10:50 +02:00
// state
newChatPrimaryChannel: React.State<string> = new React.State("");
2024-07-20 14:16:26 +02:00
chatViewModels: React.ListState<ChatViewModel> = new React.ListState();
2024-07-23 14:15:01 +02:00
indexManager: IndexManager<ChatViewModel> = new IndexManager(
(chatViewModel: ChatViewModel) =>
chatViewModel.settingsPageViewModel.primaryChannel.value
);
2024-07-20 14:16:26 +02:00
selectedChat: React.State<ChatViewModel | undefined> = new React.State<
2024-07-20 16:49:01 +02:00
ChatViewModel | undefined
2024-07-20 14:16:26 +02:00
>(undefined);
2024-07-20 05:10:50 +02:00
// guards
cannotCreateChat = React.createProxyState(
[this.newChatPrimaryChannel],
() => this.newChatPrimaryChannel.value == ""
);
// methods
createChat = (): void => {
const chatModel: ChatModel = this.chatListModel.createChat(
this.newChatPrimaryChannel.value
);
this.newChatPrimaryChannel.value = "";
2024-07-20 14:16:26 +02:00
2024-07-20 20:42:43 +02:00
const chatViewModel: ChatViewModel = this.createChatViewModel(chatModel);
2024-07-23 14:15:01 +02:00
this.trackChat(chatViewModel);
this.updateIndices();
};
trackChat = (chatViewModel: ChatViewModel): void => {
2024-07-20 14:16:26 +02:00
this.chatViewModels.add(chatViewModel);
};
untrackChat = (chatViewModel: ChatViewModel): void => {
this.chatListModel.untrackChat(chatViewModel.chatModel);
this.chatViewModels.remove(chatViewModel);
2024-07-20 05:10:50 +02:00
};
2024-07-20 20:42:43 +02:00
createChatViewModel = (chatModel: ChatModel): ChatViewModel => {
2024-07-20 21:04:41 +02:00
return new ChatViewModel(
chatModel,
this.storageModel,
this.settingsViewModel,
this
);
};
2024-07-20 20:42:43 +02:00
2024-07-23 14:15:01 +02:00
updateIndices = (): void => {
this.indexManager.update([...this.chatViewModels.value.values()]);
for (const chatViewModel of this.chatViewModels.value) {
chatViewModel.updateIndex();
}
};
2024-07-20 20:42:43 +02:00
// view
2024-07-20 14:16:26 +02:00
openChat = (chatViewModel: ChatViewModel): void => {
this.selectedChat.value = chatViewModel;
};
closeChat = (): void => {
this.selectedChat.value = undefined;
2024-07-20 05:10:50 +02:00
};
2024-07-21 21:41:19 +02:00
// load
loadChats = (): void => {
2024-07-20 20:48:13 +02:00
this.chatViewModels.clear();
2024-07-20 14:16:26 +02:00
for (const chatModel of this.chatListModel.chatModels.values()) {
2024-07-20 20:42:43 +02:00
const chatViewModel = this.createChatViewModel(chatModel);
2024-07-23 14:15:01 +02:00
this.trackChat(chatViewModel);
2024-07-20 05:10:50 +02:00
}
2024-07-23 14:15:01 +02:00
this.updateIndices();
2024-07-20 05:10:50 +02:00
};
// init
2024-07-20 21:04:41 +02:00
constructor(
chatListModel: ChatListModel,
storageModel: StorageModel,
settingsViewModel: SettingsViewModel
) {
2024-07-20 05:10:50 +02:00
this.chatListModel = chatListModel;
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 05:10:50 +02:00
2024-07-21 21:41:19 +02:00
this.loadChats();
2024-07-20 05:10:50 +02:00
}
}