Skip to content

Commit

Permalink
refactor: fetch all select options from field type option instead of …
Browse files Browse the repository at this point in the history
…cell (AppFlowy-IO#5122)

* refactor: fetch all options from type option

* chore: rustfmt

* ci: fix rust ci

* chore: fix clippy
  • Loading branch information
richardshiue authored Apr 12, 2024
1 parent 0ebcca6 commit 891fd16
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 74 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/rust_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

# 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: 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

- name: Checkout source code
uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -48,7 +50,8 @@ class SelectOptionCellEditorBloc
super(SelectOptionCellEditorState.initial(cellController)) {
_dispatch();
_startListening();
_loadOptions();
final loadedOptions = _loadAllOptions(cellController);
add(SelectOptionCellEditorEvent.didUpdateOptions(loadedOptions));
}

final SelectOptionCellBackendService _selectOptionService;
Expand All @@ -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,
),
);
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/rust-lib/event-integration/src/folder_event.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion frontend/rust-lib/event-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion frontend/rust-lib/flowy-search/src/folder/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 891fd16

Please sign in to comment.