|
@@ -1,40 +1,23 @@
|
|
|
import 'dart:async';
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
-import 'package:flowy_sdk/log.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
|
|
|
import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
import 'block/block_cache.dart';
|
|
|
-import 'grid_service.dart';
|
|
|
+import 'grid_data_controller.dart';
|
|
|
import 'row/row_service.dart';
|
|
|
import 'dart:collection';
|
|
|
|
|
|
part 'grid_bloc.freezed.dart';
|
|
|
|
|
|
class GridBloc extends Bloc<GridEvent, GridState> {
|
|
|
- final String gridId;
|
|
|
- final GridService _gridService;
|
|
|
- final GridFieldCache fieldCache;
|
|
|
-
|
|
|
- // key: the block id
|
|
|
- final LinkedHashMap<String, GridBlockCache> _blocks;
|
|
|
-
|
|
|
- List<GridRowInfo> get rowInfos {
|
|
|
- final List<GridRowInfo> rows = [];
|
|
|
- for (var block in _blocks.values) {
|
|
|
- rows.addAll(block.rows);
|
|
|
- }
|
|
|
- return rows;
|
|
|
- }
|
|
|
+ final GridDataController dataController;
|
|
|
|
|
|
GridBloc({required ViewPB view})
|
|
|
- : gridId = view.id,
|
|
|
- _blocks = LinkedHashMap.identity(),
|
|
|
- _gridService = GridService(gridId: view.id),
|
|
|
- fieldCache = GridFieldCache(gridId: view.id),
|
|
|
+ : dataController = GridDataController(view: view),
|
|
|
super(GridState.initial(view.id)) {
|
|
|
on<GridEvent>(
|
|
|
(event, emit) async {
|
|
@@ -44,13 +27,21 @@ class GridBloc extends Bloc<GridEvent, GridState> {
|
|
|
await _loadGrid(emit);
|
|
|
},
|
|
|
createRow: () {
|
|
|
- _gridService.createRow();
|
|
|
+ dataController.createRow();
|
|
|
},
|
|
|
- didReceiveRowUpdate: (newRowInfos, reason) {
|
|
|
- emit(state.copyWith(rowInfos: newRowInfos, reason: reason));
|
|
|
+ didReceiveGridUpdate: (grid) {
|
|
|
+ emit(state.copyWith(grid: Some(grid)));
|
|
|
},
|
|
|
didReceiveFieldUpdate: (fields) {
|
|
|
- emit(state.copyWith(rowInfos: rowInfos, fields: GridFieldEquatable(fields)));
|
|
|
+ emit(state.copyWith(
|
|
|
+ fields: GridFieldEquatable(fields),
|
|
|
+ ));
|
|
|
+ },
|
|
|
+ didReceiveRowUpdate: (newRowInfos, reason) {
|
|
|
+ emit(state.copyWith(
|
|
|
+ rowInfos: newRowInfos,
|
|
|
+ reason: reason,
|
|
|
+ ));
|
|
|
},
|
|
|
);
|
|
|
},
|
|
@@ -59,89 +50,63 @@ class GridBloc extends Bloc<GridEvent, GridState> {
|
|
|
|
|
|
@override
|
|
|
Future<void> close() async {
|
|
|
- await _gridService.closeGrid();
|
|
|
- await fieldCache.dispose();
|
|
|
-
|
|
|
- for (final blockCache in _blocks.values) {
|
|
|
- blockCache.dispose();
|
|
|
- }
|
|
|
+ await dataController.dispose();
|
|
|
return super.close();
|
|
|
}
|
|
|
|
|
|
GridRowCache? getRowCache(String blockId, String rowId) {
|
|
|
- final GridBlockCache? blockCache = _blocks[blockId];
|
|
|
+ final GridBlockCache? blockCache = dataController.blocks[blockId];
|
|
|
return blockCache?.rowCache;
|
|
|
}
|
|
|
|
|
|
void _startListening() {
|
|
|
- fieldCache.addListener(
|
|
|
- listenWhen: () => !isClosed,
|
|
|
- onFields: (fields) => add(GridEvent.didReceiveFieldUpdate(fields)),
|
|
|
+ dataController.addListener(
|
|
|
+ onGridChanged: (grid) {
|
|
|
+ if (!isClosed) {
|
|
|
+ add(GridEvent.didReceiveGridUpdate(grid));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onRowsChanged: (rowInfos, reason) {
|
|
|
+ if (!isClosed) {
|
|
|
+ add(GridEvent.didReceiveRowUpdate(rowInfos, reason));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onFieldsChanged: (fields) {
|
|
|
+ if (!isClosed) {
|
|
|
+ add(GridEvent.didReceiveFieldUpdate(fields));
|
|
|
+ }
|
|
|
+ },
|
|
|
);
|
|
|
}
|
|
|
|
|
|
Future<void> _loadGrid(Emitter<GridState> emit) async {
|
|
|
- final result = await _gridService.loadGrid();
|
|
|
- return Future(
|
|
|
- () => result.fold(
|
|
|
- (grid) async {
|
|
|
- _initialBlocks(grid.blocks);
|
|
|
- await _loadFields(grid, emit);
|
|
|
- },
|
|
|
- (err) => emit(state.copyWith(loadingState: GridLoadingState.finish(right(err)))),
|
|
|
+ final result = await dataController.loadData();
|
|
|
+ result.fold(
|
|
|
+ (grid) => emit(
|
|
|
+ state.copyWith(loadingState: GridLoadingState.finish(left(unit))),
|
|
|
),
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- Future<void> _loadFields(GridPB grid, Emitter<GridState> emit) async {
|
|
|
- final result = await _gridService.getFields(fieldIds: grid.fields);
|
|
|
- return Future(
|
|
|
- () => result.fold(
|
|
|
- (fields) {
|
|
|
- fieldCache.fields = fields.items;
|
|
|
-
|
|
|
- emit(state.copyWith(
|
|
|
- grid: Some(grid),
|
|
|
- fields: GridFieldEquatable(fieldCache.fields),
|
|
|
- rowInfos: rowInfos,
|
|
|
- loadingState: GridLoadingState.finish(left(unit)),
|
|
|
- ));
|
|
|
- },
|
|
|
- (err) => emit(state.copyWith(loadingState: GridLoadingState.finish(right(err)))),
|
|
|
+ (err) => emit(
|
|
|
+ state.copyWith(loadingState: GridLoadingState.finish(right(err))),
|
|
|
),
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
- void _initialBlocks(List<GridBlockPB> blocks) {
|
|
|
- for (final block in blocks) {
|
|
|
- if (_blocks[block.id] != null) {
|
|
|
- Log.warn("Intial duplicate block's cache: ${block.id}");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- final cache = GridBlockCache(
|
|
|
- gridId: gridId,
|
|
|
- block: block,
|
|
|
- fieldCache: fieldCache,
|
|
|
- );
|
|
|
-
|
|
|
- cache.addListener(
|
|
|
- listenWhen: () => !isClosed,
|
|
|
- onChangeReason: (reason) => add(GridEvent.didReceiveRowUpdate(rowInfos, reason)),
|
|
|
- );
|
|
|
-
|
|
|
- _blocks[block.id] = cache;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
@freezed
|
|
|
class GridEvent with _$GridEvent {
|
|
|
const factory GridEvent.initial() = InitialGrid;
|
|
|
const factory GridEvent.createRow() = _CreateRow;
|
|
|
- const factory GridEvent.didReceiveRowUpdate(List<GridRowInfo> rows, GridRowChangeReason listState) =
|
|
|
- _DidReceiveRowUpdate;
|
|
|
- const factory GridEvent.didReceiveFieldUpdate(List<GridFieldPB> fields) = _DidReceiveFieldUpdate;
|
|
|
+ const factory GridEvent.didReceiveRowUpdate(
|
|
|
+ List<GridRowInfo> rows,
|
|
|
+ GridRowChangeReason listState,
|
|
|
+ ) = _DidReceiveRowUpdate;
|
|
|
+ const factory GridEvent.didReceiveFieldUpdate(
|
|
|
+ UnmodifiableListView<GridFieldPB> fields,
|
|
|
+ ) = _DidReceiveFieldUpdate;
|
|
|
+
|
|
|
+ const factory GridEvent.didReceiveGridUpdate(
|
|
|
+ GridPB grid,
|
|
|
+ ) = _DidReceiveGridUpdate;
|
|
|
}
|
|
|
|
|
|
@freezed
|
|
@@ -156,7 +121,7 @@ class GridState with _$GridState {
|
|
|
}) = _GridState;
|
|
|
|
|
|
factory GridState.initial(String gridId) => GridState(
|
|
|
- fields: const GridFieldEquatable([]),
|
|
|
+ fields: GridFieldEquatable(UnmodifiableListView([])),
|
|
|
rowInfos: [],
|
|
|
grid: none(),
|
|
|
gridId: gridId,
|
|
@@ -168,18 +133,27 @@ class GridState with _$GridState {
|
|
|
@freezed
|
|
|
class GridLoadingState with _$GridLoadingState {
|
|
|
const factory GridLoadingState.loading() = _Loading;
|
|
|
- const factory GridLoadingState.finish(Either<Unit, FlowyError> successOrFail) = _Finish;
|
|
|
+ const factory GridLoadingState.finish(
|
|
|
+ Either<Unit, FlowyError> successOrFail) = _Finish;
|
|
|
}
|
|
|
|
|
|
class GridFieldEquatable extends Equatable {
|
|
|
- final List<GridFieldPB> _fields;
|
|
|
- const GridFieldEquatable(List<GridFieldPB> fields) : _fields = fields;
|
|
|
+ final UnmodifiableListView<GridFieldPB> _fields;
|
|
|
+ const GridFieldEquatable(
|
|
|
+ UnmodifiableListView<GridFieldPB> fields,
|
|
|
+ ) : _fields = fields;
|
|
|
|
|
|
@override
|
|
|
List<Object?> get props {
|
|
|
+ if (_fields.isEmpty) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
return [
|
|
|
_fields.length,
|
|
|
- _fields.map((field) => field.width).reduce((value, element) => value + element),
|
|
|
+ _fields
|
|
|
+ .map((field) => field.width)
|
|
|
+ .reduce((value, element) => value + element),
|
|
|
];
|
|
|
}
|
|
|
|