Skip to content

Commit 951206d

Browse files
committed
chore: handle edit state
1 parent 86e2091 commit 951206d

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

frontend/app_flowy/lib/plugins/board/presentation/card/board_cell.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:app_flowy/plugins/grid/application/prelude.dart';
22
import 'package:flowy_infra/notifier.dart';
3+
import 'package:flutter/material.dart';
34

45
abstract class FocusableBoardCell {
56
set becomeFocus(bool isFocus);
@@ -10,7 +11,16 @@ class EditableCellNotifier {
1011

1112
final Notifier resignFirstResponder = Notifier();
1213

13-
EditableCellNotifier();
14+
final ValueNotifier<bool> isCellEditing;
15+
16+
EditableCellNotifier({bool isEditing = false})
17+
: isCellEditing = ValueNotifier(isEditing);
18+
19+
void dispose() {
20+
becomeFirstResponder.dispose();
21+
resignFirstResponder.dispose();
22+
isCellEditing.dispose();
23+
}
1424
}
1525

1626
class EditableRowNotifier {
@@ -20,6 +30,11 @@ class EditableRowNotifier {
2030
GridCellIdentifier cellIdentifier,
2131
EditableCellNotifier notifier,
2232
) {
33+
final id = EditableCellId.from(cellIdentifier);
34+
_cells[id]?.dispose();
35+
36+
notifier.isCellEditing.addListener(() {});
37+
2338
_cells[EditableCellId.from(cellIdentifier)] = notifier;
2439
}
2540

@@ -36,6 +51,9 @@ class EditableRowNotifier {
3651
}
3752

3853
void clear() {
54+
for (final notifier in _cells.values) {
55+
notifier.dispose();
56+
}
3957
_cells.clear();
4058
}
4159

frontend/app_flowy/lib/plugins/board/presentation/card/board_text_cell.dart

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'define.dart';
99

1010
class BoardTextCell extends StatefulWidget with EditableCell {
1111
final String groupId;
12-
final bool isFocus;
1312
@override
1413
final EditableCellNotifier? editableNotifier;
1514
final GridCellControllerBuilder cellControllerBuilder;
@@ -18,7 +17,6 @@ class BoardTextCell extends StatefulWidget with EditableCell {
1817
required this.groupId,
1918
required this.cellControllerBuilder,
2019
this.editableNotifier,
21-
this.isFocus = false,
2220
Key? key,
2321
}) : super(key: key);
2422

@@ -39,21 +37,16 @@ class _BoardTextCellState extends State<BoardTextCell> {
3937
_cellBloc = BoardTextCellBloc(cellController: cellController)
4038
..add(const BoardTextCellEvent.initial());
4139
_controller = TextEditingController(text: _cellBloc.state.content);
42-
focusWhenInit = widget.isFocus;
43-
44-
if (widget.isFocus) {
40+
focusWhenInit = widget.editableNotifier?.isCellEditing.value ?? false;
41+
if (focusWhenInit) {
4542
focusNode.requestFocus();
4643
}
4744

4845
focusNode.addListener(() {
4946
if (!focusNode.hasFocus) {
47+
focusWhenInit = false;
48+
widget.editableNotifier?.isCellEditing.value = false;
5049
_cellBloc.add(const BoardTextCellEvent.enableEdit(false));
51-
52-
if (focusWhenInit) {
53-
setState(() {
54-
focusWhenInit = false;
55-
});
56-
}
5750
}
5851
});
5952
_bindEditableNotifier();

frontend/app_flowy/lib/plugins/board/presentation/card/card.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,20 @@ class _BoardCardState extends State<BoardCard> {
9696
rowNotifier.clear();
9797
cells.asMap().forEach(
9898
(int index, GridCellIdentifier cellId) {
99-
final cellNotifier = EditableCellNotifier();
99+
EditableCellNotifier cellNotifier;
100+
if (index == 0) {
101+
cellNotifier = EditableCellNotifier(isEditing: widget.isEditing);
102+
rowNotifier.insertCell(cellId, cellNotifier);
103+
} else {
104+
cellNotifier = EditableCellNotifier();
105+
}
106+
100107
Widget child = widget.cellBuilder.buildCell(
101108
widget.groupId,
102109
cellId,
103-
index == 0 ? widget.isEditing : false,
104110
cellNotifier,
105111
);
106112

107-
if (index == 0) {
108-
rowNotifier.insertCell(cellId, cellNotifier);
109-
}
110113
child = Padding(
111114
key: cellId.key(),
112115
padding: const EdgeInsets.only(left: 4, right: 4),

frontend/app_flowy/lib/plugins/board/presentation/card/card_cell_builder.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class BoardCellBuilder {
2323
Widget buildCell(
2424
String groupId,
2525
GridCellIdentifier cellId,
26-
bool isEditing,
2726
EditableCellNotifier cellNotifier,
2827
) {
2928
final cellControllerBuilder = GridCellControllerBuilder(
@@ -69,7 +68,6 @@ class BoardCellBuilder {
6968
return BoardTextCell(
7069
groupId: groupId,
7170
cellControllerBuilder: cellControllerBuilder,
72-
isFocus: isEditing,
7371
editableNotifier: cellNotifier,
7472
key: key,
7573
);

0 commit comments

Comments
 (0)