2024-07-20 05:10:50 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
import ChatListModel from "../Model/Chat/chatListModel";
|
2024-07-22 15:07:31 +02:00
|
|
|
import ChatModel from "../Model/Chat/chatModel";
|
2024-07-20 14:16:26 +02:00
|
|
|
import ChatViewModel from "./chatViewModel";
|
2024-07-20 21:04:41 +02:00
|
|
|
import SettingsViewModel from "./settingsViewModel";
|
2024-07-22 02:05:27 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
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-20 14:16:26 +02:00
|
|
|
this.chatViewModels.add(chatViewModel);
|
2024-07-20 20:42:43 +02:00
|
|
|
|
|
|
|
|
this.updateIndices();
|
2024-07-20 14:16:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// 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-20 20:42:43 +02:00
|
|
|
// sorting
|
|
|
|
|
updateIndices = (): void => {
|
|
|
|
|
for (const chatViewModel of this.chatViewModels.value) {
|
|
|
|
|
chatViewModel.updateIndex();
|
|
|
|
|
}
|
2024-07-20 21:04:41 +02:00
|
|
|
};
|
2024-07-20 20:42:43 +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-20 14:16:26 +02:00
|
|
|
this.chatViewModels.add(chatViewModel);
|
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
|
|
|
}
|
|
|
|
|
}
|