refactor: fetch all select options from field type option instead of cell ()

* refactor: fetch all options from type option

* chore: rustfmt

* ci: fix rust ci

* chore: fix clippy
This commit is contained in:
Richard Shiue 2024-04-13 01:08:47 +08:00 committed by GitHub
parent 0ebcca63bb
commit 891fd16a0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 74 additions and 73 deletions
.github/workflows
frontend
appflowy_flutter/lib/plugins/database/application/cell
rust-lib
event-integration
src
tests/database/local_test
flowy-database2/src
entities/type_option_entities
services/field/type_options/selection_type_option
flowy-search/src/folder

@ -25,21 +25,21 @@ jobs:
test-on-ubuntu:
runs-on: ubuntu-latest
steps:
- name: Maximize build space
uses: easimon/maximize-build-space@master
with:
root-reserve-mb: 2048
swap-size-mb: 1024
remove-dotnet: 'true'
# - name: Maximize build space
# uses: easimon/maximize-build-space@master
# with:
# root-reserve-mb: 2048
# swap-size-mb: 1024
# remove-dotnet: 'true'
# the following step is required to avoid running out of space
- name: Maximize build space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
sudo docker image prune --all --force
# # the following step is required to avoid running out of space
# - name: Maximize build space
# run: |
# sudo rm -rf /usr/share/dotnet
# sudo rm -rf /opt/ghc
# sudo rm -rf "/usr/local/share/boost"
# sudo rm -rf "$AGENT_TOOLSDIRECTORY"
# sudo docker image prune --all --force
- name: Checkout source code
uses: actions/checkout@v4

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart';
import 'package:appflowy/plugins/database/application/field/type_option/select_type_option_actions.dart';
import 'package:appflowy/plugins/database/application/field/type_option/type_option_data_parser.dart';
import 'package:appflowy/plugins/database/domain/field_service.dart';
import 'package:appflowy/plugins/database/domain/select_option_cell_service.dart';
import 'package:appflowy_backend/log.dart';
@ -18,8 +19,9 @@ const String createSelectOptionSuggestionId =
class SelectOptionCellEditorBloc
extends Bloc<SelectOptionCellEditorEvent, SelectOptionCellEditorState> {
SelectOptionCellEditorBloc({required this.cellController})
: _selectOptionService = SelectOptionCellBackendService(
SelectOptionCellEditorBloc({
required this.cellController,
}) : _selectOptionService = SelectOptionCellBackendService(
viewId: cellController.viewId,
fieldId: cellController.fieldId,
rowId: cellController.rowId,
@ -48,7 +50,8 @@ class SelectOptionCellEditorBloc
super(SelectOptionCellEditorState.initial(cellController)) {
_dispatch();
_startListening();
_loadOptions();
final loadedOptions = _loadAllOptions(cellController);
add(SelectOptionCellEditorEvent.didUpdateOptions(loadedOptions));
}
final SelectOptionCellBackendService _selectOptionService;
@ -64,17 +67,19 @@ class SelectOptionCellEditorBloc
on<SelectOptionCellEditorEvent>(
(event, emit) async {
await event.when(
didReceiveOptions: (options, selectedOptions) {
final result = _getVisibleOptions(options);
didUpdateCell: (selectedOptions) {
emit(state.copyWith(selectedOptions: selectedOptions));
},
didUpdateOptions: (options) {
allOptions
..clear()
..addAll(options);
final result = _getVisibleOptions(options);
emit(
state.copyWith(
options: result.options,
createSelectOptionSuggestion:
result.createSelectOptionSuggestion,
selectedOptions: selectedOptions,
),
);
},
@ -166,37 +171,28 @@ class SelectOptionCellEditorBloc
void _startListening() {
_onCellChangedFn = cellController.addListener(
onCellChanged: (_) {
_loadOptions();
onCellChanged: (cellData) {
if (isClosed) {
Log.warn("Unexpecteded closing the bloc");
return;
}
add(
SelectOptionCellEditorEvent.didUpdateCell(
cellData == null ? [] : cellData.selectOptions,
),
);
},
onCellFieldChanged: (field) {
_loadOptions();
if (isClosed) {
Log.warn("Unexpecteded closing the bloc");
return;
}
final loadedOptions = _loadAllOptions(cellController);
add(SelectOptionCellEditorEvent.didUpdateOptions(loadedOptions));
},
);
}
void _loadOptions() {
if (isClosed) {
Log.warn("Unexpecteded closing the bloc");
return;
}
final cellData = cellController.getCellData();
if (cellData != null) {
add(
SelectOptionCellEditorEvent.didReceiveOptions(
cellData.options,
cellData.selectOptions,
),
);
} else {
add(
const SelectOptionCellEditorEvent.didReceiveOptions([], []),
);
}
}
Future<void> _createOption({
required String name,
required SelectOptionColorPB color,
@ -347,10 +343,12 @@ class SelectOptionCellEditorBloc
@freezed
class SelectOptionCellEditorEvent with _$SelectOptionCellEditorEvent {
const factory SelectOptionCellEditorEvent.didReceiveOptions(
List<SelectOptionPB> options,
const factory SelectOptionCellEditorEvent.didUpdateCell(
List<SelectOptionPB> selectedOptions,
) = _DidReceiveOptions;
) = _DidUpdateCell;
const factory SelectOptionCellEditorEvent.didUpdateOptions(
List<SelectOptionPB> options,
) = _DidUpdateOptions;
const factory SelectOptionCellEditorEvent.createOption() = _CreateOption;
const factory SelectOptionCellEditorEvent.selectOption(String optionId) =
_SelectOption;
@ -400,11 +398,12 @@ class SelectOptionCellEditorState with _$SelectOptionCellEditorState {
}) = _SelectOptionEditorState;
factory SelectOptionCellEditorState.initial(
SelectOptionCellController context,
SelectOptionCellController cellController,
) {
final data = context.getCellData(loadIfNotExist: false);
final allOptions = _loadAllOptions(cellController);
final data = cellController.getCellData();
return SelectOptionCellEditorState(
options: data?.options ?? [],
options: allOptions,
selectedOptions: data?.selectOptions ?? [],
createSelectOptionSuggestion: null,
focusedOptionId: null,
@ -432,3 +431,21 @@ class CreateSelectOptionSuggestion {
final String name;
final SelectOptionColorPB color;
}
List<SelectOptionPB> _loadAllOptions(
SelectOptionCellController cellController,
) {
if (cellController.fieldType == FieldType.SingleSelect) {
return cellController
.getTypeOption<SingleSelectTypeOptionPB>(
SingleSelectTypeOptionDataParser(),
)
.options;
} else {
return cellController
.getTypeOption<MultiSelectTypeOptionPB>(
MultiSelectTypeOptionDataParser(),
)
.options;
}
}

@ -161,7 +161,7 @@ class CellController<T, D> {
/// Return the TypeOptionPB that can be parsed into corresponding class using the [parser].
/// [PD] is the type that the parser return.
PD getTypeOption<PD, P extends TypeOptionParser>(P parser) {
PD getTypeOption<PD>(TypeOptionParser parser) {
return parser.fromBuffer(fieldInfo.field.typeOptionData);
}

@ -1,3 +1,5 @@
use std::sync::Arc;
use collab_folder::{FolderData, View};
use flowy_folder::entities::icon::UpdateViewIconPayloadPB;
use flowy_folder::event_map::FolderEvent;
@ -11,7 +13,6 @@ use flowy_user::entities::{
};
use flowy_user::errors::FlowyError;
use flowy_user::event_map::UserEvent;
use std::sync::Arc;
use flowy_user_pub::entities::Role;
use crate::event_builder::EventBuilder;

@ -14,7 +14,6 @@ use tokio::select;
use tokio::time::sleep;
use flowy_core::config::AppFlowyCoreConfig;
use flowy_core::integrate::log::create_log_filter;
use flowy_core::AppFlowyCore;
use flowy_notification::register_notification_sender;
use flowy_server::AppFlowyServer;

@ -504,7 +504,6 @@ async fn update_single_select_cell_event_test() {
let cell = test.get_cell(&grid_view.id, &row_id, &field_id).await;
let select_option_cell = SelectOptionCellDataPB::try_from(Bytes::from(cell.data)).unwrap();
assert_eq!(select_option_cell.options.len(), 1);
assert_eq!(select_option_cell.select_options.len(), 1);
}

@ -103,16 +103,10 @@ impl From<SelectOptionColorPB> for SelectOptionColor {
}
}
/// [SelectOptionCellDataPB] contains a list of user's selected options and a list of all the options
/// that the cell can use.
/// [SelectOptionCellDataPB] contains a list of user's selected options
#[derive(Clone, Debug, Default, ProtoBuf)]
pub struct SelectOptionCellDataPB {
/// The available options that the cell can use.
#[pb(index = 1)]
pub options: Vec<SelectOptionPB>,
/// The selected options for the cell.
#[pb(index = 2)]
pub select_options: Vec<SelectOptionPB>,
}

@ -47,18 +47,12 @@ pub enum SelectOptionColor {
#[derive(Debug)]
pub struct SelectOptionCellData {
pub options: Vec<SelectOption>,
pub select_options: Vec<SelectOption>,
}
impl From<SelectOptionCellData> for SelectOptionCellDataPB {
fn from(data: SelectOptionCellData) -> Self {
SelectOptionCellDataPB {
options: data
.options
.into_iter()
.map(|option| option.into())
.collect(),
select_options: data
.select_options
.into_iter()

@ -58,10 +58,7 @@ pub trait SelectTypeOptionSharedAction: Send + Sync {
select_options.truncate(number_of_max_options);
},
}
SelectOptionCellData {
options: self.options().clone(),
select_options,
}
SelectOptionCellData { select_options }
}
fn to_type_option_data(&self) -> TypeOptionData;

@ -170,7 +170,7 @@ impl FolderIndexManagerImpl {
Ok(writer) => Ok(writer),
Err(e) => {
tracing::error!("FolderIndexManager failed to lock index writer: {:?}", e);
return Err(FlowyError::folder_index_manager_unavailable());
Err(FlowyError::folder_index_manager_unavailable())
},
},
None => Err(FlowyError::folder_index_manager_unavailable()),