refactor: Date & SingleSelect & Multi-Select type option

This commit is contained in:
appflowy 2022-05-28 15:30:15 +08:00
parent 8d766f3bb4
commit 62c322ab21
27 changed files with 382 additions and 485 deletions

View File

@ -16,7 +16,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart';
import 'package:app_flowy/workspace/application/grid/cell/select_option_service.dart';
import 'package:app_flowy/workspace/application/grid/field/field_service.dart';
import 'dart:convert' show utf8;
part 'cell_service.freezed.dart';
part 'data_loader.dart';
part 'context_builder.dart';

View File

@ -1,6 +1,6 @@
part of 'cell_service.dart';
typedef GridCellContext = _GridCellContext<Cell, String>;
typedef GridCellContext = _GridCellContext<String, String>;
typedef GridSelectOptionCellContext = _GridCellContext<SelectOptionCellData, String>;
typedef GridDateCellContext = _GridCellContext<DateCellData, DateCalData>;
@ -16,26 +16,33 @@ class GridCellContextBuilder {
_GridCellContext build() {
switch (_gridCell.field.fieldType) {
case FieldType.Checkbox:
final cellDataLoader = GridCellDataLoader(
gridCell: _gridCell,
parser: StringCellDataParser(),
);
return GridCellContext(
gridCell: _gridCell,
cellCache: _cellCache,
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
cellDataLoader: cellDataLoader,
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
);
case FieldType.DateTime:
final cellDataLoader = GridCellDataLoader(
gridCell: _gridCell,
parser: DateCellDataParser(),
);
return GridDateCellContext(
gridCell: _gridCell,
cellCache: _cellCache,
cellDataLoader: DateCellDataLoader(gridCell: _gridCell),
cellDataLoader: cellDataLoader,
cellDataPersistence: DateCellDataPersistence(gridCell: _gridCell),
);
case FieldType.Number:
final cellDataLoader = GridCellDataLoader(
gridCell: _gridCell,
config: const GridCellDataConfig(
reloadOnCellChanged: true,
reloadOnFieldChanged: true,
),
parser: StringCellDataParser(),
config: const GridCellDataConfig(reloadOnCellChanged: true, reloadOnFieldChanged: true),
);
return GridCellContext(
gridCell: _gridCell,
@ -44,18 +51,28 @@ class GridCellContextBuilder {
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
);
case FieldType.RichText:
final cellDataLoader = GridCellDataLoader(
gridCell: _gridCell,
parser: StringCellDataParser(),
);
return GridCellContext(
gridCell: _gridCell,
cellCache: _cellCache,
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
cellDataLoader: cellDataLoader,
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
);
case FieldType.MultiSelect:
case FieldType.SingleSelect:
final cellDataLoader = GridCellDataLoader(
gridCell: _gridCell,
parser: SelectOptionCellDataParser(),
config: const GridCellDataConfig(reloadOnFieldChanged: true),
);
return GridSelectOptionCellContext(
gridCell: _gridCell,
cellCache: _cellCache,
cellDataLoader: SelectOptionCellDataLoader(gridCell: _gridCell),
cellDataLoader: cellDataLoader,
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
);
default:
@ -131,10 +148,7 @@ class _GridCellContext<T, D> extends Equatable {
}
onCellChangedFn() {
final value = _cellDataNotifier.value;
if (value is T) {
onCellChanged(value);
}
onCellChanged(_cellDataNotifier.value as T);
if (cellDataLoader.config.reloadOnCellChanged) {
_loadData();

View File

@ -4,8 +4,8 @@ abstract class IGridCellDataConfig {
// The cell data will reload if it receives the field's change notification.
bool get reloadOnFieldChanged;
// The cell data will reload if it receives the cell's change notification.
// For example, the number cell should be reloaded after user input the number.
// When the reloadOnCellChanged is true, it will load the cell data after user input.
// For example: The number cell reload the cell data that carries the format
// user input: 12
// cell display: $12
bool get reloadOnCellChanged;
@ -31,63 +31,44 @@ abstract class IGridCellDataLoader<T> {
}
abstract class ICellDataParser<T> {
T? parserData();
T? parserData(List<int> data);
}
class GridCellDataLoader extends IGridCellDataLoader<String> {
class GridCellDataLoader<T> extends IGridCellDataLoader<T> {
final CellService service = CellService();
final GridCell gridCell;
final ICellDataParser<T> parser;
@override
final IGridCellDataConfig config;
GridCellDataLoader({
required this.gridCell,
required this.parser,
this.config = const GridCellDataConfig(),
});
@override
Future<String> loadData() {
Future<T?> loadData() {
final fut = service.getCell(
gridId: gridCell.gridId,
fieldId: gridCell.field.id,
rowId: gridCell.rowId,
);
return fut.then((result) {
return result.fold((Cell data) => data.content, (err) {
Log.error(err);
return "";
});
});
}
}
class DateCellDataLoader extends IGridCellDataLoader<DateCellData> {
final GridCell gridCell;
final IGridCellDataConfig _config;
DateCellDataLoader({
required this.gridCell,
}) : _config = const GridCellDataConfig(reloadOnFieldChanged: true);
@override
IGridCellDataConfig get config => _config;
@override
Future<DateCellData?> loadData() {
final payload = CellIdentifierPayload.create()
..gridId = gridCell.gridId
..fieldId = gridCell.field.id
..rowId = gridCell.rowId;
return GridEventGetDateCellData(payload).send().then((result) {
return result.fold(
(data) => data,
(err) {
Log.error(err);
return fut.then(
(result) => result.fold((Cell cell) {
try {
return parser.parserData(cell.data);
} catch (e, s) {
Log.error('$parser parser cellData failed, $e');
Log.error('Stack trace \n $s');
return null;
},
);
});
}
}, (err) {
Log.error(err);
return null;
}),
);
}
}
@ -113,3 +94,30 @@ class SelectOptionCellDataLoader extends IGridCellDataLoader<SelectOptionCellDat
@override
IGridCellDataConfig get config => const GridCellDataConfig(reloadOnFieldChanged: true);
}
class StringCellDataParser implements ICellDataParser<String> {
@override
String? parserData(List<int> data) {
return utf8.decode(data);
}
}
class DateCellDataParser implements ICellDataParser<DateCellData> {
@override
DateCellData? parserData(List<int> data) {
if (data.isEmpty) {
return null;
}
return DateCellData.fromBuffer(data);
}
}
class SelectOptionCellDataParser implements ICellDataParser<SelectOptionCellData> {
@override
SelectOptionCellData? parserData(List<int> data) {
if (data.isEmpty) {
return null;
}
return SelectOptionCellData.fromBuffer(data);
}
}

View File

@ -21,7 +21,6 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
},
didReceiveOptions: (_DidReceiveOptions value) {
emit(state.copyWith(
options: value.options,
selectedOptions: value.selectedOptions,
));
},
@ -45,7 +44,6 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
onCellChanged: ((selectOptionContext) {
if (!isClosed) {
add(SelectOptionCellEvent.didReceiveOptions(
selectOptionContext.options,
selectOptionContext.selectOptions,
));
}
@ -58,7 +56,6 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
class SelectOptionCellEvent with _$SelectOptionCellEvent {
const factory SelectOptionCellEvent.initial() = _InitialCell;
const factory SelectOptionCellEvent.didReceiveOptions(
List<SelectOption> options,
List<SelectOption> selectedOptions,
) = _DidReceiveOptions;
}
@ -66,7 +63,6 @@ class SelectOptionCellEvent with _$SelectOptionCellEvent {
@freezed
class SelectOptionCellState with _$SelectOptionCellState {
const factory SelectOptionCellState({
required List<SelectOption> options,
required List<SelectOption> selectedOptions,
}) = _SelectOptionCellState;
@ -74,7 +70,6 @@ class SelectOptionCellState with _$SelectOptionCellState {
final data = context.getCellData();
return SelectOptionCellState(
options: data?.options ?? [],
selectedOptions: data?.selectOptions ?? [],
);
}

View File

@ -24,6 +24,7 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
await event.map(
initial: (_Initial value) async {
_startListening();
_loadOptions();
},
didReceiveOptions: (_DidReceiveOptions value) {
final result = _makeOptions(state.filter, value.options);
@ -106,6 +107,26 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
));
}
void _loadOptions() {
final selectionCellData = cellContext.getCellData();
if (selectionCellData == null) {
final service = SelectOptionService(gridCell: cellContext.gridCell);
service.getOpitonContext().then((result) {
return result.fold(
(data) {
if (!isClosed) {
add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
}
},
(err) {
Log.error(err);
return null;
},
);
});
}
}
_MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) {
final List<SelectOption> options = List.from(allOptions);
Option<String> createOption = filter;

View File

@ -13,19 +13,16 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
}) : super(TextCellState.initial(cellContext)) {
on<TextCellEvent>(
(event, emit) async {
await event.map(
initial: (_InitialCell value) async {
await event.when(
initial: () async {
_startListening();
},
updateText: (_UpdateText value) {
cellContext.saveCellData(value.text);
emit(state.copyWith(content: value.text));
updateText: (text) {
cellContext.saveCellData(text);
emit(state.copyWith(content: text));
},
didReceiveCellData: (_DidReceiveCellData value) {
emit(state.copyWith(content: value.cellData.cell?.content ?? ""));
},
didReceiveCellUpdate: (_DidReceiveCellUpdate value) {
emit(state.copyWith(content: value.cellContent));
didReceiveCellUpdate: (content) {
emit(state.copyWith(content: content));
},
);
},
@ -56,7 +53,6 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
@freezed
class TextCellEvent with _$TextCellEvent {
const factory TextCellEvent.initial() = _InitialCell;
const factory TextCellEvent.didReceiveCellData(GridCell cellData) = _DidReceiveCellData;
const factory TextCellEvent.didReceiveCellUpdate(String cellContent) = _DidReceiveCellUpdate;
const factory TextCellEvent.updateText(String text) = _UpdateText;
}

View File

@ -392,20 +392,3 @@ class GridEventUpdateDateCell {
}
}
class GridEventGetDateCellData {
CellIdentifierPayload request;
GridEventGetDateCellData(this.request);
Future<Either<DateCellData, FlowyError>> send() {
final request = FFIRequest.create()
..event = GridEvent.GetDateCellData.toString()
..payload = requestToBytes(this.request);
return Dispatch.asyncRequest(request)
.then((bytesResult) => bytesResult.fold(
(okBytes) => left(DateCellData.fromBuffer(okBytes)),
(errBytes) => right(FlowyError.fromBuffer(errBytes)),
));
}
}

View File

@ -1366,24 +1366,19 @@ class GridBlock extends $pb.GeneratedMessage {
class Cell extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Cell', createEmptyInstance: create)
..aOS(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'fieldId')
..aOS(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'content')
..a<$core.List<$core.int>>(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'data', $pb.PbFieldType.OY)
..a<$core.List<$core.int>>(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'data', $pb.PbFieldType.OY)
..hasRequiredFields = false
;
Cell._() : super();
factory Cell({
$core.String? fieldId,
$core.String? content,
$core.List<$core.int>? data,
}) {
final _result = create();
if (fieldId != null) {
_result.fieldId = fieldId;
}
if (content != null) {
_result.content = content;
}
if (data != null) {
_result.data = data;
}
@ -1420,22 +1415,13 @@ class Cell extends $pb.GeneratedMessage {
void clearFieldId() => clearField(1);
@$pb.TagNumber(2)
$core.String get content => $_getSZ(1);
$core.List<$core.int> get data => $_getN(1);
@$pb.TagNumber(2)
set content($core.String v) { $_setString(1, v); }
set data($core.List<$core.int> v) { $_setBytes(1, v); }
@$pb.TagNumber(2)
$core.bool hasContent() => $_has(1);
$core.bool hasData() => $_has(1);
@$pb.TagNumber(2)
void clearContent() => clearField(2);
@$pb.TagNumber(3)
$core.List<$core.int> get data => $_getN(2);
@$pb.TagNumber(3)
set data($core.List<$core.int> v) { $_setBytes(2, v); }
@$pb.TagNumber(3)
$core.bool hasData() => $_has(2);
@$pb.TagNumber(3)
void clearData() => clearField(3);
void clearData() => clearField(2);
}
class RepeatedCell extends $pb.GeneratedMessage {

View File

@ -289,13 +289,12 @@ const Cell$json = const {
'1': 'Cell',
'2': const [
const {'1': 'field_id', '3': 1, '4': 1, '5': 9, '10': 'fieldId'},
const {'1': 'content', '3': 2, '4': 1, '5': 9, '10': 'content'},
const {'1': 'data', '3': 3, '4': 1, '5': 12, '10': 'data'},
const {'1': 'data', '3': 2, '4': 1, '5': 12, '10': 'data'},
],
};
/// Descriptor for `Cell`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List cellDescriptor = $convert.base64Decode('CgRDZWxsEhkKCGZpZWxkX2lkGAEgASgJUgdmaWVsZElkEhgKB2NvbnRlbnQYAiABKAlSB2NvbnRlbnQSEgoEZGF0YRgDIAEoDFIEZGF0YQ==');
final $typed_data.Uint8List cellDescriptor = $convert.base64Decode('CgRDZWxsEhkKCGZpZWxkX2lkGAEgASgJUgdmaWVsZElkEhIKBGRhdGEYAiABKAxSBGRhdGE=');
@$core.Deprecated('Use repeatedCellDescriptor instead')
const RepeatedCell$json = const {
'1': 'RepeatedCell',

View File

@ -33,7 +33,6 @@ class GridEvent extends $pb.ProtobufEnum {
static const GridEvent UpdateCell = GridEvent._(71, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'UpdateCell');
static const GridEvent UpdateSelectOptionCell = GridEvent._(72, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'UpdateSelectOptionCell');
static const GridEvent UpdateDateCell = GridEvent._(80, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'UpdateDateCell');
static const GridEvent GetDateCellData = GridEvent._(90, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'GetDateCellData');
static const $core.List<GridEvent> values = <GridEvent> [
GetGridData,
@ -59,7 +58,6 @@ class GridEvent extends $pb.ProtobufEnum {
UpdateCell,
UpdateSelectOptionCell,
UpdateDateCell,
GetDateCellData,
];
static final $core.Map<$core.int, GridEvent> _byValue = $pb.ProtobufEnum.initByValue(values);

View File

@ -35,9 +35,8 @@ const GridEvent$json = const {
const {'1': 'UpdateCell', '2': 71},
const {'1': 'UpdateSelectOptionCell', '2': 72},
const {'1': 'UpdateDateCell', '2': 80},
const {'1': 'GetDateCellData', '2': 90},
],
};
/// Descriptor for `GridEvent`. Decode as a `google.protobuf.EnumDescriptorProto`.
final $typed_data.Uint8List gridEventDescriptor = $convert.base64Decode('CglHcmlkRXZlbnQSDwoLR2V0R3JpZERhdGEQABIRCg1HZXRHcmlkQmxvY2tzEAESDQoJR2V0RmllbGRzEAoSDwoLVXBkYXRlRmllbGQQCxIZChVVcGRhdGVGaWVsZFR5cGVPcHRpb24QDBIPCgtJbnNlcnRGaWVsZBANEg8KC0RlbGV0ZUZpZWxkEA4SEQoNU3dpdGNoVG9GaWVsZBAUEhIKDkR1cGxpY2F0ZUZpZWxkEBUSDAoITW92ZUl0ZW0QFhIWChJHZXRGaWVsZFR5cGVPcHRpb24QFxIZChVDcmVhdGVGaWVsZFR5cGVPcHRpb24QGBITCg9OZXdTZWxlY3RPcHRpb24QHhIbChdHZXRTZWxlY3RPcHRpb25DZWxsRGF0YRAfEhYKElVwZGF0ZVNlbGVjdE9wdGlvbhAgEg0KCUNyZWF0ZVJvdxAyEgoKBkdldFJvdxAzEg0KCURlbGV0ZVJvdxA0EhAKDER1cGxpY2F0ZVJvdxA1EgsKB0dldENlbGwQRhIOCgpVcGRhdGVDZWxsEEcSGgoWVXBkYXRlU2VsZWN0T3B0aW9uQ2VsbBBIEhIKDlVwZGF0ZURhdGVDZWxsEFASEwoPR2V0RGF0ZUNlbGxEYXRhEFo=');
final $typed_data.Uint8List gridEventDescriptor = $convert.base64Decode('CglHcmlkRXZlbnQSDwoLR2V0R3JpZERhdGEQABIRCg1HZXRHcmlkQmxvY2tzEAESDQoJR2V0RmllbGRzEAoSDwoLVXBkYXRlRmllbGQQCxIZChVVcGRhdGVGaWVsZFR5cGVPcHRpb24QDBIPCgtJbnNlcnRGaWVsZBANEg8KC0RlbGV0ZUZpZWxkEA4SEQoNU3dpdGNoVG9GaWVsZBAUEhIKDkR1cGxpY2F0ZUZpZWxkEBUSDAoITW92ZUl0ZW0QFhIWChJHZXRGaWVsZFR5cGVPcHRpb24QFxIZChVDcmVhdGVGaWVsZFR5cGVPcHRpb24QGBITCg9OZXdTZWxlY3RPcHRpb24QHhIbChdHZXRTZWxlY3RPcHRpb25DZWxsRGF0YRAfEhYKElVwZGF0ZVNlbGVjdE9wdGlvbhAgEg0KCUNyZWF0ZVJvdxAyEgoKBkdldFJvdxAzEg0KCURlbGV0ZVJvdxA0EhAKDER1cGxpY2F0ZVJvdxA1EgsKB0dldENlbGwQRhIOCgpVcGRhdGVDZWxsEEcSGgoWVXBkYXRlU2VsZWN0T3B0aW9uQ2VsbBBIEhIKDlVwZGF0ZURhdGVDZWxsEFA=');

View File

@ -263,27 +263,6 @@ pub(crate) async fn update_cell_handler(
Ok(())
}
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn get_date_cell_data_handler(
data: Data<CellIdentifierPayload>,
manager: AppData<Arc<GridManager>>,
) -> DataResult<DateCellData, FlowyError> {
let params: CellIdentifier = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
match editor.get_field_meta(&params.field_id).await {
None => {
tracing::error!("Can't find the corresponding field with id: {}", params.field_id);
data_result(DateCellData::default())
}
Some(field_meta) => {
let cell_meta = editor.get_cell_meta(&params.row_id, &params.field_id).await?;
let type_option = DateTypeOption::from(&field_meta);
let date_cell_data = type_option.make_date_cell_data(&cell_meta)?;
data_result(date_cell_data)
}
}
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn new_select_option_handler(
data: Data<CreateSelectOptionPayload>,

View File

@ -29,7 +29,6 @@ pub fn create(grid_manager: Arc<GridManager>) -> Module {
// Cell
.event(GridEvent::GetCell, get_cell_handler)
.event(GridEvent::UpdateCell, update_cell_handler)
.event(GridEvent::GetDateCellData, get_date_cell_data_handler)
// SelectOption
.event(GridEvent::NewSelectOption, new_select_option_handler)
.event(GridEvent::UpdateSelectOption, update_select_option_handler)
@ -112,7 +111,4 @@ pub enum GridEvent {
#[event(input = "DateChangesetPayload")]
UpdateDateCell = 80,
#[event(input = "CellIdentifierPayload", output = "DateCellData")]
GetDateCellData = 90,
}

View File

@ -48,7 +48,6 @@ pub enum GridEvent {
UpdateCell = 71,
UpdateSelectOptionCell = 72,
UpdateDateCell = 80,
GetDateCellData = 90,
}
impl ::protobuf::ProtobufEnum for GridEvent {
@ -81,7 +80,6 @@ impl ::protobuf::ProtobufEnum for GridEvent {
71 => ::std::option::Option::Some(GridEvent::UpdateCell),
72 => ::std::option::Option::Some(GridEvent::UpdateSelectOptionCell),
80 => ::std::option::Option::Some(GridEvent::UpdateDateCell),
90 => ::std::option::Option::Some(GridEvent::GetDateCellData),
_ => ::std::option::Option::None
}
}
@ -111,7 +109,6 @@ impl ::protobuf::ProtobufEnum for GridEvent {
GridEvent::UpdateCell,
GridEvent::UpdateSelectOptionCell,
GridEvent::UpdateDateCell,
GridEvent::GetDateCellData,
];
values
}
@ -140,7 +137,7 @@ impl ::protobuf::reflect::ProtobufValue for GridEvent {
}
static file_descriptor_proto_data: &'static [u8] = b"\
\n\x0fevent_map.proto*\xdc\x03\n\tGridEvent\x12\x0f\n\x0bGetGridData\x10\
\n\x0fevent_map.proto*\xc7\x03\n\tGridEvent\x12\x0f\n\x0bGetGridData\x10\
\0\x12\x11\n\rGetGridBlocks\x10\x01\x12\r\n\tGetFields\x10\n\x12\x0f\n\
\x0bUpdateField\x10\x0b\x12\x19\n\x15UpdateFieldTypeOption\x10\x0c\x12\
\x0f\n\x0bInsertField\x10\r\x12\x0f\n\x0bDeleteField\x10\x0e\x12\x11\n\r\
@ -151,7 +148,7 @@ static file_descriptor_proto_data: &'static [u8] = b"\
\x10\x20\x12\r\n\tCreateRow\x102\x12\n\n\x06GetRow\x103\x12\r\n\tDeleteR\
ow\x104\x12\x10\n\x0cDuplicateRow\x105\x12\x0b\n\x07GetCell\x10F\x12\x0e\
\n\nUpdateCell\x10G\x12\x1a\n\x16UpdateSelectOptionCell\x10H\x12\x12\n\
\x0eUpdateDateCell\x10P\x12\x13\n\x0fGetDateCellData\x10Zb\x06proto3\
\x0eUpdateDateCell\x10Pb\x06proto3\
";
static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT;

View File

@ -24,5 +24,4 @@ enum GridEvent {
UpdateCell = 71;
UpdateSelectOptionCell = 72;
UpdateDateCell = 80;
GetDateCellData = 90;
}

View File

@ -58,7 +58,7 @@ impl CellDataOperation<String, String> for CheckboxTypeOption {
let encoded_data = encoded_data.into();
if encoded_data == YES || encoded_data == NO {
return Ok(DecodedCellData::from_content(encoded_data));
return Ok(DecodedCellData::new(encoded_data));
}
Ok(DecodedCellData::default())
@ -104,37 +104,37 @@ mod tests {
let field_meta = FieldBuilder::from_field_type(&FieldType::Checkbox).build();
let data = apply_cell_data_changeset("true", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
YES
);
let data = apply_cell_data_changeset("1", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
YES
);
let data = apply_cell_data_changeset("yes", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
YES
);
let data = apply_cell_data_changeset("false", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
NO
);
let data = apply_cell_data_changeset("no", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
NO
);
let data = apply_cell_data_changeset("12", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).to_string(),
NO
);
}

View File

@ -1,9 +1,7 @@
use crate::entities::{CellIdentifier, CellIdentifierPayload};
use crate::impl_type_option;
use crate::services::field::{BoxTypeOptionBuilder, TypeOptionBuilder};
use crate::services::row::{
CellContentChangeset, CellDataOperation, DecodedCellData, EncodedCellData, TypeOptionCellData,
};
use crate::services::row::{CellContentChangeset, CellDataOperation, DecodedCellData, EncodedCellData};
use bytes::Bytes;
use chrono::format::strftime::StrftimeItems;
use chrono::NaiveDateTime;
@ -79,32 +77,12 @@ impl DateTypeOption {
}
}
pub fn make_date_cell_data(&self, cell_meta: &Option<CellMeta>) -> FlowyResult<DateCellData> {
if cell_meta.is_none() {
return Ok(DateCellData::default());
}
let json = &cell_meta.as_ref().unwrap().data;
let result = TypeOptionCellData::from_str(json);
if result.is_err() {
return Ok(DateCellData::default());
}
let serde_cell_data = DateCellDataSerde::from_str(&result.unwrap().data)?;
let date = self.decode_cell_data_from_timestamp(&serde_cell_data).content;
let time = serde_cell_data.time.unwrap_or_else(|| "".to_owned());
let timestamp = serde_cell_data.timestamp;
Ok(DateCellData { date, time, timestamp })
}
fn decode_cell_data_from_timestamp(&self, serde_cell_data: &DateCellDataSerde) -> DecodedCellData {
fn date_desc_from_timestamp(&self, serde_cell_data: &DateCellDataSerde) -> String {
if serde_cell_data.timestamp == 0 {
return DecodedCellData::default();
return "".to_owned();
}
let cell_content = self.today_desc_from_timestamp(serde_cell_data.timestamp, &serde_cell_data.time);
DecodedCellData::new(serde_cell_data.timestamp.to_string(), cell_content)
self.today_desc_from_timestamp(serde_cell_data.timestamp, &serde_cell_data.time)
}
fn timestamp_from_utc_with_time(
@ -156,7 +134,11 @@ impl CellDataOperation<EncodedCellData<DateCellDataSerde>, DateCellDataSerde> fo
}
let encoded_data = encoded_data.into().try_into_inner()?;
Ok(self.decode_cell_data_from_timestamp(&encoded_data))
let date = self.date_desc_from_timestamp(&encoded_data);
let time = encoded_data.time.unwrap_or_else(|| "".to_owned());
let timestamp = encoded_data.timestamp;
DecodedCellData::try_from_bytes(DateCellData { date, time, timestamp })
}
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<DateCellDataSerde, FlowyError>
@ -417,20 +399,27 @@ impl std::convert::From<DateCellContentChangeset> for CellContentChangeset {
#[cfg(test)]
mod tests {
use crate::services::field::FieldBuilder;
use crate::services::field::{DateCellContentChangeset, DateCellDataSerde, DateFormat, DateTypeOption, TimeFormat};
use crate::services::row::{
apply_cell_data_changeset, decode_cell_data_from_type_option_cell_data, CellDataOperation, EncodedCellData,
use crate::services::field::{
DateCellContentChangeset, DateCellData, DateCellDataSerde, DateFormat, DateTypeOption, TimeFormat,
};
use crate::services::row::{CellDataOperation, EncodedCellData};
use flowy_grid_data_model::entities::{FieldMeta, FieldType};
use strum::IntoEnumIterator;
#[test]
fn date_description_invalid_input_test() {
let field_meta = FieldBuilder::from_field_type(&FieldType::Number).build();
let data = apply_cell_data_changeset("1e", None, &field_meta).unwrap();
assert_eq!(
decode_cell_data_from_type_option_cell_data(data, &field_meta, &field_meta.field_type).content,
"".to_owned()
let type_option = DateTypeOption::default();
let field_type = FieldType::DateTime;
let field_meta = FieldBuilder::from_field_type(&field_type).build();
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some("1e".to_string()),
time: Some("23:00".to_owned()),
},
&field_type,
&field_meta,
"",
);
}
@ -442,40 +431,16 @@ mod tests {
type_option.date_format = date_format;
match date_format {
DateFormat::Friendly => {
assert_eq!(
"Mar 14,2022".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
assert_decode_timestamp(1647251762, &type_option, &field_meta, "Mar 14,2022");
}
DateFormat::US => {
assert_eq!(
"2022/03/14".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
assert_decode_timestamp(1647251762, &type_option, &field_meta, "2022/03/14");
}
DateFormat::ISO => {
assert_eq!(
"2022-03-14".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
assert_decode_timestamp(1647251762, &type_option, &field_meta, "2022-03-14");
}
DateFormat::Local => {
assert_eq!(
"2022/03/14".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
assert_decode_timestamp(1647251762, &type_option, &field_meta, "2022/03/14");
}
}
}
@ -483,43 +448,6 @@ mod tests {
#[test]
fn date_description_time_format_test() {
let mut type_option = DateTypeOption::default();
let field_meta = FieldBuilder::from_field_type(&FieldType::DateTime).build();
for time_format in TimeFormat::iter() {
type_option.time_format = time_format;
match time_format {
TimeFormat::TwentyFourHour => {
assert_eq!(
"Mar 14,2022".to_owned(),
type_option.today_desc_from_timestamp(1647251762, &None)
);
assert_eq!(
"Mar 14,2022".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
}
TimeFormat::TwelveHour => {
assert_eq!(
"Mar 14,2022".to_owned(),
type_option.today_desc_from_timestamp(1647251762, &None)
);
assert_eq!(
"Mar 14,2022".to_owned(),
type_option
.decode_cell_data(data(1647251762), &FieldType::DateTime, &field_meta)
.unwrap()
.content
);
}
}
}
}
#[test]
fn date_description_time_format_test2() {
let mut type_option = DateTypeOption::default();
let field_type = FieldType::DateTime;
let field_meta = FieldBuilder::from_field_type(&field_type).build();
@ -529,7 +457,7 @@ mod tests {
type_option.include_time = true;
match time_format {
TimeFormat::TwentyFourHour => {
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(1653609600.to_string()),
@ -539,7 +467,7 @@ mod tests {
&field_meta,
"May 27,2022 00:00",
);
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(1653609600.to_string()),
@ -551,7 +479,7 @@ mod tests {
);
}
TimeFormat::TwelveHour => {
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(1653609600.to_string()),
@ -562,7 +490,7 @@ mod tests {
"May 27,2022 12:00 AM",
);
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(1653609600.to_string()),
@ -573,7 +501,7 @@ mod tests {
"May 27,2022",
);
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(1653609600.to_string()),
@ -595,7 +523,7 @@ mod tests {
let field_meta = FieldBuilder::from_field_type(&field_type).build();
let date_timestamp = "1653609600".to_owned();
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(date_timestamp.clone()),
@ -607,7 +535,7 @@ mod tests {
);
type_option.include_time = true;
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(date_timestamp.clone()),
@ -618,7 +546,7 @@ mod tests {
"May 27,2022 00:00",
);
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(date_timestamp.clone()),
@ -630,7 +558,7 @@ mod tests {
);
type_option.time_format = TimeFormat::TwelveHour;
assert_result(
assert_changeset_result(
&type_option,
DateCellContentChangeset {
date: Some(date_timestamp),
@ -670,22 +598,39 @@ mod tests {
type_option.apply_changeset("he", None).unwrap();
}
fn data(s: i64) -> String {
serde_json::to_string(&DateCellDataSerde::from_timestamp(s, None)).unwrap()
}
fn assert_result(
fn assert_changeset_result(
type_option: &DateTypeOption,
changeset: DateCellContentChangeset,
field_type: &FieldType,
_field_type: &FieldType,
field_meta: &FieldMeta,
expected: &str,
) {
let encoded_data = EncodedCellData(Some(type_option.apply_changeset(changeset, None).unwrap()));
let content = type_option
.decode_cell_data(encoded_data, field_type, field_meta)
assert_eq!(
expected.to_owned(),
decode_cell_data(encoded_data, type_option, field_meta)
);
}
fn assert_decode_timestamp(timestamp: i64, type_option: &DateTypeOption, field_meta: &FieldMeta, expected: &str) {
let serde_json = DateCellDataSerde { timestamp, time: None }.to_string();
assert_eq!(
expected.to_owned(),
decode_cell_data(serde_json, type_option, field_meta)
);
}
fn decode_cell_data<T: Into<EncodedCellData<DateCellDataSerde>>>(
encoded_data: T,
type_option: &DateTypeOption,
field_meta: &FieldMeta,
) -> String {
type_option
.decode_cell_data(encoded_data, &FieldType::DateTime, &field_meta)
.unwrap()
.content;
assert_eq!(expected.to_owned(), content);
.parse::<DateCellData>()
.unwrap()
.date
}
}

View File

@ -94,22 +94,22 @@ impl CellDataOperation<String, String> for NumberTypeOption {
match self.format {
NumberFormat::Number => {
if let Ok(v) = cell_data.parse::<f64>() {
return Ok(DecodedCellData::from_content(v.to_string()));
return Ok(DecodedCellData::new(v.to_string()));
}
if let Ok(v) = cell_data.parse::<i64>() {
return Ok(DecodedCellData::from_content(v.to_string()));
return Ok(DecodedCellData::new(v.to_string()));
}
Ok(DecodedCellData::default())
}
NumberFormat::Percent => {
let content = cell_data.parse::<f64>().map_or(String::new(), |v| v.to_string());
Ok(DecodedCellData::from_content(content))
Ok(DecodedCellData::new(content))
}
_ => {
let content = self.money_from_str(&cell_data);
Ok(DecodedCellData::from_content(content))
Ok(DecodedCellData::new(content))
}
}
}
@ -738,7 +738,7 @@ mod tests {
type_option
.decode_cell_data(data(cell_data), field_type, field_meta)
.unwrap()
.content,
.to_string(),
expected_str.to_owned()
);
}

View File

@ -109,16 +109,18 @@ impl CellDataOperation<String, String> for SingleSelectTypeOption {
return Ok(DecodedCellData::default());
}
let cell_data = encoded_data.into();
if let Some(option_id) = select_option_ids(cell_data).first() {
let data = match self.options.iter().find(|option| &option.id == option_id) {
None => DecodedCellData::default(),
Some(option) => DecodedCellData::from_content(option.name.clone()),
};
Ok(data)
} else {
Ok(DecodedCellData::default())
let encoded_data = encoded_data.into();
let mut cell_data = SelectOptionCellData {
options: self.options.clone(),
select_options: vec![],
};
if let Some(option_id) = select_option_ids(encoded_data).first() {
if let Some(option) = self.options.iter().find(|option| &option.id == option_id) {
cell_data.select_options.push(option.clone());
}
}
DecodedCellData::try_from_bytes(cell_data)
}
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
@ -204,17 +206,21 @@ impl CellDataOperation<String, String> for MultiSelectTypeOption {
if !decoded_field_type.is_select_option() {
return Ok(DecodedCellData::default());
}
let cell_data = encoded_data.into();
let option_ids = select_option_ids(cell_data);
let content = self
.options
.iter()
.filter(|option| option_ids.contains(&option.id))
.map(|option| option.name.clone())
.collect::<Vec<String>>()
.join(SELECTION_IDS_SEPARATOR);
Ok(DecodedCellData::from_content(content))
tracing::info!("😁{}", self.options.len());
let encoded_data = encoded_data.into();
let select_options = select_option_ids(encoded_data)
.into_iter()
.flat_map(|option_id| self.options.iter().find(|option| option.id == option_id).cloned())
.collect::<Vec<SelectOption>>();
let cell_data = SelectOptionCellData {
options: self.options.clone(),
select_options,
};
DecodedCellData::try_from_bytes(cell_data)
}
fn apply_changeset<T>(&self, changeset: T, cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
@ -280,7 +286,7 @@ fn select_option_ids(data: String) -> Vec<String> {
.collect::<Vec<String>>()
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, ProtoBuf)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
pub struct SelectOption {
#[pb(index = 1)]
pub id: String,
@ -446,7 +452,7 @@ pub struct SelectOptionCellData {
pub select_options: Vec<SelectOption>,
}
#[derive(ProtoBuf_Enum, Serialize, Deserialize, Debug, Clone)]
#[derive(ProtoBuf_Enum, PartialEq, Eq, Serialize, Deserialize, Debug, Clone)]
#[repr(u8)]
pub enum SelectOptionColor {
Purple = 0,
@ -502,9 +508,10 @@ mod tests {
use crate::services::field::FieldBuilder;
use crate::services::field::{
MultiSelectTypeOption, MultiSelectTypeOptionBuilder, SelectOption, SelectOptionCellContentChangeset,
SingleSelectTypeOption, SingleSelectTypeOptionBuilder, SELECTION_IDS_SEPARATOR,
SelectOptionCellData, SingleSelectTypeOption, SingleSelectTypeOptionBuilder, SELECTION_IDS_SEPARATOR,
};
use crate::services::row::CellDataOperation;
use flowy_grid_data_model::entities::FieldMeta;
#[test]
fn single_select_test() {
@ -526,47 +533,24 @@ mod tests {
let option_ids = vec![google_option.id.clone(), facebook_option.id].join(SELECTION_IDS_SEPARATOR);
let data = SelectOptionCellContentChangeset::from_insert(&option_ids).to_str();
let cell_data = type_option.apply_changeset(data, None).unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
google_option.name,
);
assert_single_select_options(cell_data, &type_option, &field_meta, vec![google_option.clone()]);
let data = SelectOptionCellContentChangeset::from_insert(&google_option.id).to_str();
let cell_data = type_option.apply_changeset(data, None).unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
google_option.name,
);
assert_single_select_options(cell_data, &type_option, &field_meta, vec![google_option.clone()]);
// Invalid option id
let cell_data = type_option
.apply_changeset(SelectOptionCellContentChangeset::from_insert("").to_str(), None)
.unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
"",
);
assert_single_select_options(cell_data, &type_option, &field_meta, vec![]);
// Invalid option id
let cell_data = type_option
.apply_changeset(SelectOptionCellContentChangeset::from_insert("123").to_str(), None)
.unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
"",
);
assert_single_select_options(cell_data, &type_option, &field_meta, vec![]);
// Invalid changeset
assert!(type_option.apply_changeset("123", None).is_err());
@ -592,49 +576,64 @@ mod tests {
let option_ids = vec![google_option.id.clone(), facebook_option.id.clone()].join(SELECTION_IDS_SEPARATOR);
let data = SelectOptionCellContentChangeset::from_insert(&option_ids).to_str();
let cell_data = type_option.apply_changeset(data, None).unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
vec![google_option.name.clone(), facebook_option.name].join(SELECTION_IDS_SEPARATOR),
assert_multi_select_options(
cell_data,
&type_option,
&field_meta,
vec![google_option.clone(), facebook_option.clone()],
);
let data = SelectOptionCellContentChangeset::from_insert(&google_option.id).to_str();
let cell_data = type_option.apply_changeset(data, None).unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
google_option.name,
);
assert_multi_select_options(cell_data, &type_option, &field_meta, vec![google_option.clone()]);
// Invalid option id
let cell_data = type_option
.apply_changeset(SelectOptionCellContentChangeset::from_insert("").to_str(), None)
.unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
"",
);
assert_multi_select_options(cell_data, &type_option, &field_meta, vec![]);
// Invalid option id
let cell_data = type_option
.apply_changeset(SelectOptionCellContentChangeset::from_insert("123,456").to_str(), None)
.unwrap();
assert_eq!(
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.content,
"",
);
assert_multi_select_options(cell_data, &type_option, &field_meta, vec![]);
// Invalid changeset
assert!(type_option.apply_changeset("123", None).is_err());
}
fn assert_multi_select_options(
cell_data: String,
type_option: &MultiSelectTypeOption,
field_meta: &FieldMeta,
expected: Vec<SelectOption>,
) {
assert_eq!(
expected,
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.parse::<SelectOptionCellData>()
.unwrap()
.select_options,
);
}
fn assert_single_select_options(
cell_data: String,
type_option: &SingleSelectTypeOption,
field_meta: &FieldMeta,
expected: Vec<SelectOption>,
) {
assert_eq!(
expected,
type_option
.decode_cell_data(cell_data, &field_meta.field_type, &field_meta)
.unwrap()
.parse::<SelectOptionCellData>()
.unwrap()
.select_options,
);
}
}

View File

@ -49,7 +49,7 @@ impl CellDataOperation<String, String> for RichTextTypeOption {
decode_cell_data(encoded_data, decoded_field_type, decoded_field_type, field_meta)
} else {
let cell_data = encoded_data.into();
Ok(DecodedCellData::from_content(cell_data))
Ok(DecodedCellData::new(cell_data))
}
}
@ -85,22 +85,26 @@ mod tests {
type_option
.decode_cell_data(json, &field_type, &date_time_field_meta)
.unwrap()
.content,
.parse::<DateCellData>()
.unwrap()
.date,
"Mar 14,2022".to_owned()
);
// Single select
let done_option = SelectOption::new("Done");
let done_option_id = done_option.id.clone();
let single_select = SingleSelectTypeOptionBuilder::default().option(done_option);
let single_select = SingleSelectTypeOptionBuilder::default().option(done_option.clone());
let single_select_field_meta = FieldBuilder::new(single_select).build();
assert_eq!(
type_option
.decode_cell_data(done_option_id, &FieldType::SingleSelect, &single_select_field_meta)
.unwrap()
.content,
"Done".to_owned()
.parse::<SelectOptionCellData>()
.unwrap()
.select_options,
vec![done_option],
);
// Multiple select
@ -109,8 +113,8 @@ mod tests {
let ids = vec![google_option.id.clone(), facebook_option.id.clone()].join(SELECTION_IDS_SEPARATOR);
let cell_data_changeset = SelectOptionCellContentChangeset::from_insert(&ids).to_str();
let multi_select = MultiSelectTypeOptionBuilder::default()
.option(google_option)
.option(facebook_option);
.option(google_option.clone())
.option(facebook_option.clone());
let multi_select_field_meta = FieldBuilder::new(multi_select).build();
let multi_type_option = MultiSelectTypeOption::from(&multi_select_field_meta);
let cell_data = multi_type_option.apply_changeset(cell_data_changeset, None).unwrap();
@ -118,8 +122,10 @@ mod tests {
type_option
.decode_cell_data(cell_data, &FieldType::MultiSelect, &multi_select_field_meta)
.unwrap()
.content,
"Google,Facebook".to_owned()
.parse::<SelectOptionCellData>()
.unwrap()
.select_options,
vec![google_option, facebook_option]
);
//Number
@ -129,7 +135,7 @@ mod tests {
type_option
.decode_cell_data("18443".to_owned(), &FieldType::Number, &number_field_meta)
.unwrap()
.content,
.to_string(),
"$18,443".to_owned()
);
}

View File

@ -1,5 +1,6 @@
use crate::services::field::*;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use bytes::Bytes;
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType};
use serde::{Deserialize, Serialize};
use std::fmt::Formatter;
@ -145,8 +146,15 @@ pub fn decode_cell_data_from_type_option_cell_data<T: TryInto<TypeOptionCellData
) -> DecodedCellData {
if let Ok(type_option_cell_data) = data.try_into() {
let (encoded_data, s_field_type) = type_option_cell_data.split();
decode_cell_data(encoded_data, &s_field_type, field_type, field_meta).unwrap_or_default()
match decode_cell_data(encoded_data, &s_field_type, field_type, field_meta) {
Ok(cell_data) => cell_data,
Err(e) => {
tracing::error!("Decode cell data failed, {:?}", e);
DecodedCellData::default()
}
}
} else {
tracing::error!("Decode type option data failed");
DecodedCellData::default()
}
}
@ -158,6 +166,7 @@ pub fn decode_cell_data<T: Into<String>>(
field_meta: &FieldMeta,
) -> FlowyResult<DecodedCellData> {
let encoded_data = encoded_data.into();
tracing::info!("😁{:?}", field_meta.type_options);
let get_cell_data = || {
let data = match t_field_type {
FieldType::RichText => field_meta
@ -183,13 +192,7 @@ pub fn decode_cell_data<T: Into<String>>(
};
match get_cell_data() {
Some(Ok(data)) => {
tracing::Span::current().record(
"content",
&format!("{:?}: {}", field_meta.field_type, data.content).as_str(),
);
Ok(data)
}
Some(Ok(data)) => Ok(data),
Some(Err(err)) => {
tracing::error!("{:?}", err);
Ok(DecodedCellData::default())
@ -227,23 +230,39 @@ where
#[derive(Default)]
pub struct DecodedCellData {
pub data: Vec<u8>,
pub content: String,
}
impl DecodedCellData {
pub fn from_content(content: String) -> Self {
pub fn new<T: AsRef<[u8]>>(data: T) -> Self {
Self {
data: content.as_bytes().to_vec(),
content,
data: data.as_ref().to_vec(),
}
}
pub fn new<T: AsRef<[u8]>>(data: T, content: String) -> Self {
let data = data.as_ref().to_vec();
Self { data, content }
pub fn try_from_bytes<T: TryInto<Bytes>>(bytes: T) -> FlowyResult<Self>
where
<T as TryInto<Bytes>>::Error: std::fmt::Debug,
{
let bytes = bytes.try_into().map_err(internal_error)?;
Ok(Self { data: bytes.to_vec() })
}
pub fn split(self) -> (Vec<u8>, String) {
(self.data, self.content)
pub fn parse<'a, T: TryFrom<&'a [u8]>>(&'a self) -> FlowyResult<T>
where
<T as TryFrom<&'a [u8]>>::Error: std::fmt::Debug,
{
T::try_from(self.data.as_ref()).map_err(internal_error)
}
}
impl ToString for DecodedCellData {
fn to_string(&self) -> String {
match String::from_utf8(self.data.clone()) {
Ok(s) => s,
Err(e) => {
tracing::error!("DecodedCellData to string failed: {:?}", e);
"".to_string()
}
}
}
}

View File

@ -31,17 +31,15 @@ pub fn make_cell_by_field_id(
cell_meta: CellMeta,
) -> Option<(String, Cell)> {
let field_meta = field_map.get(&field_id)?;
let (raw, content) =
decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).split();
let cell = Cell::new(&field_id, content, raw);
let data = decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).data;
let cell = Cell::new(&field_id, data);
Some((field_id, cell))
}
pub fn make_cell(field_id: &str, field_meta: &FieldMeta, row_meta: &RowMeta) -> Option<Cell> {
let cell_meta = row_meta.cells.get(field_id)?.clone();
let (raw, content) =
decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).split();
Some(Cell::new(field_id, content, raw))
let data = decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).data;
Some(Cell::new(field_id, data))
}
pub(crate) fn make_row_orders_from_row_metas(row_metas: &[Arc<RowMeta>]) -> Vec<RowOrder> {

View File

@ -2,7 +2,7 @@ use crate::grid::script::EditorScript::*;
use crate::grid::script::*;
use chrono::NaiveDateTime;
use flowy_grid::services::field::{
DateCellContentChangeset, MultiSelectTypeOption, SelectOption, SelectOptionCellContentChangeset,
DateCellContentChangeset, DateCellData, MultiSelectTypeOption, SelectOption, SelectOptionCellContentChangeset,
SingleSelectTypeOption, SELECTION_IDS_SEPARATOR,
};
use flowy_grid::services::row::{decode_cell_data_from_type_option_cell_data, CreateRowMetaBuilder};
@ -292,8 +292,9 @@ async fn grid_row_add_date_cell_test() {
let cell_data = context.cell_by_field_id.get(&date_field.id).unwrap().clone();
assert_eq!(
decode_cell_data_from_type_option_cell_data(cell_data.data.clone(), &date_field, &date_field.field_type)
.split()
.1,
.parse::<DateCellData>()
.unwrap()
.date,
"2022/03/16",
);
let scripts = vec![CreateRow { context }];

View File

@ -30,6 +30,14 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrea
}
}
impl std::convert::TryFrom<&[u8]> for #struct_ident {
type Error = ::protobuf::ProtobufError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(bytes)?;
#struct_ident::try_from(pb)
}
}
impl std::convert::TryFrom<crate::protobuf::#pb_ty> for #struct_ident {
type Error = ::protobuf::ProtobufError;
fn try_from(mut pb: crate::protobuf::#pb_ty) -> Result<Self, Self::Error> {

View File

@ -484,17 +484,13 @@ pub struct Cell {
pub field_id: String,
#[pb(index = 2)]
pub content: String,
#[pb(index = 3)]
pub data: Vec<u8>,
}
impl Cell {
pub fn new(field_id: &str, content: String, data: Vec<u8>) -> Self {
pub fn new(field_id: &str, data: Vec<u8>) -> Self {
Self {
field_id: field_id.to_owned(),
content,
data,
}
}
@ -502,7 +498,6 @@ impl Cell {
pub fn empty(field_id: &str) -> Self {
Self {
field_id: field_id.to_owned(),
content: "".to_string(),
data: vec![],
}
}

View File

@ -4743,7 +4743,6 @@ impl ::protobuf::reflect::ProtobufValue for GridBlock {
pub struct Cell {
// message fields
pub field_id: ::std::string::String,
pub content: ::std::string::String,
pub data: ::std::vec::Vec<u8>,
// special fields
pub unknown_fields: ::protobuf::UnknownFields,
@ -4787,33 +4786,7 @@ impl Cell {
::std::mem::replace(&mut self.field_id, ::std::string::String::new())
}
// string content = 2;
pub fn get_content(&self) -> &str {
&self.content
}
pub fn clear_content(&mut self) {
self.content.clear();
}
// Param is passed by value, moved
pub fn set_content(&mut self, v: ::std::string::String) {
self.content = v;
}
// Mutable pointer to the field.
// If field is not initialized, it is initialized with default value first.
pub fn mut_content(&mut self) -> &mut ::std::string::String {
&mut self.content
}
// Take field
pub fn take_content(&mut self) -> ::std::string::String {
::std::mem::replace(&mut self.content, ::std::string::String::new())
}
// bytes data = 3;
// bytes data = 2;
pub fn get_data(&self) -> &[u8] {
@ -4853,9 +4826,6 @@ impl ::protobuf::Message for Cell {
::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.field_id)?;
},
2 => {
::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.content)?;
},
3 => {
::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.data)?;
},
_ => {
@ -4873,11 +4843,8 @@ impl ::protobuf::Message for Cell {
if !self.field_id.is_empty() {
my_size += ::protobuf::rt::string_size(1, &self.field_id);
}
if !self.content.is_empty() {
my_size += ::protobuf::rt::string_size(2, &self.content);
}
if !self.data.is_empty() {
my_size += ::protobuf::rt::bytes_size(3, &self.data);
my_size += ::protobuf::rt::bytes_size(2, &self.data);
}
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
self.cached_size.set(my_size);
@ -4888,11 +4855,8 @@ impl ::protobuf::Message for Cell {
if !self.field_id.is_empty() {
os.write_string(1, &self.field_id)?;
}
if !self.content.is_empty() {
os.write_string(2, &self.content)?;
}
if !self.data.is_empty() {
os.write_bytes(3, &self.data)?;
os.write_bytes(2, &self.data)?;
}
os.write_unknown_fields(self.get_unknown_fields())?;
::std::result::Result::Ok(())
@ -4937,11 +4901,6 @@ impl ::protobuf::Message for Cell {
|m: &Cell| { &m.field_id },
|m: &mut Cell| { &mut m.field_id },
));
fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
"content",
|m: &Cell| { &m.content },
|m: &mut Cell| { &mut m.content },
));
fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
"data",
|m: &Cell| { &m.data },
@ -4964,7 +4923,6 @@ impl ::protobuf::Message for Cell {
impl ::protobuf::Clear for Cell {
fn clear(&mut self) {
self.field_id.clear();
self.content.clear();
self.data.clear();
self.unknown_fields.clear();
}
@ -8340,50 +8298,49 @@ static file_descriptor_proto_data: &'static [u8] = b"\
derR\x0bdeletedRows\x123\n\x0cupdated_rows\x18\x04\x20\x03(\x0b2\x10.Upd\
atedRowOrderR\x0bupdatedRows\"E\n\tGridBlock\x12\x0e\n\x02id\x18\x01\x20\
\x01(\tR\x02id\x12(\n\nrow_orders\x18\x02\x20\x03(\x0b2\t.RowOrderR\trow\
Orders\"O\n\x04Cell\x12\x19\n\x08field_id\x18\x01\x20\x01(\tR\x07fieldId\
\x12\x18\n\x07content\x18\x02\x20\x01(\tR\x07content\x12\x12\n\x04data\
\x18\x03\x20\x01(\x0cR\x04data\"+\n\x0cRepeatedCell\x12\x1b\n\x05items\
\x18\x01\x20\x03(\x0b2\x05.CellR\x05items\"'\n\x11CreateGridPayload\x12\
\x12\n\x04name\x18\x01\x20\x01(\tR\x04name\"\x1e\n\x06GridId\x12\x14\n\
\x05value\x18\x01\x20\x01(\tR\x05value\"#\n\x0bGridBlockId\x12\x14\n\x05\
value\x18\x01\x20\x01(\tR\x05value\"f\n\x10CreateRowPayload\x12\x17\n\
\x07grid_id\x18\x01\x20\x01(\tR\x06gridId\x12\"\n\x0cstart_row_id\x18\
\x02\x20\x01(\tH\0R\nstartRowIdB\x15\n\x13one_of_start_row_id\"\xb6\x01\
\n\x12InsertFieldPayload\x12\x17\n\x07grid_id\x18\x01\x20\x01(\tR\x06gri\
dId\x12\x1c\n\x05field\x18\x02\x20\x01(\x0b2\x06.FieldR\x05field\x12(\n\
\x10type_option_data\x18\x03\x20\x01(\x0cR\x0etypeOptionData\x12&\n\x0es\
tart_field_id\x18\x04\x20\x01(\tH\0R\x0cstartFieldIdB\x17\n\x15one_of_st\
art_field_id\"|\n\x1cUpdateFieldTypeOptionPayload\x12\x17\n\x07grid_id\
\x18\x01\x20\x01(\tR\x06gridId\x12\x19\n\x08field_id\x18\x02\x20\x01(\tR\
\x07fieldId\x12(\n\x10type_option_data\x18\x03\x20\x01(\x0cR\x0etypeOpti\
onData\"d\n\x11QueryFieldPayload\x12\x17\n\x07grid_id\x18\x01\x20\x01(\t\
R\x06gridId\x126\n\x0cfield_orders\x18\x02\x20\x01(\x0b2\x13.RepeatedFie\
ldOrderR\x0bfieldOrders\"e\n\x16QueryGridBlocksPayload\x12\x17\n\x07grid\
_id\x18\x01\x20\x01(\tR\x06gridId\x122\n\x0cblock_orders\x18\x02\x20\x03\
(\x0b2\x0f.GridBlockOrderR\x0bblockOrders\"\xa8\x03\n\x15FieldChangesetP\
ayload\x12\x19\n\x08field_id\x18\x01\x20\x01(\tR\x07fieldId\x12\x17\n\
\x07grid_id\x18\x02\x20\x01(\tR\x06gridId\x12\x14\n\x04name\x18\x03\x20\
\x01(\tH\0R\x04name\x12\x14\n\x04desc\x18\x04\x20\x01(\tH\x01R\x04desc\
\x12+\n\nfield_type\x18\x05\x20\x01(\x0e2\n.FieldTypeH\x02R\tfieldType\
\x12\x18\n\x06frozen\x18\x06\x20\x01(\x08H\x03R\x06frozen\x12\x20\n\nvis\
ibility\x18\x07\x20\x01(\x08H\x04R\nvisibility\x12\x16\n\x05width\x18\
\x08\x20\x01(\x05H\x05R\x05width\x12*\n\x10type_option_data\x18\t\x20\
\x01(\x0cH\x06R\x0etypeOptionDataB\r\n\x0bone_of_nameB\r\n\x0bone_of_des\
cB\x13\n\x11one_of_field_typeB\x0f\n\rone_of_frozenB\x13\n\x11one_of_vis\
ibilityB\x0e\n\x0cone_of_widthB\x19\n\x17one_of_type_option_data\"\x9c\
\x01\n\x0fMoveItemPayload\x12\x17\n\x07grid_id\x18\x01\x20\x01(\tR\x06gr\
idId\x12\x17\n\x07item_id\x18\x02\x20\x01(\tR\x06itemId\x12\x1d\n\nfrom_\
index\x18\x03\x20\x01(\x05R\tfromIndex\x12\x19\n\x08to_index\x18\x04\x20\
\x01(\x05R\x07toIndex\x12\x1d\n\x02ty\x18\x05\x20\x01(\x0e2\r.MoveItemTy\
peR\x02ty\"\xb3\x01\n\rCellChangeset\x12\x17\n\x07grid_id\x18\x01\x20\
\x01(\tR\x06gridId\x12\x15\n\x06row_id\x18\x02\x20\x01(\tR\x05rowId\x12\
\x19\n\x08field_id\x18\x03\x20\x01(\tR\x07fieldId\x126\n\x16cell_content\
_changeset\x18\x04\x20\x01(\tH\0R\x14cellContentChangesetB\x1f\n\x1done_\
of_cell_content_changeset**\n\x0cMoveItemType\x12\r\n\tMoveField\x10\0\
\x12\x0b\n\x07MoveRow\x10\x01*d\n\tFieldType\x12\x0c\n\x08RichText\x10\0\
\x12\n\n\x06Number\x10\x01\x12\x0c\n\x08DateTime\x10\x02\x12\x10\n\x0cSi\
ngleSelect\x10\x03\x12\x0f\n\x0bMultiSelect\x10\x04\x12\x0c\n\x08Checkbo\
x\x10\x05b\x06proto3\
Orders\"5\n\x04Cell\x12\x19\n\x08field_id\x18\x01\x20\x01(\tR\x07fieldId\
\x12\x12\n\x04data\x18\x02\x20\x01(\x0cR\x04data\"+\n\x0cRepeatedCell\
\x12\x1b\n\x05items\x18\x01\x20\x03(\x0b2\x05.CellR\x05items\"'\n\x11Cre\
ateGridPayload\x12\x12\n\x04name\x18\x01\x20\x01(\tR\x04name\"\x1e\n\x06\
GridId\x12\x14\n\x05value\x18\x01\x20\x01(\tR\x05value\"#\n\x0bGridBlock\
Id\x12\x14\n\x05value\x18\x01\x20\x01(\tR\x05value\"f\n\x10CreateRowPayl\
oad\x12\x17\n\x07grid_id\x18\x01\x20\x01(\tR\x06gridId\x12\"\n\x0cstart_\
row_id\x18\x02\x20\x01(\tH\0R\nstartRowIdB\x15\n\x13one_of_start_row_id\
\"\xb6\x01\n\x12InsertFieldPayload\x12\x17\n\x07grid_id\x18\x01\x20\x01(\
\tR\x06gridId\x12\x1c\n\x05field\x18\x02\x20\x01(\x0b2\x06.FieldR\x05fie\
ld\x12(\n\x10type_option_data\x18\x03\x20\x01(\x0cR\x0etypeOptionData\
\x12&\n\x0estart_field_id\x18\x04\x20\x01(\tH\0R\x0cstartFieldIdB\x17\n\
\x15one_of_start_field_id\"|\n\x1cUpdateFieldTypeOptionPayload\x12\x17\n\
\x07grid_id\x18\x01\x20\x01(\tR\x06gridId\x12\x19\n\x08field_id\x18\x02\
\x20\x01(\tR\x07fieldId\x12(\n\x10type_option_data\x18\x03\x20\x01(\x0cR\
\x0etypeOptionData\"d\n\x11QueryFieldPayload\x12\x17\n\x07grid_id\x18\
\x01\x20\x01(\tR\x06gridId\x126\n\x0cfield_orders\x18\x02\x20\x01(\x0b2\
\x13.RepeatedFieldOrderR\x0bfieldOrders\"e\n\x16QueryGridBlocksPayload\
\x12\x17\n\x07grid_id\x18\x01\x20\x01(\tR\x06gridId\x122\n\x0cblock_orde\
rs\x18\x02\x20\x03(\x0b2\x0f.GridBlockOrderR\x0bblockOrders\"\xa8\x03\n\
\x15FieldChangesetPayload\x12\x19\n\x08field_id\x18\x01\x20\x01(\tR\x07f\
ieldId\x12\x17\n\x07grid_id\x18\x02\x20\x01(\tR\x06gridId\x12\x14\n\x04n\
ame\x18\x03\x20\x01(\tH\0R\x04name\x12\x14\n\x04desc\x18\x04\x20\x01(\tH\
\x01R\x04desc\x12+\n\nfield_type\x18\x05\x20\x01(\x0e2\n.FieldTypeH\x02R\
\tfieldType\x12\x18\n\x06frozen\x18\x06\x20\x01(\x08H\x03R\x06frozen\x12\
\x20\n\nvisibility\x18\x07\x20\x01(\x08H\x04R\nvisibility\x12\x16\n\x05w\
idth\x18\x08\x20\x01(\x05H\x05R\x05width\x12*\n\x10type_option_data\x18\
\t\x20\x01(\x0cH\x06R\x0etypeOptionDataB\r\n\x0bone_of_nameB\r\n\x0bone_\
of_descB\x13\n\x11one_of_field_typeB\x0f\n\rone_of_frozenB\x13\n\x11one_\
of_visibilityB\x0e\n\x0cone_of_widthB\x19\n\x17one_of_type_option_data\"\
\x9c\x01\n\x0fMoveItemPayload\x12\x17\n\x07grid_id\x18\x01\x20\x01(\tR\
\x06gridId\x12\x17\n\x07item_id\x18\x02\x20\x01(\tR\x06itemId\x12\x1d\n\
\nfrom_index\x18\x03\x20\x01(\x05R\tfromIndex\x12\x19\n\x08to_index\x18\
\x04\x20\x01(\x05R\x07toIndex\x12\x1d\n\x02ty\x18\x05\x20\x01(\x0e2\r.Mo\
veItemTypeR\x02ty\"\xb3\x01\n\rCellChangeset\x12\x17\n\x07grid_id\x18\
\x01\x20\x01(\tR\x06gridId\x12\x15\n\x06row_id\x18\x02\x20\x01(\tR\x05ro\
wId\x12\x19\n\x08field_id\x18\x03\x20\x01(\tR\x07fieldId\x126\n\x16cell_\
content_changeset\x18\x04\x20\x01(\tH\0R\x14cellContentChangesetB\x1f\n\
\x1done_of_cell_content_changeset**\n\x0cMoveItemType\x12\r\n\tMoveField\
\x10\0\x12\x0b\n\x07MoveRow\x10\x01*d\n\tFieldType\x12\x0c\n\x08RichText\
\x10\0\x12\n\n\x06Number\x10\x01\x12\x0c\n\x08DateTime\x10\x02\x12\x10\n\
\x0cSingleSelect\x10\x03\x12\x0f\n\x0bMultiSelect\x10\x04\x12\x0c\n\x08C\
heckbox\x10\x05b\x06proto3\
";
static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT;

View File

@ -95,8 +95,7 @@ message GridBlock {
}
message Cell {
string field_id = 1;
string content = 2;
bytes data = 3;
bytes data = 2;
}
message RepeatedCell {
repeated Cell items = 1;