Files
CT/src/ViewModel/Pages/settingsPageViewModel.ts
T

136 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-07-22 21:07:44 +02:00
import * as React from "bloatless-react";
2026-04-12 19:20:37 +00:00
import ChatViewModel, { ChatPageTypes } from "../Chat/chatViewModel";
2026-04-08 14:02:12 +00:00
import { Colors } from "../../colors";
2026-04-12 19:20:37 +00:00
import CoreViewModel, { Context } from "../Global/coreViewModel";
export default class SettingsPageViewModel extends Context {
contextDebugDescription = "settings";
2024-07-22 21:07:44 +02:00
2026-04-01 08:29:39 +00:00
// state
primaryChannel: React.State<string> = new React.State("");
primaryChannelInput: React.State<string> = new React.State("");
secondaryChannels: React.ListState<string> = new React.ListState();
newSecondaryChannelInput: React.State<string> = new React.State("");
encryptionKeyInput: React.State<string> = new React.State("");
shouldShowEncryptionKey: React.State<boolean> = new React.State(false);
encryptionKeyInputType: React.State<"text" | "password"> =
React.createProxyState([this.shouldShowEncryptionKey], () =>
this.shouldShowEncryptionKey.value == true ? "text" : "password",
);
2026-04-08 14:02:12 +00:00
color: React.State<Colors> = new React.State<any>(Colors.Standard);
2026-04-01 08:29:39 +00:00
// guards
cannotSetPrimaryChannel: React.State<boolean> = React.createProxyState(
[this.primaryChannel, this.primaryChannelInput],
() =>
this.primaryChannelInput.value == "" ||
this.primaryChannelInput.value == this.primaryChannel.value,
2024-07-22 21:07:44 +02:00
);
2026-04-01 08:29:39 +00:00
cannotAddSecondaryChannel: React.State<boolean> = React.createProxyState(
[this.newSecondaryChannelInput],
() => this.newSecondaryChannelInput.value == "",
2024-07-22 21:07:44 +02:00
);
2026-04-01 08:29:39 +00:00
cannotSetEncryptionKey: React.State<boolean>;
// methods
setPrimaryChannel = (): void => {
this.chatViewModel.chatModel.setPrimaryChannel(
this.primaryChannelInput.value,
);
this.primaryChannel.value =
this.chatViewModel.chatModel.info.primaryChannel;
this.chatViewModel.chatListViewModel.updateIndices();
};
addSecondaryChannel = (): void => {
this.secondaryChannels.add(this.newSecondaryChannelInput.value);
this.newSecondaryChannelInput.value = "";
this.storeSecondaryChannels();
this.loadSecondaryChannels();
};
removeSecondaryChannel = (secondaryChannel: string): void => {
this.secondaryChannels.remove(secondaryChannel);
this.storeSecondaryChannels();
};
storeSecondaryChannels = (): void => {
this.chatViewModel.chatModel.setSecondaryChannels([
...this.secondaryChannels.value.values(),
]);
};
setEncryptionKey = (): void => {
this.chatViewModel.chatModel.setEncryptionKey(
this.encryptionKeyInput.value,
);
// disable button
this.encryptionKeyInput.callSubscriptions();
};
2026-04-08 14:02:12 +00:00
applyColor = (newColor: Colors): void => {
2026-04-01 08:29:39 +00:00
this.chatViewModel.setColor(newColor);
};
remove = (): void => {
this.chatViewModel.close();
this.chatViewModel.chatModel.delete();
this.chatViewModel.chatListViewModel.untrackChat(this.chatViewModel);
};
// load
loadListRelevantData = (): void => {
this.primaryChannel.value =
this.chatViewModel.chatModel.info.primaryChannel;
this.color.value = this.chatViewModel.chatModel.color;
};
loadData = (): void => {
this.primaryChannelInput.value =
this.chatViewModel.chatModel.info.primaryChannel;
this.loadSecondaryChannels();
this.encryptionKeyInput.value =
this.chatViewModel.chatModel.info.encryptionKey;
};
loadSecondaryChannels = (): void => {
this.secondaryChannels.clear();
for (const secondaryChannel of this.chatViewModel.chatModel
.secondaryChannels) {
this.secondaryChannels.add(secondaryChannel);
}
};
// init
constructor(
2026-04-13 10:58:21 +00:00
public readonly coreViewModel: CoreViewModel,
public readonly chatViewModel: ChatViewModel,
2026-04-01 08:29:39 +00:00
) {
2026-04-12 19:20:37 +00:00
super();
2026-04-01 08:29:39 +00:00
this.loadListRelevantData();
this.cannotSetEncryptionKey = React.createProxyState(
[this.encryptionKeyInput],
() =>
this.encryptionKeyInput.value ==
this.chatViewModel.chatModel.info.encryptionKey,
);
this.color.subscribe((newColor) => {
this.applyColor(newColor);
});
2026-04-12 19:20:37 +00:00
this.chatViewModel.registerContext(ChatPageTypes.Settings, this);
2024-07-22 21:07:44 +02:00
}
}