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

Fix: network view flicker #632

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions qaul_ui/packages/qaul_rpc/lib/src/models/user.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:fast_base58/fast_base58.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand Down Expand Up @@ -92,6 +93,42 @@ class UserListNotifier extends StateNotifier<List<User>> {
state = [...state, u];
}

/// [updateMany] safely assigns [users] to this notifier's state.
///
/// If [users] and [state] are deeply equal, will do nothing. As a result, it
/// avoids re-rendering UI code that depends on the [List<User>] that this
/// notifier exposes.
///
/// New users get appended to the list, whilst existing ones get their data
/// updated.
void updateMany(List<User> users) {
if (const ListEquality().equals(state, users)) {
return;
}

final usrs = [...state];
for (final u in users) {
final idx = usrs.indexOf(u);
if (idx == -1) {
usrs.add(u);
continue;
}
final current = usrs.elementAt(idx);
usrs[idx] = User(
name: current.name == 'Name Undefined' ? u.name : current.name,
id: u.id,
conversationId: u.conversationId ?? current.conversationId,
status:
u.status == ConnectionStatus.offline ? current.status : u.status,
keyBase58: u.keyBase58 ?? current.keyBase58,
isBlocked: u.isBlocked ?? current.isBlocked,
isVerified: u.isVerified ?? current.isVerified,
availableTypes: u.availableTypes ?? current.availableTypes,
);
}
state = usrs;
}

void update(User u) {
state = [
for (final usr in state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ class UsersTranslator extends RpcModuleTranslator {
if (res.module != type) return;
if (res.data is List<User>) {
final provider = reader(usersProvider.notifier);
for (final user in res.data) {
provider.contains(user) ? provider.update(user) : provider.add(user);
}
provider.updateMany(res.data);
}
if (res.data is SecurityNumber) {
reader(currentSecurityNoProvider.notifier).state = res.data;
Expand Down
Loading