Refactor ScanDirBloc construction

This commit is contained in:
Ming Ming 2021-04-11 23:38:01 +08:00
parent 81eabdf332
commit 0ff7e29d7a
2 changed files with 18 additions and 15 deletions

View file

@ -1,6 +1,7 @@
import 'dart:io';
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/entity/file.dart';
@ -105,6 +106,22 @@ class ScanDirBloc extends Bloc<ScanDirBlocEvent, ScanDirBlocState> {
_fileMetadataUpdatedEventListener.begin();
}
static ScanDirBloc of(Account account) {
final id =
"${account.scheme}://${account.username}@${account.address}?${account.roots.join('&')}";
try {
_log.fine("[of] Resolving bloc for '$id'");
return KiwiContainer().resolve<ScanDirBloc>("ScanDirBloc($id)");
} catch (_) {
// no created instance for this account, make a new one
_log.info("[of] New bloc instance for account: $account");
final bloc = ScanDirBloc();
KiwiContainer()
.registerInstance<ScanDirBloc>(bloc, name: "ScanDirBloc($id)");
return bloc;
}
}
@override
mapEventToState(ScanDirBlocEvent event) async* {
_log.info("[mapEventToState] $event");

View file

@ -61,21 +61,7 @@ class _HomePhotosState extends State<HomePhotos> {
}
void _initBloc() {
ScanDirBloc bloc;
final blocId =
"${widget.account.scheme}://${widget.account.username}@${widget.account.address}?${widget.account.roots.join('&')}";
try {
_log.fine("[_initBloc] Resolving bloc for '$blocId'");
bloc = KiwiContainer().resolve<ScanDirBloc>("ScanDirBloc($blocId)");
} catch (e) {
// no created instance for this account, make a new one
_log.info("[_initBloc] New bloc instance for account: ${widget.account}");
bloc = ScanDirBloc();
KiwiContainer()
.registerInstance<ScanDirBloc>(bloc, name: "ScanDirBloc($blocId)");
}
_bloc = bloc;
_bloc = ScanDirBloc.of(widget.account);
if (_bloc.state is ScanDirBlocInit) {
_log.info("[_initBloc] Initialize bloc");
_reqQuery();