Threads notifications after app startup (#7253)
This commit is contained in:
72
src/stores/notifications/ThreadsRoomNotificationState.ts
Normal file
72
src/stores/notifications/ThreadsRoomNotificationState.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IDestroyable } from "../../utils/IDestroyable";
|
||||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
import { NotificationState, NotificationStateEvents } from "./NotificationState";
|
||||
import { Thread, ThreadEvent } from "matrix-js-sdk/src/models/thread";
|
||||
import { ThreadNotificationState } from "./ThreadNotificationState";
|
||||
import { NotificationColor } from "./NotificationColor";
|
||||
|
||||
export class ThreadsRoomNotificationState extends NotificationState implements IDestroyable {
|
||||
private threadsState = new Map<Thread, ThreadNotificationState>();
|
||||
|
||||
protected _symbol = null;
|
||||
protected _count = 0;
|
||||
protected _color = NotificationColor.None;
|
||||
|
||||
constructor(public readonly room: Room) {
|
||||
super();
|
||||
this.room.on(ThreadEvent.New, this.onNewThread);
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
super.destroy();
|
||||
this.room.on(ThreadEvent.New, this.onNewThread);
|
||||
for (const [, notificationState] of this.threadsState) {
|
||||
notificationState.off(NotificationStateEvents.Update, this.onThreadUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
private onNewThread = (thread: Thread): void => {
|
||||
const notificationState = new ThreadNotificationState(this.room, thread);
|
||||
this.threadsState.set(
|
||||
thread,
|
||||
notificationState,
|
||||
);
|
||||
notificationState.on(NotificationStateEvents.Update, this.onThreadUpdate);
|
||||
};
|
||||
|
||||
private onThreadUpdate = (): void => {
|
||||
let color = NotificationColor.None;
|
||||
for (const [, notificationState] of this.threadsState) {
|
||||
if (notificationState.color === NotificationColor.Red) {
|
||||
color = NotificationColor.Red;
|
||||
break;
|
||||
} else if (notificationState.color === NotificationColor.Grey) {
|
||||
color = NotificationColor.Grey;
|
||||
}
|
||||
}
|
||||
this.updateNotificationState(color);
|
||||
};
|
||||
|
||||
private updateNotificationState(color: NotificationColor): void {
|
||||
const snapshot = this.snapshot();
|
||||
this._color = color;
|
||||
// finally, publish an update if needed
|
||||
this.emitIfUpdated(snapshot);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user