Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ open class EditorHandlerActivity :
return@observeFiles
}
getOpenedFiles().also {
val cache = OpenedFilesCache(currentFile, it)
val cache = OpenedFilesCache(
projectPath = ProjectManagerImpl.getInstance().projectDirPath,
selectedFile = currentFile,
allFiles = it,
)
editorViewModel.writeOpenedFiles(cache)
editorViewModel.openedFilesCache = cache
}
Expand Down Expand Up @@ -285,7 +289,11 @@ open class EditorHandlerActivity :
}

val cache =
OpenedFilesCache(selectedFile = selectedFile.absolutePath, allFiles = openedFiles)
OpenedFilesCache(
projectPath = ProjectManagerImpl.getInstance().projectDirPath,
selectedFile = selectedFile.absolutePath,
allFiles = openedFiles,
)

val jsonCache = Gson().toJson(cache)
prefs.putString(PREF_KEY_OPEN_FILES_CACHE, jsonCache)
Expand Down Expand Up @@ -342,6 +350,12 @@ open class EditorHandlerActivity :
private fun onReadOpenedFilesCache(cache: OpenedFilesCache?) {
cache ?: return

val currentProjectPath = ProjectManagerImpl.getInstance().projectDirPath
if (cache.projectPath.isNotEmpty() && cache.projectPath != currentProjectPath) {
log.debug("[onStart] Discarding stale tab cache from project: {}", cache.projectPath)
return
}

lifecycleScope.launch(Dispatchers.IO) {
val existingFiles = cache.allFiles.filter { File(it.filePath).exists() }
val selectedFileExists = File(cache.selectedFile).exists()
Expand Down
22 changes: 13 additions & 9 deletions app/src/main/java/com/itsaky/androidide/models/OpenedFilesCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ import java.io.Reader
/**
* @author Akash Yadav
*/
data class OpenedFilesCache(@SerializedName(KEY_SELECTED_FILE) val selectedFile: String,
@SerializedName(KEY_ALL_FILES) val allFiles: List<OpenedFile>) {
data class OpenedFilesCache(
@SerializedName(KEY_PROJECT_PATH) val projectPath: String = "",
@SerializedName(KEY_SELECTED_FILE) val selectedFile: String,
@SerializedName(KEY_ALL_FILES) val allFiles: List<OpenedFile>,
) {

companion object {

private const val KEY_PROJECT_PATH = "projectPath"
private const val KEY_SELECTED_FILE = "selectedFile"
private const val KEY_ALL_FILES = "allFiles"
private val log = LoggerFactory.getLogger(OpenedFilesCache::class.java)
Expand All @@ -45,20 +49,20 @@ data class OpenedFilesCache(@SerializedName(KEY_SELECTED_FILE) val selectedFile:
}

reader.beginObject()
var projectPath = ""
var selectedFile = ""
var allFiles = emptyList<OpenedFile>()
while (reader.hasNext()) {
val name = reader.nextName()

if (name == KEY_SELECTED_FILE) {
selectedFile = reader.nextString()
} else if (name == KEY_ALL_FILES) {
allFiles = Gson().fromJson(reader, object : TypeToken<List<OpenedFile>>() {})
when (reader.nextName()) {
KEY_PROJECT_PATH -> projectPath = reader.nextString()
KEY_SELECTED_FILE -> selectedFile = reader.nextString()
KEY_ALL_FILES -> allFiles = Gson().fromJson(reader, object : TypeToken<List<OpenedFile>>() {})
else -> reader.skipValue()
}
}
reader.endObject()

return@use OpenedFilesCache(selectedFile, allFiles)
return@use OpenedFilesCache(projectPath, selectedFile, allFiles)
}
} catch (err: Exception) {
log.error("Failed to parse opened files cache", err)
Expand Down