Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date headers in timeline (second stab) #938

Merged
merged 16 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/domain/ViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {ILogger} from "../logging/types";
import type {Navigation} from "./navigation/Navigation";
import type {SegmentType} from "./navigation/index";
import type {IURLRouter} from "./navigation/URLRouter";
import type { ITimeFormatter } from "../platform/types/types";

export type Options<T extends object = SegmentType> = {
platform: Platform;
Expand Down Expand Up @@ -145,4 +146,8 @@ export class ViewModel<N extends object = SegmentType, O extends Options<N> = Op
// typescript needs a little help here
return this._options.navigation as unknown as Navigation<N>;
}

get timeFormatter(): ITimeFormatter {
return this._options.platform.timeFormatter;
}
}
172 changes: 158 additions & 14 deletions src/domain/session/room/timeline/TilesCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import {BaseObservableList} from "../../../../observable/list/BaseObservableList";
import {sortedIndex} from "../../../../utils/sortedIndex";
import {TileShape} from "./tiles/ITile";

// maps 1..n entries to 0..1 tile. Entries are what is stored in the timeline, either an event or fragmentboundary
// for now, tileClassForEntry should be stable in whether it returns a tile or not.
Expand Down Expand Up @@ -51,6 +52,7 @@ export class TilesCollection extends BaseObservableList {
}

