Skip to content

Core: DebugActivity migrate to compose #20

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
merged 1 commit into from
Aug 8, 2024
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
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
api(androidx.compose.ui.tooling.preview)
api(androidx.compose.ui.viewbinding)
api(androidx.constraintlayout.compose)
api(androidx.navigation.compose)
api(androidx.core)
api(androidx.fragment)
api(androidx.lifecycle.viewmodel.compose)
Expand Down
1 change: 0 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ android {
}

buildFeatures {
viewBinding = true
compose = true
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<application>
<activity
android:name=".ui.debugpanel.DebugActivity"
android:name=".ui.settings.DebugSettingsActivity"
android:exported="false"
android:taskAffinity="com.redmadrobot.debug_panel.DebugActivity"
android:theme="@style/DebugPanelTheme" />
Expand All @@ -21,7 +21,7 @@
android:enabled="true"
android:exported="true"
android:label="@string/debug_panel"
android:targetActivity=".ui.debugpanel.DebugActivity"
android:targetActivity=".ui.settings.DebugSettingsActivity"
android:taskAffinity="com.redmadrobot.debug_panel.DebugActivity"
android:theme="@style/DebugPanelTheme">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.redmadrobot.debug.core.extension

import androidx.fragment.app.Fragment
import com.redmadrobot.debug.core.DebugPanelInstance
import com.redmadrobot.debug.core.annotation.DebugPanelInternal
import com.redmadrobot.debug.core.plugin.Plugin
import com.redmadrobot.debug.core.ui.debugpanel.DebugActivity

@PublishedApi
internal fun getPlugin(pluginName: String): Plugin {
Expand All @@ -20,9 +18,4 @@ internal fun getAllPlugins(): List<Plugin> {
public inline fun <reified T : Plugin> getPlugin(): T {
val plugin = getPlugin(T::class.java.name)
return plugin as T
}

@DebugPanelInternal
public fun Fragment.isSettingMode(): Boolean {
return activity?.javaClass == DebugActivity::class.java
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.redmadrobot.debug.core.internal

import androidx.compose.runtime.Composable
import com.redmadrobot.debug.core.annotation.DebugPanelInternal

/**
* Plugin that will be displayed when opening the settings
*/
@DebugPanelInternal
public interface EditablePlugin {
@Composable
public fun settingsContent() {
}
}
12 changes: 0 additions & 12 deletions core/src/main/kotlin/com/redmadrobot/debug/core/plugin/Plugin.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.redmadrobot.debug.core.plugin

import androidx.compose.runtime.Composable
import androidx.fragment.app.Fragment
import com.redmadrobot.debug.core.DebugEvent
import com.redmadrobot.debug.core.DebugPanelInstance
import com.redmadrobot.debug.core.internal.CommonContainer
Expand All @@ -22,21 +21,10 @@ public abstract class Plugin {

public fun <T> getContainer(): T = pluginContainer as T

@Deprecated(
message = "You shouldn't use fragments for you plugins. Please use Jetpack Compose",
replaceWith = ReplaceWith("content()", "com.redmadrobot.debug.core.plugin.Plugin"),
level = DeprecationLevel.WARNING,
)
public open fun getSettingFragment(): Fragment? = null

@Composable
public open fun content() {
}

@Composable
public open fun settingsContent() {
}

public abstract fun getPluginContainer(commonContainer: CommonContainer): PluginDependencyContainer

public abstract fun getName(): String
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.redmadrobot.debug.core.ui.settings

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.remember
import androidx.navigation.compose.rememberNavController
import com.redmadrobot.debug.core.extension.getAllPlugins
import com.redmadrobot.debug.core.internal.EditablePlugin


internal class DebugSettingsActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
MaterialTheme {
val navController = rememberNavController()
val pluginItems = remember { getSettingItems() }
DebugSettingsNavHost(navController = navController, pluginItems = pluginItems)
}
}
}

private fun getSettingItems(): List<PluginSettingsItem> {
return getAllPlugins()
.filter { it is EditablePlugin }
.map { plugin ->
PluginSettingsItem(
pluginClassName = plugin::class.java.name,
pluginName = plugin.getName(),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.redmadrobot.debug.core.ui.settings

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.redmadrobot.debug.core.extension.getPlugin
import com.redmadrobot.debug.core.internal.EditablePlugin

private const val MAIN_SCREEN_ROUTE = "main"

@Composable
internal fun DebugSettingsNavHost(
navController: NavHostController,
pluginItems: List<PluginSettingsItem>,
) {
NavHost(navController = navController, startDestination = MAIN_SCREEN_ROUTE) {
composable(MAIN_SCREEN_ROUTE) {
DebugSettingsScreen(pluginItems = pluginItems, navController = navController)
}

pluginItems.forEach { plugin ->
composable(plugin.pluginClassName) {
(getPlugin(plugin.pluginClassName) as? EditablePlugin)?.settingsContent()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.redmadrobot.debug.core.ui.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController

@Composable
internal fun DebugSettingsScreen(
navController: NavController,
pluginItems: List<PluginSettingsItem>,
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
) {
items(pluginItems) { item ->
PluginItem(
pluginName = item.pluginName,
onClick = { navController.navigate(item.pluginClassName) },
)
}
}
}

@Composable
private fun PluginItem(pluginName: String, onClick: () -> Unit) {
Text(
modifier = Modifier
.fillMaxSize()
.clickable { onClick.invoke() }
.padding(horizontal = 16.dp, vertical = 16.dp),
text = pluginName,
fontSize = 18.sp,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.redmadrobot.debug.core.ui.settings

internal class PluginSettingsItem(
val pluginName: String,
val pluginClassName: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.TaskStackBuilder
import androidx.core.content.ContextCompat
import com.redmadrobot.debug.core.ui.debugpanel.DebugActivity
import com.redmadrobot.debug.core.R
import com.redmadrobot.debug.core.ui.settings.DebugSettingsActivity

internal class DebugPanelNotification(private val context: Context) {

Expand Down Expand Up @@ -74,7 +74,7 @@ internal class DebugPanelNotification(private val context: Context) {
}

private fun getSettingActivityPendingIntent(context: Context): PendingIntent? {
val settingActivityIntent = Intent(context, DebugActivity::class.java)
val settingActivityIntent = Intent(context, DebugSettingsActivity::class.java)

return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(settingActivityIntent)
Expand Down
15 changes: 0 additions & 15 deletions core/src/main/res/layout/activity_debug.xml

This file was deleted.

29 changes: 0 additions & 29 deletions core/src/main/res/layout/item_plugin_setting.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import com.redmadrobot.debug.core.data.DebugDataProvider
import com.redmadrobot.debug.core.internal.CommonContainer
import com.redmadrobot.debug.core.internal.PluginDependencyContainer
import com.redmadrobot.debug.core.internal.EditablePlugin
import com.redmadrobot.debug.core.plugin.Plugin
import com.redmadrobot.debug.plugin.accounts.authenticator.DebugAuthenticator
import com.redmadrobot.debug.plugin.accounts.authenticator.DefaultAuthenticator
Expand All @@ -13,7 +14,7 @@ import com.redmadrobot.debug.plugin.accounts.ui.AccountsScreen
public class AccountsPlugin(
private val preInstalledAccounts: List<DebugAccount> = emptyList(),
public val debugAuthenticator: DebugAuthenticator = DefaultAuthenticator()
) : Plugin() {
) : Plugin(), EditablePlugin {

internal companion object {
const val NAME = "ACCOUNTS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.redmadrobot.debug.core.data.DebugDataProvider
import com.redmadrobot.debug.core.extension.getPlugin
import com.redmadrobot.debug.core.internal.CommonContainer
import com.redmadrobot.debug.core.internal.PluginDependencyContainer
import com.redmadrobot.debug.core.internal.EditablePlugin
import com.redmadrobot.debug.core.plugin.Plugin
import com.redmadrobot.debug.plugin.servers.data.model.DebugServer
import com.redmadrobot.debug.plugin.servers.data.model.DebugServerData
Expand All @@ -16,7 +17,7 @@ import kotlinx.coroutines.runBlocking
@OptIn(DebugPanelInternal::class)
public class ServersPlugin(
private val preInstalledServers: List<DebugServerData> = emptyList(),
) : Plugin() {
) : Plugin(), EditablePlugin {

init {
preInstalledServers.find { it.isDefault }
Expand Down
Loading