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 @@ -200,6 +200,14 @@ abstract class BaseEditorActivity :
override val subscribeToEvents: Boolean
get() = true

protected val contentOrNull: ContentEditorBinding?
get() {
if (_binding == null || isDestroyed || isDestroying) {
return null
}
return _binding!!.content
}

private val onBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import android.view.ViewGroup.LayoutParams
import androidx.collection.MutableIntObjectMap
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.GravityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.blankj.utilcode.util.ImageUtils
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
Expand Down Expand Up @@ -350,6 +348,7 @@ open class EditorHandlerActivity :
if (existingFiles.isEmpty()) return@launch

withContext(Dispatchers.Main) {
if (contentOrNull == null) return@withContext
existingFiles.forEach { file ->
openFile(File(file.filePath), file.selection)
}
Expand Down Expand Up @@ -576,6 +575,7 @@ open class EditorHandlerActivity :
}

override fun getEditorForFile(file: File): CodeEditorView? {
val content = contentOrNull ?: return null
for (i in 0 until content.editorContainer.childCount) {
val child = content.editorContainer.getChildAt(i)
if (child is CodeEditorView && file == child.file) {
Expand Down Expand Up @@ -629,6 +629,7 @@ open class EditorHandlerActivity :
// don't bother to switch the context if we don't need to
if (notify || (result.gradleSaved && requestSync)) {
withContext(Dispatchers.Main) {
if (contentOrNull == null) return@withContext
if (notify) {
flashSuccess(string.all_saved)
}
Expand Down Expand Up @@ -700,12 +701,16 @@ open class EditorHandlerActivity :
val hasUnsaved = hasUnsavedFiles()

withContext(Dispatchers.Main) {
val content = contentOrNull ?: return@withContext
editorViewModel.areFilesModified = hasUnsaved

// set tab as unmodified
val tab = content.tabs.getTabAt(index) ?: return@withContext
if (tab.text!!.startsWith('*')) {
tab.text = tab.text!!.substring(startIndex = 1)
val tabPosition = getTabPositionForFileIndex(index)
if (tabPosition < 0) return@withContext
val tab = content.tabs.getTabAt(tabPosition) ?: return@withContext
val text = tab.text?.toString() ?: return@withContext
if (text.startsWith('*')) {
tab.text = text.substring(1)
}
}

Expand Down Expand Up @@ -929,6 +934,7 @@ open class EditorHandlerActivity :

@Subscribe(threadMode = ThreadMode.MAIN)
fun onFileRenamed(event: FileRenameEvent) {
val content = contentOrNull ?: return
val index = findIndexOfEditorByFile(event.file)
if (index < 0 || index >= content.tabs.tabCount) {
return
Expand All @@ -943,6 +949,7 @@ open class EditorHandlerActivity :

@Subscribe(threadMode = ThreadMode.MAIN)
fun onDocumentChange(event: DocumentChangeEvent) {
if (contentOrNull == null) return
editorViewModel.areFilesModified = true

val fileIndex = findIndexOfEditorByFile(event.file.toFile())
Expand Down Expand Up @@ -984,6 +991,7 @@ open class EditorHandlerActivity :
}

withContext(Dispatchers.Main) {
val content = contentOrNull ?: return@withContext
names.forEach { index, (name, iconId) ->
val tab = content.tabs.getTabAt(index) ?: return@forEach
tab.icon = ResourcesCompat.getDrawable(resources, iconId, theme)
Expand Down Expand Up @@ -1063,6 +1071,8 @@ open class EditorHandlerActivity :
Log.d("EditorHandlerActivity", "Creating UI tab for plugin: ${pluginTab.id} (${pluginTab.title})")

runOnUiThread {
val content = contentOrNull ?: return@runOnUiThread

val tab = content.tabs.newTab()
tab.text = pluginTab.title

Expand Down Expand Up @@ -1280,6 +1290,7 @@ open class EditorHandlerActivity :
}

private fun confirmProjectClose() {
val content = contentOrNull ?: return
val builder = newMaterialDialogBuilder(this)
builder.setTitle(string.title_confirm_project_close)
builder.setMessage(string.msg_confirm_project_close)
Expand All @@ -1304,6 +1315,7 @@ open class EditorHandlerActivity :
saveAllAsync(notify = false) {

runOnUiThread {
if (contentOrNull == null) return@runOnUiThread
performCloseAllFiles(manualFinish = true)
}
recentProjectsViewModel.updateProjectModifiedDate(
Expand Down