-
-
Notifications
You must be signed in to change notification settings - Fork 28
Improve Editor code quality, migrate editor state management to EditorViewModel
#220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ed3016b
0817c81
604c2c4
9afa2a5
0a9ef64
639ee31
83a905e
4e8ef6b
5fc6226
52ce406
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,8 @@ import com.ethran.notable.editor.state.EditorState | |
| import com.ethran.notable.editor.state.History | ||
| import com.ethran.notable.editor.state.HistoryBusActions | ||
| import com.ethran.notable.editor.state.Mode | ||
| import com.ethran.notable.editor.state.Operation | ||
| import com.ethran.notable.editor.state.PlacementMode | ||
| import com.ethran.notable.editor.state.Operation | ||
| import com.ethran.notable.editor.state.SelectionState | ||
| import com.ethran.notable.editor.state.UndoRedoType | ||
| import com.ethran.notable.editor.utils.divideStrokesFromCut | ||
|
|
@@ -90,19 +90,9 @@ class EditorControlTower( | |
| * @param id The unique identifier of the page to switch to. | ||
| */ | ||
| private suspend fun switchPage(id: String) { | ||
| // TODO: Check if this is a problem: | ||
| //`switchPage()` now calls `page.changePage(id)` inside `withContext(Dispatchers.IO)`, | ||
| // but `PageView.changePage()` mutates in-memory state (e.g., `currentPageId`, | ||
| // `zoomLevel.value`) before it does its own IO work. Calling it from IO risks | ||
| // off-main state mutations and also makes it easier for callers to accidentally | ||
| // invoke `page.changePage()` twice (which currently happens in `registerObservers()`). | ||
| // Prefer calling `page.changePage(id)` from the main thread (or let | ||
| // `PageView.changePage()` manage its own dispatching) and ensure callers don’t call | ||
| // it again after `switchPage()`. | ||
|
|
||
| // Switch to Main thread for Compose state mutations | ||
| withContext(Dispatchers.Main) { | ||
| state.changePage(id) | ||
| state.viewModel.changePage(id) | ||
| history.cleanHistory() | ||
| } | ||
|
|
||
|
|
@@ -121,11 +111,11 @@ class EditorControlTower( | |
| } | ||
|
|
||
| fun toggleTool() { | ||
| state.mode = if (state.mode == Mode.Draw) Mode.Erase else Mode.Draw | ||
| state.viewModel.onToolbarAction(ToolbarAction.ChangeMode(if (state.mode == Mode.Draw) Mode.Erase else Mode.Draw)) | ||
| } | ||
|
|
||
| fun toggleZen() { | ||
| state.isToolbarOpen = !state.isToolbarOpen | ||
| state.viewModel.onToolbarAction(ToolbarAction.ToggleToolbar) | ||
| } | ||
|
|
||
| fun getSnapshotOfSelectionState(): SelectionState { | ||
|
|
@@ -137,21 +127,15 @@ class EditorControlTower( | |
| } | ||
|
|
||
| fun goToNextPage() { | ||
| scope.launch(Dispatchers.IO) { | ||
| logEditorControlTower.i("Going to next page") | ||
| val next = state.getNextPage() | ||
| if (next != null) | ||
| switchPage(next) | ||
| } | ||
| logEditorControlTower.i("Going to next page") | ||
| state.viewModel.goToNextPage() | ||
| history.cleanHistory() | ||
| } | ||
|
Comment on lines
129
to
133
|
||
|
|
||
| fun goToPreviousPage() { | ||
| scope.launch(Dispatchers.IO) { | ||
| logEditorControlTower.i("Going to previous page") | ||
| val previous = state.getPreviousPage() | ||
| if (previous != null) | ||
| switchPage(previous) | ||
| } | ||
| logEditorControlTower.i("Going to previous page") | ||
| state.viewModel.goToPreviousPage() | ||
| history.cleanHistory() | ||
| } | ||
|
Comment on lines
135
to
139
|
||
|
|
||
| fun undo() { | ||
|
|
@@ -325,5 +309,4 @@ class EditorControlTower( | |
|
|
||
| showHint("Pasted content from clipboard", scope) | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
switchPage(), the laterpage.changePage(id)call (in the IO block) runs off the main thread, butPageView.changePage()mutates in-memory state immediately (e.g.,currentPageId,zoomLevel.value). This can introduce thread-safety issues, andregisterObservers()also callspage.changePage(pageId)afterswitchPage(pageId), which risks double-switching. Consider ensuringpage.changePage(id)is invoked on the main thread (or thatPageView.changePage()handles dispatching internally) and that it’s only called once per page change.