Skip to content

Commit 62c322a

Browse files
committed
refactor: Date & SingleSelect & Multi-Select type option
1 parent 8d766f3 commit 62c322a

File tree

27 files changed

+379
-482
lines changed

27 files changed

+379
-482
lines changed

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/cell_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';
1616
import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart';
1717
import 'package:app_flowy/workspace/application/grid/cell/select_option_service.dart';
1818
import 'package:app_flowy/workspace/application/grid/field/field_service.dart';
19-
19+
import 'dart:convert' show utf8;
2020
part 'cell_service.freezed.dart';
2121
part 'data_loader.dart';
2222
part 'context_builder.dart';

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of 'cell_service.dart';
22

3-
typedef GridCellContext = _GridCellContext<Cell, String>;
3+
typedef GridCellContext = _GridCellContext<String, String>;
44
typedef GridSelectOptionCellContext = _GridCellContext<SelectOptionCellData, String>;
55
typedef GridDateCellContext = _GridCellContext<DateCellData, DateCalData>;
66

@@ -16,26 +16,33 @@ class GridCellContextBuilder {
1616
_GridCellContext build() {
1717
switch (_gridCell.field.fieldType) {
1818
case FieldType.Checkbox:
19+
final cellDataLoader = GridCellDataLoader(
20+
gridCell: _gridCell,
21+
parser: StringCellDataParser(),
22+
);
1923
return GridCellContext(
2024
gridCell: _gridCell,
2125
cellCache: _cellCache,
22-
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
26+
cellDataLoader: cellDataLoader,
2327
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
2428
);
2529
case FieldType.DateTime:
30+
final cellDataLoader = GridCellDataLoader(
31+
gridCell: _gridCell,
32+
parser: DateCellDataParser(),
33+
);
34+
2635
return GridDateCellContext(
2736
gridCell: _gridCell,
2837
cellCache: _cellCache,
29-
cellDataLoader: DateCellDataLoader(gridCell: _gridCell),
38+
cellDataLoader: cellDataLoader,
3039
cellDataPersistence: DateCellDataPersistence(gridCell: _gridCell),
3140
);
3241
case FieldType.Number:
3342
final cellDataLoader = GridCellDataLoader(
3443
gridCell: _gridCell,
35-
config: const GridCellDataConfig(
36-
reloadOnCellChanged: true,
37-
reloadOnFieldChanged: true,
38-
),
44+
parser: StringCellDataParser(),
45+
config: const GridCellDataConfig(reloadOnCellChanged: true, reloadOnFieldChanged: true),
3946
);
4047
return GridCellContext(
4148
gridCell: _gridCell,
@@ -44,18 +51,28 @@ class GridCellContextBuilder {
4451
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
4552
);
4653
case FieldType.RichText:
54+
final cellDataLoader = GridCellDataLoader(
55+
gridCell: _gridCell,
56+
parser: StringCellDataParser(),
57+
);
4758
return GridCellContext(
4859
gridCell: _gridCell,
4960
cellCache: _cellCache,
50-
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
61+
cellDataLoader: cellDataLoader,
5162
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
5263
);
5364
case FieldType.MultiSelect:
5465
case FieldType.SingleSelect:
66+
final cellDataLoader = GridCellDataLoader(
67+
gridCell: _gridCell,
68+
parser: SelectOptionCellDataParser(),
69+
config: const GridCellDataConfig(reloadOnFieldChanged: true),
70+
);
71+
5572
return GridSelectOptionCellContext(
5673
gridCell: _gridCell,
5774
cellCache: _cellCache,
58-
cellDataLoader: SelectOptionCellDataLoader(gridCell: _gridCell),
75+
cellDataLoader: cellDataLoader,
5976
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
6077
);
6178
default:
@@ -131,10 +148,7 @@ class _GridCellContext<T, D> extends Equatable {
131148
}
132149

133150
onCellChangedFn() {
134-
final value = _cellDataNotifier.value;
135-
if (value is T) {
136-
onCellChanged(value);
137-
}
151+
onCellChanged(_cellDataNotifier.value as T);
138152

139153
if (cellDataLoader.config.reloadOnCellChanged) {
140154
_loadData();

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_loader.dart

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ abstract class IGridCellDataConfig {
44
// The cell data will reload if it receives the field's change notification.
55
bool get reloadOnFieldChanged;
66

7-
// The cell data will reload if it receives the cell's change notification.
8-
// For example, the number cell should be reloaded after user input the number.
7+
// When the reloadOnCellChanged is true, it will load the cell data after user input.
8+
// For example: The number cell reload the cell data that carries the format
99
// user input: 12
1010
// cell display: $12
1111
bool get reloadOnCellChanged;
@@ -31,63 +31,44 @@ abstract class IGridCellDataLoader<T> {
3131
}
3232

3333
abstract class ICellDataParser<T> {
34-
T? parserData();
34+
T? parserData(List<int> data);
3535
}
3636

37-
class GridCellDataLoader extends IGridCellDataLoader<String> {
37+
class GridCellDataLoader<T> extends IGridCellDataLoader<T> {
3838
final CellService service = CellService();
3939
final GridCell gridCell;
40+
final ICellDataParser<T> parser;
4041

4142
@override
4243
final IGridCellDataConfig config;
4344

4445
GridCellDataLoader({
4546
required this.gridCell,
47+
required this.parser,
4648
this.config = const GridCellDataConfig(),
4749
});
4850

4951
@override
50-
Future<String> loadData() {
52+
Future<T?> loadData() {
5153
final fut = service.getCell(
5254
gridId: gridCell.gridId,
5355
fieldId: gridCell.field.id,
5456
rowId: gridCell.rowId,
5557
);
56-
return fut.then((result) {
57-
return result.fold((Cell data) => data.content, (err) {
58-
Log.error(err);
59-
return "";
60-
});
61-
});
62-
}
63-
}
64-
65-
class DateCellDataLoader extends IGridCellDataLoader<DateCellData> {
66-
final GridCell gridCell;
67-
final IGridCellDataConfig _config;
68-
DateCellDataLoader({
69-
required this.gridCell,
70-
}) : _config = const GridCellDataConfig(reloadOnFieldChanged: true);
71-
72-
@override
73-
IGridCellDataConfig get config => _config;
74-
75-
@override
76-
Future<DateCellData?> loadData() {
77-
final payload = CellIdentifierPayload.create()
78-
..gridId = gridCell.gridId
79-
..fieldId = gridCell.field.id
80-
..rowId = gridCell.rowId;
81-
82-
return GridEventGetDateCellData(payload).send().then((result) {
83-
return result.fold(
84-
(data) => data,
85-
(err) {
86-
Log.error(err);
58+
return fut.then(
59+
(result) => result.fold((Cell cell) {
60+
try {
61+
return parser.parserData(cell.data);
62+
} catch (e, s) {
63+
Log.error('$parser parser cellData failed, $e');
64+
Log.error('Stack trace \n $s');
8765
return null;
88-
},
89-
);
90-
});
66+
}
67+
}, (err) {
68+
Log.error(err);
69+
return null;
70+
}),
71+
);
9172
}
9273
}
9374

@@ -113,3 +94,30 @@ class SelectOptionCellDataLoader extends IGridCellDataLoader<SelectOptionCellDat
11394
@override
11495
IGridCellDataConfig get config => const GridCellDataConfig(reloadOnFieldChanged: true);
11596
}
97+
98+
class StringCellDataParser implements ICellDataParser<String> {
99+
@override
100+
String? parserData(List<int> data) {
101+
return utf8.decode(data);
102+
}
103+
}
104+
105+
class DateCellDataParser implements ICellDataParser<DateCellData> {
106+
@override
107+
DateCellData? parserData(List<int> data) {
108+
if (data.isEmpty) {
109+
return null;
110+
}
111+
return DateCellData.fromBuffer(data);
112+
}
113+
}
114+
115+
class SelectOptionCellDataParser implements ICellDataParser<SelectOptionCellData> {
116+
@override
117+
SelectOptionCellData? parserData(List<int> data) {
118+
if (data.isEmpty) {
119+
return null;
120+
}
121+
return SelectOptionCellData.fromBuffer(data);
122+
}
123+
}

frontend/app_flowy/lib/workspace/application/grid/cell/select_option_cell_bloc.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
2121
},
2222
didReceiveOptions: (_DidReceiveOptions value) {
2323
emit(state.copyWith(
24-
options: value.options,
2524
selectedOptions: value.selectedOptions,
2625
));
2726
},
@@ -45,7 +44,6 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
4544
onCellChanged: ((selectOptionContext) {
4645
if (!isClosed) {
4746
add(SelectOptionCellEvent.didReceiveOptions(
48-
selectOptionContext.options,
4947
selectOptionContext.selectOptions,
5048
));
5149
}
@@ -58,23 +56,20 @@ class SelectOptionCellBloc extends Bloc<SelectOptionCellEvent, SelectOptionCellS
5856
class SelectOptionCellEvent with _$SelectOptionCellEvent {
5957
const factory SelectOptionCellEvent.initial() = _InitialCell;
6058
const factory SelectOptionCellEvent.didReceiveOptions(
61-
List<SelectOption> options,
6259
List<SelectOption> selectedOptions,
6360
) = _DidReceiveOptions;
6461
}
6562

6663
@freezed
6764
class SelectOptionCellState with _$SelectOptionCellState {
6865
const factory SelectOptionCellState({
69-
required List<SelectOption> options,
7066
required List<SelectOption> selectedOptions,
7167
}) = _SelectOptionCellState;
7268

7369
factory SelectOptionCellState.initial(GridSelectOptionCellContext context) {
7470
final data = context.getCellData();
7571

7672
return SelectOptionCellState(
77-
options: data?.options ?? [],
7873
selectedOptions: data?.selectOptions ?? [],
7974
);
8075
}

frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
2424
await event.map(
2525
initial: (_Initial value) async {
2626
_startListening();
27+
_loadOptions();
2728
},
2829
didReceiveOptions: (_DidReceiveOptions value) {
2930
final result = _makeOptions(state.filter, value.options);
@@ -106,6 +107,26 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
106107
));
107108
}
108109

110+
void _loadOptions() {
111+
final selectionCellData = cellContext.getCellData();
112+
if (selectionCellData == null) {
113+
final service = SelectOptionService(gridCell: cellContext.gridCell);
114+
service.getOpitonContext().then((result) {
115+
return result.fold(
116+
(data) {
117+
if (!isClosed) {
118+
add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
119+
}
120+
},
121+
(err) {
122+
Log.error(err);
123+
return null;
124+
},
125+
);
126+
});
127+
}
128+
}
129+
109130
_MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) {
110131
final List<SelectOption> options = List.from(allOptions);
111132
Option<String> createOption = filter;

frontend/app_flowy/lib/workspace/application/grid/cell/text_cell_bloc.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
1313
}) : super(TextCellState.initial(cellContext)) {
1414
on<TextCellEvent>(
1515
(event, emit) async {
16-
await event.map(
17-
initial: (_InitialCell value) async {
16+
await event.when(
17+
initial: () async {
1818
_startListening();
1919
},
20-
updateText: (_UpdateText value) {
21-
cellContext.saveCellData(value.text);
22-
emit(state.copyWith(content: value.text));
20+
updateText: (text) {
21+
cellContext.saveCellData(text);
22+
emit(state.copyWith(content: text));
2323
},
24-
didReceiveCellData: (_DidReceiveCellData value) {
25-
emit(state.copyWith(content: value.cellData.cell?.content ?? ""));
26-
},
27-
didReceiveCellUpdate: (_DidReceiveCellUpdate value) {
28-
emit(state.copyWith(content: value.cellContent));
24+
didReceiveCellUpdate: (content) {
25+
emit(state.copyWith(content: content));
2926
},
3027
);
3128
},
@@ -56,7 +53,6 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
5653
@freezed
5754
class TextCellEvent with _$TextCellEvent {
5855
const factory TextCellEvent.initial() = _InitialCell;
59-
const factory TextCellEvent.didReceiveCellData(GridCell cellData) = _DidReceiveCellData;
6056
const factory TextCellEvent.didReceiveCellUpdate(String cellContent) = _DidReceiveCellUpdate;
6157
const factory TextCellEvent.updateText(String text) = _UpdateText;
6258
}

frontend/app_flowy/packages/flowy_sdk/lib/dispatch/dart_event/flowy-grid/dart_event.dart

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -392,20 +392,3 @@ class GridEventUpdateDateCell {
392392
}
393393
}
394394

395-
class GridEventGetDateCellData {
396-
CellIdentifierPayload request;
397-
GridEventGetDateCellData(this.request);
398-
399-
Future<Either<DateCellData, FlowyError>> send() {
400-
final request = FFIRequest.create()
401-
..event = GridEvent.GetDateCellData.toString()
402-
..payload = requestToBytes(this.request);
403-
404-
return Dispatch.asyncRequest(request)
405-
.then((bytesResult) => bytesResult.fold(
406-
(okBytes) => left(DateCellData.fromBuffer(okBytes)),
407-
(errBytes) => right(FlowyError.fromBuffer(errBytes)),
408-
));
409-
}
410-
}
411-

0 commit comments

Comments
 (0)