Skip to content

Commit

Permalink
refactor: GridCellRequestFocusNotifier only accepts single listener
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Apr 30, 2022
1 parent 371f753 commit 6b5126b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ abstract class GridCellWidget extends HoverWidget {
}

class GridCellRequestFocusNotifier extends ChangeNotifier {
VoidCallback? _listener;

@override
void addListener(VoidCallback listener) {
if (_listener != null) {
removeListener(_listener!);
}

_listener = listener;
super.addListener(listener);
}

void removeAllListener() {
if (_listener != null) {
removeListener(_listener!);
}
}

void notify() {
notifyListeners();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class _CheckboxCellState extends State<CheckboxCell> {

@override
Future<void> dispose() async {
widget.requestFocus.removeAllListener();
_cellBloc.close();
super.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class _NumberCellState extends State<NumberCell> {
late NumberCellBloc _cellBloc;
late TextEditingController _controller;
late FocusNode _focusNode;
VoidCallback? _focusListener;
Timer? _delayOperation;

@override
Expand Down Expand Up @@ -70,9 +69,7 @@ class _NumberCellState extends State<NumberCell> {

@override
Future<void> dispose() async {
if (_focusListener != null) {
widget.requestFocus.removeListener(_focusListener!);
}
widget.requestFocus.removeAllListener();
_delayOperation?.cancel();
_cellBloc.close();
_focusNode.dispose();
Expand All @@ -96,17 +93,10 @@ class _NumberCellState extends State<NumberCell> {
}

void _listenCellRequestFocus(BuildContext context) {
if (_focusListener != null) {
widget.requestFocus.removeListener(_focusListener!);
}

focusListener() {
widget.requestFocus.addListener(() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode);
}
}

_focusListener = focusListener;
widget.requestFocus.addListener(focusListener);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class _SelectOptionCell extends StatelessWidget {
);
}

final cellContext = cellContextBuilder.build() as GridSelectOptionCellContext;
return Stack(
alignment: AlignmentDirectional.center,
fit: StackFit.expand,
Expand All @@ -166,6 +165,7 @@ class _SelectOptionCell extends StatelessWidget {
InkWell(
onTap: () {
onFocus(true);
final cellContext = cellContextBuilder.build() as GridSelectOptionCellContext;
SelectOptionCellEditor.show(context, cellContext, () => onFocus(false));
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class _GridTextCellState extends State<GridTextCell> {
late TextCellBloc _cellBloc;
late TextEditingController _controller;
late FocusNode _focusNode;
VoidCallback? _focusListener;

VoidCallback? _focusNodeListener;
Timer? _delayOperation;

@override
Expand Down Expand Up @@ -87,32 +88,29 @@ class _GridTextCellState extends State<GridTextCell> {
);
}

void _listenCellRequestFocus(BuildContext context) {
if (_focusListener != null) {
widget.requestFocus.removeListener(_focusListener!);
}

focusListener() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode);
}
}

_focusListener = focusListener;
widget.requestFocus.addListener(focusListener);
}

@override
Future<void> dispose() async {
if (_focusListener != null) {
widget.requestFocus.removeListener(_focusListener!);
}
widget.requestFocus.removeAllListener();
_delayOperation?.cancel();
_cellBloc.close();
_focusNode.dispose();
super.dispose();
}

@override
void didUpdateWidget(covariant GridTextCell oldWidget) {
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
}

void _listenCellRequestFocus(BuildContext context) {
widget.requestFocus.addListener(() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode);
}
});
}

Future<void> focusChanged() async {
if (mounted) {
_delayOperation?.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class _RowCells extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<RowBloc, RowState>(
buildWhen: (previous, current) => previous.cellDataMap.length != current.cellDataMap.length,
buildWhen: (previous, current) => previous.cellDataMap != current.cellDataMap,
builder: (context, state) {
return IntrinsicHeight(
child: Row(
Expand Down

0 comments on commit 6b5126b

Please sign in to comment.