Browse Source

fix: triger rowbloc event after it was closed

appflowy 3 years ago
parent
commit
792f8b95aa

+ 9 - 6
frontend/app_flowy/lib/workspace/application/grid/grid_service.dart

@@ -162,7 +162,7 @@ class GridRowCache {
     }
 
     final List<RowData> newRows = [];
-    final List<Tuple2<int, RowData>> deletedIndex = [];
+    final DeletedIndex deletedIndex = [];
     final Map<String, RowOrder> deletedRowMap = {for (var rowOrder in deletedRows) rowOrder.rowId: rowOrder};
     _rows.asMap().forEach((index, value) {
       if (deletedRowMap[value.rowId] == null) {
@@ -181,13 +181,13 @@ class GridRowCache {
       return none();
     }
 
-    List<int> insertIndexs = [];
+    InsertedIndexs insertIndexs = [];
     for (final newRow in createdRows) {
       if (newRow.hasIndex()) {
-        insertIndexs.add(newRow.index);
+        insertIndexs.add(Tuple2(newRow.index, newRow.rowOrder.rowId));
         _rows.insert(newRow.index, _toRowData(newRow.rowOrder));
       } else {
-        insertIndexs.add(_rows.length);
+        insertIndexs.add(Tuple2(newRow.index, newRow.rowOrder.rowId));
         _rows.add(_toRowData(newRow.rowOrder));
       }
     }
@@ -216,9 +216,12 @@ class GridRowCache {
   }
 }
 
+typedef InsertedIndexs = List<Tuple2<int, String>>;
+typedef DeletedIndex = List<Tuple2<int, RowData>>;
+
 @freezed
 class GridListState with _$GridListState {
-  const factory GridListState.insert(List<int> indexs) = _Insert;
-  const factory GridListState.delete(List<Tuple2<int, RowData>> indexs) = _Delete;
+  const factory GridListState.insert(InsertedIndexs items) = _Insert;
+  const factory GridListState.delete(DeletedIndex items) = _Delete;
   const factory GridListState.initial() = InitialListState;
 }

+ 18 - 6
frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart

@@ -35,20 +35,23 @@ class RowBloc extends Bloc<RowEvent, RowState> {
             _rowService.createRow();
           },
           didUpdateRow: (_DidUpdateRow value) async {
-            _handleRowUpdate(value, emit);
+            _handleRowUpdate(value.row, emit);
           },
           fieldsDidUpdate: (_FieldsDidUpdate value) async {
             await _handleFieldUpdate(emit);
           },
+          didLoadRow: (_DidLoadRow value) {
+            _handleRowUpdate(value.row, emit);
+          },
         );
       },
     );
   }
 
-  void _handleRowUpdate(_DidUpdateRow value, Emitter<RowState> emit) {
-    final CellDataMap cellDataMap = _makeCellDatas(value.row, state.rowData.fields);
+  void _handleRowUpdate(Row row, Emitter<RowState> emit) {
+    final CellDataMap cellDataMap = _makeCellDatas(row, state.rowData.fields);
     emit(state.copyWith(
-      row: Future(() => Some(value.row)),
+      row: Future(() => Some(row)),
       cellDataMap: Some(cellDataMap),
     ));
   }
@@ -75,7 +78,11 @@ class RowBloc extends Bloc<RowEvent, RowState> {
   Future<void> _startListening() async {
     _rowlistener.updateRowNotifier?.addPublishListener((result) {
       result.fold(
-        (row) => add(RowEvent.didUpdateRow(row)),
+        (row) {
+          if (!isClosed) {
+            add(RowEvent.didUpdateRow(row));
+          }
+        },
         (err) => Log.error(err),
       );
     });
@@ -92,7 +99,11 @@ class RowBloc extends Bloc<RowEvent, RowState> {
   Future<void> _loadRow(Emitter<RowState> emit) async {
     _rowService.getRow().then((result) {
       return result.fold(
-        (row) => add(RowEvent.didUpdateRow(row)),
+        (row) {
+          if (!isClosed) {
+            add(RowEvent.didLoadRow(row));
+          }
+        },
         (err) => Log.error(err),
       );
     });
@@ -119,6 +130,7 @@ class RowEvent with _$RowEvent {
   const factory RowEvent.initial() = _InitialRow;
   const factory RowEvent.createRow() = _CreateRow;
   const factory RowEvent.fieldsDidUpdate() = _FieldsDidUpdate;
+  const factory RowEvent.didLoadRow(Row row) = _DidLoadRow;
   const factory RowEvent.didUpdateRow(Row row) = _DidUpdateRow;
 }
 

+ 6 - 6
frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/grid_page.dart

@@ -6,7 +6,6 @@ import 'package:flowy_infra_ui/style_widget/scrolling/styled_scroll_bar.dart';
 import 'package:flowy_infra_ui/style_widget/scrolling/styled_scrollview.dart';
 import 'package:flowy_infra_ui/widget/error_page.dart';
 import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
-import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart' show Field;
 import 'package:flutter_bloc/flutter_bloc.dart';
 import 'package:flutter/material.dart';
 import 'package:linked_scroll_controller/linked_scroll_controller.dart';
@@ -193,15 +192,15 @@ class _GridRowsState extends State<_GridRows> {
       listener: (context, state) {
         state.listState.map(
           insert: (value) {
-            for (final index in value.indexs) {
-              _key.currentState?.insertItem(index);
+            for (final item in value.items) {
+              _key.currentState?.insertItem(item.value1);
             }
           },
           delete: (value) {
-            for (final index in value.indexs) {
+            for (final item in value.items) {
               _key.currentState?.removeItem(
-                index.value1,
-                (context, animation) => _renderRow(context, index.value2, animation),
+                item.value1,
+                (context, animation) => _renderRow(context, item.value2, animation),
               );
             }
           },
@@ -224,6 +223,7 @@ class _GridRowsState extends State<_GridRows> {
 
   Widget _renderRow(BuildContext context, RowData rowData, Animation<double> animation) {
     final fieldCache = context.read<GridBloc>().fieldCache;
+
     return SizeTransition(
       sizeFactor: animation,
       child: GridRowWidget(

+ 1 - 1
frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_container.dart

@@ -33,7 +33,7 @@ class CellContainer extends StatelessWidget {
       child: Consumer<CellStateNotifier>(
         builder: (context, state, _) {
           return Container(
-            constraints: BoxConstraints(maxWidth: width, maxHeight: 42),
+            constraints: BoxConstraints(maxWidth: width),
             decoration: _makeBoxDecoration(context, state),
             padding: GridSize.cellContentInsets,
             child: Center(child: child),

+ 2 - 2
frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/row/grid_row.dart

@@ -44,8 +44,8 @@ class _GridRowWidgetState extends State<GridRowWidget> {
           child: BlocBuilder<RowBloc, RowState>(
             buildWhen: (p, c) => p.rowData.height != c.rowData.height,
             builder: (context, state) {
-              return LimitedBox(
-                maxHeight: 200,
+              return SizedBox(
+                height: 42,
                 child: Row(
                   mainAxisSize: MainAxisSize.max,
                   crossAxisAlignment: CrossAxisAlignment.center,