sort unread rooms first, then on last message timestamp, then alphabet.

This commit is contained in:
Bruno Windels 2020-08-21 11:58:05 +02:00
parent dbf5e59d87
commit eae5bc4230

View File

@ -17,6 +17,10 @@ limitations under the License.
import {avatarInitials, getIdentifierColorNumber} from "../../avatar.js"; import {avatarInitials, getIdentifierColorNumber} from "../../avatar.js";
import {ViewModel} from "../../ViewModel.js"; import {ViewModel} from "../../ViewModel.js";
function isSortedAsUnread(vm) {
return vm.isUnread || (vm.isOpen && vm._wasUnreadWhenOpening);
}
export class RoomTileViewModel extends ViewModel { export class RoomTileViewModel extends ViewModel {
// we use callbacks to parent VM instead of emit because // we use callbacks to parent VM instead of emit because
// it would be annoying to keep track of subscriptions in // it would be annoying to keep track of subscriptions in
@ -28,6 +32,7 @@ export class RoomTileViewModel extends ViewModel {
this._room = room; this._room = room;
this._emitOpen = emitOpen; this._emitOpen = emitOpen;
this._isOpen = false; this._isOpen = false;
this._wasUnreadWhenOpening = false;
} }
// called by parent for now (later should integrate with router) // called by parent for now (later should integrate with router)
@ -40,17 +45,32 @@ export class RoomTileViewModel extends ViewModel {
open() { open() {
this._isOpen = true; this._isOpen = true;
this._wasUnreadWhenOpening = this._room.isUnread;
this.emitChange("isOpen"); this.emitChange("isOpen");
this._emitOpen(this._room, this); this._emitOpen(this._room, this);
} }
compare(other) { compare(other) {
// sort alphabetically const myRoom = this._room;
const nameCmp = this._room.name.localeCompare(other._room.name); const theirRoom = other._room;
if (nameCmp === 0) {
return this._room.id.localeCompare(other._room.id); if (isSortedAsUnread(this) !== isSortedAsUnread(other)) {
if (isSortedAsUnread(this)) {
return -1;
}
return 1;
} }
return nameCmp;
const timeDiff = theirRoom.lastMessageTimestamp - myRoom.lastMessageTimestamp;
if (timeDiff === 0) {
// sort alphabetically
const nameCmp = this._room.name.localeCompare(other._room.name);
if (nameCmp === 0) {
return this._room.id.localeCompare(other._room.id);
}
return nameCmp;
}
return timeDiff;
} }
get isOpen() { get isOpen() {