2023-04-13 17:32:31 +02:00
|
|
|
part of '../new_collection_dialog.dart';
|
|
|
|
|
|
|
|
@npLog
|
|
|
|
class _Bloc extends Bloc<_Event, _State> {
|
|
|
|
_Bloc({
|
|
|
|
required this.account,
|
|
|
|
required Set<_ProviderOption> supportedProviders,
|
|
|
|
}) : super(_State.init(
|
2023-05-11 19:12:17 +02:00
|
|
|
account: account,
|
2023-04-13 17:32:31 +02:00
|
|
|
supportedProviders: supportedProviders,
|
|
|
|
)) {
|
|
|
|
on<_FormEvent>(_onFormEvent);
|
|
|
|
on<_HideDialog>(_onHideDialog);
|
2023-05-20 13:07:07 +02:00
|
|
|
|
|
|
|
on<_SetError>(_onSetError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onError(Object error, StackTrace stackTrace) {
|
|
|
|
// we need this to prevent onError being triggered recursively
|
|
|
|
if (!isClosed && !_isHandlingError) {
|
|
|
|
_isHandlingError = true;
|
|
|
|
try {
|
|
|
|
add(_SetError(error, stackTrace));
|
|
|
|
} catch (_) {}
|
|
|
|
_isHandlingError = false;
|
|
|
|
}
|
|
|
|
super.onError(error, stackTrace);
|
2023-04-13 17:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _onFormEvent(_FormEvent ev, Emitter<_State> emit) {
|
|
|
|
_log.info("$ev");
|
|
|
|
if (ev is _SubmitName) {
|
|
|
|
_onSubmitName(ev, emit);
|
|
|
|
} else if (ev is _SubmitProvider) {
|
|
|
|
_onSubmitProvider(ev, emit);
|
|
|
|
} else if (ev is _SubmitDirs) {
|
|
|
|
_onSubmitDirs(ev, emit);
|
|
|
|
} else if (ev is _SubmitTags) {
|
|
|
|
_onSubmitTags(ev, emit);
|
|
|
|
} else if (ev is _SubmitForm) {
|
|
|
|
_onSubmitForm(ev, emit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onHideDialog(_HideDialog ev, Emitter<_State> emit) {
|
|
|
|
_log.info("$ev");
|
|
|
|
emit(state.copyWith(showDialog: false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitName(_SubmitName ev, Emitter<_State> emit) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
formValue: state.formValue.copyWith(name: ev.value),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitProvider(_SubmitProvider ev, Emitter<_State> emit) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
formValue: state.formValue.copyWith(provider: ev.value),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitDirs(_SubmitDirs ev, Emitter<_State> emit) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
formValue: state.formValue.copyWith(dirs: ev.value),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitTags(_SubmitTags ev, Emitter<_State> emit) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
formValue: state.formValue.copyWith(tags: ev.value),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitForm(_SubmitForm ev, Emitter<_State> emit) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
result: Collection(
|
|
|
|
name: state.formValue.name,
|
|
|
|
contentProvider: _buildProvider(),
|
|
|
|
),
|
|
|
|
));
|
2023-05-11 19:12:17 +02:00
|
|
|
unawaited(AccountPref.of(account)
|
|
|
|
.setLastNewCollectionType(state.formValue.provider.index));
|
2023-04-13 17:32:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-20 13:07:07 +02:00
|
|
|
void _onSetError(_SetError ev, Emitter<_State> emit) {
|
|
|
|
_log.info(ev);
|
|
|
|
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:32:31 +02:00
|
|
|
CollectionContentProvider _buildProvider() {
|
|
|
|
switch (state.formValue.provider) {
|
|
|
|
case _ProviderOption.appAlbum:
|
|
|
|
return CollectionAlbumProvider(
|
|
|
|
account: account,
|
|
|
|
album: Album(
|
|
|
|
name: state.formValue.name,
|
|
|
|
provider: AlbumStaticProvider(items: const []),
|
2023-04-17 18:58:09 +02:00
|
|
|
coverProvider: const AlbumAutoCoverProvider(),
|
2023-04-13 17:32:31 +02:00
|
|
|
sortProvider: const AlbumTimeSortProvider(isAscending: false),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case _ProviderOption.dir:
|
|
|
|
return CollectionAlbumProvider(
|
|
|
|
account: account,
|
|
|
|
album: Album(
|
|
|
|
name: state.formValue.name,
|
|
|
|
provider: AlbumDirProvider(dirs: state.formValue.dirs),
|
2023-04-17 18:58:09 +02:00
|
|
|
coverProvider: const AlbumAutoCoverProvider(),
|
2023-04-13 17:32:31 +02:00
|
|
|
sortProvider: const AlbumTimeSortProvider(isAscending: false),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case _ProviderOption.tag:
|
|
|
|
return CollectionAlbumProvider(
|
|
|
|
account: account,
|
|
|
|
album: Album(
|
|
|
|
name: state.formValue.name,
|
|
|
|
provider: AlbumTagProvider(tags: state.formValue.tags),
|
2023-04-17 18:58:09 +02:00
|
|
|
coverProvider: const AlbumAutoCoverProvider(),
|
2023-04-13 17:32:31 +02:00
|
|
|
sortProvider: const AlbumTimeSortProvider(isAscending: false),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case _ProviderOption.ncAlbum:
|
|
|
|
return CollectionNcAlbumProvider(
|
|
|
|
account: account,
|
|
|
|
album: NcAlbum.createNew(
|
|
|
|
account: account,
|
|
|
|
name: state.formValue.name,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
2023-05-20 13:07:07 +02:00
|
|
|
|
|
|
|
var _isHandlingError = false;
|
2023-04-13 17:32:31 +02:00
|
|
|
}
|