Skip to content

Fix crash when deleting all workouts from a cycle#501

Merged
9thLevelSoftware merged 2 commits into
mainfrom
claude/beautiful-einstein-rAbWU
Jun 7, 2026
Merged

Fix crash when deleting all workouts from a cycle#501
9thLevelSoftware merged 2 commits into
mainfrom
claude/beautiful-einstein-rAbWU

Conversation

@9thLevelSoftware

Copy link
Copy Markdown
Owner

Summary

  • Crash fix: CycleEditorViewModel.deleteItem() threw IndexOutOfBoundsException when swipe-deleting cycle items rapidly (reported via TestFlight, iPhone 14 Pro Max / iOS 26.5)
  • Root cause: SwipeToDismissBox.confirmValueChange fires with the Compose-captured index, but MutableStateFlow.update retries its lambda with the latest state — which may have fewer items after a concurrent delete
  • Fix: Added bounds checks (index !in state.items.indices) to all six index-based mutation methods: deleteItem, duplicateItem, reorderItems, convertToWorkout, convertToRest, changeRoutine

Test plan

  • Open cycle editor with multiple workout/rest days
  • Swipe-delete all items one by one rapidly — app should not crash
  • Delete last remaining item — should show empty state, no crash
  • Swipe-duplicate an item — verify it still works
  • Reorder items via drag — verify it still works
  • Undo a delete via snackbar — verify item is restored

https://claude.ai/code/session_011C2KidTSW3skiLyPgRTNqx


Generated by Claude Code

Add bounds checks to all index-based operations in CycleEditorViewModel.
The crash occurred because SwipeToDismissBox's confirmValueChange callback
can fire with a stale Compose-captured index after MutableStateFlow.update
retries its lambda with a newer (shorter) item list.

https://claude.ai/code/session_011C2KidTSW3skiLyPgRTNqx

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces bounds checks across several functions in CycleEditorViewModel.kt (such as deleteItem, duplicateItem, reorderItems, convertToWorkout, convertToRest, and changeRoutine) to prevent out-of-bounds errors. The review feedback suggests a minor optimization in reorderItems to return early when the source and destination indices are identical, avoiding unnecessary list operations.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.


fun reorderItems(from: Int, to: Int) {
_uiState.update { state ->
if (from !in state.items.indices || to !in state.items.indices) return@update state

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If from and to are equal, the reordering operation is a no-op. We can optimize this by returning early when from == to, which avoids unnecessary list copying, item removal, insertion, and renumbering.

Suggested change
if (from !in state.items.indices || to !in state.items.indices) return@update state
if (from == to || from !in state.items.indices || to !in state.items.indices) return@update state

@9thLevelSoftware 9thLevelSoftware marked this pull request as ready for review June 7, 2026 01:51
Copilot AI review requested due to automatic review settings June 7, 2026 01:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@9thLevelSoftware 9thLevelSoftware merged commit cf3db71 into main Jun 7, 2026
10 checks passed
@9thLevelSoftware 9thLevelSoftware deleted the claude/beautiful-einstein-rAbWU branch June 7, 2026 01:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 244356bc73

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


fun deleteItem(index: Int) {
_uiState.update { state ->
if (index !in state.items.indices) return@update state

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle stale delete indices without reusing undo state

When a stale onDelete arrives after a prior delete (for example, swipe-delete day 1 and then day 2 before recomposition, so the second captured index is no longer in state.items.indices), this guard returns without changing lastDeletedItem. CycleEditorScreen still shows the “Day removed” snackbar and may call undoDelete(), which then restores the previous deletion rather than the item represented by the second swipe, or reports a removal that never happened. In the rapid-delete path this fix targets, no-op deletes need to be reported/handled so snackbar and undo state stay aligned.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants