From 951206db7107c264e2e7f89b95b381ef85f461b0 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 6 Sep 2022 21:10:41 +0800 Subject: [PATCH] chore: handle edit state --- .../board/presentation/card/board_cell.dart | 20 ++++++++++++++++++- .../presentation/card/board_text_cell.dart | 15 ++++---------- .../plugins/board/presentation/card/card.dart | 13 +++++++----- .../presentation/card/card_cell_builder.dart | 2 -- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/frontend/app_flowy/lib/plugins/board/presentation/card/board_cell.dart b/frontend/app_flowy/lib/plugins/board/presentation/card/board_cell.dart index e8a0406e0f..77fc4b0f46 100644 --- a/frontend/app_flowy/lib/plugins/board/presentation/card/board_cell.dart +++ b/frontend/app_flowy/lib/plugins/board/presentation/card/board_cell.dart @@ -1,5 +1,6 @@ import 'package:app_flowy/plugins/grid/application/prelude.dart'; import 'package:flowy_infra/notifier.dart'; +import 'package:flutter/material.dart'; abstract class FocusableBoardCell { set becomeFocus(bool isFocus); @@ -10,7 +11,16 @@ class EditableCellNotifier { final Notifier resignFirstResponder = Notifier(); - EditableCellNotifier(); + final ValueNotifier isCellEditing; + + EditableCellNotifier({bool isEditing = false}) + : isCellEditing = ValueNotifier(isEditing); + + void dispose() { + becomeFirstResponder.dispose(); + resignFirstResponder.dispose(); + isCellEditing.dispose(); + } } class EditableRowNotifier { @@ -20,6 +30,11 @@ class EditableRowNotifier { GridCellIdentifier cellIdentifier, EditableCellNotifier notifier, ) { + final id = EditableCellId.from(cellIdentifier); + _cells[id]?.dispose(); + + notifier.isCellEditing.addListener(() {}); + _cells[EditableCellId.from(cellIdentifier)] = notifier; } @@ -36,6 +51,9 @@ class EditableRowNotifier { } void clear() { + for (final notifier in _cells.values) { + notifier.dispose(); + } _cells.clear(); } diff --git a/frontend/app_flowy/lib/plugins/board/presentation/card/board_text_cell.dart b/frontend/app_flowy/lib/plugins/board/presentation/card/board_text_cell.dart index c6a6fb13cc..08068a8e49 100644 --- a/frontend/app_flowy/lib/plugins/board/presentation/card/board_text_cell.dart +++ b/frontend/app_flowy/lib/plugins/board/presentation/card/board_text_cell.dart @@ -9,7 +9,6 @@ import 'define.dart'; class BoardTextCell extends StatefulWidget with EditableCell { final String groupId; - final bool isFocus; @override final EditableCellNotifier? editableNotifier; final GridCellControllerBuilder cellControllerBuilder; @@ -18,7 +17,6 @@ class BoardTextCell extends StatefulWidget with EditableCell { required this.groupId, required this.cellControllerBuilder, this.editableNotifier, - this.isFocus = false, Key? key, }) : super(key: key); @@ -39,21 +37,16 @@ class _BoardTextCellState extends State { _cellBloc = BoardTextCellBloc(cellController: cellController) ..add(const BoardTextCellEvent.initial()); _controller = TextEditingController(text: _cellBloc.state.content); - focusWhenInit = widget.isFocus; - - if (widget.isFocus) { + focusWhenInit = widget.editableNotifier?.isCellEditing.value ?? false; + if (focusWhenInit) { focusNode.requestFocus(); } focusNode.addListener(() { if (!focusNode.hasFocus) { + focusWhenInit = false; + widget.editableNotifier?.isCellEditing.value = false; _cellBloc.add(const BoardTextCellEvent.enableEdit(false)); - - if (focusWhenInit) { - setState(() { - focusWhenInit = false; - }); - } } }); _bindEditableNotifier(); diff --git a/frontend/app_flowy/lib/plugins/board/presentation/card/card.dart b/frontend/app_flowy/lib/plugins/board/presentation/card/card.dart index cd088a3d5b..a2fc7ee7f5 100644 --- a/frontend/app_flowy/lib/plugins/board/presentation/card/card.dart +++ b/frontend/app_flowy/lib/plugins/board/presentation/card/card.dart @@ -96,17 +96,20 @@ class _BoardCardState extends State { rowNotifier.clear(); cells.asMap().forEach( (int index, GridCellIdentifier cellId) { - final cellNotifier = EditableCellNotifier(); + EditableCellNotifier cellNotifier; + if (index == 0) { + cellNotifier = EditableCellNotifier(isEditing: widget.isEditing); + rowNotifier.insertCell(cellId, cellNotifier); + } else { + cellNotifier = EditableCellNotifier(); + } + Widget child = widget.cellBuilder.buildCell( widget.groupId, cellId, - index == 0 ? widget.isEditing : false, cellNotifier, ); - if (index == 0) { - rowNotifier.insertCell(cellId, cellNotifier); - } child = Padding( key: cellId.key(), padding: const EdgeInsets.only(left: 4, right: 4), diff --git a/frontend/app_flowy/lib/plugins/board/presentation/card/card_cell_builder.dart b/frontend/app_flowy/lib/plugins/board/presentation/card/card_cell_builder.dart index 99bd3f3b3a..1485bb7bd0 100644 --- a/frontend/app_flowy/lib/plugins/board/presentation/card/card_cell_builder.dart +++ b/frontend/app_flowy/lib/plugins/board/presentation/card/card_cell_builder.dart @@ -23,7 +23,6 @@ class BoardCellBuilder { Widget buildCell( String groupId, GridCellIdentifier cellId, - bool isEditing, EditableCellNotifier cellNotifier, ) { final cellControllerBuilder = GridCellControllerBuilder( @@ -69,7 +68,6 @@ class BoardCellBuilder { return BoardTextCell( groupId: groupId, cellControllerBuilder: cellControllerBuilder, - isFocus: isEditing, editableNotifier: cellNotifier, key: key, );