2023-05-25 03:52:43 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-11-17 17:04:55 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2023-05-25 03:52:43 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/album/suggested_shared_users.provider.dart';
|
|
|
|
import 'package:immich_mobile/services/partner.service.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/user.entity.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
2023-05-25 03:52:43 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
|
|
|
|
class PartnerSharedWithNotifier extends StateNotifier<List<User>> {
|
2023-11-13 15:54:41 +00:00
|
|
|
PartnerSharedWithNotifier(Isar db, this._ps) : super([]) {
|
2024-11-17 17:04:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-05-25 03:52:43 +00:00
|
|
|
}
|
2023-11-13 15:54:41 +00:00
|
|
|
|
|
|
|
Future<bool> updatePartner(User partner, {required bool inTimeline}) {
|
|
|
|
return _ps.updatePartner(partner, inTimeline: inTimeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
final PartnerService _ps;
|
2023-05-25 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final partnerSharedWithProvider =
|
|
|
|
StateNotifierProvider<PartnerSharedWithNotifier, List<User>>((ref) {
|
2023-11-13 15:54:41 +00:00
|
|
|
return PartnerSharedWithNotifier(
|
|
|
|
ref.watch(dbProvider),
|
|
|
|
ref.watch(partnerServiceProvider),
|
|
|
|
);
|
2023-05-25 03:52:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
class PartnerSharedByNotifier extends StateNotifier<List<User>> {
|
|
|
|
PartnerSharedByNotifier(Isar db) : super([]) {
|
2024-11-17 17:04:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-05-25 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
late final StreamSubscription<List<User>> streamSub;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
streamSub.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final partnerSharedByProvider =
|
|
|
|
StateNotifierProvider<PartnerSharedByNotifier, List<User>>((ref) {
|
|
|
|
return PartnerSharedByNotifier(ref.watch(dbProvider));
|
|
|
|
});
|
|
|
|
|
|
|
|
final partnerAvailableProvider =
|
|
|
|
FutureProvider.autoDispose<List<User>>((ref) async {
|
|
|
|
final otherUsers = await ref.watch(otherUsersProvider.future);
|
|
|
|
final currentPartners = ref.watch(partnerSharedByProvider);
|
|
|
|
final available = Set<User>.of(otherUsers);
|
|
|
|
available.removeAll(currentPartners);
|
|
|
|
return available.toList();
|
|
|
|
});
|