Skip to content

Refactor task editor - #29

Merged
Markel15 merged 10 commits into
masterfrom
refactor-task_editor
Oct 12, 2026
Merged

Markel15 merged 10 commits into
masterfrom
refactor-task_editor

Conversation

@Markel15

Copy link
Copy Markdown
Owner

Fixed a lot of possible bugs connected to the architecture. Mainly the bugs were about possible race conditions or windows that should be ahrdened to avoid any problem.

Improve architecture and maintenance of the code.

Added some test that check some actions that should work after the refactorization.
Avoid using some duplicated code, now everything is managed from the same variable and makes it easier to keep everything synchronized.

Updated tests to match the new logic.
- Replace nullable `TaskEditorState` with a sealed interface (`Loading`, `Ready`, `NotFound`, `Error`) to cleanly handle multiple loading and error states.
- Add `taskOrNull` extension property for safe access to the task only when in the `Ready` state.
- Refactor task loading to use `repository.getTaskById(taskId)` directly instead of querying and filtering the entire list.
- Prevent race conditions by cancelling previous load jobs (`loadTaskJob?.cancel()`) when a new task request comes in.
- Properly propagate `CancellationException` while converting other repository failures into the `Error` state.
- Centralize state updates via `updateReadyTask` to ensure editing actions only execute when the editor state is `Ready`.
- Remove non-null assertions (`!!`) and safely handle deletion with `task?.let`.
- Introduce `TaskEditorTopBar.actionsEnabled` parameter and disable action buttons while the task is loading.
- Implement exhaustive `when` branches in `TaskEditorScreen` to render loading indicators, error views, retry buttons, and the main editor content.
- Add localized resource strings for task-not-found, load errors, and retry actions across base, English, and Spanish values.
- Update unit tests to mock `getTaskById` and explicitly verify new editor states (`Loading`, `NotFound`, `Error`, `taskOrNull`).
- Update TaskEditorState.Ready to include an autosaveRevision counter,
  allowing precise tracking of field modifications that require persistence.
- Refactor TaskEditorViewModel with granular update methods (title, description,
  priority, dueDate, subtasks) that increment the autosave revision.
- Re-architect TaskEditorSheetContent to use a revision-triggered LaunchedEffect
  for a cleaner 600ms debounce mechanism, avoiding full draft copy passing.
- Refactor a DisposableEffect in the UI to safely flush any pending revisions on disposal.
- Ensure robust concurrency control and alarm reconciliation via Mutex and persistedTask snapshots.
The UI doesn't need to know anything about the debounce, now that is separated from the Compose file.
The logic now cancels the debounced autosave and persists any pending changes. If the saving process fails the editor doesn't exit without showing a warning
Now the logic is hardened so if a pending update was waiting the lock after deleting a task doesn't recreate it.
A deletion may occur before flushed gets the lock, that's why I added a check inside, to return in that case and don't try persisting any change if the task was deleted.
The effect is no longer needed, if an emergency autosave is needed is managed by the flushPendingChanges method safely. That method is better because guarantees that the save is made before navigating back if fails shows an error instead of exiting without a warning.
Use the created methods to reconcile alarms.
@Markel15
Markel15 merged commit 53938da into master Oct 12, 2026
1 check passed
@Markel15
Markel15 deleted the refactor-task_editor branch October 12, 2026 01:01
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.

1 participant