From d9a6d04ae8f7a1c605b6ecd64faf4a2d01413cf1 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 20 Nov 2021 04:36:03 +0800 Subject: [PATCH] Remove obsolete ListPendingSharedAlbum classes --- lib/bloc/list_pending_shared_album.dart | 182 -------------------- lib/use_case/list_pending_shared_album.dart | 59 ------- 2 files changed, 241 deletions(-) delete mode 100644 lib/bloc/list_pending_shared_album.dart delete mode 100644 lib/use_case/list_pending_shared_album.dart diff --git a/lib/bloc/list_pending_shared_album.dart b/lib/bloc/list_pending_shared_album.dart deleted file mode 100644 index 64f45b89..00000000 --- a/lib/bloc/list_pending_shared_album.dart +++ /dev/null @@ -1,182 +0,0 @@ -import 'package:bloc/bloc.dart'; -import 'package:logging/logging.dart'; -import 'package:nc_photos/account.dart'; -import 'package:nc_photos/app_db.dart'; -import 'package:nc_photos/entity/album.dart'; -import 'package:nc_photos/entity/file.dart'; -import 'package:nc_photos/entity/file/data_source.dart'; -import 'package:nc_photos/event/event.dart'; -import 'package:nc_photos/exception_event.dart'; -import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util; -import 'package:nc_photos/use_case/list_pending_shared_album.dart'; - -class ListPendingSharedAlbumBlocItem { - ListPendingSharedAlbumBlocItem(this.album); - - final Album album; -} - -abstract class ListPendingSharedAlbumBlocEvent { - const ListPendingSharedAlbumBlocEvent(); -} - -class ListPendingSharedAlbumBlocQuery extends ListPendingSharedAlbumBlocEvent { - const ListPendingSharedAlbumBlocQuery( - this.account, - ); - - @override - toString() { - return "$runtimeType {" - "account: $account, " - "}"; - } - - final Account account; -} - -/// An external event has happened and may affect the state of this bloc -class _ListPendingSharedAlbumBlocExternalEvent - extends ListPendingSharedAlbumBlocEvent { - const _ListPendingSharedAlbumBlocExternalEvent(); - - @override - toString() { - return "$runtimeType {" - "}"; - } -} - -abstract class ListPendingSharedAlbumBlocState { - const ListPendingSharedAlbumBlocState(this.items); - - @override - toString() { - return "$runtimeType {" - "items: List {length: ${items.length}}, " - "}"; - } - - final List items; -} - -class ListPendingSharedAlbumBlocInit extends ListPendingSharedAlbumBlocState { - ListPendingSharedAlbumBlocInit() : super(const []); -} - -class ListPendingSharedAlbumBlocLoading - extends ListPendingSharedAlbumBlocState { - const ListPendingSharedAlbumBlocLoading( - List items) - : super(items); -} - -class ListPendingSharedAlbumBlocSuccess - extends ListPendingSharedAlbumBlocState { - const ListPendingSharedAlbumBlocSuccess( - List items) - : super(items); -} - -class ListPendingSharedAlbumBlocFailure - extends ListPendingSharedAlbumBlocState { - const ListPendingSharedAlbumBlocFailure( - List items, this.exception) - : super(items); - - @override - toString() { - return "$runtimeType {" - "super: ${super.toString()}, " - "exception: $exception, " - "}"; - } - - final dynamic exception; -} - -/// The state of this bloc is inconsistent. This typically means that the data -/// may have been changed externally -class ListPendingSharedAlbumBlocInconsistent - extends ListPendingSharedAlbumBlocState { - const ListPendingSharedAlbumBlocInconsistent( - List items) - : super(items); -} - -/// Return a list of importable shared albums in the pending dir -class ListPendingSharedAlbumBloc extends Bloc { - ListPendingSharedAlbumBloc() : super(ListPendingSharedAlbumBlocInit()) { - _fileMovedEventListener.begin(); - } - - @override - mapEventToState(ListPendingSharedAlbumBlocEvent event) async* { - _log.info("[mapEventToState] $event"); - if (event is ListPendingSharedAlbumBlocQuery) { - yield* _onEventQuery(event); - } else if (event is _ListPendingSharedAlbumBlocExternalEvent) { - yield* _onExternalEvent(event); - } - } - - @override - close() { - _fileMovedEventListener.end(); - return super.close(); - } - - Stream _onEventQuery( - ListPendingSharedAlbumBlocQuery ev) async* { - yield const ListPendingSharedAlbumBlocLoading([]); - try { - final fileRepo = FileRepo(FileCachedDataSource(AppDb())); - final albumRepo = AlbumRepo(AlbumCachedDataSource(AppDb())); - final albums = []; - final errors = []; - await for (final result - in ListPendingSharedAlbum(fileRepo, albumRepo)(ev.account)) { - if (result is ExceptionEvent) { - _log.severe("[_onEventQuery] Exception while ListPendingSharedAlbum", - result.error, result.stackTrace); - errors.add(result.error); - } else if (result is Album) { - albums.add(result); - } - } - final items = - albums.map((e) => ListPendingSharedAlbumBlocItem(e)).toList(); - if (errors.isEmpty) { - yield ListPendingSharedAlbumBlocSuccess(items); - } else { - yield ListPendingSharedAlbumBlocFailure(items, errors.first); - } - } catch (e) { - _log.severe("[_onEventQuery] Exception", e); - yield ListPendingSharedAlbumBlocFailure(state.items, e); - } - } - - Stream _onExternalEvent( - _ListPendingSharedAlbumBlocExternalEvent ev) async* { - yield ListPendingSharedAlbumBlocInconsistent(state.items); - } - - void _onFileMovedEvent(FileMovedEvent ev) { - if (state is ListPendingSharedAlbumBlocInit) { - // no data in this bloc, ignore - return; - } - if (ev.file.path.startsWith( - remote_storage_util.getRemotePendingSharedAlbumsDir(ev.account))) { - add(const _ListPendingSharedAlbumBlocExternalEvent()); - } - } - - late final _fileMovedEventListener = - AppEventListener(_onFileMovedEvent); - - static final _log = - Logger("bloc.list_pending_shared_album.ListPendingSharedAlbumBloc"); -} diff --git a/lib/use_case/list_pending_shared_album.dart b/lib/use_case/list_pending_shared_album.dart deleted file mode 100644 index c8a3d2a8..00000000 --- a/lib/use_case/list_pending_shared_album.dart +++ /dev/null @@ -1,59 +0,0 @@ -import 'package:logging/logging.dart'; -import 'package:nc_photos/account.dart'; -import 'package:nc_photos/entity/album.dart'; -import 'package:nc_photos/entity/file.dart'; -import 'package:nc_photos/exception.dart'; -import 'package:nc_photos/exception_event.dart'; -import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util; -import 'package:nc_photos/use_case/ls.dart'; - -class ListPendingSharedAlbum { - ListPendingSharedAlbum(this.fileRepo, this.albumRepo); - - /// Return shared albums that are known to us (in pending dir) but not added - /// to the user library - /// - /// The returned stream would emit either Album data or a tuple of exception - /// and stacktrace - Stream call(Account account) async* { - List ls; - try { - ls = await Ls(fileRepo)( - account, - File( - path: remote_storage_util.getRemotePendingSharedAlbumsDir(account), - )); - } catch (e, stackTrace) { - if (e is ApiException && e.response.statusCode == 404) { - // no albums - return; - } - yield ExceptionEvent(e, stackTrace); - return; - } - final albumFiles = - ls.where((element) => element.isCollection != true).toList(); - for (final f in albumFiles) { - try { - yield await albumRepo.get(account, f); - } catch (e, stackTrace) { - yield ExceptionEvent(e, stackTrace); - } - } - try { - albumRepo.cleanUp( - account, - remote_storage_util.getRemotePendingSharedAlbumsDir(account), - albumFiles); - } catch (e, stacktrace) { - // not important, log and ignore - _log.shout("[_call] Failed while cleanUp", e, stacktrace); - } - } - - final FileRepo fileRepo; - final AlbumRepo albumRepo; - - static final _log = - Logger("user_case.list_pending_shared_album.ListPendingSharedAlbum"); -}