chore: Set correct stories start position

This commit is contained in:
Christian Pauly 2021-12-26 10:56:38 +01:00
parent 80bd291805
commit 71a2ff5170
2 changed files with 79 additions and 57 deletions

View File

@ -140,6 +140,7 @@ class StoryPageController extends State<StoryPage> {
index++; index++;
}); });
_restartTimer(); _restartTimer();
maybeSetReadMarker();
} }
DateTime _holdedAt = DateTime.fromMicrosecondsSinceEpoch(0); DateTime _holdedAt = DateTime.fromMicrosecondsSinceEpoch(0);
@ -198,71 +199,89 @@ class StoryPageController extends State<StoryPage> {
Future<void>? loadStory; Future<void>? loadStory;
Future<void> _loadStory() async { Future<void> _loadStory() async {
final client = Matrix.of(context).client; try {
await client.roomsLoading; final client = Matrix.of(context).client;
await client.accountDataLoading; await client.roomsLoading;
final room = client.getRoomById(roomId); await client.accountDataLoading;
if (room == null) return; final room = client.getRoomById(roomId);
if (room.membership != Membership.join) { if (room == null) return;
final joinedFuture = room.client.onSync.stream if (room.membership != Membership.join) {
.where((u) => u.rooms?.join?.containsKey(room.id) ?? false) final joinedFuture = room.client.onSync.stream
.first; .where((u) => u.rooms?.join?.containsKey(room.id) ?? false)
await room.join(); .first;
await joinedFuture; await room.join();
} await joinedFuture;
final timeline = this.timeline = await room.getTimeline();
timeline.requestKeys();
var events =
timeline.events.where((e) => e.type == EventTypes.Message).toList();
final hasOutdatedEvents = events.removeOutdatedEvents();
// Request history if possible
if (!hasOutdatedEvents &&
timeline.events.first.type != EventTypes.RoomCreate &&
events.length < 30) {
try {
await timeline.requestHistory(historyCount: 100);
events =
timeline.events.where((e) => e.type == EventTypes.Message).toList();
events.removeOutdatedEvents();
} catch (e, s) {
Logs().d('Unable to request history in stories', e, s);
} }
} final timeline = this.timeline = await room.getTimeline();
timeline.requestKeys();
var events =
timeline.events.where((e) => e.type == EventTypes.Message).toList();
max = events.length; final hasOutdatedEvents = events.removeOutdatedEvents();
if (events.isNotEmpty) {
_restartTimer();
}
// Preload images and videos // Request history if possible
events if (!hasOutdatedEvents &&
.where((event) => {MessageTypes.Image, MessageTypes.Video} timeline.events.first.type != EventTypes.RoomCreate &&
.contains(event.messageType)) events.length < 30) {
.forEach((event) => downloadAndDecryptAttachment(event, try {
event.messageType == MessageTypes.Video && PlatformInfos.isMobile)); await timeline
.requestHistory(historyCount: 100)
if (!events.last.receipts .timeout(const Duration(seconds: 5));
.any((receipt) => receipt.user.id == room.client.userID)) { events = timeline.events
for (var j = 0; j < events.length; j++) { .where((e) => e.type == EventTypes.Message)
if (events[j] .toList();
.receipts events.removeOutdatedEvents();
.any((receipt) => receipt.user.id == room.client.userID)) { } catch (e, s) {
index = j; Logs().d('Unable to request history in stories', e, s);
room.setReadMarker(
events[index].eventId,
mRead: events[index].eventId,
);
break;
} }
} }
max = events.length;
if (events.isNotEmpty) {
_restartTimer();
}
// Preload images and videos
events
.where((event) => {MessageTypes.Image, MessageTypes.Video}
.contains(event.messageType))
.forEach((event) => downloadAndDecryptAttachment(
event,
event.messageType == MessageTypes.Video &&
PlatformInfos.isMobile));
// Reverse list
this.events.clear();
this.events.addAll(events.reversed.toList());
// Set start position
if (this.events.isNotEmpty) {
final receiptId = room.roomAccountData['m.receipt']?.content
.tryGetMap<String, dynamic>(room.client.userID!)
?.tryGet<String>('event_id');
index = this.events.indexWhere((event) => event.eventId == receiptId);
index++;
if (index >= this.events.length) index = 0;
}
maybeSetReadMarker();
} catch (e, s) {
Logs().e('Unable to load story', e, s);
} }
this.events.clear();
this.events.addAll(events.reversed.toList());
return; return;
} }
void maybeSetReadMarker() {
final currentEvent = this.currentEvent;
if (currentEvent == null) return;
final room = currentEvent.room;
if (!currentSeenByUsers.any((u) => u.id == u.room.client.userID)) {
room.setReadMarker(
currentEvent.eventId,
mRead: currentEvent.eventId,
);
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
loadStory ??= _loadStory(); loadStory ??= _loadStory();

View File

@ -97,7 +97,10 @@ class StoryView extends StatelessWidget {
Text( Text(
L10n.of(context)!.thisUserHasNotPostedAnythingYet, L10n.of(context)!.thisUserHasNotPostedAnythingYet,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: const TextStyle(fontSize: 20), style: const TextStyle(
fontSize: 20,
color: Colors.white,
),
), ),
], ],
), ),