Retain list person bloc state

This commit is contained in:
Ming Ming 2021-10-07 13:25:09 +08:00
parent 2fd3f7feff
commit 129f72e7fe
2 changed files with 28 additions and 3 deletions

View file

@ -1,4 +1,5 @@
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/person.dart';
@ -70,6 +71,22 @@ class ListPersonBlocFailure extends ListPersonBlocState {
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('&')}";
try {
_log.fine("[of] Resolving bloc for '$id'");
return KiwiContainer().resolve<ListPersonBloc>("ListPersonBloc($id)");
} 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)");
return bloc;
}
}
@override
mapEventToState(ListPersonBlocEvent event) async* {
_log.info("[mapEventToState] $event");

View file

@ -78,7 +78,16 @@ class _PeopleBrowserState extends State<PeopleBrowser> {
}
void _initBloc() {
_log.info("[_initBloc] Initialize bloc");
if (_bloc.state is ListPersonBlocInit) {
_log.info("[_initBloc] Initialize bloc");
} else {
// process the current state
WidgetsBinding.instance!.addPostFrameCallback((_) {
setState(() {
_onStateChange(context, _bloc.state);
});
});
}
_reqQuery();
}
@ -212,14 +221,13 @@ class _PeopleBrowserState extends State<PeopleBrowser> {
onTap: () => _onItemTap(e),
))
.toList();
// _items = [];
}
void _reqQuery() {
_bloc.add(ListPersonBlocQuery(widget.account));
}
final _bloc = ListPersonBloc();
late final _bloc = ListPersonBloc.of(widget.account);
var _items = <_ListItem>[];