From 08e2be367c48d17dbfceddbef0d66193d7574d1b Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Thu, 5 Sep 2024 11:57:54 +0800 Subject: [PATCH] feat: optimize error page in database row page and remove unused code (#6193) --- .../database/widgets/row/row_document.dart | 78 ++++++++----------- .../database_document_page.dart | 11 ++- 2 files changed, 39 insertions(+), 50 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart b/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart index 4d58d6fc32..415e741ad1 100644 --- a/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart +++ b/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart @@ -1,15 +1,14 @@ -import 'package:flutter/material.dart'; - import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/database/grid/application/row/row_document_bloc.dart'; import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_page.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; +import 'package:appflowy/shared/flowy_error_page.dart'; import 'package:appflowy/workspace/application/view_info/view_info_bloc.dart'; import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra_ui/widget/error_page.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class RowDocument extends StatelessWidget { @@ -27,17 +26,22 @@ class RowDocument extends StatelessWidget { return BlocProvider( create: (context) => RowDocumentBloc(viewId: viewId, rowId: rowId) ..add(const RowDocumentEvent.initial()), - child: BlocBuilder( + child: BlocConsumer( + listener: (_, state) => state.loadingState.maybeWhen( + error: (error) => Log.error('RowDocument error: $error'), + orElse: () => null, + ), builder: (context, state) { return state.loadingState.when( loading: () => const Center( child: CircularProgressIndicator.adaptive(), ), - error: (error) => FlowyErrorPage.message( - error.toString(), - howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(), + error: (error) => Center( + child: AppFlowyErrorPage( + error: error, + ), ), - finish: () => RowEditor( + finish: () => _RowEditor( viewPB: state.viewPB!, onIsEmptyChanged: (isEmpty) => context .read() @@ -50,9 +54,8 @@ class RowDocument extends StatelessWidget { } } -class RowEditor extends StatefulWidget { - const RowEditor({ - super.key, +class _RowEditor extends StatelessWidget { + const _RowEditor({ required this.viewPB, this.onIsEmptyChanged, }); @@ -60,36 +63,23 @@ class RowEditor extends StatefulWidget { final ViewPB viewPB; final void Function(bool)? onIsEmptyChanged; - @override - State createState() => _RowEditorState(); -} - -class _RowEditorState extends State { - late final DocumentBloc documentBloc; - - @override - void initState() { - super.initState(); - documentBloc = DocumentBloc(documentId: widget.viewPB.id) - ..add(const DocumentEvent.initial()); - } - - @override - void dispose() { - documentBloc.close(); - super.dispose(); - } - @override Widget build(BuildContext context) { - return MultiBlocProvider( - providers: [BlocProvider.value(value: documentBloc)], + return BlocProvider( + create: (context) => DocumentBloc(documentId: viewPB.id) + ..add(const DocumentEvent.initial()), child: BlocListener( listenWhen: (previous, current) => previous.isDocumentEmpty != current.isDocumentEmpty, - listener: (context, state) { + listener: (_, state) { if (state.isDocumentEmpty != null) { - widget.onIsEmptyChanged?.call(state.isDocumentEmpty!); + onIsEmptyChanged?.call(state.isDocumentEmpty!); + } + if (state.error != null) { + Log.error('RowEditor error: ${state.error}'); + } + if (state.editorState == null) { + Log.error('RowEditor unable to get editorState'); } }, child: BlocBuilder( @@ -101,18 +91,18 @@ class _RowEditorState extends State { final editorState = state.editorState; final error = state.error; if (error != null || editorState == null) { - Log.error(error); - return FlowyErrorPage.message( - error.toString(), - howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(), + return Center( + child: AppFlowyErrorPage( + error: error, + ), ); } - return IntrinsicHeight( - child: Container( - constraints: const BoxConstraints(minHeight: 300), - child: BlocProvider( - create: (context) => ViewInfoBloc(view: widget.viewPB), + return BlocProvider( + create: (context) => ViewInfoBloc(view: viewPB), + child: IntrinsicHeight( + child: Container( + constraints: const BoxConstraints(minHeight: 300), child: AppFlowyEditorPage( shrinkWrap: true, autoFocus: false, diff --git a/frontend/appflowy_flutter/lib/plugins/database_document/database_document_page.dart b/frontend/appflowy_flutter/lib/plugins/database_document/database_document_page.dart index c08b32d9ec..65b6542d4f 100644 --- a/frontend/appflowy_flutter/lib/plugins/database_document/database_document_page.dart +++ b/frontend/appflowy_flutter/lib/plugins/database_document/database_document_page.dart @@ -1,4 +1,3 @@ -import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/database/application/row/related_row_detail_bloc.dart'; import 'package:appflowy/plugins/database/grid/application/row/row_detail_bloc.dart'; import 'package:appflowy/plugins/database/grid/presentation/widgets/common/type_option_separator.dart'; @@ -9,14 +8,13 @@ import 'package:appflowy/plugins/document/presentation/banner.dart'; import 'package:appflowy/plugins/document/presentation/editor_notification.dart'; import 'package:appflowy/plugins/document/presentation/editor_page.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; +import 'package:appflowy/shared/flowy_error_page.dart'; import 'package:appflowy/startup/startup.dart'; import 'package:appflowy/workspace/application/action_navigation/action_navigation_bloc.dart'; import 'package:appflowy/workspace/application/action_navigation/navigation_action.dart'; import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart'; import 'package:appflowy_editor/appflowy_editor.dart' hide Log; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra_ui/widget/error_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -83,9 +81,10 @@ class _DatabaseDocumentPageState extends State { final error = state.error; if (error != null || editorState == null) { Log.error(error); - return FlowyErrorPage.message( - error.toString(), - howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(), + return Center( + child: AppFlowyErrorPage( + error: error, + ), ); }