Skip to content

Commit bb7cddc

Browse files
committed
chore: reload group when group by new field
1 parent e75d8f2 commit bb7cddc

File tree

26 files changed

+353
-209
lines changed

26 files changed

+353
-209
lines changed

frontend/app_flowy/lib/plugins/board/application/board_bloc.dart

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
110110
emit(state.copyWith(noneOrError: some(error)));
111111
},
112112
didReceiveGroups: (List<GroupPB> groups) {
113-
emit(state.copyWith(
114-
groupIds: groups.map((group) => group.groupId).toList(),
115-
));
113+
emit(
114+
state.copyWith(
115+
groupIds: groups.map((group) => group.groupId).toList(),
116+
),
117+
);
116118
},
117119
);
118120
},
@@ -154,6 +156,23 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
154156
}
155157

156158
void initializeGroups(List<GroupPB> groups) {
159+
for (var controller in groupControllers.values) {
160+
controller.dispose();
161+
}
162+
groupControllers.clear();
163+
boardController.clear();
164+
165+
//
166+
List<AFBoardColumnData> columns = groups.map((group) {
167+
return AFBoardColumnData(
168+
id: group.groupId,
169+
name: group.desc,
170+
items: _buildRows(group),
171+
customData: group,
172+
);
173+
}).toList();
174+
boardController.addColumns(columns);
175+
157176
for (final group in groups) {
158177
final delegate = GroupControllerDelegateImpl(
159178
controller: boardController,
@@ -184,16 +203,6 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
184203
}
185204
},
186205
didLoadGroups: (groups) {
187-
List<AFBoardColumnData> columns = groups.map((group) {
188-
return AFBoardColumnData(
189-
id: group.groupId,
190-
name: group.desc,
191-
items: _buildRows(group),
192-
customData: group,
193-
);
194-
}).toList();
195-
196-
boardController.addColumns(columns);
197206
initializeGroups(groups);
198207
add(BoardEvent.didReceiveGroups(groups));
199208
},
@@ -204,18 +213,19 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
204213
//
205214
},
206215
onUpdatedGroup: (updatedGroups) {
207-
//
208216
for (final group in updatedGroups) {
209217
final columnController =
210218
boardController.getColumnController(group.groupId);
211-
if (columnController != null) {
212-
columnController.updateColumnName(group.desc);
213-
}
219+
columnController?.updateColumnName(group.desc);
214220
}
215221
},
216222
onError: (err) {
217223
Log.error(err);
218224
},
225+
onResetGroups: (groups) {
226+
initializeGroups(groups);
227+
add(BoardEvent.didReceiveGroups(groups));
228+
},
219229
);
220230
}
221231

frontend/app_flowy/lib/plugins/board/application/board_data_controller.dart

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef DidLoadGroups = void Function(List<GroupPB>);
1818
typedef OnUpdatedGroup = void Function(List<GroupPB>);
1919
typedef OnDeletedGroup = void Function(List<String>);
2020
typedef OnInsertedGroup = void Function(List<InsertedGroupPB>);
21+
typedef OnResetGroups = void Function(List<GroupPB>);
2122

2223
typedef OnRowsChanged = void Function(
2324
List<RowInfo>,
@@ -65,6 +66,7 @@ class BoardDataController {
6566
required OnUpdatedGroup onUpdatedGroup,
6667
required OnDeletedGroup onDeletedGroup,
6768
required OnInsertedGroup onInsertedGroup,
69+
required OnResetGroups onResetGroups,
6870
required OnError? onError,
6971
}) {
7072
_onGridChanged = onGridChanged;
@@ -77,24 +79,32 @@ class BoardDataController {
7779
_onFieldsChanged?.call(UnmodifiableListView(fields));
7880
});
7981

80-
_listener.start(onBoardChanged: (result) {
81-
result.fold(
82-
(changeset) {
83-
if (changeset.updateGroups.isNotEmpty) {
84-
onUpdatedGroup.call(changeset.updateGroups);
85-
}
86-
87-
if (changeset.insertedGroups.isNotEmpty) {
88-
onInsertedGroup.call(changeset.insertedGroups);
89-
}
90-
91-
if (changeset.deletedGroups.isNotEmpty) {
92-
onDeletedGroup.call(changeset.deletedGroups);
93-
}
94-
},
95-
(e) => _onError?.call(e),
96-
);
97-
});
82+
_listener.start(
83+
onBoardChanged: (result) {
84+
result.fold(
85+
(changeset) {
86+
if (changeset.updateGroups.isNotEmpty) {
87+
onUpdatedGroup.call(changeset.updateGroups);
88+
}
89+
90+
if (changeset.insertedGroups.isNotEmpty) {
91+
onInsertedGroup.call(changeset.insertedGroups);
92+
}
93+
94+
if (changeset.deletedGroups.isNotEmpty) {
95+
onDeletedGroup.call(changeset.deletedGroups);
96+
}
97+
},
98+
(e) => _onError?.call(e),
99+
);
100+
},
101+
onGroupByNewField: (result) {
102+
result.fold(
103+
(groups) => onResetGroups(groups),
104+
(e) => _onError?.call(e),
105+
);
106+
},
107+
);
98108
}
99109

