re-implement chat sorting
This commit is contained in:
Vendored
+43
-1
@@ -333,6 +333,29 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
var IndexManager = class {
|
||||||
|
itemToString;
|
||||||
|
sortedStrings = [];
|
||||||
|
// methods
|
||||||
|
update = (items) => {
|
||||||
|
this.sortedStrings = [];
|
||||||
|
let strings = [];
|
||||||
|
for (const item of items) {
|
||||||
|
const string = this.itemToString(item);
|
||||||
|
strings.push(string);
|
||||||
|
}
|
||||||
|
this.sortedStrings = strings.sort(localeCompare);
|
||||||
|
};
|
||||||
|
getIndex = (item) => {
|
||||||
|
const string = this.itemToString(item);
|
||||||
|
const index = this.sortedStrings.indexOf(string);
|
||||||
|
return index;
|
||||||
|
};
|
||||||
|
// init
|
||||||
|
constructor(itemToString) {
|
||||||
|
this.itemToString = itemToString;
|
||||||
|
}
|
||||||
|
};
|
||||||
function stringify(data) {
|
function stringify(data) {
|
||||||
return JSON.stringify(data, null, 4);
|
return JSON.stringify(data, null, 4);
|
||||||
}
|
}
|
||||||
@@ -1330,6 +1353,7 @@
|
|||||||
this.primaryChannelInput.value
|
this.primaryChannelInput.value
|
||||||
);
|
);
|
||||||
this.primaryChannel.value = this.chatViewModel.chatModel.info.primaryChannel;
|
this.primaryChannel.value = this.chatViewModel.chatModel.info.primaryChannel;
|
||||||
|
this.chatViewModel.chatListViewModel.updateIndices();
|
||||||
};
|
};
|
||||||
addSecondaryChannel = () => {
|
addSecondaryChannel = () => {
|
||||||
this.secondaryChannels.add(this.newSecondaryChannelInput.value);
|
this.secondaryChannels.add(this.newSecondaryChannelInput.value);
|
||||||
@@ -1549,6 +1573,10 @@
|
|||||||
resetColor = () => {
|
resetColor = () => {
|
||||||
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
||||||
};
|
};
|
||||||
|
updateIndex = () => {
|
||||||
|
const index = this.chatListViewModel.indexManager.getIndex(this);
|
||||||
|
this.index.value = index;
|
||||||
|
};
|
||||||
// load
|
// load
|
||||||
loadPageSelection = () => {
|
loadPageSelection = () => {
|
||||||
const path = StorageModel.getPath(
|
const path = StorageModel.getPath(
|
||||||
@@ -1594,6 +1622,9 @@
|
|||||||
// state
|
// state
|
||||||
newChatPrimaryChannel = new State("");
|
newChatPrimaryChannel = new State("");
|
||||||
chatViewModels = new ListState();
|
chatViewModels = new ListState();
|
||||||
|
indexManager = new IndexManager(
|
||||||
|
(chatViewModel) => chatViewModel.settingsPageViewModel.primaryChannel.value
|
||||||
|
);
|
||||||
selectedChat = new State(void 0);
|
selectedChat = new State(void 0);
|
||||||
// guards
|
// guards
|
||||||
cannotCreateChat = createProxyState(
|
cannotCreateChat = createProxyState(
|
||||||
@@ -1607,6 +1638,10 @@
|
|||||||
);
|
);
|
||||||
this.newChatPrimaryChannel.value = "";
|
this.newChatPrimaryChannel.value = "";
|
||||||
const chatViewModel = this.createChatViewModel(chatModel);
|
const chatViewModel = this.createChatViewModel(chatModel);
|
||||||
|
this.trackChat(chatViewModel);
|
||||||
|
this.updateIndices();
|
||||||
|
};
|
||||||
|
trackChat = (chatViewModel) => {
|
||||||
this.chatViewModels.add(chatViewModel);
|
this.chatViewModels.add(chatViewModel);
|
||||||
};
|
};
|
||||||
untrackChat = (chatViewModel) => {
|
untrackChat = (chatViewModel) => {
|
||||||
@@ -1621,6 +1656,12 @@
|
|||||||
this
|
this
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
updateIndices = () => {
|
||||||
|
this.indexManager.update([...this.chatViewModels.value.values()]);
|
||||||
|
for (const chatViewModel of this.chatViewModels.value) {
|
||||||
|
chatViewModel.updateIndex();
|
||||||
|
}
|
||||||
|
};
|
||||||
// view
|
// view
|
||||||
openChat = (chatViewModel) => {
|
openChat = (chatViewModel) => {
|
||||||
this.selectedChat.value = chatViewModel;
|
this.selectedChat.value = chatViewModel;
|
||||||
@@ -1633,8 +1674,9 @@
|
|||||||
this.chatViewModels.clear();
|
this.chatViewModels.clear();
|
||||||
for (const chatModel of this.chatListModel.chatModels.values()) {
|
for (const chatModel of this.chatListModel.chatModels.values()) {
|
||||||
const chatViewModel = this.createChatViewModel(chatModel);
|
const chatViewModel = this.createChatViewModel(chatModel);
|
||||||
this.chatViewModels.add(chatViewModel);
|
this.trackChat(chatViewModel);
|
||||||
}
|
}
|
||||||
|
this.updateIndices();
|
||||||
};
|
};
|
||||||
// init
|
// init
|
||||||
constructor(chatListModel2, storageModel2, settingsViewModel2) {
|
constructor(chatListModel2, storageModel2, settingsViewModel2) {
|
||||||
|
|||||||
@@ -28,6 +28,37 @@ export class HandlerManager<T> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sorting
|
||||||
|
export class IndexManager<T> {
|
||||||
|
private itemToString: (item: T) => string;
|
||||||
|
|
||||||
|
sortedStrings: string[] = [];
|
||||||
|
|
||||||
|
// methods
|
||||||
|
update = (items: T[]): void => {
|
||||||
|
this.sortedStrings = [];
|
||||||
|
|
||||||
|
let strings: string[] = [];
|
||||||
|
for (const item of items) {
|
||||||
|
const string: string = this.itemToString(item);
|
||||||
|
strings.push(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sortedStrings = strings.sort(localeCompare);
|
||||||
|
};
|
||||||
|
|
||||||
|
getIndex = (item: T): number => {
|
||||||
|
const string: string = this.itemToString(item);
|
||||||
|
const index: number = this.sortedStrings.indexOf(string);
|
||||||
|
return index;
|
||||||
|
};
|
||||||
|
|
||||||
|
// init
|
||||||
|
constructor(itemToString: (item: T) => string) {
|
||||||
|
this.itemToString = itemToString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// string
|
// string
|
||||||
export function stringify(data: any): string {
|
export function stringify(data: any): string {
|
||||||
return JSON.stringify(data, null, 4);
|
return JSON.stringify(data, null, 4);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as React from "bloatless-react";
|
|||||||
import ChatListModel from "../../Model/Chat/chatListModel";
|
import ChatListModel from "../../Model/Chat/chatListModel";
|
||||||
import ChatModel from "../../Model/Chat/chatModel";
|
import ChatModel from "../../Model/Chat/chatModel";
|
||||||
import ChatViewModel from "./chatViewModel";
|
import ChatViewModel from "./chatViewModel";
|
||||||
|
import { IndexManager } from "../../Model/Utility/utility";
|
||||||
import SettingsViewModel from "../Global/settingsViewModel";
|
import SettingsViewModel from "../Global/settingsViewModel";
|
||||||
import StorageModel from "../../Model/Global/storageModel";
|
import StorageModel from "../../Model/Global/storageModel";
|
||||||
|
|
||||||
@@ -14,6 +15,10 @@ export default class ChatListViewModel {
|
|||||||
// state
|
// state
|
||||||
newChatPrimaryChannel: React.State<string> = new React.State("");
|
newChatPrimaryChannel: React.State<string> = new React.State("");
|
||||||
chatViewModels: React.ListState<ChatViewModel> = new React.ListState();
|
chatViewModels: React.ListState<ChatViewModel> = new React.ListState();
|
||||||
|
indexManager: IndexManager<ChatViewModel> = new IndexManager(
|
||||||
|
(chatViewModel: ChatViewModel) =>
|
||||||
|
chatViewModel.settingsPageViewModel.primaryChannel.value
|
||||||
|
);
|
||||||
|
|
||||||
selectedChat: React.State<ChatViewModel | undefined> = new React.State<
|
selectedChat: React.State<ChatViewModel | undefined> = new React.State<
|
||||||
ChatViewModel | undefined
|
ChatViewModel | undefined
|
||||||
@@ -33,6 +38,11 @@ export default class ChatListViewModel {
|
|||||||
this.newChatPrimaryChannel.value = "";
|
this.newChatPrimaryChannel.value = "";
|
||||||
|
|
||||||
const chatViewModel: ChatViewModel = this.createChatViewModel(chatModel);
|
const chatViewModel: ChatViewModel = this.createChatViewModel(chatModel);
|
||||||
|
this.trackChat(chatViewModel);
|
||||||
|
this.updateIndices();
|
||||||
|
};
|
||||||
|
|
||||||
|
trackChat = (chatViewModel: ChatViewModel): void => {
|
||||||
this.chatViewModels.add(chatViewModel);
|
this.chatViewModels.add(chatViewModel);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,6 +60,13 @@ export default class ChatListViewModel {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
updateIndices = (): void => {
|
||||||
|
this.indexManager.update([...this.chatViewModels.value.values()]);
|
||||||
|
for (const chatViewModel of this.chatViewModels.value) {
|
||||||
|
chatViewModel.updateIndex();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// view
|
// view
|
||||||
openChat = (chatViewModel: ChatViewModel): void => {
|
openChat = (chatViewModel: ChatViewModel): void => {
|
||||||
this.selectedChat.value = chatViewModel;
|
this.selectedChat.value = chatViewModel;
|
||||||
@@ -64,8 +81,9 @@ export default class ChatListViewModel {
|
|||||||
this.chatViewModels.clear();
|
this.chatViewModels.clear();
|
||||||
for (const chatModel of this.chatListModel.chatModels.values()) {
|
for (const chatModel of this.chatListModel.chatModels.values()) {
|
||||||
const chatViewModel = this.createChatViewModel(chatModel);
|
const chatViewModel = this.createChatViewModel(chatModel);
|
||||||
this.chatViewModels.add(chatViewModel);
|
this.trackChat(chatViewModel);
|
||||||
}
|
}
|
||||||
|
this.updateIndices();
|
||||||
};
|
};
|
||||||
|
|
||||||
// init
|
// init
|
||||||
|
|||||||
@@ -54,6 +54,11 @@ export default class ChatViewModel {
|
|||||||
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
updateIndex = (): void => {
|
||||||
|
const index: number = this.chatListViewModel.indexManager.getIndex(this);
|
||||||
|
this.index.value = index;
|
||||||
|
};
|
||||||
|
|
||||||
// load
|
// load
|
||||||
loadPageSelection = (): void => {
|
loadPageSelection = (): void => {
|
||||||
const path: string[] = StorageModel.getPath(
|
const path: string[] = StorageModel.getPath(
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ export default class SettingsPageViewModel {
|
|||||||
);
|
);
|
||||||
this.primaryChannel.value =
|
this.primaryChannel.value =
|
||||||
this.chatViewModel.chatModel.info.primaryChannel;
|
this.chatViewModel.chatModel.info.primaryChannel;
|
||||||
|
|
||||||
|
this.chatViewModel.chatListViewModel.updateIndices();
|
||||||
};
|
};
|
||||||
|
|
||||||
addSecondaryChannel = (): void => {
|
addSecondaryChannel = (): void => {
|
||||||
|
|||||||
Reference in New Issue
Block a user