Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
fix: field type option transform (#6451)
Richard Shiue99962dc3c673
diff --git a/frontend/appflowy_flutter/integration_test/desktop/database/database_cell_test.dart b/frontend/appflowy_flutter/integration_test/desktop/database/database_cell_test.dartindex 33ed0acf4f..7cc87d86e0 100644--- a/frontend/appflowy_flutter/integration_test/desktop/database/database_cell_test.dart+++ b/frontend/appflowy_flutter/integration_test/desktop/database/database_cell_test.dart@@ -299,7 +299,7 @@ void main() {await tester.dismissCellEditor();// Make sure the option is created and displayed in the cell- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: 'tag 1',);@@ -311,12 +311,12 @@ void main() {await tester.createOption(name: 'tag 2');await tester.dismissCellEditor();- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: 'tag 2',);- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsOneWidget,);@@ -328,12 +328,12 @@ void main() {await tester.selectOption(name: 'tag 1');await tester.dismissCellEditor();- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: 'tag 1',);- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsOneWidget,);@@ -345,7 +345,7 @@ void main() {await tester.selectOption(name: 'tag 1');await tester.dismissCellEditor();- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsNothing,);@@ -378,7 +378,7 @@ void main() {await tester.dismissCellEditor();// Make sure the option is created and displayed in the cell- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: tags.first,);@@ -393,13 +393,13 @@ void main() {await tester.dismissCellEditor();for (final tag in tags) {- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: tag,);}- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsNWidgets(4),);@@ -413,7 +413,7 @@ void main() {}await tester.dismissCellEditor();- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsNothing,);@@ -426,16 +426,16 @@ void main() {await tester.selectOption(name: tags[3]);await tester.dismissCellEditor();- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: tags[1],);- await tester.findSelectOptionWithNameInGrid(+ tester.findSelectOptionWithNameInGrid(rowIndex: 0,name: tags[3],);- await tester.assertNumberOfSelectedOptionsInGrid(+ tester.assertNumberOfSelectedOptionsInGrid(rowIndex: 0,matcher: findsNWidgets(2),);diff --git a/frontend/appflowy_flutter/integration_test/desktop/database/database_field_test.dart b/frontend/appflowy_flutter/integration_test/desktop/database/database_field_test.dartindex 3c1192bb04..8cfe024008 100644--- a/frontend/appflowy_flutter/integration_test/desktop/database/database_field_test.dart+++ b/frontend/appflowy_flutter/integration_test/desktop/database/database_field_test.dart@@ -15,7 +15,7 @@ import '../../shared/util.dart';void main() {IntegrationTestWidgetsFlutterBinding.ensureInitialized();- group('grid field editor:', () {+ group('grid edit field test:', () {testWidgets('rename existing field', (tester) async {await tester.initializeAppFlowy();await tester.tapAnonymousSignInButton();@@ -270,7 +270,6 @@ void main() {matching: find.byType(TextField),);await tester.enterText(inputField, text);- await tester.pumpAndSettle();await tester.testTextInput.receiveAction(TextInputAction.done);await tester.pumpAndSettle(const Duration(milliseconds: 500));@@ -382,5 +381,189 @@ void main() {// content: DateFormat('dd/MM/y hh:mm a').format(modified),// );// });++ testWidgets('select option transform', (tester) async {+ await tester.initializeAppFlowy();+ await tester.tapAnonymousSignInButton();++ await tester.createNewPageWithNameUnderParent(+ layout: ViewLayoutPB.Grid,+ );++ // invoke the field editor of existing Single-Select field Type+ await tester.tapGridFieldWithName('Type');+ await tester.tapEditFieldButton();++ // add some select options+ await tester.tapAddSelectOptionButton();+ for (final optionName in ['A', 'B', 'C']) {+ final inputField = find.descendant(+ of: find.byType(CreateOptionTextField),+ matching: find.byType(TextField),+ );+ await tester.enterText(inputField, optionName);+ await tester.testTextInput.receiveAction(TextInputAction.done);+ }+ await tester.dismissFieldEditor();++ // select A in first row's cell under the Type field+ await tester.tapCellInGrid(+ rowIndex: 0,+ fieldType: FieldType.SingleSelect,+ );+ await tester.selectOption(name: 'A');+ await tester.dismissCellEditor();+ tester.findSelectOptionWithNameInGrid(name: 'A', rowIndex: 0);++ await tester.changeFieldTypeOfFieldWithName('Type', FieldType.RichText);+ tester.assertCellContent(+ rowIndex: 0,+ fieldType: FieldType.RichText,+ content: "A",+ cellIndex: 1,+ );++ // add some random text in the second row+ await tester.editCell(+ rowIndex: 1,+ fieldType: FieldType.RichText,+ input: "random",+ cellIndex: 1,+ );+ tester.assertCellContent(+ rowIndex: 1,+ fieldType: FieldType.RichText,+ content: "random",+ cellIndex: 1,+ );++ await tester.changeFieldTypeOfFieldWithName(+ 'Type',+ FieldType.SingleSelect,+ );+ tester.findSelectOptionWithNameInGrid(name: 'A', rowIndex: 0);+ tester.assertNumberOfSelectedOptionsInGrid(+ rowIndex: 1,+ matcher: findsNothing,+ );++ // create a new field for testing+ await tester.createField(FieldType.RichText, name: 'Test');++ // edit the first 2 rows+ await tester.editCell(+ rowIndex: 0,+ fieldType: FieldType.RichText,+ input: "E,F",+ cellIndex: 1,+ );+ await tester.editCell(+ rowIndex: 1,+ fieldType: FieldType.RichText,+ input: "G",+ cellIndex: 1,+ );++ await tester.changeFieldTypeOfFieldWithName(+ 'Test',+ FieldType.MultiSelect,+ );+ tester.assertMultiSelectOption(contents: ['E', 'F'], rowIndex: 0);+ tester.assertMultiSelectOption(contents: ['G'], rowIndex: 1);++ await tester.tapCellInGrid(+ rowIndex: 2,+ fieldType: FieldType.MultiSelect,+ );+ await tester.selectOption(name: 'G');+ await tester.createOption(name: 'H');+ await tester.dismissCellEditor();+ tester.findSelectOptionWithNameInGrid(name: 'A', rowIndex: 0);+ tester.assertMultiSelectOption(contents: ['G', 'H'], rowIndex: 2);++ await tester.changeFieldTypeOfFieldWithName(+ 'Test',+ FieldType.RichText,+ );+ tester.assertCellContent(+ rowIndex: 2,+ fieldType: FieldType.RichText,+ content: "G,H",+ cellIndex: 1,+ );+ await tester.changeFieldTypeOfFieldWithName(+ 'Test',+ FieldType.MultiSelect,+ );++ tester.assertMultiSelectOption(contents: ['E', 'F'], rowIndex: 0);+ tester.assertMultiSelectOption(contents: ['G'], rowIndex: 1);+ tester.assertMultiSelectOption(contents: ['G', 'H'], rowIndex: 2);+ });++ testWidgets('date time transform', (tester) async {+ await tester.initializeAppFlowy();+ await tester.tapAnonymousSignInButton();++ await tester.createNewPageWithNameUnderParent(layout: ViewLayoutPB.Grid);+ await tester.scrollToRight(find.byType(GridPage));++ // create a date field+ await tester.createField(FieldType.DateTime);++ // edit the first date cell+ await tester.tapCellInGrid(rowIndex: 0, fieldType: FieldType.DateTime);+ await tester.toggleIncludeTime();+ final now = DateTime.now();+ await tester.selectDay(content: now.day);++ await tester.dismissCellEditor();++ tester.assertCellContent(+ rowIndex: 0,+ fieldType: FieldType.DateTime,+ content: DateFormat('MMM dd, y HH:mm').format(now),+ );++ await tester.changeFieldTypeOfFieldWithName(+ 'Date',+ FieldType.RichText,+ );+ tester.assertCellContent(+ rowIndex: 0,+ fieldType: FieldType.RichText,+ content: DateFormat('MMM dd, y HH:mm').format(now),+ cellIndex: 1,+ );++ await tester.editCell(+ rowIndex: 1,+ fieldType: FieldType.RichText,+ input: "Oct 5, 2024",+ cellIndex: 1,+ );+ tester.assertCellContent(+ rowIndex: 1,+ fieldType: FieldType.RichText,+ content: "Oct 5, 2024",+ cellIndex: 1,+ );++ await tester.changeFieldTypeOfFieldWithName(+ 'Date',+ FieldType.DateTime,+ );+ tester.assertCellContent(+ rowIndex: 0,+ fieldType: FieldType.DateTime,+ content: DateFormat('MMM dd, y').format(now),+ // content: DateFormat('MMM dd, y HH:mm').format(now),+ );+ tester.assertCellContent(+ rowIndex: 1,+ fieldType: FieldType.DateTime,+ content: "Oct 05, 2024",+ );+ });});}diff --git a/frontend/appflowy_flutter/integration_test/desktop/database/database_share_test.dart b/frontend/appflowy_flutter/integration_test/desktop/database/database_share_test.dartindex a04948b35e..2beb74a5f2 100644--- a/frontend/appflowy_flutter/integration_test/desktop/database/database_share_test.dart+++ b/frontend/appflowy_flutter/integration_test/desktop/database/database_share_test.dart@@ -115,7 +115,7 @@ void main() {[],];for (final (index, contents) in multiSelectCells.indexed) {- await tester.assertMultiSelectOption(+ tester.assertMultiSelectOption(rowIndex: index,contents: contents,);diff --git a/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart b/frontend/appflowy_flutter/integration_test/shared/database_test_op.dartindex b4f23072ae..81a2df8ce5 100644--- a/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart+++ b/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart@@ -247,10 +247,10 @@ extension AppFlowyDatabaseTest on WidgetTester {}}- Future<void> assertMultiSelectOption({+ void assertMultiSelectOption({required int rowIndex,required List<String> contents,- }) async {+ }) {final findCell = cellFinder(rowIndex, FieldType.MultiSelect);for (final content in contents) {if (content.isNotEmpty) {@@ -418,10 +418,10 @@ extension AppFlowyDatabaseTest on WidgetTester {await tapButton(option);}- Future<void> findSelectOptionWithNameInGrid({+ void findSelectOptionWithNameInGrid({required int rowIndex,required String name,- }) async {+ }) {final findRow = find.byType(GridRow);final option = find.byWidgetPredicate((widget) =>@@ -433,10 +433,10 @@ extension AppFlowyDatabaseTest on WidgetTester {expect(cell, findsOneWidget);}- Future<void> assertNumberOfSelectedOptionsInGrid({+ void assertNumberOfSelectedOptionsInGrid({required int rowIndex,required Matcher matcher,- }) async {+ }) {final findRow = find.byType(GridRow);final options = find.byWidgetPredicate(diff --git a/frontend/rust-lib/flowy-database2/src/event_handler.rs b/frontend/rust-lib/flowy-database2/src/event_handler.rsindex 2966220224..47633ee675 100644--- a/frontend/rust-lib/flowy-database2/src/event_handler.rs+++ b/frontend/rust-lib/flowy-database2/src/event_handler.rs@@ -334,7 +334,6 @@ pub(crate) async fn switch_to_field_handler(let database_editor = manager.get_database_editor_with_view_id(¶ms.view_id).await?;- let old_field = database_editor.get_field(¶ms.field_id).await;database_editor.switch_to_field_type(¶ms.view_id,@@ -344,22 +343,6 @@ pub(crate) async fn switch_to_field_handler().await?;- if let Some(new_type_option) = database_editor- .get_field(¶ms.field_id)- .await- .map(|field| field.get_any_type_option(field.field_type))- {- match (old_field, new_type_option) {- (Some(old_field), Some(new_type_option)) => {- database_editor- .update_field_type_option(¶ms.field_id, new_type_option, old_field)- .await?;- },- _ => {- tracing::warn!("Old field and the new type option should not be empty");- },- }- }Ok(())}diff --git a/frontend/rust-lib/flowy-database2/src/services/database/database_editor.rs b/frontend/rust-lib/flowy-database2/src/services/database/database_editor.rsindex c9ddda3247..c1b5652995 100644--- a/frontend/rust-lib/flowy-database2/src/services/database/database_editor.rs+++ b/frontend/rust-lib/flowy-database2/src/services/database/database_editor.rs@@ -483,48 +483,49 @@ impl DatabaseEditor {field_name: Option<String>,) -> FlowyResult<()> {let mut database = self.database.write().await;- let field = database.get_field(field_id);- match field {- None => {},- Some(field) => {- if field.is_primary {- return Err(FlowyError::new(- ErrorCode::Internal,- "Can not update primary field's field type",- ));- }+ if let Some(field) = database.get_field(field_id) {+ if field.is_primary {+ return Err(FlowyError::new(+ ErrorCode::Internal,+ "Can not update primary field's field type",+ ));+ }- let old_field_type = FieldType::from(field.field_type);- let old_type_option_data = field.get_any_type_option(old_field_type);- let new_type_option_data = field- .get_any_type_option(new_field_type)- .unwrap_or_else(|| default_type_option_data_from_type(new_field_type));-- let transformed_type_option = transform_type_option(- view_id,- field_id,- old_field_type,- new_field_type,- old_type_option_data,- new_type_option_data,- &mut database,- )- .await;+ let old_field_type = FieldType::from(field.field_type);+ let old_type_option_data = field.get_any_type_option(old_field_type);+ let new_type_option_data = field+ .get_any_type_option(new_field_type)+ .unwrap_or_else(|| default_type_option_data_from_type(new_field_type));- database.update_field(field_id, |update| {- update- .set_field_type(new_field_type.into())- .set_name_if_not_none(field_name)- .set_type_option(new_field_type.into(), Some(transformed_type_option));- });+ let transformed_type_option = transform_type_option(+ view_id,+ field_id,+ old_field_type,+ new_field_type,+ old_type_option_data,+ new_type_option_data,+ &mut database,+ )+ .await;- for view in self.database_views.editors().await {- view.v_did_update_field_type(field_id, new_field_type).await;- }- },+ database.update_field(field_id, |update| {+ update+ .set_field_type(new_field_type.into())+ .set_name_if_not_none(field_name)+ .set_type_option(new_field_type.into(), Some(transformed_type_option));+ });++ drop(database);++ for view in self.database_views.editors().await {+ view.v_did_update_field_type(field_id, new_field_type).await;+ }++ let database = self.database.read().await;++ notify_did_update_database_field(&database, field_id)?;}- notify_did_update_database_field(&database, field_id)?;Ok(())}diff --git a/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs b/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rsindex e5873507d4..3f60c322ee 100644--- a/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs+++ b/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs@@ -876,6 +876,16 @@ impl DatabaseViewEditor {.calculations_controller.did_receive_field_type_changed(field_id.to_owned(), new_field_type).await;+ if self.filter_controller.has_filters().await {+ let changeset = FilterChangeset::DeleteAllWithFieldId {+ field_id: field_id.to_string(),+ };+ let notification = self.filter_controller.apply_changeset(changeset).await;+ notify_did_update_filter(notification).await;+ }+ if self.is_grouping_field(field_id).await {+ let _ = self.v_group_by_field(field_id).await;+ }}/// Notifies the view's field type-option data is changed@@ -893,22 +903,13 @@ impl DatabaseViewEditor {.did_update_field_type_option(&field).await;- if old_field.field_type != field.field_type {- let changeset = FilterChangeset::DeleteAllWithFieldId {- field_id: field.id.clone(),- };- let notification = self.filter_controller.apply_changeset(changeset).await;- notify_did_update_filter(notification).await;- }-// If the id of the grouping field is equal to the updated field's id// and something critical changed, then we need to update the group settingif self.is_grouping_field(field_id).await- && (old_field.field_type != field.field_type- || matches!(- FieldType::from(field.field_type),- FieldType::SingleSelect | FieldType::MultiSelect- ))+ && matches!(+ FieldType::from(field.field_type),+ FieldType::SingleSelect | FieldType::MultiSelect+ ){self.v_group_by_field(field_id).await?;}diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/type_option_transform.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/type_option_transform.rsindex 4b63f7e9c1..ffe5bc8b95 100644--- a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/type_option_transform.rs+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/type_option_transform.rs@@ -3,7 +3,7 @@ use crate::services::cell::CellDataDecoder;use crate::services::field::{SelectTypeOptionSharedAction, TypeOption, CHECK, UNCHECK};use collab_database::database::Database;use collab_database::fields::select_type_option::{- SelectOption, SelectOptionColor, SelectOptionIds, SelectTypeOption,+ SelectOption, SelectOptionColor, SelectOptionIds, SelectTypeOption, SELECTION_IDS_SEPARATOR,};use collab_database::fields::text_type_option::RichTextTypeOption;use collab_database::fields::TypeOptionData;@@ -58,9 +58,16 @@ impl SelectOptionTypeOptionTransformHelper {);for (row_id, text_cell) in rows {let mut transformed_ids = Vec::new();- if let Some(option) = options.iter().find(|option| option.name == text_cell) {- transformed_ids.push(option.id.clone());- }+ let names = text_cell+ .split(SELECTION_IDS_SEPARATOR)+ .map(|name| name.to_string())+ .collect::<Vec<_>>();++ names.into_iter().for_each(|name| {+ if let Some(option) = options.iter().find(|option| option.name == name) {+ transformed_ids.push(option.id.clone());+ }+ });database.update_row(row_id, |row| {