Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 54
ADFA-2369 | Avoid file I/O on main thread#754
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
Merged
+183
−98
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f27157
fix(layouteditor): move layout save/import file I/O off the main thread
jatezzz 503631d
Merge remote-tracking branch 'origin/stage' into fix/ADFA-2369-file-i…
jatezzz 0be21af
fix(layouteditor): Avoid possible race condition
jatezzz aedfda5
fix(layouteditor): Avoid IO operations on main thread when user disca…
jatezzz 94b61ee
Merge branch 'stage' into fix/ADFA-2369-file-io-on-main-thread
jatezzz 4d889c0
Merge branch 'stage' into fix/ADFA-2369-file-io-on-main-thread
jatezzz bd8500e
Merge branch 'stage' into fix/ADFA-2369-file-io-on-main-thread
jatezzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
100 changes: 61 additions & 39 deletions
100 app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -73,6 +73,7 @@ import com.itsaky.androidide.utils.UniqueNameBuilder | ||
| import com.itsaky.androidide.utils.flashSuccess | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.coroutines.withContext | ||
| import org.adfa.constants.CONTENT_KEY | ||
| import org.greenrobot.eventbus.Subscribe | ||
| @@ -196,14 +197,17 @@ open class EditorHandlerActivity : | ||
| override fun onPause() { | ||
| super.onPause() | ||
| // Record timestamps for all currently open files before saving the cache | ||
| editorViewModel.getOpenedFiles().forEach { file -> | ||
| // Note: Using the file's absolutePath as the key | ||
| fileTimestamps[file.absolutePath] = file.lastModified() | ||
| val openFiles = editorViewModel.getOpenedFiles() | ||
| lifecycleScope.launch(Dispatchers.IO) { | ||
| openFiles.forEach { file -> | ||
| // Note: Using the file's absolutePath as the key | ||
| fileTimestamps[file.absolutePath] = file.lastModified() | ||
| } | ||
| } | ||
| ActionContextProvider.clearActivity() | ||
| if (!isOpenedFilesSaved.get()) { | ||
| saveOpenedFiles() | ||
| saveAllAsync(notify = false) | ||
| saveAllAsync(notify = false) | ||
| } | ||
| } | ||
| @@ -221,19 +225,24 @@ open class EditorHandlerActivity : | ||
| val openFiles = editorViewModel.getOpenedFiles() | ||
| if (openFiles.isEmpty() || fileTimestamps.isEmpty()) return | ||
| // Check each open file | ||
| openFiles.forEach { file -> | ||
| val lastKnownTimestamp = fileTimestamps[file.absolutePath] ?: return@forEach | ||
| val currentTimestamp = file.lastModified() | ||
| val editorView = getEditorForFile(file) | ||
| // If the file on disk is newer AND the editor for it exists AND has no unsaved changes... | ||
| if (currentTimestamp > lastKnownTimestamp && editorView != null && !editorView.isModified) { | ||
| val newContent = file.readText() | ||
| editorView.editor?.post { | ||
| editorView.editor?.setText(newContent) | ||
| editorView.markAsSaved() | ||
| updateTabs() | ||
| lifecycleScope.launch(Dispatchers.IO) { | ||
| // Check each open file | ||
| openFiles.forEach { file -> | ||
| val lastKnownTimestamp = fileTimestamps[file.absolutePath] ?: return@forEach | ||
| val currentTimestamp = file.lastModified() | ||
| // If the file on disk is newer. | ||
| if (currentTimestamp > lastKnownTimestamp) { | ||
| val newContent = runCatching { file.readText() }.getOrNull() ?: return@forEach | ||
| withContext(Dispatchers.Main) { | ||
| // If the editor for the new file exists AND has no unsaved changes... | ||
| val editorView = getEditorForFile(file) ?: return@withContext | ||
| if (editorView.isModified) return@withContext | ||
| editorView.editor?.setText(newContent) | ||
| editorView.markAsSaved() | ||
| updateTabs() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| @@ -270,35 +279,44 @@ open class EditorHandlerActivity : | ||
| override fun onStart() { | ||
| super.onStart() | ||
| try { | ||
| val prefs = (application as BaseApplication).prefManager | ||
| val jsonCache = prefs.getString(PREF_KEY_OPEN_FILES_CACHE, null) | ||
| if (jsonCache != null) { | ||
| val cache = Gson().fromJson(jsonCache, OpenedFilesCache::class.java) | ||
| lifecycleScope.launch { | ||
| try { | ||
| val prefs = (application as BaseApplication).prefManager | ||
| val jsonCache = withContext(Dispatchers.IO) { | ||
| prefs.getString(PREF_KEY_OPEN_FILES_CACHE, null) | ||
| } ?: return@launch | ||
| val cache = withContext(Dispatchers.Default) { | ||
| Gson().fromJson(jsonCache, OpenedFilesCache::class.java) | ||
| } | ||
| onReadOpenedFilesCache(cache) | ||
| // Clear the preference so it's only loaded once on startup | ||
| prefs.putString(PREF_KEY_OPEN_FILES_CACHE, null) | ||
| withContext(Dispatchers.IO) { prefs.putString(PREF_KEY_OPEN_FILES_CACHE, null) } | ||
| } catch (err: Throwable) { | ||
| log.error("Failed to reopen recently opened files", err) | ||
| } | ||
| } catch (err: Throwable) { | ||
| log.error("Failed to reopen recently opened files", err) | ||
| } | ||
| } | ||
| private fun onReadOpenedFilesCache(cache: OpenedFilesCache?) { | ||
| cache ?: return | ||
| val existingFiles = cache.allFiles.filter { File(it.filePath).exists() } | ||
| val selectedFileExists = File(cache.selectedFile).exists() | ||
| lifecycleScope.launch(Dispatchers.IO) { | ||
| val existingFiles = cache.allFiles.filter { File(it.filePath).exists() } | ||
| val selectedFileExists = File(cache.selectedFile).exists() | ||
| if (existingFiles.isEmpty()) return | ||
| if (existingFiles.isEmpty()) return@launch | ||
| existingFiles.forEach { file -> | ||
| openFile(File(file.filePath), file.selection) | ||
| } | ||
| withContext(Dispatchers.Main) { | ||
| existingFiles.forEach { file -> | ||
| openFile(File(file.filePath), file.selection) | ||
| } | ||
| if (selectedFileExists) { | ||
| openFile(File(cache.selectedFile)) | ||
| if (selectedFileExists) { | ||
| openFile(File(cache.selectedFile)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| @@ -386,7 +404,11 @@ open class EditorHandlerActivity : | ||
| selection: Range?, | ||
| ): CodeEditorView? { | ||
| val range = selection ?: Range.NONE | ||
| if (ImageUtils.isImage(file)) { | ||
| val isImage = runBlocking { | ||
| withContext(Dispatchers.IO) { ImageUtils.isImage(file) } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (isImage) { | ||
| openImage(this, file) | ||
| return null | ||
| } | ||
| @@ -515,11 +537,11 @@ open class EditorHandlerActivity : | ||
| progressConsumer: ((Int, Int) -> Unit)?, | ||
| runAfter: (() -> Unit)?, | ||
| ) { | ||
| lifecycleScope.launch { | ||
| saveAll(notify, requestSync, processResources, progressConsumer) | ||
| runAfter?.invoke() | ||
| } | ||
| } | ||
| lifecycleScope.launch { | ||
| saveAll(notify, requestSync, processResources, progressConsumer) | ||
| runAfter?.invoke() | ||
| } | ||
| } | ||
| override suspend fun saveAll( | ||
| notify: Boolean, | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.