mirror of
https://github.com/immich-app/immich.git
synced 2025-01-08 21:06:48 +01:00
8708867c1c
* feature(mobile): sync assets, albums & users to local database on device * try to fix tests * move DB sync operations to new SyncService * clear db on user logout * fix reason for endless loading timeline * fix error when deleting album * fix thumbnail of device albums * add a few comments * fix Hive box not open in album service when loading local assets * adjust tests to int IDs * fix bug: show all albums when Recent is selected * update generated api * reworked Recents album isAll handling * guard against wrongly interleaved sync operations * fix: timeline asset ordering (sort asset state by created at) * fix: sort assets in albums by created at
87 lines
2.5 KiB
Dart
87 lines
2.5 KiB
Dart
import 'package:collection/collection.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
|
import 'package:isar/isar.dart';
|
|
|
|
class SharedAlbumNotifier extends StateNotifier<List<Album>> {
|
|
SharedAlbumNotifier(this._albumService, this._db) : super([]);
|
|
|
|
final AlbumService _albumService;
|
|
final Isar _db;
|
|
|
|
Future<Album?> createSharedAlbum(
|
|
String albumName,
|
|
Iterable<Asset> assets,
|
|
Iterable<User> sharedUsers,
|
|
) async {
|
|
try {
|
|
final Album? newAlbum = await _albumService.createAlbum(
|
|
albumName,
|
|
assets,
|
|
sharedUsers,
|
|
);
|
|
|
|
if (newAlbum != null) {
|
|
state = [...state, newAlbum];
|
|
return newAlbum;
|
|
}
|
|
} catch (e) {
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> getAllSharedAlbums() async {
|
|
var albums = await _db.albums.filter().sharedEqualTo(true).findAll();
|
|
if (!const ListEquality().equals(albums, state)) {
|
|
state = albums;
|
|
}
|
|
await _albumService.refreshRemoteAlbums(isShared: true);
|
|
albums = await _db.albums.filter().sharedEqualTo(true).findAll();
|
|
if (!const ListEquality().equals(albums, state)) {
|
|
state = albums;
|
|
}
|
|
}
|
|
|
|
Future<bool> deleteAlbum(Album album) {
|
|
state = state.where((a) => a.id != album.id).toList();
|
|
return _albumService.deleteAlbum(album);
|
|
}
|
|
|
|
Future<bool> leaveAlbum(Album album) async {
|
|
var res = await _albumService.leaveAlbum(album);
|
|
|
|
if (res) {
|
|
await deleteAlbum(album);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> removeAssetFromAlbum(Album album, Iterable<Asset> assets) {
|
|
return _albumService.removeAssetFromAlbum(album, assets);
|
|
}
|
|
}
|
|
|
|
final sharedAlbumProvider =
|
|
StateNotifierProvider<SharedAlbumNotifier, List<Album>>((ref) {
|
|
return SharedAlbumNotifier(
|
|
ref.watch(albumServiceProvider),
|
|
ref.watch(dbProvider),
|
|
);
|
|
});
|
|
|
|
final sharedAlbumDetailProvider =
|
|
FutureProvider.autoDispose.family<Album?, int>((ref, albumId) async {
|
|
final AlbumService sharedAlbumService = ref.watch(albumServiceProvider);
|
|
|
|
final Album? a = await sharedAlbumService.getAlbumDetail(albumId);
|
|
await a?.loadSortedAssets();
|
|
return a;
|
|
});
|