100110
Future<Either<Unit, FlowyError>> loadData() async {

frontend/app_flowy/lib/plugins/board/application/board_listener.dart

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ import 'package:flowy_infra/notifier.dart';
55
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
66
import 'package:flowy_sdk/protobuf/flowy-grid/dart_notification.pb.dart';
77
import 'package:dartz/dartz.dart';
8+
import 'package:flowy_sdk/protobuf/flowy-grid/group.pb.dart';
89
import 'package:flowy_sdk/protobuf/flowy-grid/group_changeset.pb.dart';
910

10-
typedef UpdateBoardNotifiedValue = Either<GroupViewChangesetPB, FlowyError>;
11+
typedef GroupUpdateValue = Either<GroupViewChangesetPB, FlowyError>;
12+
typedef GroupByNewFieldValue = Either<List<GroupPB>, FlowyError>;
1113

1214
class BoardListener {
1315
final String viewId;
14-
PublishNotifier<UpdateBoardNotifiedValue>? _groupNotifier = PublishNotifier();
16+
PublishNotifier<GroupUpdateValue>? _groupUpdateNotifier = PublishNotifier();
17+
PublishNotifier<GroupByNewFieldValue>? _groupByNewFieldNotifier =
18+
PublishNotifier();
1519
GridNotificationListener? _listener;
1620
BoardListener(this.viewId);
1721

1822
void start({
19-
required void Function(UpdateBoardNotifiedValue) onBoardChanged,
23+
required void Function(GroupUpdateValue) onBoardChanged,
24+
required void Function(GroupByNewFieldValue) onGroupByNewField,
2025
}) {
21-
_groupNotifier?.addPublishListener(onBoardChanged);
26+
_groupUpdateNotifier?.addPublishListener(onBoardChanged);
27+
_groupByNewFieldNotifier?.addPublishListener(onGroupByNewField);
2228
_listener = GridNotificationListener(
2329
objectId: viewId,
2430
handler: _handler,
@@ -32,9 +38,16 @@ class BoardListener {
3238
switch (ty) {
3339
case GridNotification.DidUpdateGroupView:
3440
result.fold(
35-
(payload) => _groupNotifier?.value =
41+
(payload) => _groupUpdateNotifier?.value =
3642
left(GroupViewChangesetPB.fromBuffer(payload)),
37-
(error) => _groupNotifier?.value = right(error),
43+
(error) => _groupUpdateNotifier?.value = right(error),
44+
);
45+
break;
46+
case GridNotification.DidGroupByNewField:
47+
result.fold(
48+
(payload) => _groupByNewFieldNotifier?.value =
49+
left(GroupViewChangesetPB.fromBuffer(payload).newGroups),
50+
(error) => _groupByNewFieldNotifier?.value = right(error),
3851
);
3952
break;
4053
default:
@@ -44,7 +57,10 @@ class BoardListener {
4457

4558
Future<void> stop() async {
4659
await _listener?.stop();
47-
_groupNotifier?.dispose();
48-
_groupNotifier = null;
60+
_groupUpdateNotifier?.dispose();
61+
_groupUpdateNotifier = null;
62+
63+
_groupByNewFieldNotifier?.dispose();
64+
_groupByNewFieldNotifier = null;
4965
}
5066
}

frontend/app_flowy/packages/appflowy_board/lib/src/widgets/board_data.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class AFBoardDataController extends ChangeNotifier
8989
if (columnIds.isNotEmpty && notify) notifyListeners();
9090
}
9191

92+
void clear() {
93+
_columnDatas.clear();
94+
notifyListeners();
95+
}
96+
9297
AFBoardColumnDataController? getColumnController(String columnId) {
9398
final columnController = _columnControllers[columnId];
9499
if (columnController == null) {

frontend/rust-lib/flowy-grid/src/dart_notification.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum GridNotification {
1313
DidUpdateField = 50,
1414
DidUpdateGroupView = 60,
1515
DidUpdateGroup = 61,
16+
DidGroupByNewField = 62,
1617
}
1718

1819
impl std::default::Default for GridNotification {

frontend/rust-lib/flowy-grid/src/entities/group_entities/group_changeset.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ pub struct GroupViewChangesetPB {
145145

146146
impl GroupViewChangesetPB {
147147
pub fn is_empty(&self) -> bool {
148-
self.inserted_groups.is_empty() && self.deleted_groups.is_empty() && self.update_groups.is_empty()
148+
self.new_groups.is_empty()
149+
&& self.inserted_groups.is_empty()
150+
&& self.deleted_groups.is_empty()
151+
&& self.update_groups.is_empty()
149152
}
150153
}
151154

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use crate::entities::{FieldChangesetParams, FieldType};
2-
use crate::services::field::{select_option_operation, SelectOptionPB};
1+
use crate::services::field::{MultiSelectTypeOptionPB, SingleSelectTypeOptionPB};
32
use crate::services::grid_editor::GridRevisionEditor;
43
use flowy_error::FlowyResult;
5-
use flowy_grid_data_model::revision::{FieldRevision, TypeOptionDataDeserializer, TypeOptionDataFormat};
4+
use flowy_grid_data_model::revision::{TypeOptionDataDeserializer, TypeOptionDataFormat};
65
use std::sync::Arc;
76

8-
pub async fn edit_field<T>(
7+
pub async fn edit_field_type_option<T>(
98
field_id: &str,
109
editor: Arc<GridRevisionEditor>,
11-
action: impl FnOnce(&mut T) -> bool,
10+
action: impl FnOnce(&mut T),
1211
) -> FlowyResult<()>
1312
where
1413
T: TypeOptionDataDeserializer + TypeOptionDataFormat,
@@ -19,29 +18,28 @@ where
1918
};
2019

2120
if let Some(mut type_option) = get_type_option.await {
22-
if action(&mut type_option) {
23-
let changeset = FieldChangesetParams { ..Default::default() };
24-
let _ = editor.update_field(changeset).await?;
25-
}
21+
action(&mut type_option);
22+
let bytes = type_option.protobuf_bytes().to_vec();
23+
let _ = editor
24+
.update_field_type_option(&editor.grid_id, field_id, bytes)
25+
.await?;
2626
}
2727

2828
Ok(())
2929
}
3030

31-
pub fn insert_single_select_option(field_rev: &mut FieldRevision, options: Vec<SelectOptionPB>) -> FlowyResult<()> {
32-
if options.is_empty() {
33-
return Ok(());
34-
}
35-
let mut type_option = select_option_operation(field_rev)?;
36-
options.into_iter().for_each(|option| type_option.insert_option(option));
37-
Ok(())
31+
pub async fn edit_single_select_type_option(
32+
field_id: &str,
33+
editor: Arc<GridRevisionEditor>,
34+
action: impl FnOnce(&mut SingleSelectTypeOptionPB),
35+
) -> FlowyResult<()> {
36+
edit_field_type_option(field_id, editor, action).await
3837
}
3938

40-
pub fn insert_multi_select_option(field_rev: &mut FieldRevision, options: Vec<SelectOptionPB>) -> FlowyResult<()> {
41-
if options.is_empty() {
42-
return Ok(());
43-
}
44-
let mut type_option = select_option_operation(field_rev)?;
45-
options.into_iter().for_each(|option| type_option.insert_option(option));
46-
Ok(())
39+
pub async fn edit_multi_select_type_option(
40+
field_id: &str,
41+
editor: Arc<GridRevisionEditor>,
42+
action: impl FnOnce(&mut MultiSelectTypeOptionPB),
43+
) -> FlowyResult<()> {
44+
edit_field_type_option(field_id, editor, action).await
4745
}

frontend/rust-lib/flowy-grid/src/services/grid_editor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ impl GridRevisionEditor {
212212
}
213213

214214
pub async fn group_field(&self, field_id: &str) -> FlowyResult<()> {
215-
todo!()
215+
let _ = self.view_manager.group_by_field(field_id).await?;
216+
Ok(())
216217
}
217218

218219
pub async fn switch_to_field_type(&self, field_id: &str, field_type: &FieldType) -> FlowyResult<()> {

0 commit comments

Comments
 (0)