505 lines
14 KiB
Dart
Raw Normal View History

2022-08-09 20:37:01 +08:00
import 'dart:async';
2022-09-05 09:24:05 +08:00
import 'dart:collection';
2022-08-09 20:37:01 +08:00
import 'package:app_flowy/plugins/grid/application/block/block_cache.dart';
import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
2022-08-09 20:37:01 +08:00
import 'package:app_flowy/plugins/grid/application/row/row_cache.dart';
import 'package:app_flowy/plugins/grid/application/row/row_service.dart';
2022-08-10 12:58:07 +08:00
import 'package:appflowy_board/appflowy_board.dart';
2022-08-09 20:37:01 +08:00
import 'package:dartz/dartz.dart';
import 'package:equatable/equatable.dart';
2022-08-10 17:59:28 +08:00
import 'package:flowy_sdk/log.dart';
2022-08-09 20:37:01 +08:00
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';
2022-08-11 15:00:36 +08:00
import 'board_data_controller.dart';
2022-08-16 17:13:56 +08:00
import 'group_controller.dart';
2022-08-11 15:00:36 +08:00
2022-08-09 20:37:01 +08:00
part 'board_bloc.freezed.dart';
class BoardBloc extends Bloc<BoardEvent, BoardState> {
2022-08-24 16:57:53 +08:00
final BoardDataController _gridDataController;
2022-09-06 11:55:53 +08:00
late final AppFlowyBoardController boardController;
final MoveRowFFIService _rowService;
2022-10-25 19:50:11 +08:00
final LinkedHashMap<String, GroupController> groupControllers =
LinkedHashMap();
2022-08-09 20:37:01 +08:00
2022-09-03 17:16:48 +08:00
GridFieldController get fieldController =>
_gridDataController.fieldController;
2022-08-24 16:57:53 +08:00
String get gridId => _gridDataController.gridId;
2022-08-12 20:10:56 +08:00
2022-08-09 20:37:01 +08:00
BoardBloc({required ViewPB view})
: _rowService = MoveRowFFIService(gridId: view.id),
2022-08-24 16:57:53 +08:00
_gridDataController = BoardDataController(view: view),
2022-08-09 20:37:01 +08:00
super(BoardState.initial(view.id)) {
2022-09-06 11:55:53 +08:00
boardController = AppFlowyBoardController(
2022-09-05 16:29:16 +08:00
onMoveGroup: (
2022-10-01 11:00:15 +08:00
fromGroupId,
2022-08-10 12:58:07 +08:00
fromIndex,
2022-10-01 11:00:15 +08:00
toGroupId,
2022-08-10 12:58:07 +08:00
toIndex,
2022-08-22 16:16:15 +08:00
) {
2022-10-01 11:00:15 +08:00
_moveGroup(fromGroupId, toGroupId);
2022-08-22 16:16:15 +08:00
},
2022-09-05 16:29:16 +08:00
onMoveGroupItem: (
2022-10-01 11:00:15 +08:00
groupId,
2022-08-10 12:58:07 +08:00
fromIndex,
toIndex,
2022-08-17 19:29:14 +08:00
) {
2022-10-01 11:00:15 +08:00
final fromRow = groupControllers[groupId]?.rowAtIndex(fromIndex);
final toRow = groupControllers[groupId]?.rowAtIndex(toIndex);
_moveRow(fromRow, groupId, toRow);
2022-08-17 19:29:14 +08:00
},
2022-09-05 16:29:16 +08:00
onMoveGroupItemToGroup: (
fromGroupId,
2022-08-10 12:58:07 +08:00
fromIndex,
2022-09-05 16:29:16 +08:00
toGroupId,
2022-08-10 12:58:07 +08:00
toIndex,
2022-08-17 19:29:14 +08:00
) {
2022-09-05 16:29:16 +08:00
final fromRow = groupControllers[fromGroupId]?.rowAtIndex(fromIndex);
final toRow = groupControllers[toGroupId]?.rowAtIndex(toIndex);
_moveRow(fromRow, toGroupId, toRow);
2022-08-17 19:29:14 +08:00
},
2022-08-10 12:58:07 +08:00
);
2022-08-09 20:37:01 +08:00
on<BoardEvent>(
(event, emit) async {
await event.when(
initial: () async {
_startListening();
2022-10-26 10:38:57 +08:00
await _openGrid(emit);
2022-08-09 20:37:01 +08:00
},
createBottomRow: (groupId) async {
final startRowId = groupControllers[groupId]?.lastRow()?.id;
final result = await _gridDataController.createBoardCard(
groupId,
startRowId: startRowId,
);
result.fold(
(_) {},
(err) => Log.error(err),
);
},
createHeaderRow: (String groupId) async {
2022-08-24 16:57:53 +08:00
final result = await _gridDataController.createBoardCard(groupId);
result.fold(
(_) {},
(err) => Log.error(err),
);
2022-08-09 20:37:01 +08:00
},
2022-10-06 22:26:18 +08:00
didCreateRow: (group, row, int? index) {
emit(state.copyWith(
editingRow: Some(BoardEditingRow(
2022-10-06 22:26:18 +08:00
group: group,
row: row,
index: index,
)),
));
2022-10-06 22:26:18 +08:00
_groupItemStartEditing(group, row, true);
},
2022-10-06 22:26:18 +08:00
startEditingRow: (group, row) {
emit(state.copyWith(
editingRow: Some(BoardEditingRow(
group: group,
row: row,
index: null,
)),
));
_groupItemStartEditing(group, row, true);
},
endEditingRow: (rowId) {
state.editingRow.fold(() => null, (editingRow) {
assert(editingRow.row.id == rowId);
2022-10-06 22:26:18 +08:00
_groupItemStartEditing(editingRow.group, editingRow.row, false);
emit(state.copyWith(editingRow: none()));
});
},
2022-08-10 12:58:07 +08:00
didReceiveGridUpdate: (GridPB grid) {
2022-08-09 20:37:01 +08:00
emit(state.copyWith(grid: Some(grid)));
},
didReceiveError: (FlowyError error) {
emit(state.copyWith(noneOrError: some(error)));
},
2022-08-26 11:51:42 +08:00
didReceiveGroups: (List<GroupPB> groups) {
emit(
state.copyWith(
groupIds: groups.map((group) => group.groupId).toList(),
),
);
2022-08-26 11:51:42 +08:00
},
2022-08-09 20:37:01 +08:00
);
},
);
}
2022-10-06 22:26:18 +08:00
void _groupItemStartEditing(GroupPB group, RowPB row, bool isEdit) {
final fieldInfo = fieldController.getField(group.fieldId);
if (fieldInfo == null) {
Log.warn("fieldInfo should not be null");
2022-10-06 22:26:18 +08:00
return;
}
boardController.enableGroupDragging(!isEdit);
// boardController.updateGroupItem(
// group.groupId,
// GroupItem(
// row: row,
// fieldInfo: fieldInfo,
2022-10-06 22:26:18 +08:00
// isDraggable: !isEdit,
// ),
// );
}
2022-08-22 16:16:15 +08:00
void _moveRow(RowPB? fromRow, String columnId, RowPB? toRow) {
if (fromRow != null) {
_rowService
2022-08-22 16:16:15 +08:00
.moveGroupRow(
fromRowId: fromRow.id,
2022-08-22 16:16:15 +08:00
toGroupId: columnId,
toRowId: toRow?.id,
)
.then((result) {
result.fold((l) => null, (r) => add(BoardEvent.didReceiveError(r)));
});
}
}
void _moveGroup(String fromGroupId, String toGroupId) {
2022-08-22 16:16:15 +08:00
_rowService
.moveGroup(
fromGroupId: fromGroupId,
toGroupId: toGroupId,
2022-08-22 16:16:15 +08:00
)
.then((result) {
result.fold((l) => null, (r) => add(BoardEvent.didReceiveError(r)));
});
}
2022-08-09 20:37:01 +08:00
@override
Future<void> close() async {
2022-08-24 16:57:53 +08:00
await _gridDataController.dispose();
2022-08-17 19:29:14 +08:00
for (final controller in groupControllers.values) {
2022-08-16 17:13:56 +08:00
controller.dispose();
}
2022-08-09 20:37:01 +08:00
return super.close();
}
2022-10-06 22:26:18 +08:00
void initializeGroups(List<GroupPB> groupsData) {
for (var controller in groupControllers.values) {
controller.dispose();
}
groupControllers.clear();
boardController.clear();
//
2022-10-06 22:26:18 +08:00
List<AppFlowyGroupData> groups = groupsData
2022-09-05 09:24:05 +08:00
.where((group) => fieldController.getField(group.fieldId) != null)
.map((group) {
2022-09-06 11:55:53 +08:00
return AppFlowyGroupData(
id: group.groupId,
name: group.desc,
2022-10-06 22:26:18 +08:00
items: _buildGroupItems(group),
customData: GroupData(
2022-09-05 09:24:05 +08:00
group: group,
fieldInfo: fieldController.getField(group.fieldId)!,
2022-09-05 09:24:05 +08:00
),
);
}).toList();
2022-10-06 22:26:18 +08:00
boardController.addGroups(groups);
2022-10-06 22:26:18 +08:00
for (final group in groupsData) {
final delegate = GroupControllerDelegateImpl(
controller: boardController,
2022-09-05 09:24:05 +08:00
fieldController: fieldController,
onNewColumnItem: (groupId, row, index) {
2022-10-06 22:26:18 +08:00
add(BoardEvent.didCreateRow(group, row, index));
},
);
2022-08-16 17:13:56 +08:00
final controller = GroupController(
2022-08-17 19:29:14 +08:00
gridId: state.gridId,
2022-08-16 17:13:56 +08:00
group: group,
delegate: delegate,
);
controller.startListening();
2022-08-17 19:29:14 +08:00
groupControllers[controller.group.groupId] = (controller);
2022-08-16 17:13:56 +08:00
}
}
2022-08-12 20:10:56 +08:00
GridRowCache? getRowCache(String blockId) {
2022-08-24 16:57:53 +08:00
final GridBlockCache? blockCache = _gridDataController.blocks[blockId];
2022-08-09 20:37:01 +08:00
return blockCache?.rowCache;
}
void _startListening() {
2022-08-24 16:57:53 +08:00
_gridDataController.addListener(
2022-08-09 20:37:01 +08:00
onGridChanged: (grid) {
if (!isClosed) {
add(BoardEvent.didReceiveGridUpdate(grid));
}
},
2022-08-16 17:13:56 +08:00
didLoadGroups: (groups) {
if (isClosed) return;
2022-08-16 17:13:56 +08:00
initializeGroups(groups);
2022-08-26 11:51:42 +08:00
add(BoardEvent.didReceiveGroups(groups));
},
2022-08-24 16:57:53 +08:00
onDeletedGroup: (groupIds) {
if (isClosed) return;
2022-08-24 16:57:53 +08:00
//
},
onInsertedGroup: (insertedGroups) {
if (isClosed) return;
2022-08-24 16:57:53 +08:00
//
},
onUpdatedGroup: (updatedGroups) {
if (isClosed) return;
2022-08-24 16:57:53 +08:00
for (final group in updatedGroups) {
final columnController =
2022-09-05 16:29:16 +08:00
boardController.getGroupController(group.groupId);
columnController?.updateGroupName(group.desc);
2022-08-24 16:57:53 +08:00
}
},
2022-08-11 21:18:27 +08:00
onError: (err) {
Log.error(err);
},
onResetGroups: (groups) {
if (isClosed) return;
initializeGroups(groups);
add(BoardEvent.didReceiveGroups(groups));
},
);
2022-08-10 12:58:07 +08:00
}
2022-10-06 22:26:18 +08:00
List<AppFlowyGroupItem> _buildGroupItems(GroupPB group) {
final items = group.rows.map((row) {
final fieldInfo = fieldController.getField(group.fieldId);
2022-10-06 22:26:18 +08:00
return GroupItem(
row: row,
fieldInfo: fieldInfo!,
2022-10-06 22:26:18 +08:00
);
2022-08-12 16:06:30 +08:00
}).toList();
2022-08-13 11:51:26 +08:00
2022-09-05 16:29:16 +08:00
return <AppFlowyGroupItem>[...items];
2022-08-12 16:06:30 +08:00
}
2022-10-26 10:38:57 +08:00
Future<void> _openGrid(Emitter<BoardState> emit) async {
final result = await _gridDataController.openGrid();
2022-08-09 20:37:01 +08:00
result.fold(
(grid) => emit(
state.copyWith(loadingState: GridLoadingState.finish(left(unit))),
),
(err) => emit(
state.copyWith(loadingState: GridLoadingState.finish(right(err))),
),
);
}
}
@freezed
class BoardEvent with _$BoardEvent {
2022-08-29 10:06:29 +08:00
const factory BoardEvent.initial() = _InitialBoard;
const factory BoardEvent.createBottomRow(String groupId) = _CreateBottomRow;
const factory BoardEvent.createHeaderRow(String groupId) = _CreateHeaderRow;
const factory BoardEvent.didCreateRow(
2022-10-06 22:26:18 +08:00
GroupPB group,
RowPB row,
int? index,
) = _DidCreateRow;
2022-10-06 22:26:18 +08:00
const factory BoardEvent.startEditingRow(
GroupPB group,
RowPB row,
) = _StartEditRow;
const factory BoardEvent.endEditingRow(String rowId) = _EndEditRow;
const factory BoardEvent.didReceiveError(FlowyError error) = _DidReceiveError;
2022-08-09 20:37:01 +08:00
const factory BoardEvent.didReceiveGridUpdate(
GridPB grid,
) = _DidReceiveGridUpdate;
2022-08-26 11:51:42 +08:00
const factory BoardEvent.didReceiveGroups(List<GroupPB> groups) =
_DidReceiveGroups;
2022-08-09 20:37:01 +08:00
}
@freezed
class BoardState with _$BoardState {
const factory BoardState({
required String gridId,
required Option<GridPB> grid,
2022-08-26 11:51:42 +08:00
required List<String> groupIds,
required Option<BoardEditingRow> editingRow,
2022-08-09 20:37:01 +08:00
required GridLoadingState loadingState,
required Option<FlowyError> noneOrError,
2022-08-09 20:37:01 +08:00
}) = _BoardState;
factory BoardState.initial(String gridId) => BoardState(
grid: none(),
gridId: gridId,
2022-08-26 11:51:42 +08:00
groupIds: [],
editingRow: none(),
noneOrError: none(),
2022-08-09 20:37:01 +08:00
loadingState: const _Loading(),
);
}
@freezed
class GridLoadingState with _$GridLoadingState {
const factory GridLoadingState.loading() = _Loading;
const factory GridLoadingState.finish(
Either<Unit, FlowyError> successOrFail) = _Finish;
}
class GridFieldEquatable extends Equatable {
final UnmodifiableListView<FieldPB> _fields;
2022-08-09 20:37:01 +08:00
const GridFieldEquatable(
UnmodifiableListView<FieldPB> fields,
2022-08-09 20:37:01 +08:00
) : _fields = fields;
@override
List<Object?> get props {
if (_fields.isEmpty) {
return [];
}
return [
_fields.length,
_fields
.map((field) => field.width)
.reduce((value, element) => value + element),
];
}
UnmodifiableListView<FieldPB> get value => UnmodifiableListView(_fields);
2022-08-09 20:37:01 +08:00
}
2022-08-10 12:58:07 +08:00
2022-10-06 22:26:18 +08:00
class GroupItem extends AppFlowyGroupItem {
2022-08-12 20:10:56 +08:00
final RowPB row;
final FieldInfo fieldInfo;
2022-08-10 12:58:07 +08:00
2022-10-06 22:26:18 +08:00
GroupItem({
required this.row,
required this.fieldInfo,
2022-10-06 22:26:18 +08:00
bool draggable = true,
}) {
super.draggable = draggable;
}
2022-08-13 11:57:14 +08:00
@override
String get id => row.id;
2022-08-13 11:57:14 +08:00
}
2022-08-16 17:13:56 +08:00
class GroupControllerDelegateImpl extends GroupControllerDelegate {
2022-09-05 09:24:05 +08:00
final GridFieldController fieldController;
2022-09-06 11:55:53 +08:00
final AppFlowyBoardController controller;
final void Function(String, RowPB, int?) onNewColumnItem;
2022-08-16 17:13:56 +08:00
GroupControllerDelegateImpl({
required this.controller,
2022-09-05 09:24:05 +08:00
required this.fieldController,
required this.onNewColumnItem,
});
2022-08-16 17:13:56 +08:00
@override
void insertRow(GroupPB group, RowPB row, int? index) {
final fieldInfo = fieldController.getField(group.fieldId);
if (fieldInfo == null) {
Log.warn("fieldInfo should not be null");
2022-09-05 09:24:05 +08:00
return;
}
2022-08-16 17:13:56 +08:00
if (index != null) {
2022-10-06 22:26:18 +08:00
final item = GroupItem(
row: row,
fieldInfo: fieldInfo,
2022-10-06 22:26:18 +08:00
);
2022-09-05 16:29:16 +08:00
controller.insertGroupItem(group.groupId, index, item);
2022-08-16 17:13:56 +08:00
} else {
2022-10-06 22:26:18 +08:00
final item = GroupItem(
row: row,
fieldInfo: fieldInfo,
2022-10-06 22:26:18 +08:00
);
2022-09-05 16:29:16 +08:00
controller.addGroupItem(group.groupId, item);
2022-08-16 17:13:56 +08:00
}
}
@override
void removeRow(GroupPB group, String rowId) {
2022-09-05 16:29:16 +08:00
controller.removeGroupItem(group.groupId, rowId);
2022-08-16 17:13:56 +08:00
}
@override
void updateRow(GroupPB group, RowPB row) {
final fieldInfo = fieldController.getField(group.fieldId);
if (fieldInfo == null) {
Log.warn("fieldInfo should not be null");
2022-09-05 09:24:05 +08:00
return;
}
2022-09-05 16:29:16 +08:00
controller.updateGroupItem(
group.groupId,
2022-10-06 22:26:18 +08:00
GroupItem(
row: row,
fieldInfo: fieldInfo,
2022-10-06 22:26:18 +08:00
),
);
2022-08-16 20:34:12 +08:00
}
@override
void addNewRow(GroupPB group, RowPB row, int? index) {
final fieldInfo = fieldController.getField(group.fieldId);
if (fieldInfo == null) {
Log.warn("fieldInfo should not be null");
2022-09-05 09:24:05 +08:00
return;
}
2022-10-06 22:26:18 +08:00
final item = GroupItem(
row: row,
fieldInfo: fieldInfo,
2022-10-06 22:26:18 +08:00
draggable: false,
);
if (index != null) {
2022-09-05 16:29:16 +08:00
controller.insertGroupItem(group.groupId, index, item);
} else {
2022-09-05 16:29:16 +08:00
controller.addGroupItem(group.groupId, item);
}
onNewColumnItem(group.groupId, row, index);
}
2022-08-16 17:13:56 +08:00
}
class BoardEditingRow {
2022-10-06 22:26:18 +08:00
GroupPB group;
RowPB row;
int? index;
BoardEditingRow({
2022-10-06 22:26:18 +08:00
required this.group,
required this.row,
required this.index,
});
}
2022-09-05 09:24:05 +08:00
2022-10-06 22:26:18 +08:00
class GroupData {
2022-09-05 09:24:05 +08:00
final GroupPB group;
final FieldInfo fieldInfo;
2022-10-06 22:26:18 +08:00
GroupData({
2022-09-05 09:24:05 +08:00
required this.group,
required this.fieldInfo,
2022-09-05 09:24:05 +08:00
});
CheckboxGroup? asCheckboxGroup() {
if (fieldType != FieldType.Checkbox) return null;
return CheckboxGroup(group);
}
FieldType get fieldType => fieldInfo.fieldType;
2022-09-05 09:24:05 +08:00
}
class CheckboxGroup {
final GroupPB group;
CheckboxGroup(this.group);
// Hardcode value: "Yes" that equal to the value defined in Rust
// pub const CHECK: &str = "Yes";
bool get isCheck => group.groupId == "Yes";
}