_populateTiles() {
this._silent = true;
this._tiles = [];
let currentTile = null;
for (let entry of this._entries) {
Expand All @@ -72,11 +74,20 @@ export class TilesCollection extends BaseObservableList {
if (prevTile) {
prevTile.updateNextSibling(null);
}
// add date headers here
for (let idx = 0; idx < this._tiles.length; idx += 1) {
const tile = this._tiles[idx];
if (tile.needsDateSeparator) {
this._addTileAt(idx, tile.createDateSeparator(), true);
idx += 1; // tile's index moved one up, don't process it again
}
}
// now everything is wired up,
// allow tiles to emit updates
for (const tile of this._tiles) {
tile.setUpdateEmit(this._emitSpontanousUpdate);
}
this._silent = false;
}

_findTileIdx(entry) {
Expand Down Expand Up @@ -130,25 +141,57 @@ export class TilesCollection extends BaseObservableList {

const newTile = this._createTile(entry);
if (newTile) {
if (prevTile) {
prevTile.updateNextSibling(newTile);
// this emits an update while the add hasn't been emitted yet
newTile.updatePreviousSibling(prevTile);
}
if (nextTile) {
newTile.updateNextSibling(nextTile);
nextTile.updatePreviousSibling(newTile);
}
this._tiles.splice(tileIdx, 0, newTile);
this.emitAdd(tileIdx, newTile);
// add event is emitted, now the tile
// can emit updates
newTile.setUpdateEmit(this._emitSpontanousUpdate);
this._addTileAt(tileIdx, newTile);
this._evaluateDateHeaderAtIdx(tileIdx);
}
// find position by sort key
// ask siblings to be included? both? yes, twice: a (insert c here) b, ask a(c), if yes ask b(a), else ask b(c)? if yes then b(a)?
}

_evaluateDateHeaderAtIdx(tileIdx) {
// consider two tiles after the inserted tile, because
// the first of the two tiles may be a DateTile in which case,
// we remove it after looking at the needsDateSeparator prop of the
// next next tile
for (let i = 0; i < 3; i += 1) {
const idx = tileIdx + i;
if (idx >= this._tiles.length) {
break;
}
const tile = this._tiles[idx];
const prevTile = idx > 0 ? this._tiles[idx - 1] : undefined;
const hasDateSeparator = prevTile?.shape === TileShape.DateHeader;
if (tile.needsDateSeparator && !hasDateSeparator) {
// adding a tile shift all the indices we need to consider
// especially given we consider removals for the tile that
// comes after a datetile
tileIdx += 1;
this._addTileAt(idx, tile.createDateSeparator());
} else if (!tile.needsDateSeparator && hasDateSeparator) {
// this is never triggered because needsDateSeparator is not cleared
// when loading more items because we don't do anything once the
// direct sibling is a DateTile
this._removeTile(idx - 1, prevTile);
}
}
}

_addTileAt(idx, newTile, silent = false) {
const prevTile = idx > 0 ? this._tiles[idx - 1] : undefined;
const nextTile = this._tiles[idx];
prevTile?.updateNextSibling(newTile);
newTile.updatePreviousSibling(prevTile);
newTile.updateNextSibling(nextTile);
nextTile?.updatePreviousSibling(newTile);
this._tiles.splice(idx, 0, newTile);
if (!silent) {
this.emitAdd(idx, newTile);
}
// add event is emitted, now the tile
// can emit updates
newTile.setUpdateEmit(this._emitSpontanousUpdate);
}

onUpdate(index, entry, params) {
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
if (!this._tiles) {
Expand Down Expand Up @@ -210,11 +253,16 @@ export class TilesCollection extends BaseObservableList {
this.emitRemove(tileIdx, tile);
prevTile?.updateNextSibling(nextTile);
nextTile?.updatePreviousSibling(prevTile);

if (prevTile && prevTile.shape === TileShape.DateHeader && (!nextTile || !nextTile.needsDateSeparator)) {
this._removeTile(tileIdx - 1, prevTile);
}
}

// would also be called when unloading a part of the timeline
onRemove(index, entry) {
const tileIdx = this._findTileIdx(entry);

const tile = this._findTileAtIdx(entry, tileIdx);
if (tile) {
const removeTile = tile.removeEntry(entry);
Expand Down Expand Up @@ -268,6 +316,7 @@ export function tests() {
constructor(entry) {
this.entry = entry;
this.update = null;
this.needsDateSeparator = false;
}
setUpdateEmit(update) {
this.update = update;
Expand Down Expand Up @@ -297,6 +346,34 @@ export function tests() {
dispose() {}
}

class DateHeaderTile extends TestTile {
get shape() { return TileShape.DateHeader; }
updateNextSibling(next) {
this.next = next;
}
updatePreviousSibling(prev) {
this.next?.updatePreviousSibling(prev);
}
compareEntry(b) {
// important that date tiles as sorted before their next item, but after their previous sibling
return this.next.compareEntry(b) - 0.5;
}
}

class MessageNeedingDateHeaderTile extends TestTile {
get shape() { return TileShape.Message; }

createDateSeparator() {
return new DateHeaderTile(this.entry);
}
updatePreviousSibling(prev) {
if (prev?.shape !== TileShape.DateHeader) {
// 1 day is 10
this.needsDateSeparator = !prev || Math.floor(prev.entry.n / 10) !== Math.floor(this.entry.n / 10);
}
}
}

return {
"don't emit update before add": assert => {
class UpdateOnSiblingTile extends TestTile {
Expand Down Expand Up @@ -355,6 +432,73 @@ export function tests() {
});
entries.remove(1);
assert.deepEqual(events, ["remove", "update"]);
},
"date tile is added when needed when populating": assert => {
const entries = new ObservableArray([{n: 15}]);
const tileOptions = {
tileClassForEntry: () => MessageNeedingDateHeaderTile,
};
const tiles = new TilesCollection(entries, tileOptions);
tiles.subscribe({});
const tilesArray = Array.from(tiles);
assert.equal(tilesArray.length, 2);
assert.equal(tilesArray[0].shape, TileShape.DateHeader);
assert.equal(tilesArray[1].shape, TileShape.Message);
},
"date header is added when receiving addition": assert => {
const entries = new ObservableArray([{n: 15}]);
const tileOptions = {
tileClassForEntry: () => MessageNeedingDateHeaderTile,
};
const tiles = new TilesCollection(entries, tileOptions);
tiles.subscribe({
onAdd() {},
onRemove() {}
});
entries.insert(0, {n: 5});
const tilesArray = Array.from(tiles);
assert.equal(tilesArray[0].shape, TileShape.DateHeader);
assert.equal(tilesArray[1].shape, TileShape.Message);
assert.equal(tilesArray[2].shape, TileShape.DateHeader);
assert.equal(tilesArray[3].shape, TileShape.Message);
assert.equal(tilesArray.length, 4);
},
"date header is removed and added when loading more messages for the same day": assert => {
const entries = new ObservableArray([{n: 15}]);
const tileOptions = {
tileClassForEntry: () => MessageNeedingDateHeaderTile,
};
const tiles = new TilesCollection(entries, tileOptions);
tiles.subscribe({
onAdd() {},
onRemove() {}
});
entries.insert(0, {n: 12});
const tilesArray = Array.from(tiles);
assert.equal(tilesArray[0].shape, TileShape.DateHeader);
assert.equal(tilesArray[1].shape, TileShape.Message);
assert.equal(tilesArray[2].shape, TileShape.Message);
assert.equal(tilesArray.length, 3);
},
"date header is removed at the end of the timeline": assert => {
const entries = new ObservableArray([{n: 5}, {n: 15}]);
const tileOptions = {
tileClassForEntry: () => MessageNeedingDateHeaderTile,
};
const tiles = new TilesCollection(entries, tileOptions);
let removals = 0;
tiles.subscribe({
onAdd() {},
onRemove() {
removals += 1;
}
});
entries.remove(1);
const tilesArray = Array.from(tiles);
assert.equal(tilesArray[0].shape, TileShape.DateHeader);
assert.equal(tilesArray[1].shape, TileShape.Message);
assert.equal(tilesArray.length, 2);
assert.equal(removals, 2);
}
}
}
7 changes: 1 addition & 6 deletions src/domain/session/room/timeline/tiles/BaseMessageTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {getIdentifierColorNumber, avatarInitials, getAvatarHttpUrl} from "../../
export class BaseMessageTile extends SimpleTile {
constructor(entry, options) {
super(entry, options);
this._date = this._entry.timestamp ? new Date(this._entry.timestamp) : null;
this._isContinuation = false;
this._reactions = null;
this._replyTile = null;
Expand Down Expand Up @@ -78,12 +77,8 @@ export class BaseMessageTile extends SimpleTile {
return this.sender;
}

get date() {
return this._date && this._date.toLocaleDateString({}, {month: "numeric", day: "numeric"});
}

get time() {
return this._date && this._date.toLocaleTimeString({}, {hour: "numeric", minute: "2-digit"});
return this._date && this.timeFormatter.formatTime(this._date);
}

get isOwn() {
Expand Down
Loading