implement contacts model and vm with chat model
This commit is contained in:
@@ -9,13 +9,9 @@ import StorageModel, {
|
||||
} from "../Global/storageModel";
|
||||
import SettingsModel from "../Global/settingsModel";
|
||||
import ConnectionModel from "../Global/connectionModel";
|
||||
import ContactListModel from "../Global/contactListModel";
|
||||
|
||||
export default class ChatListModel {
|
||||
// models
|
||||
readonly storageModel: StorageModel;
|
||||
readonly settingsModel: SettingsModel;
|
||||
readonly connectionModel: ConnectionModel;
|
||||
|
||||
// data
|
||||
readonly chatModels = new Set<ChatModel>();
|
||||
|
||||
@@ -32,6 +28,7 @@ export default class ChatListModel {
|
||||
this.connectionModel,
|
||||
this.settingsModel,
|
||||
this,
|
||||
this.contactListModel,
|
||||
id,
|
||||
);
|
||||
chatModel.setName(primaryChannel);
|
||||
@@ -93,6 +90,7 @@ export default class ChatListModel {
|
||||
this.connectionModel,
|
||||
this.settingsModel,
|
||||
this,
|
||||
this.contactListModel,
|
||||
chatId,
|
||||
);
|
||||
this.addChatModel(chatModel);
|
||||
@@ -101,20 +99,18 @@ export default class ChatListModel {
|
||||
|
||||
// init
|
||||
constructor(
|
||||
storageModel: StorageModel,
|
||||
settingsModel: SettingsModel,
|
||||
connectionModel: ConnectionModel,
|
||||
public storageModel: StorageModel,
|
||||
public settingsModel: SettingsModel,
|
||||
public connectionModel: ConnectionModel,
|
||||
public contactListModel: ContactListModel
|
||||
) {
|
||||
this.storageModel = storageModel;
|
||||
this.settingsModel = settingsModel;
|
||||
this.connectionModel = connectionModel;
|
||||
this.loadChats();
|
||||
|
||||
connectionModel.messageHandlerManager.setHandler(
|
||||
this.connectionModel.messageHandlerManager.setHandler(
|
||||
"chat-list",
|
||||
this.messageHandler,
|
||||
);
|
||||
connectionModel.messageSentHandlerManager.setHandler(
|
||||
this.connectionModel.messageSentHandlerManager.setHandler(
|
||||
"chat-list",
|
||||
this.messageSentHandler,
|
||||
);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// cleanup: Phase A
|
||||
|
||||
import { v4 } from "uuid";
|
||||
import ChatListModel from "./chatListModel";
|
||||
import {
|
||||
@@ -23,13 +21,10 @@ import SettingsModel from "../Global/settingsModel";
|
||||
import ConnectionModel from "../Global/connectionModel";
|
||||
import FileModel, { FileContent } from "../Files/fileModel";
|
||||
import { Colors } from "../../colors";
|
||||
import ContactListModel from "../Global/contactListModel";
|
||||
|
||||
export default class ChatModel {
|
||||
// models
|
||||
readonly connectionModel: ConnectionModel;
|
||||
readonly storageModel: StorageModel;
|
||||
readonly settingsModel: SettingsModel;
|
||||
readonly chatListModel: ChatListModel;
|
||||
readonly fileModel: FileModel;
|
||||
|
||||
// data
|
||||
@@ -167,6 +162,8 @@ export default class ChatModel {
|
||||
readonly addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
||||
await this.decryptMessage(chatMessage);
|
||||
|
||||
this.contactListModel.storeContact(chatMessage.senderId, chatMessage.senderName);
|
||||
|
||||
// message
|
||||
if (chatMessage.body != "") {
|
||||
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
||||
@@ -349,17 +346,14 @@ export default class ChatModel {
|
||||
|
||||
// init
|
||||
constructor(
|
||||
storageModel: StorageModel,
|
||||
connectionModel: ConnectionModel,
|
||||
settingsModel: SettingsModel,
|
||||
chatListModel: ChatListModel,
|
||||
public storageModel: StorageModel,
|
||||
public connectionModel: ConnectionModel,
|
||||
public settingsModel: SettingsModel,
|
||||
public chatListModel: ChatListModel,
|
||||
public contactListModel: ContactListModel,
|
||||
chatId: string,
|
||||
) {
|
||||
this.id = chatId;
|
||||
this.connectionModel = connectionModel;
|
||||
this.settingsModel = settingsModel;
|
||||
this.storageModel = storageModel;
|
||||
this.chatListModel = chatListModel;
|
||||
|
||||
this.loadInfo();
|
||||
this.subscribe();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import {HandlerManager} from "../Utility/utility";
|
||||
import StorageModel, {StorageModelSubPaths} from "./storageModel";
|
||||
|
||||
export default class ContactListModel {
|
||||
contactHandlerManager = new HandlerManager<[string, string]>();
|
||||
|
||||
private getContactPath = (contactId: string): string[] => {
|
||||
return [StorageModelSubPaths.ContactsModel, contactId];
|
||||
}
|
||||
@@ -14,6 +17,7 @@ export default class ContactListModel {
|
||||
storeContact = (id: string, name: string) => {
|
||||
const path = this.getContactPath(id);
|
||||
this.storageModel.write(path, name)
|
||||
this.contactHandlerManager.trigger([id, name]);
|
||||
}
|
||||
|
||||
constructor(public storageModel: StorageModel) {}
|
||||
|
||||
@@ -13,7 +13,10 @@ export default class ContactListViewModel {
|
||||
}
|
||||
const vm = new ContactViewModel(id, name);
|
||||
this.contacts.set(id, vm);
|
||||
this.contactListModel.storeContact(id, name);
|
||||
}
|
||||
|
||||
handleContact = (contact: [string, string]): void => {
|
||||
this.updateContact(...contact);
|
||||
}
|
||||
|
||||
// load
|
||||
@@ -28,6 +31,7 @@ export default class ContactListViewModel {
|
||||
// init
|
||||
constructor(public contactListModel: ContactListModel) {
|
||||
this.loadData();
|
||||
this.contactListModel.contactHandlerManager.setHandler("contact-list-view-model", this.handleContact);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,15 +20,19 @@ import SettingsModel from "./Model/Global/settingsModel";
|
||||
import FileTransferModel from "./Model/Global/fileTransferModel";
|
||||
import ConnectionModel from "./Model/Global/connectionModel";
|
||||
import ChatListModel from "./Model/Chat/chatListModel";
|
||||
import ContactListModel from "./Model/Global/contactListModel";
|
||||
import ContactListViewModel from "./ViewModel/Global/contactViewModel";
|
||||
|
||||
// models
|
||||
const storageModel = new StorageModel();
|
||||
const settingsModel = new SettingsModel(storageModel);
|
||||
const contactListModel = new ContactListModel(storageModel);
|
||||
const connectionModel = new ConnectionModel(storageModel);
|
||||
const chatListModel = new ChatListModel(
|
||||
storageModel,
|
||||
settingsModel,
|
||||
connectionModel,
|
||||
contactListModel,
|
||||
);
|
||||
const fileTransferModel = new FileTransferModel(storageModel, connectionModel);
|
||||
|
||||
@@ -43,6 +47,7 @@ const coreViewModel = new CoreViewModel(
|
||||
|
||||
const storageViewModel = new StorageViewModel(coreViewModel);
|
||||
const settingsViewModel = new SettingsViewModel(coreViewModel);
|
||||
const contactListViewModel = new ContactListViewModel(contactListModel);
|
||||
const connectionViewModel = new ConnectionViewModel(coreViewModel);
|
||||
const chatListViewModel = new ChatListViewModel(
|
||||
coreViewModel,
|
||||
|
||||
Reference in New Issue
Block a user