forked from mystiq/hydrogen-web
sort unread rooms first, then on last message timestamp, then alphabet.
This commit is contained in:
parent
dbf5e59d87
commit
eae5bc4230
1 changed files with 25 additions and 5 deletions
|
@ -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,11 +45,24 @@ 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) {
|
||||||
|
const myRoom = this._room;
|
||||||
|
const theirRoom = other._room;
|
||||||
|
|
||||||
|
if (isSortedAsUnread(this) !== isSortedAsUnread(other)) {
|
||||||
|
if (isSortedAsUnread(this)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeDiff = theirRoom.lastMessageTimestamp - myRoom.lastMessageTimestamp;
|
||||||
|
if (timeDiff === 0) {
|
||||||
// sort alphabetically
|
// sort alphabetically
|
||||||
const nameCmp = this._room.name.localeCompare(other._room.name);
|
const nameCmp = this._room.name.localeCompare(other._room.name);
|
||||||
if (nameCmp === 0) {
|
if (nameCmp === 0) {
|
||||||
|
@ -52,6 +70,8 @@ export class RoomTileViewModel extends ViewModel {
|
||||||
}
|
}
|
||||||
return nameCmp;
|
return nameCmp;
|
||||||
}
|
}
|
||||||
|
return timeDiff;
|
||||||
|
}
|
||||||
|
|
||||||
get isOpen() {
|
get isOpen() {
|
||||||
return this._isOpen;
|
return this._isOpen;
|
||||||
|
|
Loading…
Reference in a new issue