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,6 +199,7 @@ class StoryPageController extends State<StoryPage> {
Future<void>? loadStory; Future<void>? loadStory;
Future<void> _loadStory() async { Future<void> _loadStory() async {
try {
final client = Matrix.of(context).client; final client = Matrix.of(context).client;
await client.roomsLoading; await client.roomsLoading;
await client.accountDataLoading; await client.accountDataLoading;
@ -222,9 +224,12 @@ class StoryPageController extends State<StoryPage> {
timeline.events.first.type != EventTypes.RoomCreate && timeline.events.first.type != EventTypes.RoomCreate &&
events.length < 30) { events.length < 30) {
try { try {
await timeline.requestHistory(historyCount: 100); await timeline
events = .requestHistory(historyCount: 100)
timeline.events.where((e) => e.type == EventTypes.Message).toList(); .timeout(const Duration(seconds: 5));
events = timeline.events
.where((e) => e.type == EventTypes.Message)
.toList();
events.removeOutdatedEvents(); events.removeOutdatedEvents();
} catch (e, s) { } catch (e, s) {
Logs().d('Unable to request history in stories', e, s); Logs().d('Unable to request history in stories', e, s);
@ -240,29 +245,43 @@ class StoryPageController extends State<StoryPage> {
events events
.where((event) => {MessageTypes.Image, MessageTypes.Video} .where((event) => {MessageTypes.Image, MessageTypes.Video}
.contains(event.messageType)) .contains(event.messageType))
.forEach((event) => downloadAndDecryptAttachment(event, .forEach((event) => downloadAndDecryptAttachment(
event.messageType == MessageTypes.Video && PlatformInfos.isMobile)); event,
event.messageType == MessageTypes.Video &&
PlatformInfos.isMobile));
if (!events.last.receipts // Reverse list
.any((receipt) => receipt.user.id == room.client.userID)) {
for (var j = 0; j < events.length; j++) {
if (events[j]
.receipts
.any((receipt) => receipt.user.id == room.client.userID)) {
index = j;
room.setReadMarker(
events[index].eventId,
mRead: events[index].eventId,
);
break;
}
}
}
this.events.clear(); this.events.clear();
this.events.addAll(events.reversed.toList()); 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);
}
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,
),
), ),
], ],
), ),