chore: fix row rebuild refresh issue

This commit is contained in:
appflowy 2022-04-14 21:16:29 +08:00
parent b606c5ba7b
commit ac766c0359
12 changed files with 59 additions and 40 deletions

View File

@ -113,7 +113,7 @@ class ApplicationBlocObserver extends BlocObserver {
// ignore: unnecessary_overrides // ignore: unnecessary_overrides
void onTransition(Bloc bloc, Transition transition) { void onTransition(Bloc bloc, Transition transition) {
// Log.debug("[current]: ${transition.currentState} \n\n[next]: ${transition.nextState}"); // Log.debug("[current]: ${transition.currentState} \n\n[next]: ${transition.nextState}");
Log.debug("${transition.nextState}"); // Log.debug("${transition.nextState}");
super.onTransition(bloc, transition); super.onTransition(bloc, transition);
} }
@ -123,9 +123,9 @@ class ApplicationBlocObserver extends BlocObserver {
super.onError(bloc, error, stackTrace); super.onError(bloc, error, stackTrace);
} }
@override // @override
void onEvent(Bloc bloc, Object? event) { // void onEvent(Bloc bloc, Object? event) {
// Log.debug("$event"); // Log.debug("$event");
super.onEvent(bloc, event); // super.onEvent(bloc, event);
} // }
} }

View File

