Skip to content

Commit

Permalink
chore: fix checklist potential panic (AppFlowy-IO#5561)
Browse files Browse the repository at this point in the history
* chore: fix checklist

* chore: fix checklist
  • Loading branch information
appflowy authored Jun 18, 2024
1 parent e607694 commit 3e75f1f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ class _ChecklistItemsState extends State<ChecklistItems> {
key: ValueKey(task.data.id),
task: task,
autofocus: widget.state.newTask && index == tasks.length - 1,
onSubmitted: index == tasks.length - 1
? () => widget.bloc
.add(const ChecklistCellEvent.createNewTask(""))
: null,
onSubmitted: () {
if (index == tasks.length - 1) {
widget.bloc.add(const ChecklistCellEvent.createNewTask(""));
}
},
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ class _ChecklistItemState extends State<ChecklistItem> {
super.didUpdateWidget(oldWidget);
if (widget.task.data.name != oldWidget.task.data.name) {
final selection = _textController.selection;
_textController.text = widget.task.data.name;
_textController.selection = selection;
// Ensure the selection offset is within the new text bounds
int offset = selection.start;
if (offset > widget.task.data.name.length) {
offset = widget.task.data.name.length;
}
_textController.selection = TextSelection.collapsed(offset: offset);
}
}

Expand All @@ -204,13 +208,20 @@ class _ChecklistItemState extends State<ChecklistItem> {
},
actions: {
_SelectTaskIntent: CallbackAction<_SelectTaskIntent>(
onInvoke: (_SelectTaskIntent intent) => context
.read<ChecklistCellBloc>()
.add(ChecklistCellEvent.selectTask(widget.task.data.id)),
onInvoke: (_SelectTaskIntent intent) {
// Log.debug("checklist widget on enter");
context
.read<ChecklistCellBloc>()
.add(ChecklistCellEvent.selectTask(widget.task.data.id));
return;
},
),
_EndEditingTaskIntent: CallbackAction<_EndEditingTaskIntent>(
onInvoke: (_EndEditingTaskIntent intent) =>
_textFieldFocusNode.unfocus(),
onInvoke: (_EndEditingTaskIntent intent) {
// Log.debug("checklist widget on escape");
_textFieldFocusNode.unfocus();
return;
},
),
},
shortcuts: {
Expand Down Expand Up @@ -278,12 +289,14 @@ class _ChecklistItemState extends State<ChecklistItem> {
}
},
onSubmitted: (description) {
_submitUpdateTaskDescription(description);
if (widget.onSubmitted != null) {
// Log.debug("checklist widget on submitted");
widget.onSubmitted?.call();
} else {
// Log.debug("checklist widget Focus next task");
Actions.invoke(context, const NextFocusIntent());
}
_submitUpdateTaskDescription(description);
},
);
},
Expand Down Expand Up @@ -454,8 +467,7 @@ class _DeleteTaskButtonState extends State<_DeleteTaskButton> {
statesController: _materialStatesController,
child: FlowySvg(
FlowySvgs.delete_s,
color: _materialStatesController.value
.contains(WidgetState.hovered) ||
color: _materialStatesController.value.contains(WidgetState.hovered) ||
_materialStatesController.value.contains(WidgetState.focused)
? Theme.of(context).colorScheme.error
: null,
Expand Down
3 changes: 2 additions & 1 deletion frontend/rust-lib/lib-infra/src/priority_task/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ impl TaskQueue {
match self.index_tasks.entry(task.handler_id.clone()) {
Entry::Occupied(entry) => {
let mut list = entry.get().borrow_mut();
assert!(list

debug_assert!(list
.peek()
.map(|old_id| pending_task.id >= old_id.id)
.unwrap_or(true));
Expand Down
4 changes: 2 additions & 2 deletions frontend/rust-lib/lib-infra/src/priority_task/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl TaskStore {
}

pub(crate) fn next_task_id(&self) -> TaskId {
let _ = self.task_id_counter.fetch_add(1, SeqCst);
self.task_id_counter.load(SeqCst)
let old = self.task_id_counter.fetch_add(1, SeqCst);
old + 1
}
}

0 comments on commit 3e75f1f

Please sign in to comment.