Files
CT/src/ViewModel/Global/contactViewModel.ts
T

69 lines
1.8 KiB
TypeScript
Raw Normal View History

import SettingsViewModel from "./settingsViewModel";
2026-09-28 15:21:50 +02:00
import * as React from "../../react";
import ContactListModel from "../../Model/Global/contactListModel";
2026-09-27 02:31:21 +02:00
export default class ContactListViewModel {
// state
2026-09-27 03:01:43 +02:00
contacts: Map<string, ContactViewModel> = new Map();
2026-09-27 02:31:21 +02:00
// main
2026-09-27 03:01:43 +02:00
updateContact = (id: string, name: string): ContactViewModel => {
2026-09-28 15:21:50 +02:00
if (id == this.settingsViewModel.settingsModel.userid) {
this.settingsViewModel.setName(name);
}
const match = this.contacts.get(id);
if (match) {
match.name.value = name;
return match;
}
const vm = new ContactViewModel(id, name);
this.contacts.set(id, vm);
return vm;
};
handleContact = (contact: [string, string]): void => {
2026-09-28 15:21:50 +02:00
this.updateContact(...contact);
};
2026-09-27 02:31:21 +02:00
2026-09-27 03:01:43 +02:00
unwrapContact = (id: string, knownName: string): ContactViewModel => {
2026-09-28 15:21:50 +02:00
if (this.contacts.has(id)) return this.contacts.get(id)!;
return this.updateContact(id, knownName);
};
2026-09-27 03:01:43 +02:00
2026-09-27 02:31:21 +02:00
// load
loadData = (): void => {
2026-09-28 15:21:50 +02:00
const contacts: [string, string][] =
this.contactListModel.loadContacts();
for (const contact of contacts) {
const vm = new ContactViewModel(...contact);
this.contacts.set(vm.id, vm);
}
};
2026-09-27 02:31:21 +02:00
// init
2026-09-28 15:21:50 +02:00
constructor(
public contactListModel: ContactListModel,
public settingsViewModel: SettingsViewModel,
) {
this.loadData();
this.contactListModel.contactHandlerManager.setHandler(
"contact-list-view-model",
this.handleContact,
);
2026-09-27 02:31:21 +02:00
}
}
export class ContactViewModel {
// state
name: React.State<string> = new React.State("");
// init
2026-09-28 15:21:50 +02:00
constructor(
public id: string,
name: string,
) {
this.name.value = name;
2026-09-27 02:31:21 +02:00
}
}