Skip to content

Commit 4840d23

Browse files
committed
chore: add GridFieldContext
1 parent a59a0af commit 4840d23

File tree

18 files changed

+351
-323
lines changed

18 files changed

+351
-323
lines changed

frontend/app_flowy/lib/startup/deps_resolver.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
1515
import 'package:app_flowy/workspace/presentation/home/menu/menu.dart';
1616
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart';
1717
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
18-
import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart' show FieldTypeOptionData;
1918
import 'package:flowy_sdk/protobuf/flowy-grid/date_type_option.pb.dart';
2019
import 'package:flowy_sdk/protobuf/flowy-grid/number_type_option.pb.dart';
2120
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
@@ -157,13 +156,6 @@ void _resolveGridDeps(GetIt getIt) {
157156
),
158157
);
159158

160-
getIt.registerFactoryParam<FieldEditorBloc, String, FieldContextLoader>(
161-
(gridId, fieldLoader) => FieldEditorBloc(
162-
gridId: gridId,
163-
fieldLoader: fieldLoader,
164-
),
165-
);
166-
167159
getIt.registerFactoryParam<TextCellBloc, GridCellContext, void>(
168160
(context, _) => TextCellBloc(
169161
cellContext: context,
@@ -195,10 +187,6 @@ void _resolveGridDeps(GetIt getIt) {
195187
),
196188
);
197189

198-
getIt.registerFactoryParam<FieldEditorPannelBloc, FieldTypeOptionData, void>(
199-
(context, _) => FieldEditorPannelBloc(context),
200-
);
201-
202190
getIt.registerFactoryParam<DateTypeOptionBloc, DateTypeOption, void>(
203191
(typeOption, _) => DateTypeOptionBloc(typeOption: typeOption),
204192
);

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GridCellContextBuilder {
1919
return GridCellContext(
2020
gridCell: _gridCell,
2121
cellCache: _cellCache,
22-
cellDataLoader: CellDataLoader(gridCell: _gridCell),
22+
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
2323
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
2424
);
2525
case FieldType.DateTime:
@@ -30,17 +30,24 @@ class GridCellContextBuilder {
3030
cellDataPersistence: DateCellDataPersistence(gridCell: _gridCell),
3131
);
3232
case FieldType.Number:
33+
final cellDataLoader = GridCellDataLoader(
34+
gridCell: _gridCell,
35+
config: const GridCellDataConfig(
36+
reloadOnCellChanged: true,
37+
reloadOnFieldChanged: true,
38+
),
39+
);
3340
return GridCellContext(
3441
gridCell: _gridCell,
3542
cellCache: _cellCache,
36-
cellDataLoader: CellDataLoader(gridCell: _gridCell, reloadOnCellChanged: true),
43+
cellDataLoader: cellDataLoader,
3744
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
3845
);
3946
case FieldType.RichText:
4047
return GridCellContext(
4148
gridCell: _gridCell,
4249
cellCache: _cellCache,
43-
cellDataLoader: CellDataLoader(gridCell: _gridCell),
50+
cellDataLoader: GridCellDataLoader(gridCell: _gridCell),
4451
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
4552
);
4653
case FieldType.MultiSelect:
@@ -62,7 +69,7 @@ class _GridCellContext<T, D> extends Equatable {
6269
final GridCell gridCell;
6370
final GridCellCache cellCache;
6471
final GridCellCacheKey _cacheKey;
65-
final _GridCellDataLoader<T> cellDataLoader;
72+
final IGridCellDataLoader<T> cellDataLoader;
6673
final _GridCellDataPersistence<D> cellDataPersistence;
6774
final FieldService _fieldService;
6875

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

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

3-
abstract class GridCellDataConfig {
3+
abstract class IGridCellDataConfig {
44
// The cell data will reload if it receives the field's change notification.
55
bool get reloadOnFieldChanged;
66

@@ -11,34 +11,36 @@ abstract class GridCellDataConfig {
1111
bool get reloadOnCellChanged;
1212
}
1313

14-
class DefaultCellDataConfig implements GridCellDataConfig {
14+
class GridCellDataConfig implements IGridCellDataConfig {
1515
@override
1616
final bool reloadOnCellChanged;
1717

1818
@override
1919
final bool reloadOnFieldChanged;
2020

21-
DefaultCellDataConfig({
21+
const GridCellDataConfig({
2222
this.reloadOnCellChanged = false,
2323
this.reloadOnFieldChanged = false,
2424
});
2525
}
2626

27-
abstract class _GridCellDataLoader<T> {
27+
abstract class IGridCellDataLoader<T> {
2828
Future<T?> loadData();
2929

30-
GridCellDataConfig get config;
30+
IGridCellDataConfig get config;
3131
}
3232

33-
class CellDataLoader extends _GridCellDataLoader<Cell> {
33+
class GridCellDataLoader extends IGridCellDataLoader<Cell> {
3434
final CellService service = CellService();
3535
final GridCell gridCell;
36-
final GridCellDataConfig _config;
3736

38-
CellDataLoader({
37+
@override
38+
final IGridCellDataConfig config;
39+
40+
GridCellDataLoader({
3941
required this.gridCell,
40-
bool reloadOnCellChanged = false,
41-
}) : _config = DefaultCellDataConfig(reloadOnCellChanged: reloadOnCellChanged);
42+
this.config = const GridCellDataConfig(),
43+
});
4244

4345
@override
4446
Future<Cell?> loadData() {
@@ -54,20 +56,17 @@ class CellDataLoader extends _GridCellDataLoader<Cell> {
5456
});
5557
});
5658
}
57-
58-
@override
59-
GridCellDataConfig get config => _config;
6059
}
6160

62-
class DateCellDataLoader extends _GridCellDataLoader<DateCellData> {
61+
class DateCellDataLoader extends IGridCellDataLoader<DateCellData> {
6362
final GridCell gridCell;
64-
final GridCellDataConfig _config;
63+
final IGridCellDataConfig _config;
6564
DateCellDataLoader({
6665
required this.gridCell,
67-
}) : _config = DefaultCellDataConfig(reloadOnFieldChanged: true);
66+
}) : _config = const GridCellDataConfig(reloadOnFieldChanged: true);
6867

6968
@override
70-
GridCellDataConfig get config => _config;
69+
IGridCellDataConfig get config => _config;
7170

7271
@override
7372
Future<DateCellData?> loadData() {
@@ -88,7 +87,7 @@ class DateCellDataLoader extends _GridCellDataLoader<DateCellData> {
8887
}
8988
}
9089

91-
class SelectOptionCellDataLoader extends _GridCellDataLoader<SelectOptionCellData> {
90+
class SelectOptionCellDataLoader extends IGridCellDataLoader<SelectOptionCellData> {
9291
final SelectOptionService service;
9392
final GridCell gridCell;
9493
SelectOptionCellDataLoader({
@@ -108,5 +107,5 @@ class SelectOptionCellDataLoader extends _GridCellDataLoader<SelectOptionCellDat
108107
}
109108

110109
@override
111-
GridCellDataConfig get config => DefaultCellDataConfig();
110+
IGridCellDataConfig get config => const GridCellDataConfig(reloadOnFieldChanged: true);
112111
}
Lines changed: 21 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
import 'dart:typed_data';
2-
import 'package:flowy_sdk/log.dart';
3-
import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart';
41
import 'package:flutter_bloc/flutter_bloc.dart';
52
import 'package:freezed_annotation/freezed_annotation.dart';
63
import 'dart:async';
74
import 'field_service.dart';
85
import 'package:dartz/dartz.dart';
9-
import 'package:protobuf/protobuf.dart';
10-
116
part 'field_editor_bloc.freezed.dart';
127

138
class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
14-
final String gridId;
15-
final FieldContextLoader _loader;
16-
179
FieldEditorBloc({
18-
required this.gridId,
19-
required FieldContextLoader fieldLoader,
20-
}) : _loader = fieldLoader,
21-
super(FieldEditorState.initial(gridId)) {
10+
required String gridId,
11+
required String fieldName,
12+
required FieldContextLoader fieldContextLoader,
13+
}) : super(FieldEditorState.initial(gridId, fieldName, fieldContextLoader)) {
2214
on<FieldEditorEvent>(
2315
(event, emit) async {
24-
await event.map(
25-
initial: (_InitialField value) async {
26-
await _getFieldTypeOptionContext(emit);
16+
await event.when(
17+
initial: () async {
18+
final fieldContext = GridFieldContext(gridId: gridId, loader: fieldContextLoader);
19+
await fieldContext.loadData().then((result) {
20+
result.fold(
21+
(l) => emit(state.copyWith(fieldContext: Some(fieldContext))),
22+
(r) => null,
23+
);
24+
});
2725
},
28-
updateName: (_UpdateName value) {
29-
final newContext = _updateEditContext(name: value.name);
30-
emit(state.copyWith(fieldTypeOptionData: newContext));
31-
},
32-
updateField: (_UpdateField value) {
33-
final data = _updateEditContext(field: value.field, typeOptionData: value.typeOptionData);
34-
emit(state.copyWith(fieldTypeOptionData: data));
35-
},
36-
done: (_Done value) async {
37-
await _saveField(emit);
26+
updateName: (name) {
27+
state.fieldContext.fold(() => null, (fieldContext) => fieldContext.fieldName = name);
28+
emit(state.copyWith(name: name));
3829
},
3930
);
4031
},
@@ -45,91 +36,27 @@ class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
4536
Future<void> close() async {
4637
return super.close();
4738
}
48-
49-
Option<FieldTypeOptionData> _updateEditContext({
50-
String? name,
51-
Field? field,
52-
List<int>? typeOptionData,
53-
}) {
54-
return state.fieldTypeOptionData.fold(
55-
() => none(),
56-
(context) {
57-
context.freeze();
58-
final newFieldTypeOptionData = context.rebuild((newContext) {
59-
newContext.field_2.rebuild((newField) {
60-
if (name != null) {
61-
newField.name = name;
62-
}
63-
64-
newContext.field_2 = newField;
65-
});
66-
67-
if (field != null) {
68-
newContext.field_2 = field;
69-
}
70-
71-
if (typeOptionData != null) {
72-
newContext.typeOptionData = typeOptionData;
73-
}
74-
});
75-
76-
FieldService.insertField(
77-
gridId: gridId,
78-
field: newFieldTypeOptionData.field_2,
79-
typeOptionData: newFieldTypeOptionData.typeOptionData,
80-
);
81-
82-
return Some(newFieldTypeOptionData);
83-
},
84-
);
85-
}
86-
87-
Future<void> _saveField(Emitter<FieldEditorState> emit) async {
88-
await state.fieldTypeOptionData.fold(
89-
() async => null,
90-
(data) async {
91-
final result = await FieldService.insertField(
92-
gridId: gridId,
93-
field: data.field_2,
94-
typeOptionData: data.typeOptionData,
95-
);
96-
result.fold((l) => null, (r) => null);
97-
},
98-
);
99-
}
100-
101-
Future<void> _getFieldTypeOptionContext(Emitter<FieldEditorState> emit) async {
102-
final result = await _loader.load();
103-
result.fold(
104-
(context) {
105-
emit(state.copyWith(
106-
fieldTypeOptionData: Some(context),
107-
));
108-
},
109-
(err) => Log.error(err),
110-
);
111-
}
11239
}
11340

11441
@freezed
11542
class FieldEditorEvent with _$FieldEditorEvent {
11643
const factory FieldEditorEvent.initial() = _InitialField;
11744
const factory FieldEditorEvent.updateName(String name) = _UpdateName;
118-
const factory FieldEditorEvent.updateField(Field field, Uint8List typeOptionData) = _UpdateField;
119-
const factory FieldEditorEvent.done() = _Done;
12045
}
12146

12247
@freezed
12348
class FieldEditorState with _$FieldEditorState {
12449
const factory FieldEditorState({
12550
required String gridId,
12651
required String errorText,
127-
required Option<FieldTypeOptionData> fieldTypeOptionData,
52+
required String name,
53+
required Option<GridFieldContext> fieldContext,
12854
}) = _FieldEditorState;
12955

130-
factory FieldEditorState.initial(String gridId) => FieldEditorState(
56+
factory FieldEditorState.initial(String gridId, String fieldName, FieldContextLoader loader) => FieldEditorState(
13157
gridId: gridId,
132-
fieldTypeOptionData: none(),
58+
fieldContext: none(),
13359
errorText: '',
60+
name: fieldName,
13461
);
13562
}

0 commit comments

Comments
 (0)