Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1675: presence status updated too many times #54

Merged
merged 4 commits into from
Apr 9, 2024

Conversation

Te-Z
Copy link

@Te-Z Te-Z commented Apr 4, 2024

lib/src/client.dart Outdated Show resolved Hide resolved
lib/src/client.dart Outdated Show resolved Hide resolved
Copy link
Member

@hoangdat hoangdat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still not get the idea for this change, can you explain a little bit @Te-Z

@Te-Z
Copy link
Author

Te-Z commented Apr 8, 2024

I still not get the idea for this change, can you explain a little bit @Te-Z

Sure @hoangdat

This was the previous code:

for (final newPresence in sync.presence ?? []) {
final cachedPresence = CachedPresence.fromMatrixEvent(newPresence);
presences[newPresence.senderId] = cachedPresence;
// ignore: deprecated_member_use_from_same_package
onPresence.add(newPresence);
onPresenceChanged.add(cachedPresence);
}

The UI previously listened to onPresenceChanged which is a CachedStreamController . This instance had a new value added in the for each loop for every items in sync.presencelist. This means if there's different values in sync.presence , the UI will be updated 6 times.

To handle that I changed for this code:

CachedPresence? lowestLastActivePresence;
for (final newPresence in sync.presence ?? []) {
final cachedPresence = CachedPresence.fromMatrixEvent(newPresence);
presences[newPresence.senderId] = cachedPresence;
// ignore: deprecated_member_use_from_same_package
onPresence.add(newPresence);
onPresenceChanged.add(cachedPresence);
if (lowestLastActivePresence == null ||
(cachedPresence.lastActiveTimestamp != null &&
lowestLastActivePresence.lastActiveTimestamp != null &&
cachedPresence.lastActiveTimestamp!
.isBefore(lowestLastActivePresence.lastActiveTimestamp!))) {
lowestLastActivePresence = cachedPresence;
}
}
if (lowestLastActivePresence != null) {
onlatestPresenceChanged.add(lowestLastActivePresence);
}

Here lowestLastActivePresence is updated for each items in sync.presence list if it is null or if the current item's timestamp is before the one in lowestLastActivePresence. Then when the loop is over and we are sure to have the right value, we can update onLatestPresenceChange with the right value and this way update the UI.

@hoangdat hoangdat merged commit 699db76 into twake-supported-0.22.6 Apr 9, 2024
4 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants