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 @@ -29,19 +29,22 @@ import com.itsaky.androidide.actions.EditorRelatedAction
import com.itsaky.androidide.actions.markInvisible
import com.itsaky.androidide.activities.editor.EditorHandlerActivity
import com.itsaky.androidide.compose.preview.ComposePreviewActivity
import com.itsaky.androidide.editor.ui.IDEEditor
import com.itsaky.androidide.idetooltips.TooltipTag
import com.itsaky.androidide.resources.R
import org.appdevforall.codeonthego.layouteditor.activities.EditorActivity
import org.appdevforall.codeonthego.layouteditor.utils.Constants
import com.itsaky.androidide.projects.IProjectManager
import org.slf4j.LoggerFactory
import java.io.File

/** @author Akash Yadav */
class PreviewLayoutAction(context: Context, override val order: Int) : EditorRelatedAction() {

override val id: String = ID
override fun retrieveTooltipTag(isReadOnlyContext: Boolean): String = TooltipTag.EDITOR_TOOLBAR_PREVIEW_LAYOUT
override fun retrieveTooltipTag(isReadOnlyContext: Boolean): String = when (previewType) {
PreviewType.COMPOSE -> TooltipTag.EDITOR_TOOLBAR_PREVIEW_COMPOSE
else -> TooltipTag.EDITOR_TOOLBAR_PREVIEW_LAYOUT
}
override var requiresUIThread: Boolean = false

private var previewType: PreviewType = PreviewType.NONE
Expand All @@ -67,56 +70,48 @@ class PreviewLayoutAction(context: Context, override val order: Int) : EditorRel

previewType = PreviewType.NONE

val viewModel = data.requireActivity().editorViewModel
if (viewModel.isInitializing) {
visible = true
enabled = false
return
}

if (!visible) {
return
}

val editor = data.requireEditor()
val file = editor.file ?: run {
LOG.warn("Editor file is null")
if (data.getActivity() == null) {
markInvisible()
return
}

when {
file.name.endsWith(".xml") -> {
val type = try {
extractPathData(file).type
} catch (err: Throwable) {
markInvisible()
return
}

if (type == LAYOUT) {
previewType = PreviewType.XML_LAYOUT
visible = true
enabled = true
} else {
markInvisible()
val viewModel = data.requireActivity().editorViewModel
val editor = data.getEditor()
val file = editor?.file

if (file != null && !viewModel.isInitializing) {
when {
file.name.endsWith(".xml") -> {
val type = try {
extractPathData(file).type
} catch (err: Throwable) {
markInvisible()
return
}

if (type == LAYOUT) {
previewType = PreviewType.XML_LAYOUT
visible = true
enabled = true
} else {
markInvisible()
}
}
}
file.name.endsWith(".kt") -> {
val content = editor.text.toString()
val hasCompose = content.contains("@Composable") ||
content.contains("androidx.compose") ||
content.contains("@Preview")

if (hasCompose) {
file.name.endsWith(".kt") && moduleUsesCompose(file) -> {
previewType = PreviewType.COMPOSE
visible = true
enabled = true
} else {
}
else -> {
markInvisible()
}
}
else -> {
} else {
if (moduleUsesCompose()) {
previewType = PreviewType.COMPOSE
visible = true
enabled = false
} else {
markInvisible()
}
}
Expand All @@ -139,15 +134,18 @@ class PreviewLayoutAction(context: Context, override val order: Int) : EditorRel

override fun postExec(data: ActionData, result: Any) {
val activity = data.requireActivity()
val editor = data.requireEditor()
val file = editor.file ?: run {
LOG.warn("Editor file is null in postExec")
return
}

when (previewType) {
PreviewType.XML_LAYOUT -> activity.previewXmlLayout(file)
PreviewType.COMPOSE -> activity.showComposePreviewSheet(file, editor.text.toString())
PreviewType.XML_LAYOUT -> {
val editor = data.getEditor() ?: return
val file = editor.file ?: return
activity.previewXmlLayout(file)
}
PreviewType.COMPOSE -> {
val editor = data.getEditor() ?: return
val file = editor.file ?: return
activity.showComposePreviewSheet(file, editor.text.toString())
}
PreviewType.NONE -> {}
}
}
Expand All @@ -163,8 +161,15 @@ class PreviewLayoutAction(context: Context, override val order: Int) : EditorRel
ComposePreviewActivity.start(this, sourceCode, file.absolutePath)
}

private fun ActionData.requireEditor(): IDEEditor {
return this.getEditor() ?: throw IllegalArgumentException(
"An editor instance is required but none was provided")
private fun moduleUsesCompose(): Boolean {
val workspace = IProjectManager.getInstance().workspace ?: return false
return workspace.findAndroidModules().any { module ->
module.hasExternalDependency("androidx.compose.runtime", "runtime")
}
}

private fun moduleUsesCompose(file: File): Boolean {
val module = IProjectManager.getInstance().findModuleForFile(file) ?: return false
return module.hasExternalDependency("androidx.compose.runtime", "runtime")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ object TooltipTag {
const val EDITOR_TOOLBAR_REDO = "editor.redo"
const val EDITOR_TOOLBAR_QUICK_SAVE = "project.save"
const val EDITOR_TOOLBAR_PREVIEW_LAYOUT = "editor.layout.preview"
const val EDITOR_TOOLBAR_PREVIEW_COMPOSE = "editor.compose.preview"
const val EDITOR_TOOLBAR_COMPUTER_VISION = "project.layout.vision"
const val EDITOR_TOOLBAR_LOG_SENDER = "editor.disconnect.logsenders"

Expand Down