Skip to content

Commit

Permalink
fix(mobile): unnecessary rebuilds from partner share notifier (#14170)
Browse files Browse the repository at this point in the history
* fix unnecessary notifications

* move equality function

* sort by id

* use same comparison for initial and later queries
  • Loading branch information
mertalev authored Nov 17, 2024
1 parent 4b5657c commit 944ea7d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions mobile/lib/providers/partner.provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/providers/album/suggested_shared_users.provider.dart';
import 'package:immich_mobile/services/partner.service.dart';
Expand All @@ -9,9 +10,19 @@ import 'package:isar/isar.dart';

class PartnerSharedWithNotifier extends StateNotifier<List<User>> {
PartnerSharedWithNotifier(Isar db, this._ps) : super([]) {
final query = db.users.filter().isPartnerSharedWithEqualTo(true);
query.findAll().then((partners) => state = partners);
query.watch().listen((partners) => state = partners);
Function eq = const ListEquality<User>().equals;
final query = db.users.filter().isPartnerSharedWithEqualTo(true).sortById();
query.findAll().then((partners) {
if (!eq(state, partners)) {
state = partners;
}
}).then((_) {
query.watch().listen((partners) {
if (!eq(state, partners)) {
state = partners;
}
});
});
}

Future<bool> updatePartner(User partner, {required bool inTimeline}) {
Expand All @@ -31,9 +42,19 @@ final partnerSharedWithProvider =

class PartnerSharedByNotifier extends StateNotifier<List<User>> {
PartnerSharedByNotifier(Isar db) : super([]) {
final query = db.users.filter().isPartnerSharedByEqualTo(true);
query.findAll().then((partners) => state = partners);
streamSub = query.watch().listen((partners) => state = partners);
Function eq = const ListEquality<User>().equals;
final query = db.users.filter().isPartnerSharedByEqualTo(true).sortById();
query.findAll().then((partners) {
if (!eq(state, partners)) {
state = partners;
}
}).then((_) {
streamSub = query.watch().listen((partners) {
if (!eq(state, partners)) {
state = partners;
}
});
});
}

late final StreamSubscription<List<User>> streamSub;
Expand Down

0 comments on commit 944ea7d

Please sign in to comment.