refactor: timeline

This commit is contained in:
Christian Pauly 2020-08-21 07:56:39 +02:00
parent d6b97b8e78
commit c46f4ba066

View file

@ -238,13 +238,10 @@ class Timeline {
: null); : null);
if (i < events.length) { if (i < events.length) {
// we want to preserve the old sort order
final tempSortOrder = events[i].sortOrder;
// if the old status is larger than the new one, we also want to preserve the old status // if the old status is larger than the new one, we also want to preserve the old status
final oldStatus = events[i].status; final oldStatus = events[i].status;
events[i] = Event.fromJson( events[i] = Event.fromJson(
eventUpdate.content, room, eventUpdate.sortOrder); eventUpdate.content, room, eventUpdate.sortOrder);
events[i].sortOrder = tempSortOrder;
// do we preserve the status? we should allow 0 -> -1 updates and status increases // do we preserve the status? we should allow 0 -> -1 updates and status increases
if (status < oldStatus && !(status == -1 && oldStatus == 0)) { if (status < oldStatus && !(status == -1 && oldStatus == 0)) {
events[i].status = oldStatus; events[i].status = oldStatus;
@ -265,23 +262,19 @@ class Timeline {
} }
} }
} }
sortAndUpdate(); _sort();
if (onUpdate != null) onUpdate();
} catch (e, s) { } catch (e, s) {
Logs.warning('Handle event update failed: ${e.toString()}', s); Logs.warning('Handle event update failed: ${e.toString()}', s);
} }
} }
bool sortLock = false; bool _sortLock = false;
void sort() { void _sort() {
if (sortLock || events.length < 2) return; if (_sortLock || events.length < 2) return;
sortLock = true; _sortLock = true;
events?.sort((a, b) => b.sortOrder - a.sortOrder > 0 ? 1 : -1); events?.sort((a, b) => b.sortOrder - a.sortOrder > 0 ? 1 : -1);
sortLock = false; _sortLock = false;
}
void sortAndUpdate() async {
sort();
if (onUpdate != null) onUpdate();
} }
} }