Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: opened files cache is always empty when user manually closes the…
Browse files Browse the repository at this point in the history
… project (#1116)
  • Loading branch information
itsaky committed Jul 20, 2023
1 parent 7eaa95d commit 304cb44
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.itsaky.androidide.utils.flashSuccess
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
Expand All @@ -68,6 +69,8 @@ import kotlin.collections.set
*/
open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {

protected val isOpenedFilesSaved = AtomicBoolean(false)

override fun doOpenFile(file: File, selection: Range?) {
openFileAndSelect(file, selection)
}
Expand Down Expand Up @@ -143,19 +146,32 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
override fun onPause() {
super.onPause()

val current =
getCurrentEditor()?.editor?.file?.absolutePath
?: run {
viewModel.writeOpenedFiles(null)
viewModel.openedFilesCache = null
return
}
// if the user manually closes the project, this will be true
// in this case, don't overwrite the already saved cache
if (!isOpenedFilesSaved.get()) {
saveOpenedFiles()
}
}

override fun saveOpenedFiles() {
writeOpenedFilesCache(getOpenedFiles(), getCurrentEditor()?.editor?.file)
}

getOpenedFiles().also {
val cache = OpenedFilesCache(current, it)
viewModel.writeOpenedFiles(cache)
viewModel.openedFilesCache = if (!isDestroying) cache else null
private fun writeOpenedFilesCache(openedFiles: List<OpenedFile>, selectedFile: File?) {
if (selectedFile == null || openedFiles.isEmpty()) {
viewModel.writeOpenedFiles(null)
viewModel.openedFilesCache = null
log.debug("[onPause]", "No opened files.", "Opened files cache reset to null.")
isOpenedFilesSaved.set(true)
return
}

val cache = OpenedFilesCache(selectedFile = selectedFile.absolutePath, allFiles = openedFiles)

viewModel.writeOpenedFiles(cache)
viewModel.openedFilesCache = if (!isDestroying) cache else null
log.debug("[onPause]", "Opened files cache reset to ${viewModel.openedFilesCache}")
isOpenedFilesSaved.set(true)
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {

abstract fun doCloseAll(runAfter: () -> Unit)

abstract fun saveOpenedFiles()

override fun doDismissSearchProgress() {
if (mSearchingProgress?.isShowing == true) {
mSearchingProgress!!.dismiss()
Expand Down Expand Up @@ -445,6 +447,13 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {
}

private fun closeProject(manualFinish: Boolean) {
if (manualFinish) {
// if the user is manually closing the project,
// save the opened files cache
// this is needed because in this case, the opened files cache will be empty
// when onPause will be called.
saveOpenedFiles()
}

// Make sure we close files
// This will make sure that file contents are not erased.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.FileUtils
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.itsaky.androidide.models.OpenedFilesCache
import com.itsaky.androidide.projects.ProjectManager
import com.itsaky.androidide.tasks.executeAsync
Expand Down Expand Up @@ -168,7 +166,7 @@ class EditorViewModel : ViewModel() {
* @return The list of opened files.
*/
fun getOpenedFiles(): List<File> {
return files.value!!
return files.value ?: mutableListOf()
}

/**
Expand Down

0 comments on commit 304cb44

Please sign in to comment.