@ -24,7 +24,7 @@ class GridFieldsListener {
void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) { void _handler(GridNotification ty, Either<Uint8List, FlowyError> result) {
switch (ty) { switch (ty) {
case GridNotification.DidUpdateGrid: case GridNotification.DidUpdateGridField:
result.fold( result.fold(
(payload) => updateFieldsNotifier.value = left(GridFieldChangeset.fromBuffer(payload)), (payload) => updateFieldsNotifier.value = left(GridFieldChangeset.fromBuffer(payload)),
(error) => updateFieldsNotifier.value = right(error), (error) => updateFieldsNotifier.value = right(error),

View File

@ -2,20 +2,18 @@ import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:async'; import 'dart:async';
import 'field/field_service.dart';
import 'grid_service.dart'; import 'grid_service.dart';
part 'grid_header_bloc.freezed.dart'; part 'grid_header_bloc.freezed.dart';
class GridHeaderBloc extends Bloc<GridHeaderEvent, GridHeaderState> { class GridHeaderBloc extends Bloc<GridHeaderEvent, GridHeaderState> {
final FieldService _fieldService; // final FieldService _fieldService;
final GridFieldCache fieldCache; final GridFieldCache fieldCache;
GridHeaderBloc({ GridHeaderBloc({
required String gridId, required String gridId,
required this.fieldCache, required this.fieldCache,
}) : _fieldService = FieldService(gridId: gridId), }) : super(GridHeaderState.initial(fieldCache.clonedFields)) {
super(GridHeaderState.initial(fieldCache.clonedFields)) {
on<GridHeaderEvent>( on<GridHeaderEvent>(
(event, emit) async { (event, emit) async {
await event.map( await event.map(
@ -31,7 +29,7 @@ class GridHeaderBloc extends Bloc<GridHeaderEvent, GridHeaderState> {
} }
Future<void> _startListening() async { Future<void> _startListening() async {
fieldCache.listenOnFieldChanged((fields) { fieldCache.addListener(() {}, onChanged: (fields) {
if (!isClosed) { if (!isClosed) {
add(GridHeaderEvent.didReceiveFieldUpdate(fields)); add(GridHeaderEvent.didReceiveFieldUpdate(fields));
} }

View File

@ -74,6 +74,15 @@ class GridFieldCache {
_fieldNotifier.addListener(() => onFieldChanged(clonedFields)); _fieldNotifier.addListener(() => onFieldChanged(clonedFields));
} }
void addListener(VoidCallback listener, {void Function(List<Field>)? onChanged}) {
_fieldNotifier.addListener(() {
if (onChanged != null) {
onChanged(clonedFields);
}
listener();
});
}
void _removeFields(List<FieldOrder> deletedFields) { void _removeFields(List<FieldOrder> deletedFields) {
if (deletedFields.isEmpty) { if (deletedFields.isEmpty) {
return; return;
@ -130,7 +139,7 @@ class GridRowCache {
GridRowCache({required this.gridId}); GridRowCache({required this.gridId});
List<RowData> get rows => _rows; List<RowData> get rows => [..._rows];
void updateWithBlock(List<GridBlockOrder> blocks, UnmodifiableListView<Field> fields) { void updateWithBlock(List<GridBlockOrder> blocks, UnmodifiableListView<Field> fields) {
_fields = fields; _fields = fields;

View File

@ -34,12 +34,12 @@ class RowBloc extends Bloc<RowEvent, RowState> {
createRow: (_CreateRow value) { createRow: (_CreateRow value) {
_rowService.createRow(); _rowService.createRow();
}, },
didReceiveFieldUpdate: (_DidReceiveFieldUpdate value) async {
await _handleFieldUpdate(emit, value);
},
didUpdateRow: (_DidUpdateRow value) async { didUpdateRow: (_DidUpdateRow value) async {
_handleRowUpdate(value, emit); _handleRowUpdate(value, emit);
}, },
fieldsDidUpdate: (_FieldsDidUpdate value) async {
await _handleFieldUpdate(emit);
},
); );
}, },
); );
@ -53,15 +53,15 @@ class RowBloc extends Bloc<RowEvent, RowState> {
)); ));
} }
Future<void> _handleFieldUpdate(Emitter<RowState> emit, _DidReceiveFieldUpdate value) async { Future<void> _handleFieldUpdate(Emitter<RowState> emit) async {
final optionRow = await state.row; final optionRow = await state.row;
final CellDataMap cellDataMap = optionRow.fold( final CellDataMap cellDataMap = optionRow.fold(
() => CellDataMap.identity(), () => CellDataMap.identity(),
(row) => _makeCellDatas(row, value.fields), (row) => _makeCellDatas(row, _fieldCache.unmodifiableFields),
); );
emit(state.copyWith( emit(state.copyWith(
rowData: state.rowData.copyWith(fields: value.fields), rowData: state.rowData.copyWith(fields: _fieldCache.unmodifiableFields),
cellDataMap: Some(cellDataMap), cellDataMap: Some(cellDataMap),
)); ));
} }
@ -80,9 +80,9 @@ class RowBloc extends Bloc<RowEvent, RowState> {
); );
}); });
_fieldCache.listenOnFieldChanged((fields) { _fieldCache.addListener(() {
if (!isClosed) { if (!isClosed) {
// add(RowEvent.didReceiveFieldUpdate(fields)); add(const RowEvent.fieldsDidUpdate());
} }
}); });
@ -118,7 +118,7 @@ class RowBloc extends Bloc<RowEvent, RowState> {
class RowEvent with _$RowEvent { class RowEvent with _$RowEvent {
const factory RowEvent.initial() = _InitialRow; const factory RowEvent.initial() = _InitialRow;
const factory RowEvent.createRow() = _CreateRow; const factory RowEvent.createRow() = _CreateRow;
const factory RowEvent.didReceiveFieldUpdate(List<Field> fields) = _DidReceiveFieldUpdate; const factory RowEvent.fieldsDidUpdate() = _FieldsDidUpdate;
const factory RowEvent.didUpdateRow(Row row) = _DidUpdateRow; const factory RowEvent.didUpdateRow(Row row) = _DidUpdateRow;
} }

View File

@ -40,7 +40,7 @@ class _GridPageState extends State<GridPage> {
return state.loadingState.map( return state.loadingState.map(
loading: (_) => const Center(child: CircularProgressIndicator.adaptive()), loading: (_) => const Center(child: CircularProgressIndicator.adaptive()),
finish: (result) => result.successOrFail.fold( finish: (result) => result.successOrFail.fold(
(_) => FlowyGrid(), (_) => const FlowyGrid(),
(err) => FlowyErrorPage(err.toString()), (err) => FlowyErrorPage(err.toString()),
), ),
); );
@ -65,9 +65,15 @@ class _GridPageState extends State<GridPage> {
} }
} }
class FlowyGrid extends StatelessWidget { class FlowyGrid extends StatefulWidget {
const FlowyGrid({Key? key}) : super(key: key);
@override
State<FlowyGrid> createState() => _FlowyGridState();
}
class _FlowyGridState extends State<FlowyGrid> {
final _scrollController = GridScrollController(); final _scrollController = GridScrollController();
FlowyGrid({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -155,9 +161,15 @@ class _GridHeader extends StatelessWidget {
} }
} }
class _GridRows extends StatelessWidget { class _GridRows extends StatefulWidget {
const _GridRows({Key? key}) : super(key: key);
@override
State<_GridRows> createState() => _GridRowsState();
}
class _GridRowsState extends State<_GridRows> {
final _key = GlobalKey<SliverAnimatedListState>(); final _key = GlobalKey<SliverAnimatedListState>();
_GridRows({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -15,7 +15,7 @@ class GridNotification extends $pb.ProtobufEnum {
static const GridNotification DidUpdateGridBlock = GridNotification._(20, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateGridBlock'); static const GridNotification DidUpdateGridBlock = GridNotification._(20, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateGridBlock');
static const GridNotification DidUpdateRow = GridNotification._(30, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateRow'); static const GridNotification DidUpdateRow = GridNotification._(30, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateRow');
static const GridNotification DidUpdateCell = GridNotification._(31, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateCell'); static const GridNotification DidUpdateCell = GridNotification._(31, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateCell');
static const GridNotification DidUpdateGrid = GridNotification._(40, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateGrid'); static const GridNotification DidUpdateGridField = GridNotification._(40, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateGridField');
static const GridNotification DidUpdateField = GridNotification._(41, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateField'); static const GridNotification DidUpdateField = GridNotification._(41, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'DidUpdateField');
static const $core.List<GridNotification> values = <GridNotification> [ static const $core.List<GridNotification> values = <GridNotification> [
@ -24,7 +24,7 @@ class GridNotification extends $pb.ProtobufEnum {
DidUpdateGridBlock, DidUpdateGridBlock,
DidUpdateRow, DidUpdateRow,
DidUpdateCell, DidUpdateCell,
DidUpdateGrid, DidUpdateGridField,
DidUpdateField, DidUpdateField,
]; ];

View File

@ -17,10 +17,10 @@ const GridNotification$json = const {
const {'1': 'DidUpdateGridBlock', '2': 20}, const {'1': 'DidUpdateGridBlock', '2': 20},
const {'1': 'DidUpdateRow', '2': 30}, const {'1': 'DidUpdateRow', '2': 30},
const {'1': 'DidUpdateCell', '2': 31}, const {'1': 'DidUpdateCell', '2': 31},
const {'1': 'DidUpdateGrid', '2': 40}, const {'1': 'DidUpdateGridField', '2': 40},
const {'1': 'DidUpdateField', '2': 41}, const {'1': 'DidUpdateField', '2': 41},
], ],
}; };
/// Descriptor for `GridNotification`. Decode as a `google.protobuf.EnumDescriptorProto`. /// Descriptor for `GridNotification`. Decode as a `google.protobuf.EnumDescriptorProto`.
final $typed_data.Uint8List gridNotificationDescriptor = $convert.base64Decode('ChBHcmlkTm90aWZpY2F0aW9uEgsKB1Vua25vd24QABISCg5EaWRDcmVhdGVCbG9jaxALEhYKEkRpZFVwZGF0ZUdyaWRCbG9jaxAUEhAKDERpZFVwZGF0ZVJvdxAeEhEKDURpZFVwZGF0ZUNlbGwQHxIRCg1EaWRVcGRhdGVHcmlkECgSEgoORGlkVXBkYXRlRmllbGQQKQ=='); final $typed_data.Uint8List gridNotificationDescriptor = $convert.base64Decode('ChBHcmlkTm90aWZpY2F0aW9uEgsKB1Vua25vd24QABISCg5EaWRDcmVhdGVCbG9jaxALEhYKEkRpZFVwZGF0ZUdyaWRCbG9jaxAUEhAKDERpZFVwZGF0ZVJvdxAeEhEKDURpZFVwZGF0ZUNlbGwQHxIWChJEaWRVcGRhdGVHcmlkRmllbGQQKBISCg5EaWRVcGRhdGVGaWVsZBAp');

View File

@ -9,7 +9,7 @@ pub enum GridNotification {
DidUpdateGridBlock = 20, DidUpdateGridBlock = 20,
DidUpdateRow = 30, DidUpdateRow = 30,
DidUpdateCell = 31, DidUpdateCell = 31,
DidUpdateGrid = 40, DidUpdateGridField = 40,
DidUpdateField = 41, DidUpdateField = 41,
} }

View File

@ -30,7 +30,7 @@ pub enum GridNotification {
DidUpdateGridBlock = 20, DidUpdateGridBlock = 20,
DidUpdateRow = 30, DidUpdateRow = 30,
DidUpdateCell = 31, DidUpdateCell = 31,
DidUpdateGrid = 40, DidUpdateGridField = 40,
DidUpdateField = 41, DidUpdateField = 41,
} }
@ -46,7 +46,7 @@ impl ::protobuf::ProtobufEnum for GridNotification {
20 => ::std::option::Option::Some(GridNotification::DidUpdateGridBlock), 20 => ::std::option::Option::Some(GridNotification::DidUpdateGridBlock),
30 => ::std::option::Option::Some(GridNotification::DidUpdateRow), 30 => ::std::option::Option::Some(GridNotification::DidUpdateRow),
31 => ::std::option::Option::Some(GridNotification::DidUpdateCell), 31 => ::std::option::Option::Some(GridNotification::DidUpdateCell),
40 => ::std::option::Option::Some(GridNotification::DidUpdateGrid), 40 => ::std::option::Option::Some(GridNotification::DidUpdateGridField),
41 => ::std::option::Option::Some(GridNotification::DidUpdateField), 41 => ::std::option::Option::Some(GridNotification::DidUpdateField),
_ => ::std::option::Option::None _ => ::std::option::Option::None
} }
@ -59,7 +59,7 @@ impl ::protobuf::ProtobufEnum for GridNotification {
GridNotification::DidUpdateGridBlock, GridNotification::DidUpdateGridBlock,
GridNotification::DidUpdateRow, GridNotification::DidUpdateRow,
GridNotification::DidUpdateCell, GridNotification::DidUpdateCell,
GridNotification::DidUpdateGrid, GridNotification::DidUpdateGridField,
GridNotification::DidUpdateField, GridNotification::DidUpdateField,
]; ];
values values
@ -89,11 +89,11 @@ impl ::protobuf::reflect::ProtobufValue for GridNotification {
} }
static file_descriptor_proto_data: &'static [u8] = b"\ static file_descriptor_proto_data: &'static [u8] = b"\
\n\x17dart_notification.proto*\x97\x01\n\x10GridNotification\x12\x0b\n\ \n\x17dart_notification.proto*\x9c\x01\n\x10GridNotification\x12\x0b\n\
\x07Unknown\x10\0\x12\x12\n\x0eDidCreateBlock\x10\x0b\x12\x16\n\x12DidUp\ \x07Unknown\x10\0\x12\x12\n\x0eDidCreateBlock\x10\x0b\x12\x16\n\x12DidUp\
dateGridBlock\x10\x14\x12\x10\n\x0cDidUpdateRow\x10\x1e\x12\x11\n\rDidUp\ dateGridBlock\x10\x14\x12\x10\n\x0cDidUpdateRow\x10\x1e\x12\x11\n\rDidUp\
dateCell\x10\x1f\x12\x11\n\rDidUpdateGrid\x10(\x12\x12\n\x0eDidUpdateFie\ dateCell\x10\x1f\x12\x16\n\x12DidUpdateGridField\x10(\x12\x12\n\x0eDidUp\
ld\x10)b\x06proto3\ dateField\x10)b\x06proto3\
"; ";
static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT;

View File

@ -6,6 +6,6 @@ enum GridNotification {
DidUpdateGridBlock = 20; DidUpdateGridBlock = 20;
DidUpdateRow = 30; DidUpdateRow = 30;
DidUpdateCell = 31; DidUpdateCell = 31;
DidUpdateGrid = 40; DidUpdateGridField = 40;
DidUpdateField = 41; DidUpdateField = 41;
} }

View File

@ -473,7 +473,7 @@ impl ClientGridEditor {
} }
async fn notify_did_update_grid(&self, changeset: GridFieldChangeset) -> FlowyResult<()> { async fn notify_did_update_grid(&self, changeset: GridFieldChangeset) -> FlowyResult<()> {
send_dart_notification(&self.grid_id, GridNotification::DidUpdateGrid) send_dart_notification(&self.grid_id, GridNotification::DidUpdateGridField)
.payload(changeset) .payload(changeset)
.send(); .send();
Ok(()) Ok(())