Skip to content

Commit

Permalink
test: rewrite bloc tests (AppFlowy-IO#6492)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardshiue authored Oct 7, 2024
1 parent a763304 commit 9ee39f4
Show file tree
Hide file tree
Showing 18 changed files with 1,359 additions and 737 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ class FieldController {
(FilterChangesetNotificationPB changeset) {
_filterNotifier?.filters =
_filterListFromPBs(changeset.filters.items);
_updateFieldInfos();
_fieldNotifier.fieldInfos =
_updateFieldInfos(_fieldNotifier.fieldInfos);
},
(err) => Log.error(err),
);
Expand Down Expand Up @@ -396,7 +397,7 @@ class FieldController {
(updatedFields, fieldInfos) =
await updateFields(changeset.updatedFields, fieldInfos);

_fieldNotifier.fieldInfos = fieldInfos;
_fieldNotifier.fieldInfos = _updateFieldInfos(fieldInfos);
for (final listener in _updatedFieldCallbacks.values) {
listener(updatedFields);
}
Expand Down Expand Up @@ -465,25 +466,22 @@ class FieldController {
_fieldSettings.clear();
_fieldSettings.addAll(setting.fieldSettings.items);

_updateFieldInfos();
_fieldNotifier.fieldInfos = _updateFieldInfos(_fieldNotifier.fieldInfos);
}

/// Attach sort, filter, group information and field settings to `FieldInfo`
void _updateFieldInfos() {
final List<FieldInfo> newFieldInfos = [];
for (final field in _fieldNotifier.fieldInfos) {
newFieldInfos.add(
field.copyWith(
fieldSettings: _fieldSettings
.firstWhereOrNull((setting) => setting.fieldId == field.id),
isGroupField: _groupConfigurationByFieldId[field.id] != null,
hasFilter: getFilterByFieldId(field.id) != null,
hasSort: getSortByFieldId(field.id) != null,
),
);
}

_fieldNotifier.fieldInfos = newFieldInfos;
List<FieldInfo> _updateFieldInfos(List<FieldInfo> fieldInfos) {
return fieldInfos
.map(
(field) => field.copyWith(
fieldSettings: _fieldSettings
.firstWhereOrNull((setting) => setting.fieldId == field.id),
isGroupField: _groupConfigurationByFieldId[field.id] != null,
hasFilter: getFilterByFieldId(field.id) != null,
hasSort: getSortByFieldId(field.id) != null,
),
)
.toList();
}

/// Load all of the fields. This is required when opening the database
Expand All @@ -506,7 +504,8 @@ class FieldController {
_loadAllFieldSettings(),
_loadSettings(),
]);
_updateFieldInfos();
_fieldNotifier.fieldInfos =
_updateFieldInfos(_fieldNotifier.fieldInfos);

return FlowyResult.success(null);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import 'package:appflowy/plugins/database/widgets/cell_editor/extension.dart';
import 'package:appflowy/util/int64_extension.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:equatable/equatable.dart';
import 'package:fixnum/fixnum.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/widgets.dart';

abstract class DatabaseFilter {
abstract class DatabaseFilter extends Equatable {
const DatabaseFilter({
required this.filterId,
required this.fieldId,
Expand Down Expand Up @@ -112,16 +113,18 @@ abstract class DatabaseFilter {
}

final class TextFilter extends DatabaseFilter {
const TextFilter({
TextFilter({
required super.filterId,
required super.fieldId,
required super.fieldType,
required this.condition,
required this.content,
});
required String content,
}) {
this.content = canAttachContent ? content : "";
}

final TextFilterConditionPB condition;
final String content;
late final String content;

@override
String get conditionName => condition.filterName;
Expand Down Expand Up @@ -182,19 +185,24 @@ final class TextFilter extends DatabaseFilter {
content: content ?? this.content,
);
}

@override
List<Object?> get props => [filterId, fieldId, condition, content];
}

final class NumberFilter extends DatabaseFilter {
const NumberFilter({
NumberFilter({
required super.filterId,
required super.fieldId,
required super.fieldType,
required this.condition,
required this.content,
});
required String content,
}) {
this.content = canAttachContent ? content : "";
}

final NumberFilterConditionPB condition;
final String content;
late final String content;

@override
String get conditionName => condition.filterName;
Expand Down Expand Up @@ -253,6 +261,9 @@ final class NumberFilter extends DatabaseFilter {
content: content ?? this.content,
);
}

@override
List<Object?> get props => [filterId, fieldId, condition, content];
}

final class CheckboxFilter extends DatabaseFilter {
Expand Down Expand Up @@ -289,6 +300,9 @@ final class CheckboxFilter extends DatabaseFilter {
condition: condition ?? this.condition,
);
}

@override
List<Object?> get props => [filterId, fieldId, condition];
}

final class ChecklistFilter extends DatabaseFilter {
Expand Down Expand Up @@ -325,19 +339,33 @@ final class ChecklistFilter extends DatabaseFilter {
condition: condition ?? this.condition,
);
}

@override
List<Object?> get props => [filterId, fieldId, condition];
}

final class SelectOptionFilter extends DatabaseFilter {
const SelectOptionFilter({
SelectOptionFilter({
required super.filterId,
required super.fieldId,
required super.fieldType,
required this.condition,
required this.optionIds,
});
required List<String> optionIds,
}) {
if (canAttachContent) {
if (fieldType == FieldType.SingleSelect &&
(condition == SelectOptionFilterConditionPB.OptionIs ||
condition == SelectOptionFilterConditionPB.OptionIsNot) &&
optionIds.isNotEmpty) {
this.optionIds.add(optionIds.first);
} else {
this.optionIds.addAll(optionIds);
}
}
}

final SelectOptionFilterConditionPB condition;
final List<String> optionIds;
final List<String> optionIds = [];

@override
String get conditionName => condition.i18n;
Expand Down Expand Up @@ -428,6 +456,9 @@ final class SelectOptionFilter extends DatabaseFilter {
field.fieldType == FieldType.SingleSelect
? const SingleSelectOptionFilterDelegateImpl()
: const MultiSelectOptionFilterDelegateImpl();

@override
List<Object?> get props => [filterId, fieldId, condition, optionIds];
}

enum DateTimeFilterCondition {
Expand Down Expand Up @@ -491,7 +522,8 @@ enum DateTimeFilterCondition {
}

static List<DateTimeFilterCondition> availableConditionsForFieldType(
FieldType fieldType,) {
FieldType fieldType,
) {
final result = [...values];
if (fieldType == FieldType.CreatedTime ||
fieldType == FieldType.LastEditedTime) {
Expand All @@ -504,20 +536,37 @@ enum DateTimeFilterCondition {
}

final class DateTimeFilter extends DatabaseFilter {
const DateTimeFilter({
DateTimeFilter({
required super.filterId,
required super.fieldId,
required super.fieldType,
required this.condition,
this.timestamp,
this.start,
this.end,
});
DateTime? timestamp,
DateTime? start,
DateTime? end,
}) {
if (canAttachContent) {
if (condition == DateFilterConditionPB.DateStartsBetween ||
condition == DateFilterConditionPB.DateEndsBetween) {
this.start = start;
this.end = end;
this.timestamp = null;
} else {
this.timestamp = timestamp;
this.start = null;
this.end = null;
}
} else {
this.timestamp = null;
this.start = null;
this.end = null;
}
}

final DateFilterConditionPB condition;
final DateTime? timestamp;
final DateTime? start;
final DateTime? end;
late final DateTime? timestamp;
late final DateTime? start;
late final DateTime? end;

@override
String get conditionName => condition.toCondition().filterName;
Expand Down Expand Up @@ -654,6 +703,10 @@ final class DateTimeFilter extends DatabaseFilter {
timestamp: timestamp,
);
}

@override
List<Object?> get props =>
[filterId, fieldId, condition, timestamp, start, end];
}

final class TimeFilter extends DatabaseFilter {
Expand Down Expand Up @@ -703,4 +756,7 @@ final class TimeFilter extends DatabaseFilter {
content: content ?? this.content,
);
}

@override
List<Object?> get props => [filterId, fieldId, condition, content];
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/import.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pbenum.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart';

class ImportPayload {
Expand All @@ -17,7 +16,7 @@ class ImportPayload {
}

class ImportBackendService {
static Future<FlowyResult<void, FlowyError>> importPages(
static Future<FlowyResult<RepeatedViewPB, FlowyError>> importPages(
String parentViewId,
List<ImportValuePayloadPB> values,
) async {
Expand Down
Loading

0 comments on commit 9ee39f4

Please sign in to comment.