chore: dart code cleanup (#4484)

* chore: use when instead of map in bloc event dispatch

* chore: dart code formatting, remove white space

* chore: remove unneeded hashmap for cell context

* fix: rebuild card content
This commit is contained in:
Richard Shiue
2024-01-25 12:26:18 +08:00
committed by GitHub
parent a1abcd7626
commit 6591d5de52
19 changed files with 88 additions and 99 deletions

View File

@ -10,7 +10,6 @@ import 'package:appflowy_popover/appflowy_popover.dart';
import 'package:collection/collection.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flowy_infra_ui/style_widget/hover.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@ -107,17 +106,6 @@ class _RowCardState extends State<RowCard> {
return BlocProvider.value(
value: _cardBloc,
child: BlocBuilder<CardBloc, CardState>(
buildWhen: (previous, current) {
// Rebuild when:
// 1. If the length of the cells is not the same or isEditing changed
if (previous.cells.length != current.cells.length ||
previous.isEditing != current.isEditing) {
return true;
}
// 2. the content of the cells changed
return !listEquals(previous.cells, current.cells);
},
builder: (context, state) =>
PlatformExtension.isMobile ? _mobile(state) : _desktop(state),
),

View File

@ -1,7 +1,6 @@
import 'dart:async';
import 'package:appflowy/plugins/database/application/cell/cell_controller.dart';
import 'package:appflowy/plugins/database/application/defines.dart';
import 'package:appflowy/plugins/database/application/field/field_controller.dart';
import 'package:appflowy/plugins/database/application/row/row_cache.dart';
import 'package:appflowy/plugins/database/application/row/row_listener.dart';
@ -103,16 +102,16 @@ class CardBloc extends Bloc<CardEvent, CardState> {
List<CellContext> _makeCells(
FieldController fieldController,
String? groupFieldId,
CellContextByFieldId cellMap,
List<CellContext> cellContexts,
) {
// Only show the non-hidden cells and cells that aren't of the grouping field
cellMap.removeWhere((_, cellContext) {
cellContexts.removeWhere((cellContext) {
final fieldInfo = fieldController.getField(cellContext.fieldId);
return fieldInfo == null ||
!fieldInfo.fieldSettings!.visibility.isVisibleState() ||
(groupFieldId != null && cellContext.fieldId == groupFieldId);
});
return cellMap.values.toList();
return cellContexts.toList();
}
@freezed

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart';
import 'package:appflowy/plugins/database/application/cell/select_option_cell_service.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option.pb.dart';
import 'package:dartz/dartz.dart';
@ -8,8 +9,6 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../../../application/cell/select_option_cell_service.dart';
part 'select_option_editor_bloc.freezed.dart';
class SelectOptionCellEditorBloc
@ -29,47 +28,47 @@ class SelectOptionCellEditorBloc
super(SelectOptionEditorState.initial(cellController)) {
on<SelectOptionEditorEvent>(
(event, emit) async {
await event.map(
initial: (_Initial value) async {
await event.when(
initial: () async {
_startListening();
await _loadOptions();
},
didReceiveOptions: (_DidReceiveOptions value) {
final result = _makeOptions(state.filter, value.options);
didReceiveOptions: (options, selectedOptions) {
final result = _makeOptions(state.filter, options);
emit(
state.copyWith(
allOptions: value.options,
allOptions: options,
options: result.options,
createOption: result.createOption,
selectedOptions: value.selectedOptions,
selectedOptions: selectedOptions,
),
);
},
newOption: (_NewOption value) async {
await _createOption(value.optionName);
newOption: (optionName) async {
await _createOption(optionName);
emit(
state.copyWith(
filter: none(),
),
);
},
deleteOption: (_DeleteOption value) async {
await _deleteOption([value.option]);
deleteOption: (option) async {
await _deleteOption([option]);
},
deleteAllOptions: (_DeleteAllOptions value) async {
deleteAllOptions: () async {
if (state.allOptions.isNotEmpty) {
await _deleteOption(state.allOptions);
}
},
updateOption: (_UpdateOption value) async {
await _updateOption(value.option);
updateOption: (option) async {
await _updateOption(option);
},
selectOption: (_SelectOption value) async {
await _selectOptionService.select(optionIds: [value.optionId]);
selectOption: (optionId) async {
await _selectOptionService.select(optionIds: [optionId]);
final selectedOption = [
...state.selectedOptions,
state.options.firstWhere(
(element) => element.id == value.optionId,
(element) => element.id == optionId,
),
];
emit(
@ -78,27 +77,27 @@ class SelectOptionCellEditorBloc
),
);
},
unSelectOption: (_UnSelectOption value) async {
await _selectOptionService.unSelect(optionIds: [value.optionId]);
unSelectOption: (optionId) async {
await _selectOptionService.unSelect(optionIds: [optionId]);
final selectedOptions = [...state.selectedOptions]
..removeWhere((e) => e.id == value.optionId);
..removeWhere((e) => e.id == optionId);
emit(
state.copyWith(
selectedOptions: selectedOptions,
),
);
},
trySelectOption: (_TrySelectOption value) {
_trySelectOption(value.optionName, emit);
trySelectOption: (optionName) {
_trySelectOption(optionName, emit);
},
selectMultipleOptions: (_SelectMultipleOptions value) {
if (value.optionNames.isNotEmpty) {
_selectMultipleOptions(value.optionNames);
selectMultipleOptions: (optionNames, remainder) {
if (optionNames.isNotEmpty) {
_selectMultipleOptions(optionNames);
}
_filterOption(value.remainder, emit);
_filterOption(remainder, emit);
},
filterOption: (_SelectOptionFilter value) {
_filterOption(value.optionName, emit);
filterOption: (optionName) {
_filterOption(optionName, emit);
},
);
},