From e3e9b84a9dd12e16a71a820eea52e7fd5c628ce8 Mon Sep 17 00:00:00 2001 From: ereio Date: Sun, 13 Dec 2020 23:28:16 -0500 Subject: [PATCH] bugfixes and touchups --- lib/global/storage/index.dart | 2 +- lib/store/events/actions.dart | 3 --- lib/store/events/storage.dart | 3 ++- lib/store/rooms/room/model.dart | 19 ++++++++++--------- lib/store/sync/actions.dart | 2 +- lib/views/home/index.dart | 8 +++++++- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/global/storage/index.dart b/lib/global/storage/index.dart index ba1106699..1ff7490d7 100644 --- a/lib/global/storage/index.dart +++ b/lib/global/storage/index.dart @@ -116,7 +116,7 @@ Future>> loadStorage(Database storage) async { encrypted: room.encryptionEnabled, ); printError( - '[loadMessages] ${messages[room.id].length.toString()} ${room.name} loaded', + '[loadMessages] ${messages[room.id]?.length} ${room.name} loaded', ); } diff --git a/lib/store/events/actions.dart b/lib/store/events/actions.dart index 20852b9c7..47dfb54cb 100644 --- a/lib/store/events/actions.dart +++ b/lib/store/events/actions.dart @@ -118,9 +118,6 @@ ThunkAction fetchMessageEvents({ "limit": limit, }); - printDebug("[fetchMessageEvents] CALLED FOR ${room.name} ${to} ${from}"); - printJson(messagesJson); - // The token the pagination ends at. If dir=b this token should be used again to request even earlier events. final String end = messagesJson['end']; diff --git a/lib/store/events/storage.dart b/lib/store/events/storage.dart index f0c60cef6..d7941c256 100644 --- a/lib/store/events/storage.dart +++ b/lib/store/events/storage.dart @@ -41,7 +41,8 @@ Future> loadMessages( final store = StoreRef(MESSAGES); // TODO: properly paginate through cold storage messages instead of loading all - final eventIdsPaginated = eventIds; //.skip(offset).take(limit).toList(); + final eventIdsPaginated = + eventIds ?? []; //.skip(offset).take(limit).toList(); final messagesPaginated = await store.records(eventIdsPaginated).get(storage); diff --git a/lib/store/rooms/room/model.dart b/lib/store/rooms/room/model.dart index dea0fba9c..c278fc7cf 100644 --- a/lib/store/rooms/room/model.dart +++ b/lib/store/rooms/room/model.dart @@ -369,7 +369,7 @@ class Room { int lastUpdate = this.lastUpdate; int namePriority = this.namePriority != 4 ? this.namePriority : 4; - var usersAdd = Map.from(this.usersNew); + var usersAdd = Map.from(this.usersNew ?? {}); var userIdsRemove = List(); Set userIds = Set.from(this.userIds ?? []); @@ -508,19 +508,20 @@ class Room { * outside displaying messages */ Room fromMessageEvents({ - List messages, + List messages = const [], String lastHash, String prevHash, // previously fetched hash String nextHash, }) { try { printDebug( - '[fromMessageEvents] ${this.name} ${messages.length.toString()}', + '[fromMessageEvents] ${this.name} ${messages.length}', ); bool limited; int lastUpdate = this.lastUpdate; List outbox = List.from(this.outbox ?? []); + final messageIds = this.messageIds ?? []; // Converting only message events final hasEncrypted = messages.firstWhere( @@ -536,11 +537,11 @@ class Room { // limited indicates need to fetch additional data for room timelines if (this.limited) { // Check to see if the new messages contain those existing in cache - if (messages.isNotEmpty && this.messageIds.isNotEmpty) { - final messageKnown = this.messageIds.firstWhere( - (id) => id == messages[0].id, - orElse: () => null, - ); + if (messages.isNotEmpty && messageIds.isNotEmpty) { + final messageKnown = messageIds.firstWhere( + (id) => id == messages[0].id, + orElse: () => null, + ); // Set limited to false if they now exist limited = messageKnown != null; @@ -572,7 +573,7 @@ class Room { // save messages and unique message id updates final messageIdsNew = Set.from(messagesMap.keys); final messagesNew = List.from(messagesMap.values); - final messageIdsAll = Set.from(this.messageIds) + final messageIdsAll = Set.from(this.messageIds ?? []) ..addAll(messageIdsNew); // Save values to room diff --git a/lib/store/sync/actions.dart b/lib/store/sync/actions.dart index 674938ecd..50805b577 100644 --- a/lib/store/sync/actions.dart +++ b/lib/store/sync/actions.dart @@ -160,10 +160,10 @@ ThunkAction stopSyncObserver() { ThunkAction initialSync() { return (Store store) async { // Start initial sync in background + await store.dispatch(SetSyncing(syncing: true)); await store.dispatch(fetchSync()); // Fetch All Room Ids - continue showing a sync - await store.dispatch(SetSyncing(syncing: true)); await store.dispatch(fetchDirectRooms()); await store.dispatch(fetchRooms()); await store.dispatch(SetSyncing(syncing: false)); diff --git a/lib/views/home/index.dart b/lib/views/home/index.dart index a55e03b21..2eb79ae67 100644 --- a/lib/views/home/index.dart +++ b/lib/views/home/index.dart @@ -650,6 +650,7 @@ class _Props extends Equatable { unauthed: store.state.syncStore.unauthed, offline: store.state.syncStore.offline, syncing: () { + final synced = store.state.syncStore.synced; final syncing = store.state.syncStore.syncing; final offline = store.state.syncStore.offline; final backgrounded = store.state.syncStore.backgrounded; @@ -658,11 +659,16 @@ class _Props extends Equatable { final lastAttempt = DateTime.fromMillisecondsSinceEpoch( store.state.syncStore.lastAttempt ?? 0); - // See if the last attempted sync is older than 60 seconds + // See if the last attempted sy nc is older than 60 seconds final isLastAttemptOld = DateTime.now() .difference(lastAttempt) .compareTo(Duration(seconds: 90)); + // syncing for the first time + if (syncing && !synced) { + return true; + } + // syncing for the first time since going offline if (syncing && offline) { return true;