2019-06-21 10:18:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Zender & Kurtz GbR.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Christian Pauly <krille@famedly.com>
|
|
|
|
* Marcel Radzio <mtrnord@famedly.com>
|
|
|
|
*
|
|
|
|
* This file is part of famedlysdk.
|
|
|
|
*
|
|
|
|
* famedlysdk is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* famedlysdk is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'Event.dart';
|
|
|
|
import 'Room.dart';
|
|
|
|
import 'User.dart';
|
|
|
|
import 'sync/EventUpdate.dart';
|
|
|
|
|
|
|
|
/// Represents the timeline of a room. The callbacks [onUpdate], [onDelete],
|
|
|
|
/// [onInsert] and [onResort] will be triggered automatically. The initial
|
|
|
|
/// event list will be retreived when created by the [room.getTimeline] method.
|
|
|
|
class Timeline {
|
|
|
|
final Room room;
|
|
|
|
List<Event> events = [];
|
|
|
|
|
2019-06-25 10:06:26 +00:00
|
|
|
final onTimelineUpdateCallback onUpdate;
|
|
|
|
final onTimelineInsertCallback onInsert;
|
2019-06-21 10:18:54 +00:00
|
|
|
|
|
|
|
StreamSubscription<EventUpdate> sub;
|
|
|
|
|
|
|
|
Timeline({this.room, this.events, this.onUpdate, this.onInsert}) {
|
|
|
|
sub ??= room.client.connection.onEvent.stream.listen(_handleEventUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleEventUpdate(EventUpdate eventUpdate) async {
|
|
|
|
try {
|
|
|
|
if (eventUpdate.roomID != room.id) return;
|
|
|
|
if (eventUpdate.type == "timeline" || eventUpdate.type == "history") {
|
|
|
|
if (!eventUpdate.content.containsKey("id"))
|
|
|
|
eventUpdate.content["id"] = eventUpdate.content["event_id"];
|
|
|
|
|
|
|
|
User user = await room.client.store
|
|
|
|
?.getUser(matrixID: eventUpdate.content["sender"], room: room);
|
|
|
|
if (user != null) {
|
|
|
|
eventUpdate.content["displayname"] = user.displayName;
|
|
|
|
eventUpdate.content["avatar_url"] = user.avatarUrl.mxc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Event newEvent = Event.fromJson(eventUpdate.content, room);
|
|
|
|
|
|
|
|
events.insert(0, newEvent);
|
|
|
|
onInsert(0);
|
|
|
|
}
|
2019-06-21 11:30:39 +00:00
|
|
|
sortAndUpdate();
|
2019-06-21 10:18:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
print("[WARNING] ${e.toString()}");
|
|
|
|
sub.cancel();
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 11:30:39 +00:00
|
|
|
|
|
|
|
sortAndUpdate() {
|
|
|
|
events
|
|
|
|
?.sort((a, b) => b.time.toTimeStamp().compareTo(a.time.toTimeStamp()));
|
|
|
|
onUpdate();
|
|
|
|
}
|
2019-06-21 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:06:26 +00:00
|
|
|
typedef onTimelineUpdateCallback = void Function();
|
|
|
|
typedef onTimelineInsertCallback = void Function(int insertID);
|