mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 06:46:22 +01:00
Refactor: extra bloc name builder fn
This commit is contained in:
parent
83449fa676
commit
c6850b7a2d
8 changed files with 46 additions and 42 deletions
7
lib/bloc/bloc_util.dart
Normal file
7
lib/bloc/bloc_util.dart
Normal file
|
@ -0,0 +1,7 @@
|
|||
import 'package:nc_photos/account.dart';
|
||||
|
||||
String getInstNameForAccount(String className, Account account) =>
|
||||
"$className(${account.scheme}://${account.username}@${account.address})";
|
||||
|
||||
String getInstNameForRootAwareAccount(String className, Account account) =>
|
||||
"$className(${account.scheme}://${account.username}@${account.address}?${account.roots.join('&')})";
|
|
@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
|
|||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/entity/album.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
|
@ -141,10 +142,10 @@ class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
|||
static bool require(DiContainer c) => true;
|
||||
|
||||
static ListAlbumBloc of(Account account) {
|
||||
final id = "${account.scheme}://${account.username}@${account.address}";
|
||||
final name = bloc_util.getInstNameForAccount("ListAlbumBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer().resolve<ListAlbumBloc>("ListAlbumBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<ListAlbumBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
|
@ -154,8 +155,7 @@ class ListAlbumBloc extends Bloc<ListAlbumBlocEvent, ListAlbumBlocState> {
|
|||
albumRepo: OrNull(AlbumRepo(AlbumAppDbDataSource(c.appDb))),
|
||||
);
|
||||
final bloc = ListAlbumBloc(c, offlineC);
|
||||
KiwiContainer()
|
||||
.registerInstance<ListAlbumBloc>(bloc, name: "ListAlbumBloc($id)");
|
||||
KiwiContainer().registerInstance<ListAlbumBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
|
|||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/entity/person.dart';
|
||||
import 'package:nc_photos/entity/person/data_source.dart';
|
||||
|
||||
|
@ -72,17 +73,15 @@ class ListPersonBloc extends Bloc<ListPersonBlocEvent, ListPersonBlocState> {
|
|||
ListPersonBloc() : super(ListPersonBlocInit());
|
||||
|
||||
static ListPersonBloc of(Account account) {
|
||||
final id =
|
||||
"${account.scheme}://${account.username}@${account.address}?${account.roots.join('&')}";
|
||||
final name = bloc_util.getInstNameForAccount("ListPersonBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer().resolve<ListPersonBloc>("ListPersonBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<ListPersonBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
final bloc = ListPersonBloc();
|
||||
KiwiContainer().registerInstance<ListPersonBloc>(bloc,
|
||||
name: "ListPersonBloc($id)");
|
||||
KiwiContainer().registerInstance<ListPersonBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
|
|||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/entity/sharee.dart';
|
||||
import 'package:nc_photos/entity/sharee/data_source.dart';
|
||||
|
||||
|
@ -72,16 +73,15 @@ class ListShareeBloc extends Bloc<ListShareeBlocEvent, ListShareeBlocState> {
|
|||
ListShareeBloc() : super(ListShareeBlocInit());
|
||||
|
||||
static ListShareeBloc of(Account account) {
|
||||
final id = "${account.scheme}://${account.username}@${account.address}";
|
||||
final name = bloc_util.getInstNameForAccount("ListShareeBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer().resolve<ListShareeBloc>("ListShareeBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<ListShareeBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
final bloc = ListShareeBloc();
|
||||
KiwiContainer()
|
||||
.registerInstance<ListShareeBloc>(bloc, name: "ListShareeBloc($id)");
|
||||
KiwiContainer().registerInstance<ListShareeBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
|
|||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/entity/album.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
|
@ -173,17 +174,16 @@ class ListSharingBloc extends Bloc<ListSharingBlocEvent, ListSharingBlocState> {
|
|||
DiContainer.has(c, DiType.shareRepo);
|
||||
|
||||
static ListSharingBloc of(Account account) {
|
||||
final id =
|
||||
"${account.scheme}://${account.username}@${account.address}?${account.roots.join('&')}";
|
||||
final name =
|
||||
bloc_util.getInstNameForRootAwareAccount("ListSharingBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer().resolve<ListSharingBloc>("ListSharingBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<ListSharingBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
final bloc = ListSharingBloc(KiwiContainer().resolve<DiContainer>());
|
||||
KiwiContainer().registerInstance<ListSharingBloc>(bloc,
|
||||
name: "ListSharingBloc($id)");
|
||||
KiwiContainer().registerInstance<ListSharingBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
|
|||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
import 'package:nc_photos/entity/file/data_source.dart';
|
||||
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
||||
|
@ -108,16 +109,15 @@ class LsTrashbinBloc extends Bloc<LsTrashbinBlocEvent, LsTrashbinBlocState> {
|
|||
}
|
||||
|
||||
static LsTrashbinBloc of(Account account) {
|
||||
final id = "${account.scheme}://${account.username}@${account.address}";
|
||||
final name = bloc_util.getInstNameForAccount("LsTrashbinBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer().resolve<LsTrashbinBloc>("LsTrashbinBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<LsTrashbinBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
final bloc = LsTrashbinBloc();
|
||||
KiwiContainer()
|
||||
.registerInstance<LsTrashbinBloc>(bloc, name: "LsTrashbinBloc($id)");
|
||||
KiwiContainer().registerInstance<LsTrashbinBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import 'package:kiwi/kiwi.dart';
|
|||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/app_db.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/debug_util.dart';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
|
@ -113,18 +114,16 @@ class ScanAccountDirBloc
|
|||
}
|
||||
|
||||
static ScanAccountDirBloc of(Account account) {
|
||||
final id =
|
||||
"${account.scheme}://${account.username}@${account.address}?${account.roots.join('&')}";
|
||||
final name =
|
||||
bloc_util.getInstNameForRootAwareAccount("ScanAccountDirBloc", account);
|
||||
try {
|
||||
_log.fine("[of] Resolving bloc for '$id'");
|
||||
return KiwiContainer()
|
||||
.resolve<ScanAccountDirBloc>("ScanAccountDirBloc($id)");
|
||||
_log.fine("[of] Resolving bloc for '$name'");
|
||||
return KiwiContainer().resolve<ScanAccountDirBloc>(name);
|
||||
} catch (_) {
|
||||
// no created instance for this account, make a new one
|
||||
_log.info("[of] New bloc instance for account: $account");
|
||||
final bloc = ScanAccountDirBloc._(account);
|
||||
KiwiContainer().registerInstance<ScanAccountDirBloc>(bloc,
|
||||
name: "ScanAccountDirBloc($id)");
|
||||
KiwiContainer().registerInstance<ScanAccountDirBloc>(bloc, name: name);
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import 'package:logging/logging.dart';
|
|||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/api/api_util.dart' as api_util;
|
||||
import 'package:nc_photos/app_localizations.dart';
|
||||
import 'package:nc_photos/bloc/bloc_util.dart' as bloc_util;
|
||||
import 'package:nc_photos/bloc/scan_account_dir.dart';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/download_handler.dart';
|
||||
|
@ -638,18 +639,16 @@ class _HomePhotosState extends State<HomePhotos>
|
|||
}
|
||||
|
||||
Primitive<bool> get _hasFiredMetadataTask {
|
||||
final blocId =
|
||||
"${widget.account.scheme}://${widget.account.username}@${widget.account.address}";
|
||||
final name = bloc_util.getInstNameForRootAwareAccount(
|
||||
"HomePhotosState.hasFiredMetadataTask", widget.account);
|
||||
try {
|
||||
_log.fine("[_hasFiredMetadataTask] Resolving bloc for '$blocId'");
|
||||
return KiwiContainer().resolve<Primitive<bool>>(
|
||||
"HomePhotosState.hasFiredMetadataTask($blocId)");
|
||||
_log.fine("[_hasFiredMetadataTask] Resolving for '$name'");
|
||||
return KiwiContainer().resolve<Primitive<bool>>(name);
|
||||
} catch (_) {
|
||||
_log.info(
|
||||
"[_hasFiredMetadataTask] New bloc instance for account: ${widget.account}");
|
||||
"[_hasFiredMetadataTask] New instance for account: ${widget.account}");
|
||||
final obj = Primitive(false);
|
||||
KiwiContainer().registerInstance<Primitive<bool>>(obj,
|
||||
name: "HomePhotosState.hasFiredMetadataTask($blocId)");
|
||||
KiwiContainer().registerInstance<Primitive<bool>>(obj, name: name);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue