2022-03-03 10:51:52 +08:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:dartz/dartz.dart';
|
2022-06-03 10:51:24 +08:00
|
|
|
import 'package:equatable/equatable.dart';
|
2022-03-03 10:51:52 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
2022-07-04 15:00:54 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
|
2022-07-01 20:32:11 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
|
2022-03-03 10:51:52 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2022-07-16 15:38:38 +08:00
|
|
|
import 'block/block_cache.dart';
|
2022-09-04 15:33:07 +08:00
|
|
|
import 'field/field_controller.dart';
|
2022-08-09 18:04:23 +08:00
|
|
|
import 'grid_data_controller.dart';
|
2022-08-09 19:06:15 +08:00
|
|
|
import 'row/row_cache.dart';
|
2022-06-03 10:51:24 +08:00
|
|
|
import 'dart:collection';
|
2022-03-03 10:51:52 +08:00
|
|
|
|
|
|
|
part 'grid_bloc.freezed.dart';
|
|
|
|
|
|
|
|
class GridBloc extends Bloc<GridEvent, GridState> {
|
2022-08-09 18:04:23 +08:00
|
|
|
final GridDataController dataController;
|
2022-06-24 15:23:39 +08:00
|
|
|
|
2022-07-19 14:11:29 +08:00
|
|
|
GridBloc({required ViewPB view})
|
2022-08-09 18:04:23 +08:00
|
|
|
: dataController = GridDataController(view: view),
|
2022-04-10 16:29:45 +08:00
|
|
|
super(GridState.initial(view.id)) {
|
2022-03-03 10:51:52 +08:00
|
|
|
on<GridEvent>(
|
|
|
|
(event, emit) async {
|
2022-06-03 10:51:24 +08:00
|
|
|
await event.when(
|
|
|
|
initial: () async {
|
2022-04-10 08:25:01 +08:00
|
|
|
_startListening();
|
2022-04-14 13:29:42 +08:00
|
|
|
await _loadGrid(emit);
|
2022-03-03 10:51:52 +08:00
|
|
|
},
|
2022-06-03 10:51:24 +08:00
|
|
|
createRow: () {
|
2022-08-09 18:04:23 +08:00
|
|
|
dataController.createRow();
|
2022-03-03 10:51:52 +08:00
|
|
|
},
|
2022-08-09 18:04:23 +08:00
|
|
|
didReceiveGridUpdate: (grid) {
|
|
|
|
emit(state.copyWith(grid: Some(grid)));
|
2022-03-18 17:14:46 +08:00
|
|
|
},
|
2022-06-03 10:51:24 +08:00
|
|
|
didReceiveFieldUpdate: (fields) {
|
2022-08-09 18:04:23 +08:00
|
|
|
emit(state.copyWith(
|
|
|
|
fields: GridFieldEquatable(fields),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
didReceiveRowUpdate: (newRowInfos, reason) {
|
|
|
|
emit(state.copyWith(
|
|
|
|
rowInfos: newRowInfos,
|
2022-09-08 11:17:37 +08:00
|
|
|
rowCount: newRowInfos.length,
|
2022-08-09 18:04:23 +08:00
|
|
|
reason: reason,
|
|
|
|
));
|
2022-03-23 22:10:31 +08:00
|
|
|
},
|
2022-03-03 10:51:52 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
2022-08-09 18:04:23 +08:00
|
|
|
await dataController.dispose();
|
2022-03-03 10:51:52 +08:00
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
2022-07-16 15:38:38 +08:00
|
|
|
GridRowCache? getRowCache(String blockId, String rowId) {
|
2022-08-09 18:04:23 +08:00
|
|
|
final GridBlockCache? blockCache = dataController.blocks[blockId];
|
2022-07-03 15:53:28 +08:00
|
|
|
return blockCache?.rowCache;
|
|
|
|
}
|
|
|
|
|
2022-04-14 13:29:42 +08:00
|
|
|
void _startListening() {
|
2022-08-09 18:04:23 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
},
|
2022-04-16 22:26:34 +08:00
|
|
|
);
|
2022-03-26 20:27:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 22:17:07 +08:00
|
|
|
Future<void> _loadGrid(Emitter<GridState> emit) async {
|
2022-08-09 18:04:23 +08:00
|
|
|
final result = await dataController.loadData();
|
|
|
|
result.fold(
|
|
|
|
(grid) => emit(
|
|
|
|
state.copyWith(loadingState: GridLoadingState.finish(left(unit))),
|
2022-03-18 17:14:46 +08:00
|
|
|
),
|
2022-08-09 18:04:23 +08:00
|
|
|
(err) => emit(
|
|
|
|
state.copyWith(loadingState: GridLoadingState.finish(right(err))),
|
2022-03-18 17:14:46 +08:00
|
|
|
),
|
2022-03-17 20:59:21 +08:00
|
|
|
);
|
2022-03-03 22:17:07 +08:00
|
|
|
}
|
2022-03-03 10:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-03-25 15:02:43 +08:00
|
|
|
class GridEvent with _$GridEvent {
|
2022-03-08 15:25:56 +08:00
|
|
|
const factory GridEvent.initial() = InitialGrid;
|
2022-03-03 10:51:52 +08:00
|
|
|
const factory GridEvent.createRow() = _CreateRow;
|
2022-08-09 18:04:23 +08:00
|
|
|
const factory GridEvent.didReceiveRowUpdate(
|
2022-08-11 13:25:55 +08:00
|
|
|
List<RowInfo> rows,
|
2022-08-13 14:59:50 +08:00
|
|
|
RowsChangedReason listState,
|
2022-08-09 18:04:23 +08:00
|
|
|
) = _DidReceiveRowUpdate;
|
|
|
|
const factory GridEvent.didReceiveFieldUpdate(
|
2022-09-03 17:16:48 +08:00
|
|
|
UnmodifiableListView<GridFieldContext> fields,
|
2022-08-09 18:04:23 +08:00
|
|
|
) = _DidReceiveFieldUpdate;
|
|
|
|
|
|
|
|
const factory GridEvent.didReceiveGridUpdate(
|
|
|
|
GridPB grid,
|
|
|
|
) = _DidReceiveGridUpdate;
|
2022-03-03 10:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-03-25 15:02:43 +08:00
|
|
|
class GridState with _$GridState {
|
2022-03-03 10:51:52 +08:00
|
|
|
const factory GridState({
|
2022-04-10 16:29:45 +08:00
|
|
|
required String gridId,
|
2022-07-17 14:13:12 +08:00
|
|
|
required Option<GridPB> grid,
|
2022-06-03 10:51:24 +08:00
|
|
|
required GridFieldEquatable fields,
|
2022-08-11 13:25:55 +08:00
|
|
|
required List<RowInfo> rowInfos,
|
2022-09-08 11:17:37 +08:00
|
|
|
required int rowCount,
|
2022-04-11 20:52:15 +08:00
|
|
|
required GridLoadingState loadingState,
|
2022-08-13 14:59:50 +08:00
|
|
|
required RowsChangedReason reason,
|
2022-03-03 10:51:52 +08:00
|
|
|
}) = _GridState;
|
|
|
|
|
2022-04-10 16:29:45 +08:00
|
|
|
factory GridState.initial(String gridId) => GridState(
|
2022-08-09 18:04:23 +08:00
|
|
|
fields: GridFieldEquatable(UnmodifiableListView([])),
|
2022-07-17 11:29:02 +08:00
|
|
|
rowInfos: [],
|
2022-09-08 11:17:37 +08:00
|
|
|
rowCount: 0,
|
2022-03-17 20:59:21 +08:00
|
|
|
grid: none(),
|
2022-04-10 16:29:45 +08:00
|
|
|
gridId: gridId,
|
2022-04-11 20:52:15 +08:00
|
|
|
loadingState: const _Loading(),
|
2022-07-03 15:53:28 +08:00
|
|
|
reason: const InitialListState(),
|
2022-03-03 10:51:52 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class GridLoadingState with _$GridLoadingState {
|
|
|
|
const factory GridLoadingState.loading() = _Loading;
|
2022-08-09 18:04:23 +08:00
|
|
|
const factory GridLoadingState.finish(
|
|
|
|
Either<Unit, FlowyError> successOrFail) = _Finish;
|
2022-03-03 10:51:52 +08:00
|
|
|
}
|
2022-06-03 10:51:24 +08:00
|
|
|
|
|
|
|
class GridFieldEquatable extends Equatable {
|
2022-09-03 17:16:48 +08:00
|
|
|
final UnmodifiableListView<GridFieldContext> _fields;
|
2022-08-09 18:04:23 +08:00
|
|
|
const GridFieldEquatable(
|
2022-09-03 17:16:48 +08:00
|
|
|
UnmodifiableListView<GridFieldContext> fields,
|
2022-08-09 18:04:23 +08:00
|
|
|
) : _fields = fields;
|
2022-06-03 10:51:24 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
List<Object?> get props {
|
2022-08-09 18:04:23 +08:00
|
|
|
if (_fields.isEmpty) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:51:24 +08:00
|
|
|
return [
|
|
|
|
_fields.length,
|
2022-08-09 18:04:23 +08:00
|
|
|
_fields
|
|
|
|
.map((field) => field.width)
|
|
|
|
.reduce((value, element) => value + element),
|
2022-06-03 10:51:24 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-03 17:16:48 +08:00
|
|
|
UnmodifiableListView<GridFieldContext> get value =>
|
|
|
|
UnmodifiableListView(_fields);
|
2022-06-03 10:51:24 +08:00
|
|
|
}
|