Skip to content

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.

fix(flutter_desktop): field info not updating in sort info (#6430)

Richard Shiueb0211be03c47
diff --git a/frontend/appflowy_flutter/integration_test/desktop/database/database_sort_test.dart b/frontend/appflowy_flutter/integration_test/desktop/database/database_sort_test.dart
index fcd71d1bc1..e09d8718be 100644
--- a/frontend/appflowy_flutter/integration_test/desktop/database/database_sort_test.dart
+++ b/frontend/appflowy_flutter/integration_test/desktop/database/database_sort_test.dart
@@ -1,3 +1,4 @@
+import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_editor.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pbenum.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
@@ -7,8 +8,8 @@ import '../../shared/database_test_op.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
- group('grid', () {
- testWidgets('add text sort', (tester) async {
+ group('grid sort:', () {
+ testWidgets('text sort', (tester) async {
await tester.openTestDatabase(v020GridFileName);
// create a sort
await tester.tapDatabaseSortButton();
@@ -37,7 +38,7 @@ void main() {
// open the sort menu and select order by descending
await tester.tapSortMenuInSettingBar();
- await tester.tapSortButtonByName('Name');
+ await tester.tapEditSortConditionButtonByFieldName('Name');
await tester.tapSortByDescending();
for (final (index, content) in <String>[
'E',
@@ -84,7 +85,7 @@ void main() {
await tester.pumpAndSettle();
});
- testWidgets('add checkbox sort', (tester) async {
+ testWidgets('checkbox', (tester) async {
await tester.openTestDatabase(v020GridFileName);
// create a sort
await tester.tapDatabaseSortButton();
@@ -111,7 +112,7 @@ void main() {
// open the sort menu and select order by descending
await tester.tapSortMenuInSettingBar();
- await tester.tapSortButtonByName('Done');
+ await tester.tapEditSortConditionButtonByFieldName('Done');
await tester.tapSortByDescending();
for (final (index, content) in <bool>[
true,
@@ -134,7 +135,7 @@ void main() {
await tester.pumpAndSettle();
});
- testWidgets('add number sort', (tester) async {
+ testWidgets('number', (tester) async {
await tester.openTestDatabase(v020GridFileName);
// create a sort
await tester.tapDatabaseSortButton();
@@ -162,7 +163,7 @@ void main() {
// open the sort menu and select order by descending
await tester.tapSortMenuInSettingBar();
- await tester.tapSortButtonByName('number');
+ await tester.tapEditSortConditionButtonByFieldName('number');
await tester.tapSortByDescending();
for (final (index, content) in <String>[
'12',
@@ -186,7 +187,7 @@ void main() {
await tester.pumpAndSettle();
});
- testWidgets('add checkbox and number sort', (tester) async {
+ testWidgets('checkbox and number', (tester) async {
await tester.openTestDatabase(v020GridFileName);
// create a sort
await tester.tapDatabaseSortButton();
@@ -194,7 +195,7 @@ void main() {
// open the sort menu and sort checkbox by descending
await tester.tapSortMenuInSettingBar();
- await tester.tapSortButtonByName('Done');
+ await tester.tapEditSortConditionButtonByFieldName('Done');
await tester.tapSortByDescending();
for (final (index, content) in <bool>[
true,
@@ -220,7 +221,7 @@ void main() {
FieldType.Number,
'number',
);
- await tester.tapSortButtonByName('number');
+ await tester.tapEditSortConditionButtonByFieldName('number');
await tester.tapSortByDescending();
// check checkbox cell order
@@ -273,7 +274,7 @@ void main() {
// open the sort menu and sort checkbox by descending
await tester.tapSortMenuInSettingBar();
- await tester.tapSortButtonByName('Done');
+ await tester.tapEditSortConditionButtonByFieldName('Done');
await tester.tapSortByDescending();
// add another sort, this time by number descending
@@ -282,7 +283,7 @@ void main() {
FieldType.Number,
'number',
);
- await tester.tapSortButtonByName('number');
+ await tester.tapEditSortConditionButtonByFieldName('number');
await tester.tapSortByDescending();
// check checkbox cell order
@@ -370,5 +371,101 @@ void main() {
);
}
});
+
+ testWidgets('edit field', (tester) async {
+ await tester.openTestDatabase(v020GridFileName);
+
+ // create a number sort
+ await tester.tapDatabaseSortButton();
+ await tester.tapCreateSortByFieldType(FieldType.Number, 'number');
+
+ // check the number cell order
+ for (final (index, content) in <String>[
+ '-2',
+ '-1',
+ '0.1',
+ '0.2',
+ '1',
+ '2',
+ '10',
+ '11',
+ '12',
+ '',
+ ].indexed) {
+ tester.assertCellContent(
+ rowIndex: index,
+ fieldType: FieldType.Number,
+ content: content,
+ );
+ }
+
+ final textCells = <String>[
+ 'B',
+ 'A',
+ 'C',
+ 'D',
+ 'E',
+ '',
+ '',
+ '',
+ '',
+ '',
+ ];
+ for (final (index, content) in textCells.indexed) {
+ tester.assertCellContent(
+ rowIndex: index,
+ fieldType: FieldType.RichText,
+ content: content,
+ );
+ }
+
+ // edit the name of the number field
+ await tester.tapGridFieldWithName('number');
+
+ await tester.renameField('hello world');
+ await tester.dismissFieldEditor();
+
+ await tester.tapGridFieldWithName('hello world');
+ await tester.dismissFieldEditor();
+
+ // expect name to be changed as well
+ await tester.tapSortMenuInSettingBar();
+ final sortItem = find.ancestor(
+ of: find.text('hello world'),
+ matching: find.byType(DatabaseSortItem),
+ );
+ expect(sortItem, findsOneWidget);
+
+ // change the field type of the field to checkbox
+ await tester.tapGridFieldWithName('hello world');
+ await tester.changeFieldTypeOfFieldWithName(
+ 'hello world',
+ FieldType.Checkbox,
+ );
+
+ // expect name to be changed as well
+ await tester.tapSortMenuInSettingBar();
+ expect(sortItem, findsOneWidget);
+
+ final newTextCells = <String>[
+ 'A',
+ 'B',
+ 'C',
+ 'D',
+ 'E',
+ '',
+ '',
+ '',
+ '',
+ '',
+ ];
+ for (final (index, content) in newTextCells.indexed) {
+ tester.assertCellContent(
+ rowIndex: index,
+ fieldType: FieldType.RichText,
+ content: content,
+ );
+ }
+ });
});
}
diff --git a/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart b/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart
index 1a7c7e5953..7bcbc0eb48 100644
--- a/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart
+++ b/frontend/appflowy_flutter/integration_test/shared/database_test_op.dart
@@ -1007,12 +1007,15 @@ extension AppFlowyDatabaseTest on WidgetTester {
}
/// Must call [tapSortMenuInSettingBar] first.
- Future<void> tapSortButtonByName(String name) async {
- final findSortItem = find.byWidgetPredicate(
- (widget) =>
- widget is DatabaseSortItem && widget.sortInfo.fieldInfo.name == name,
+ Future<void> tapEditSortConditionButtonByFieldName(String name) async {
+ final sortItem = find.descendant(
+ of: find.ancestor(
+ of: find.text(name),
+ matching: find.byType(DatabaseSortItem),
+ ),
+ matching: find.byType(SortConditionButton),
);
- await tapButton(findSortItem);
+ await tapButton(sortItem);
}
/// Must call [tapSortMenuInSettingBar] first.
@@ -1020,18 +1023,26 @@ extension AppFlowyDatabaseTest on WidgetTester {
(FieldType, String) from,
(FieldType, String) to,
) async {
- final fromSortItem = find.byWidgetPredicate(
- (widget) =>
- widget is DatabaseSortItem &&
- widget.sortInfo.fieldInfo.fieldType == from.$1 &&
- widget.sortInfo.fieldInfo.name == from.$2,
- );
- final toSortItem = find.byWidgetPredicate(
- (widget) =>
- widget is DatabaseSortItem &&
- widget.sortInfo.fieldInfo.fieldType == to.$1 &&
- widget.sortInfo.fieldInfo.name == to.$2,
- );
+ final fromSortItem = find.ancestor(
+ of: find.text(from.$2),
+ matching: find.byType(DatabaseSortItem),
+ );
+ final toSortItem = find.ancestor(
+ of: find.text(to.$2),
+ matching: find.byType(DatabaseSortItem),
+ );
+ // final fromSortItem = find.byWidgetPredicate(
+ // (widget) =>
+ // widget is DatabaseSortItem &&
+ // widget.sort.fieldInfo.fieldType == from.$1 &&
+ // widget.sort.fieldInfo.name == from.$2,
+ // );
+ // final toSortItem = find.byWidgetPredicate(
+ // (widget) =>
+ // widget is DatabaseSortItem &&
+ // widget.sort.fieldInfo.fieldType == to.$1 &&
+ // widget.sort.fieldInfo.name == to.$2,
+ // );
final dragElement = find.descendant(
of: fromSortItem,
matching: find.byType(ReorderableDragStartListener),
@@ -1040,16 +1051,13 @@ extension AppFlowyDatabaseTest on WidgetTester {
await pumpAndSettle(const Duration(milliseconds: 200));
}
- /// Must call [tapSortButtonByName] first.
+ /// Must call [tapEditSortConditionButtonByFieldName] first.
Future<void> tapSortByDescending() async {
await tapButton(
- find.descendant(
- of: find.byType(OrderPannelItem),
- matching: find.byWidgetPredicate(
- (widget) =>
- widget is FlowyText &&
- widget.text == LocaleKeys.grid_sort_descending.tr(),
- ),
+ find.byWidgetPredicate(
+ (widget) =>
+ widget is OrderPanelItem &&
+ widget.condition == SortConditionPB.Descending,
),
);
await sendKeyEvent(LogicalKeyboardKey.escape);
diff --git a/frontend/appflowy_flutter/lib/mobile/presentation/database/view/database_sort_bottom_sheet.dart b/frontend/appflowy_flutter/lib/mobile/presentation/database/view/database_sort_bottom_sheet.dart
index 69141e2cee..9d7df6c63b 100644
--- a/frontend/appflowy_flutter/lib/mobile/presentation/database/view/database_sort_bottom_sheet.dart
+++ b/frontend/appflowy_flutter/lib/mobile/presentation/database/view/database_sort_bottom_sheet.dart
@@ -2,10 +2,12 @@ import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/mobile/presentation/base/app_bar/app_bar_actions.dart';
import 'package:appflowy/mobile/presentation/widgets/widgets.dart';
+import 'package:appflowy/plugins/database/application/field/field_info.dart';
+import 'package:appflowy/plugins/database/application/field/sort_entities.dart';
import 'package:appflowy/plugins/database/grid/application/sort/sort_editor_bloc.dart';
-import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_info.dart';
import 'package:appflowy/util/field_type_extension.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
+import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
@@ -233,7 +235,7 @@ class _Overview extends StatelessWidget {
class _SortItem extends StatelessWidget {
const _SortItem({super.key, required this.sort});
- final SortInfo sort;
+ final DatabaseSort sort;
@override
Widget build(BuildContext context) {
@@ -289,9 +291,18 @@ class _SortItem extends StatelessWidget {
child: Row(
children: [
Expanded(
- child: FlowyText(
- sort.fieldInfo.name,
- overflow: TextOverflow.ellipsis,
+ child: BlocSelector<SortEditorBloc,
+ SortEditorState, FieldInfo?>(
+ selector: (state) =>
+ state.allFields.firstWhereOrNull(
+ (field) => field.id == sort.fieldId,
+ ),
+ builder: (context, field) {
+ return FlowyText(
+ field?.name ?? "",
+ overflow: TextOverflow.ellipsis,
+ );
+ },
),
),
const HSpace(6.0),
@@ -328,7 +339,7 @@ class _SortItem extends StatelessWidget {
children: [
Expanded(
child: FlowyText(
- sort.sortPB.condition.name,
+ sort.condition.name,
),
),
const HSpace(6.0),
@@ -354,7 +365,7 @@ class _SortItem extends StatelessWidget {
child: InkWell(
onTap: () => context
.read<SortEditorBloc>()
- .add(SortEditorEvent.deleteSort(sort)),
+ .add(SortEditorEvent.deleteSort(sort.sortId)),
// steal from the container LongClickReorderWidget thing
onLongPress: () {},
borderRadius: BorderRadius.circular(10),
@@ -386,14 +397,14 @@ class _SortDetail extends StatelessWidget {
return isCreatingNewSort
? const _SortDetailContent()
- : BlocSelector<SortEditorBloc, SortEditorState, SortInfo>(
+ : BlocSelector<SortEditorBloc, SortEditorState, DatabaseSort>(
selector: (state) => state.sorts.firstWhere(
- (sortInfo) =>
- sortInfo.sortId ==
+ (sort) =>
+ sort.sortId ==
context.read<MobileSortEditorCubit>().state.editingSortId,
),
- builder: (context, sortInfo) {
- return _SortDetailContent(sortInfo: sortInfo);
+ builder: (context, sort) {
+ return _SortDetailContent(sort: sort);
},
);
}
@@ -401,12 +412,12 @@ class _SortDetail extends StatelessWidget {
class _SortDetailContent extends StatelessWidget {
const _SortDetailContent({
- this.sortInfo,
+ this.sort,
});
- final SortInfo? sortInfo;
+ final DatabaseSort? sort;
- bool get isCreatingNewSort => sortInfo == null;
+ bool get isCreatingNewSort => sort == null;
@override
Widget build(BuildContext context) {
@@ -420,7 +431,7 @@ class _SortDetailContent extends StatelessWidget {
length: 2,
initialIndex: isCreatingNewSort
? 0
- : sortInfo!.sortPB.condition == SortConditionPB.Ascending
+ : sort!.condition == SortConditionPB.Ascending
? 0
: 1,
child: Container(
@@ -502,12 +513,12 @@ class _SortDetailContent extends StatelessWidget {
.state
.newSortFieldId ==
fieldInfo.id
- : sortInfo!.fieldId == fieldInfo.id;
+ : sort!.fieldId == fieldInfo.id;
final canSort =
fieldInfo.fieldType.canCreateSort && !fieldInfo.hasSort;
final beingEdited =
- !isCreatingNewSort && sortInfo!.fieldId == fieldInfo.id;
+ !isCreatingNewSort && sort!.fieldId == fieldInfo.id;
final enabled = canSort || beingEdited;
return FlowyOptionTile.checkbox(
@@ -547,7 +558,7 @@ class _SortDetailContent extends StatelessWidget {
} else {
context.read<SortEditorBloc>().add(
SortEditorEvent.editSort(
- sortId: sortInfo!.sortId,
+ sortId: sort!.sortId,
condition: newCondition,
),
);
@@ -560,7 +571,7 @@ class _SortDetailContent extends StatelessWidget {
} else {
context.read<SortEditorBloc>().add(
SortEditorEvent.editSort(
- sortId: sortInfo!.sortId,
+ sortId: sort!.sortId,
fieldId: newFieldId,
),
);
diff --git a/frontend/appflowy_flutter/lib/plugins/database/application/defines.dart b/frontend/appflowy_flutter/lib/plugins/database/application/defines.dart
index d700d3b7ba..0aa67b9775 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/application/defines.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/application/defines.dart
@@ -1,14 +1,13 @@
import 'dart:collection';
-// TODO(RS): remove dependency on presentation code
import 'package:appflowy/plugins/database/application/field/filter_entities.dart';
-import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_info.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/database_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'field/field_info.dart';
+import 'field/sort_entities.dart';
import 'row/row_cache.dart';
import 'row/row_service.dart';
@@ -16,7 +15,7 @@ part 'defines.freezed.dart';
typedef OnFieldsChanged = void Function(UnmodifiableListView<FieldInfo>);
typedef OnFiltersChanged = void Function(List<DatabaseFilter>);
-typedef OnSortsChanged = void Function(List<SortInfo>);
+typedef OnSortsChanged = void Function(List<DatabaseSort>);
typedef OnDatabaseChanged = void Function(DatabasePB);
typedef OnRowsCreated = void Function(List<RowId> rowIds);
diff --git a/frontend/appflowy_flutter/lib/plugins/database/application/field/field_controller.dart b/frontend/appflowy_flutter/lib/plugins/database/application/field/field_controller.dart
index f1b027df48..b9ac7c6bb4 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/application/field/field_controller.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/application/field/field_controller.dart
@@ -1,6 +1,5 @@
import 'dart:collection';
-import 'package:appflowy/plugins/database/application/field/filter_entities.dart';
import 'package:appflowy/plugins/database/application/row/row_cache.dart';
import 'package:appflowy/plugins/database/application/setting/setting_listener.dart';
import 'package:appflowy/plugins/database/domain/database_view_service.dart';
@@ -11,7 +10,6 @@ import 'package:appflowy/plugins/database/domain/filter_listener.dart';
import 'package:appflowy/plugins/database/domain/filter_service.dart';
import 'package:appflowy/plugins/database/domain/sort_listener.dart';
import 'package:appflowy/plugins/database/domain/sort_service.dart';
-import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_info.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
@@ -21,6 +19,8 @@ import 'package:flutter/foundation.dart';
import '../setting/setting_service.dart';
import 'field_info.dart';
+import 'filter_entities.dart';
+import 'sort_entities.dart';
class _GridFieldNotifier extends ChangeNotifier {
List<FieldInfo> _fieldInfos = [];
@@ -54,9 +54,9 @@ class _GridFilterNotifier extends ChangeNotifier {
}
class _GridSortNotifier extends ChangeNotifier {
- List<SortInfo> _sorts = [];
+ List<DatabaseSort> _sorts = [];
- set sorts(List<SortInfo> sorts) {
+ set sorts(List<DatabaseSort> sorts) {
_sorts = sorts;
notifyListeners();
}
@@ -65,14 +65,14 @@ class _GridSortNotifier extends ChangeNotifier {
notifyListeners();
}
- List<SortInfo> get sorts => _sorts;
+ List<DatabaseSort> get sorts => _sorts;
}
typedef OnReceiveUpdateFields = void Function(List<FieldInfo>);
typedef OnReceiveField = void Function(FieldInfo);
typedef OnReceiveFields = void Function(List<FieldInfo>);
typedef OnReceiveFilters = void Function(List<DatabaseFilter>);
-typedef OnReceiveSorts = void Function(List<SortInfo>);
+typedef OnReceiveSorts = void Function(List<DatabaseSort>);
class FieldController {
FieldController({required this.viewId})
@@ -133,7 +133,7 @@ class FieldController {
// Getters
List<FieldInfo> get fieldInfos => [..._fieldNotifier.fieldInfos];
List<DatabaseFilter> get filters => [..._filterNotifier?.filters ?? []];
- List<SortInfo> get sortInfos => [..._sortNotifier?.sorts ?? []];
+ List<DatabaseSort> get sorts => [..._sortNotifier?.sorts ?? []];
List<GroupSettingPB> get groupSettings =>
_groupConfigurationByFieldId.entries.map((e) => e.value).toList();
@@ -152,12 +152,12 @@ class FieldController {
.firstWhereOrNull((element) => element.fieldId == fieldId);
}
- SortInfo? getSortBySortId(String sortId) {
+ DatabaseSort? getSortBySortId(String sortId) {
return _sortNotifier?.sorts
.firstWhereOrNull((element) => element.sortId == sortId);
}
- SortInfo? getSortByFieldId(String fieldId) {
+ DatabaseSort? getSortByFieldId(String fieldId) {
return _sortNotifier?.sorts
.firstWhereOrNull((element) => element.fieldId == fieldId);
}
@@ -173,7 +173,7 @@ class FieldController {
result.fold(
(FilterChangesetNotificationPB changeset) {
_filterNotifier?.filters =
- _filterInfoListFromPBs(changeset.filters.items);
+ _filterListFromPBs(changeset.filters.items);
_updateFieldInfos();
},
(err) => Log.error(err),
@@ -185,76 +185,55 @@ class FieldController {
/// Listen for sort changes in the backend.
void _listenOnSortChanged() {
void deleteSortFromChangeset(
- List<SortInfo> newSortInfos,
+ List<DatabaseSort> newDatabaseSorts,
SortChangesetNotificationPB changeset,
) {
final deleteSortIds = changeset.deleteSorts.map((e) => e.id).toList();
if (deleteSortIds.isNotEmpty) {
- newSortInfos.retainWhere(
+ newDatabaseSorts.retainWhere(
(element) => !deleteSortIds.contains(element.sortId),
);
}
}
void insertSortFromChangeset(
- List<SortInfo> newSortInfos,
+ List<DatabaseSort> newDatabaseSorts,
SortChangesetNotificationPB changeset,
) {
for (final newSortPB in changeset.insertSorts) {
- final sortIndex = newSortInfos
+ final sortIndex = newDatabaseSorts
.indexWhere((element) => element.sortId == newSortPB.sort.id);
if (sortIndex == -1) {
- final fieldInfo = _findFieldInfo(
- fieldInfos: fieldInfos,
- fieldId: newSortPB.sort.fieldId,
- fieldType: null,
+ newDatabaseSorts.insert(
+ newSortPB.index,
+ DatabaseSort.fromPB(newSortPB.sort),
);
-
- if (fieldInfo != null) {
- newSortInfos.insert(
- newSortPB.index,
- SortInfo(sortPB: newSortPB.sort, fieldInfo: fieldInfo),
- );
- }
}
}
}
void updateSortFromChangeset(
- List<SortInfo> newSortInfos,
+ List<DatabaseSort> newDatabaseSorts,
SortChangesetNotificationPB changeset,
) {
for (final updatedSort in changeset.updateSorts) {
- final sortIndex = newSortInfos.indexWhere(
- (element) => element.sortId == updatedSort.id,
- );
- // Remove the old filter
- if (sortIndex != -1) {
- newSortInfos.removeAt(sortIndex);
- }
+ final newDatabaseSort = DatabaseSort.fromPB(updatedSort);
- final fieldInfo = _findFieldInfo(
- fieldInfos: fieldInfos,
- fieldId: updatedSort.fieldId,
- fieldType: null,
+ final sortIndex = newDatabaseSorts.indexWhere(
+ (element) => element.sortId == updatedSort.id,
);
- if (fieldInfo != null) {
- final newSortInfo = SortInfo(
- sortPB: updatedSort,
- fieldInfo: fieldInfo,
- );
- if (sortIndex != -1) {
- newSortInfos.insert(sortIndex, newSortInfo);
- } else {
- newSortInfos.add(newSortInfo);
- }
+ if (sortIndex != -1) {
+ newDatabaseSorts.removeAt(sortIndex);
+ newDatabaseSorts.insert(sortIndex, newDatabaseSort);
+ } else {
+ newDatabaseSorts.add(newDatabaseSort);
}
}
}
void updateFieldInfos(
- List<SortInfo> newSortInfos,
+ List<DatabaseSort> newDatabaseSorts,
SortChangesetNotificationPB changeset,
) {
final changedFieldIds = HashSet<String>.from([
@@ -273,7 +252,7 @@ class FieldController {
continue;
}
newFieldInfos[index] = newFieldInfos[index].copyWith(
- hasSort: newSortInfos.any((sort) => sort.fieldId == fieldId),
+ hasSort: newDatabaseSorts.any((sort) => sort.fieldId == fieldId),
);
}
@@ -287,13 +266,13 @@ class FieldController {
}
result.fold(
(SortChangesetNotificationPB changeset) {
- final List<SortInfo> newSortInfos = sortInfos;
- deleteSortFromChangeset(newSortInfos, changeset);
- insertSortFromChangeset(newSortInfos, changeset);
- updateSortFromChangeset(newSortInfos, changeset);
+ final List<DatabaseSort> newDatabaseSorts = sorts;
+ deleteSortFromChangeset(newDatabaseSorts, changeset);
+ insertSortFromChangeset(newDatabaseSorts, changeset);
+ updateSortFromChangeset(newDatabaseSorts, changeset);
- updateFieldInfos(newSortInfos, changeset);
- _sortNotifier?.sorts = newSortInfos;
+ updateFieldInfos(newDatabaseSorts, changeset);
+ _sortNotifier?.sorts = newDatabaseSorts;
},
(err) => Log.error(err),
);
@@ -479,9 +458,9 @@ class FieldController {
_groupConfigurationByFieldId[configuration.fieldId] = configuration;
}
- _filterNotifier?.filters = _filterInfoListFromPBs(setting.filters.items);
+ _filterNotifier?.filters = _filterListFromPBs(setting.filters.items);
- _sortNotifier?.sorts = _sortInfoListFromPBs(setting.sorts.items);
+ _sortNotifier?.sorts = _sortListFromPBs(setting.sorts.items);
_fieldSettings.clear();
_fieldSettings.addAll(setting.fieldSettings.items);
@@ -541,7 +520,7 @@ class FieldController {
return _filterBackendSvc.getAllFilters().then((result) {
return result.fold(
(filterPBs) {
- _filterNotifier?.filters = _filterInfoListFromPBs(filterPBs);
+ _filterNotifier?.filters = _filterListFromPBs(filterPBs);
return FlowyResult.success(null);
},
(err) => FlowyResult.failure(err),
@@ -554,7 +533,7 @@ class FieldController {
return _sortBackendSvc.getAllSorts().then((result) {
return result.fold(
(sortPBs) {
- _sortNotifier?.sorts = _sortInfoListFromPBs(sortPBs);
+ _sortNotifier?.sorts = _sortListFromPBs(sortPBs);
return FlowyResult.success(null);
},
(err) => FlowyResult.failure(err),
@@ -593,27 +572,13 @@ class FieldController {
}
/// Attach corresponding `FieldInfo`s to the `FilterPB`s
- List<DatabaseFilter> _filterInfoListFromPBs(List<FilterPB> filterPBs) {
+ List<DatabaseFilter> _filterListFromPBs(List<FilterPB> filterPBs) {
return filterPBs.map(DatabaseFilter.fromPB).toList();
}
/// Attach corresponding `FieldInfo`s to the `SortPB`s
- List<SortInfo> _sortInfoListFromPBs(List<SortPB> sortPBs) {
- SortInfo? getSortInfo(SortPB sortPB) {
- final fieldInfo = _findFieldInfo(
- fieldInfos: fieldInfos,
- fieldId: sortPB.fieldId,
- fieldType: null,
- );
- return fieldInfo != null
- ? SortInfo(sortPB: sortPB, fieldInfo: fieldInfo)
- : null;
- }
-
- return sortPBs
- .map((sortPB) => getSortInfo(sortPB))
- .whereType<SortInfo>()
- .toList();
+ List<DatabaseSort> _sortListFromPBs(List<SortPB> sortPBs) {
+ return sortPBs.map(DatabaseSort.fromPB).toList();
}
void addListener({
@@ -663,7 +628,7 @@ class FieldController {
if (listenWhen != null && listenWhen() == false) {
return;
}
- onSorts(sortInfos);
+ onSorts(sorts);
}
_sortCallbacks[onSorts] = callback;
@@ -802,15 +767,3 @@ class RowCacheDependenciesImpl extends RowFieldsDelegate with RowLifeCycle {
}
}
}
-
-FieldInfo? _findFieldInfo({
- required List<FieldInfo> fieldInfos,
- required String fieldId,
- required FieldType? fieldType,
-}) {
- return fieldInfos.firstWhereOrNull(
- (element) =>
- element.id == fieldId &&
- (fieldType == null || element.fieldType == fieldType),
- );
-}
diff --git a/frontend/appflowy_flutter/lib/plugins/database/application/field/sort_entities.dart b/frontend/appflowy_flutter/lib/plugins/database/application/field/sort_entities.dart
new file mode 100644
index 0000000000..a5aeaa3e28
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/database/application/field/sort_entities.dart
@@ -0,0 +1,22 @@
+import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
+import 'package:equatable/equatable.dart';
+
+final class DatabaseSort extends Equatable {
+ const DatabaseSort({
+ required this.sortId,
+ required this.fieldId,
+ required this.condition,
+ });
+
+ DatabaseSort.fromPB(SortPB sort)
+ : sortId = sort.id,
+ fieldId = sort.fieldId,
+ condition = sort.condition;
+
+ final String sortId;
+ final String fieldId;
+ final SortConditionPB condition;
+
+ @override
+ List<Object> get props => [sortId, fieldId, condition];
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/database/domain/sort_service.dart b/frontend/appflowy_flutter/lib/plugins/database/domain/sort_service.dart
index 12255afb7f..bdd8ea9716 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/domain/sort_service.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/domain/sort_service.dart
@@ -85,7 +85,6 @@ class SortBackendService {
}
Future<FlowyResult<void, FlowyError>> deleteSort({
- required String fieldId,
required String sortId,
}) {
final deleteSortPayload = DeleteSortPayloadPB.create()
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/application/grid_bloc.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/application/grid_bloc.dart
index 69be5f595c..9649f4491a 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/application/grid_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/grid/application/grid_bloc.dart
@@ -3,9 +3,9 @@ import 'dart:async';
import 'package:appflowy/plugins/database/application/defines.dart';
import 'package:appflowy/plugins/database/application/field/field_info.dart';
import 'package:appflowy/plugins/database/application/field/filter_entities.dart';
+import 'package:appflowy/plugins/database/application/field/sort_entities.dart';
import 'package:appflowy/plugins/database/application/row/row_cache.dart';
import 'package:appflowy/plugins/database/application/row/row_service.dart';
-import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_info.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
@@ -99,7 +99,7 @@ class GridBloc extends Bloc<GridEvent, GridState> {
state.copyWith(filters: filters),
);
},
- didReceveSorts: (List<SortInfo> sorts) {
+ didReceveSorts: (sorts) {
emit(
state.copyWith(
reorderable: sorts.isEmpty,
@@ -194,7 +194,7 @@ class GridEvent with _$GridEvent {
const factory GridEvent.didReceveFilters(List<DatabaseFilter> filters) =
_DidReceiveFilters;
- const factory GridEvent.didReceveSorts(List<SortInfo> sorts) =
+ const factory GridEvent.didReceveSorts(List<DatabaseSort> sorts) =
_DidReceiveSorts;
}
@@ -210,7 +210,7 @@ class GridState with _$GridState {
required LoadingState loadingState,
required bool reorderable,
required ChangedReason reason,
- required List<SortInfo> sorts,
+ required List<DatabaseSort> sorts,
required List<DatabaseFilter> filters,
required bool openRowDetail,
}) = _GridState;
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/application/sort/sort_editor_bloc.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/application/sort/sort_editor_bloc.dart
index 4d54f1aaac..37b0e37747 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/application/sort/sort_editor_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/grid/application/sort/sort_editor_bloc.dart
@@ -2,8 +2,8 @@ import 'dart:async';
import 'package:appflowy/plugins/database/application/field/field_controller.dart';
import 'package:appflowy/plugins/database/application/field/field_info.dart';
+import 'package:appflowy/plugins/database/application/field/sort_entities.dart';
import 'package:appflowy/plugins/database/domain/sort_service.dart';
-import 'package:appflowy/plugins/database/grid/presentation/widgets/sort/sort_info.dart';
import 'package:appflowy/util/field_type_extension.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
@@ -20,7 +20,7 @@ class SortEditorBloc extends Bloc<SortEditorEvent, SortEditorState> {
}) : _sortBackendSvc = SortBackendService(viewId: viewId),
super(
SortEditorState.initial(
- fieldController.sortInfos,
+ fieldController.sorts,
fieldController.fieldInfos,
),
) {
@@ -33,7 +33,7 @@ class SortEditorBloc extends Bloc<SortEditorEvent, SortEditorState> {
final FieldController fieldController;
void Function(List<FieldInfo>)? _onFieldFn;
- void Function(List<SortInfo>)? _onSortsFn;
+ void Function(List<DatabaseSort>)? _onSortsFn;
void _dispatch() {
on<SortEditorEvent>(
@@ -62,16 +62,16 @@ class SortEditorBloc extends Bloc<SortEditorEvent, SortEditorState> {
String? fieldId,
SortConditionPB? condition,
) async {
- final sortInfo = state.sorts
+ final sort = state.sorts
.firstWhereOrNull((element) => element.sortId == sortId);
- if (sortInfo == null) {
+ if (sort == null) {
return;
}
final result = await _sortBackendSvc.updateSort(
sortId: sortId,
- fieldId: fieldId ?? sortInfo.fieldId,
- condition: condition ?? sortInfo.sortPB.condition,
+ fieldId: fieldId ?? sort.fieldId,
+ condition: condition ?? sort.condition,
);
result.fold((l) => {}, (err) => Log.error(err));
},
@@ -79,13 +79,12 @@ class SortEditorBloc extends Bloc<SortEditorEvent, SortEditorState> {
final result = await _sortBackendSvc.deleteAllSorts();
result.fold((l) => {}, (err) => Log.error(err));
},
- didReceiveSorts: (List<SortInfo> sortInfos) {
- emit(state.copyWith(sorts: sortInfos));
+ didReceiveSorts: (sorts) {
+ emit(state.copyWith(sorts: sorts));
},
- deleteSort: (SortInfo sortInfo) async {
+ deleteSort: (sortId) async {
final result = await _sortBackendSvc.deleteSort(
- fieldId: sortInfo.fieldInfo.id,
- sortId: sortInfo.sortId,
+ sortId: sortId,
);
result.fold((l) => null, (err) => Log.error(err));
},
@@ -142,7 +141,7 @@ class SortEditorBloc extends Bloc<SortEditorEvent, SortEditorState> {
class SortEditorEvent with _$SortEditorEvent {
const factory SortEditorEvent.didReceiveFields(List<FieldInfo> fieldInfos) =
_DidReceiveFields;
- const factory SortEditorEvent.didReceiveSorts(List<SortInfo> sortInfos) =
+ const factory SortEditorEvent.didReceiveSorts(List<DatabaseSort> sorts) =
_DidReceiveSorts;
const factory SortEditorEvent.createSort({
required String fieldId,
@@ -155,20 +154,20 @@ class SortEditorEvent with _$SortEditorEvent {
}) = _EditSort;
const factory SortEditorEvent.reorderSort(int oldIndex, int newIndex) =
_ReorderSort;
- const factory SortEditorEvent.deleteSort(SortInfo sortInfo) = _DeleteSort;
+ const factory SortEditorEvent.deleteSort(String sortId) = _DeleteSort;
const factory SortEditorEvent.deleteAllSorts() = _DeleteAllSorts;
}
@freezed
class SortEditorState with _$SortEditorState {
const factory SortEditorState({
- required List<SortInfo> sorts,
+ required List<DatabaseSort> sorts,
required List<FieldInfo> allFields,
required List<FieldInfo> creatableFields,
}) = _SortEditorState;
factory SortEditorState.initial(
- List<SortInfo> sorts,
+ List<DatabaseSort> sorts,
List<FieldInfo> fields,
) {
return SortEditorState(
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/order_panel.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/order_panel.dart
index 0d1a0fe1d0..8b2fc247cb 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/order_panel.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/order_panel.dart
@@ -13,7 +13,7 @@ class OrderPanel extends StatelessWidget {
@override
Widget build(BuildContext context) {
final List<Widget> children = SortConditionPB.values.map((condition) {
- return OrderPannelItem(
+ return OrderPanelItem(
condition: condition,
onCondition: onCondition,
);
@@ -32,8 +32,8 @@ class OrderPanel extends StatelessWidget {
}
}
-class OrderPannelItem extends StatelessWidget {
- const OrderPannelItem({
+class OrderPanelItem extends StatelessWidget {
+ const OrderPanelItem({
super.key,
required this.condition,
required this.onCondition,
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_editor.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_editor.dart
index a08e0c4ae8..cf49b344d0 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_editor.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_editor.dart
@@ -1,5 +1,8 @@
import 'dart:io';
+import 'package:appflowy/plugins/database/application/field/field_info.dart';
+import 'package:appflowy/plugins/database/application/field/sort_entities.dart';
+import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:appflowy/generated/flowy_svgs.g.dart';
@@ -16,7 +19,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'create_sort_list.dart';
import 'order_panel.dart';
import 'sort_choice_button.dart';
-import 'sort_info.dart';
class SortEditor extends StatefulWidget {
const SortEditor({super.key});
@@ -38,16 +40,15 @@ class _SortEditorState extends State<SortEditor> {
Widget build(BuildContext context) {
return BlocBuilder<SortEditorBloc, SortEditorState>(
builder: (context, state) {
- final sortInfos = state.sorts;
return ReorderableListView.builder(
onReorder: (oldIndex, newIndex) => context
.read<SortEditorBloc>()
.add(SortEditorEvent.reorderSort(oldIndex, newIndex)),
itemCount: state.sorts.length,
itemBuilder: (context, index) => DatabaseSortItem(
- key: ValueKey(sortInfos[index].sortId),
+ key: ValueKey(state.sorts[index].sortId),
index: index,
- sortInfo: sortInfos[index],
+ sort: state.sorts[index],
popoverMutex: popoverMutex,
),
proxyDecorator: (child, index, animation) => Material(
@@ -96,12 +97,12 @@ class DatabaseSortItem extends StatelessWidget {
super.key,
required this.index,
required this.popoverMutex,
- required this.sortInfo,
+ required this.sort,
});
final int index;
final PopoverMutex popoverMutex;
- final SortInfo sortInfo;
+ final DatabaseSort sort;
@override
Widget build(BuildContext context) {
@@ -131,9 +132,16 @@ class DatabaseSortItem extends StatelessWidget {
fit: FlexFit.tight,
child: SizedBox(
height: 26,
- child: SortChoiceButton(
- text: sortInfo.fieldInfo.name,
- editable: false,
+ child: BlocSelector<SortEditorBloc, SortEditorState, FieldInfo?>(
+ selector: (state) => state.allFields.firstWhereOrNull(
+ (field) => field.id == sort.fieldId,
+ ),
+ builder: (context, field) {
+ return SortChoiceButton(
+ text: field?.name ?? "",
+ editable: false,
+ );
+ },
),
),
),
@@ -143,7 +151,7 @@ class DatabaseSortItem extends StatelessWidget {
child: SizedBox(
height: 26,
child: SortConditionButton(
- sortInfo: sortInfo,
+ sort: sort,
popoverMutex: popoverMutex,
),
),
@@ -154,7 +162,7 @@ class DatabaseSortItem extends StatelessWidget {
onPressed: () {
context
.read<SortEditorBloc>()
- .add(SortEditorEvent.deleteSort(sortInfo));
+ .add(SortEditorEvent.deleteSort(sort.sortId));
PopoverContainer.of(context).close();
},
hoverColor: AFThemeExtension.of(context).lightGreyHover,
@@ -260,11 +268,11 @@ class SortConditionButton extends StatefulWidget {
const SortConditionButton({
super.key,
required this.popoverMutex,
- required this.sortInfo,
+ required this.sort,
});
final PopoverMutex popoverMutex;
- final SortInfo sortInfo;
+ final DatabaseSort sort;
@override
State<SortConditionButton> createState() => _SortConditionButtonState();
@@ -286,7 +294,7 @@ class _SortConditionButtonState extends State<SortConditionButton> {
onCondition: (condition) {
context.read<SortEditorBloc>().add(
SortEditorEvent.editSort(
- sortId: widget.sortInfo.sortId,
+ sortId: widget.sort.sortId,
condition: condition,
),
);
@@ -295,7 +303,7 @@ class _SortConditionButtonState extends State<SortConditionButton> {
);
},
child: SortChoiceButton(
- text: widget.sortInfo.sortPB.condition.title,
+ text: widget.sort.condition.title,
rightIcon: FlowySvg(
FlowySvgs.arrow_down_s,
color: Theme.of(context).iconTheme.color,
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_info.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_info.dart
deleted file mode 100644
index eef8d700ba..0000000000
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_info.dart
+++ /dev/null
@@ -1,13 +0,0 @@
-import 'package:appflowy/plugins/database/application/field/field_info.dart';
-import 'package:appflowy_backend/protobuf/flowy-database2/sort_entities.pb.dart';
-
-class SortInfo {
- SortInfo({required this.sortPB, required this.fieldInfo});
-
- final SortPB sortPB;
- final FieldInfo fieldInfo;
-
- String get sortId => sortPB.id;
-
- String get fieldId => sortPB.fieldId;
-}
diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_menu.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_menu.dart
index da7d63f9c5..9355b5f697 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_menu.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/sort/sort_menu.dart
@@ -2,6 +2,7 @@ import 'dart:math' as math;
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/plugins/database/application/field/field_controller.dart';
+import 'package:appflowy/plugins/database/application/field/sort_entities.dart';
import 'package:appflowy/plugins/database/grid/application/sort/sort_editor_bloc.dart';
import 'package:appflowy_popover/appflowy_popover.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
@@ -12,7 +13,6 @@ import 'package:easy_localization/easy_localization.dart';
import 'sort_choice_button.dart';
import 'sort_editor.dart';
-import 'sort_info.dart';
class SortMenu extends StatelessWidget {
const SortMenu({
@@ -47,7 +47,7 @@ class SortMenu extends StatelessWidget {
child: const SortEditor(),
);
},
- child: SortChoiceChip(sortInfos: state.sorts),
+ child: SortChoiceChip(sorts: state.sorts),
);
},
),
@@ -58,11 +58,11 @@ class SortMenu extends StatelessWidget {
class SortChoiceChip extends StatelessWidget {
const SortChoiceChip({
super.key,
- required this.sortInfos,
+ required this.sorts,
this.onTap,
});
- final List<SortInfo> sortInfos;
+ final List<DatabaseSort> sorts;
final VoidCallback? onTap;
@override