chore: refacotor share bloc

This commit is contained in:
Lucas.Xu 2024-06-21 15:32:44 +08:00
parent 0709a2d2ee
commit a2c52508fa
3 changed files with 28 additions and 18 deletions

View File

@ -14,7 +14,7 @@ part 'document_share_bloc.freezed.dart';
class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> { class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
DocumentShareBloc({ DocumentShareBloc({
required this.view, required this.view,
}) : super(const DocumentShareState.initial()) { }) : super(DocumentShareState.initial()) {
on<DocumentShareEvent>((event, emit) async { on<DocumentShareEvent>((event, emit) async {
await event.when( await event.when(
share: (type, path) async { share: (type, path) async {
@ -23,7 +23,9 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
return; return;
} }
emit(const DocumentShareState.loading()); emit(
state.copyWith(isLoading: true),
);
final exporter = DocumentExporter(view); final exporter = DocumentExporter(view);
final FlowyResult<ExportDataPB, FlowyError> result = final FlowyResult<ExportDataPB, FlowyError> result =
@ -46,7 +48,12 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
); );
}); });
emit(DocumentShareState.finish(result)); emit(
state.copyWith(
isLoading: false,
exportResult: result,
),
);
}, },
); );
}); });
@ -93,15 +100,20 @@ enum DocumentShareType {
@freezed @freezed
class DocumentShareEvent with _$DocumentShareEvent { class DocumentShareEvent with _$DocumentShareEvent {
const factory DocumentShareEvent.share(DocumentShareType type, String? path) = const factory DocumentShareEvent.share(
Share; DocumentShareType type,
String? path,
) = Share;
} }
@freezed @freezed
class DocumentShareState with _$DocumentShareState { class DocumentShareState with _$DocumentShareState {
const factory DocumentShareState.initial() = _Initial; const factory DocumentShareState({
const factory DocumentShareState.loading() = _Loading; required bool isLoading,
const factory DocumentShareState.finish( FlowyResult<ExportDataPB, FlowyError>? exportResult,
FlowyResult<ExportDataPB, FlowyError> successOrFail, }) = _DocumentShareState;
) = _Finish;
factory DocumentShareState.initial() => const DocumentShareState(
isLoading: false,
);
} }

View File

@ -33,14 +33,12 @@ class DocumentShareButton extends StatelessWidget {
create: (context) => getIt<DocumentShareBloc>(param1: view), create: (context) => getIt<DocumentShareBloc>(param1: view),
child: BlocListener<DocumentShareBloc, DocumentShareState>( child: BlocListener<DocumentShareBloc, DocumentShareState>(
listener: (context, state) { listener: (context, state) {
state.mapOrNull( if (state.isLoading == false && state.exportResult != null) {
finish: (state) { state.exportResult!.fold(
state.successOrFail.fold( (data) => _handleExportData(context, data),
(data) => _handleExportData(context, data), _handleExportError,
_handleExportError, );
); }
},
);
}, },
child: BlocBuilder<DocumentShareBloc, DocumentShareState>( child: BlocBuilder<DocumentShareBloc, DocumentShareState>(
builder: (context, state) => SizedBox( builder: (context, state) => SizedBox(