Skip to content

Commit cc481cd

Browse files
committed
feature: minor code improves
1 parent f3670e0 commit cc481cd

File tree

6 files changed

+15
-18
lines changed

6 files changed

+15
-18
lines changed

panel-core/src/main/kotlin/com/redmadrobot/debug/core/DebugPanel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.emptyFlow
1414

1515
@OptIn(DebugPanelInternal::class)
1616
public object DebugPanel {
17+
@Volatile
1718
private var instance: DebugPanelInstance? = null
1819
public val isInitialized: Boolean
1920
get() = instance != null
@@ -40,6 +41,8 @@ public object DebugPanel {
4041
}
4142
}
4243

44+
internal fun getInstance(): DebugPanelInstance? = instance
45+
4346
private fun openDebugPanel(activity: Activity) {
4447
val intent = Intent(activity, DebugPanelActivity::class.java)
4548
activity.startActivity(intent)

panel-core/src/main/kotlin/com/redmadrobot/debug/core/DebugPanelInstance.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ internal class DebugPanelInstance(
2626
init {
2727
initContainer(application.applicationContext)
2828
initPluginManager(plugins, requireNotNull(commonContainer))
29-
instance = this
3029
}
3130

3231
internal fun getEventLiveData(): LiveData<DebugEvent> {
@@ -42,7 +41,7 @@ internal class DebugPanelInstance(
4241
eventSharedFlow.tryEmit(debugEvent)
4342
}
4443

45-
internal fun getPluginManger(): PluginManager {
44+
internal fun getPluginManager(): PluginManager {
4645
return pluginManager
4746
?: error("PluginManager not initialised")
4847
}
@@ -59,8 +58,4 @@ internal class DebugPanelInstance(
5958
start(commonContainer)
6059
}
6160
}
62-
63-
companion object {
64-
var instance: DebugPanelInstance? = null
65-
}
6661
}

panel-core/src/main/kotlin/com/redmadrobot/debug/core/extension/PluginsExt.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.redmadrobot.debug.core.extension
22

3-
import com.redmadrobot.debug.core.DebugPanelInstance
3+
import com.redmadrobot.debug.core.DebugPanel
44
import com.redmadrobot.debug.core.annotation.DebugPanelInternal
55
import com.redmadrobot.debug.core.plugin.Plugin
66

77
@PublishedApi
88
internal fun getPlugin(pluginName: String): Plugin {
9-
val plugin = DebugPanelInstance.instance?.getPluginManger()?.findPluginByName(pluginName)
9+
val plugin = DebugPanel.getInstance()?.getPluginManager()?.findPluginByName(pluginName)
1010
return requireNotNull(plugin)
1111
}
1212

1313
internal fun getAllPlugins(): List<Plugin> {
14-
return DebugPanelInstance.instance?.getPluginManger()?.plugins ?: emptyList()
14+
return DebugPanel.getInstance()?.getPluginManager()?.plugins ?: emptyList()
1515
}
1616

1717
@DebugPanelInternal

panel-core/src/main/kotlin/com/redmadrobot/debug/core/plugin/Plugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.redmadrobot.debug.core.plugin
22

33
import androidx.compose.runtime.Composable
44
import com.redmadrobot.debug.core.DebugEvent
5-
import com.redmadrobot.debug.core.DebugPanelInstance
5+
import com.redmadrobot.debug.core.DebugPanel
66
import com.redmadrobot.debug.core.internal.CommonContainer
77
import com.redmadrobot.debug.core.internal.PluginDependencyContainer
88

@@ -15,7 +15,7 @@ public abstract class Plugin {
1515
}
1616

1717
public fun pushEvent(debugEvent: DebugEvent) {
18-
DebugPanelInstance.instance?.pushEvent(debugEvent)
18+
DebugPanel.getInstance()?.pushEvent(debugEvent)
1919
}
2020

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

panel-core/src/main/kotlin/com/redmadrobot/debug/core/util/ApplicationLifecycleHandler.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import android.content.IntentFilter
77
import androidx.activity.ComponentActivity
88
import androidx.core.content.ContextCompat
99
import timber.log.Timber
10+
import java.util.concurrent.atomic.AtomicInteger
1011

1112
internal class ApplicationLifecycleHandler(
1213
private val application: Application,
1314
) {
1415
// open Activity counter
15-
private var openActivityCount = 0
16+
private val openActivityCounter = AtomicInteger(0)
1617

1718
private var debugPanelBroadcastReceiver: BroadcastReceiver? = null
1819
private val debugPanelNotification = DebugPanelNotification(application.applicationContext)
@@ -25,8 +26,7 @@ internal class ApplicationLifecycleHandler(
2526
application.registerActivityLifecycleCallbacks(
2627
object : ActivityLifecycleCallbacksAdapter() {
2728
override fun onActivityResumed(activity: Activity) {
28-
if (openActivityCount == 0) onAppResumed()
29-
++openActivityCount
29+
if (openActivityCounter.getAndIncrement() == 0) onAppResumed()
3030

3131
// register BroadcastReceiver for debug panel inner actions
3232
debugPanelBroadcastReceiver = DebugPanelBroadcastReceiver(activity)
@@ -41,11 +41,10 @@ internal class ApplicationLifecycleHandler(
4141
}
4242

4343
override fun onActivityPaused(activity: Activity) {
44-
--openActivityCount
4544
(activity as? ComponentActivity)?.let {
4645
activity.unregisterReceiver(debugPanelBroadcastReceiver)
4746
}
48-
if (openActivityCount == 0) onAppPaused()
47+
if (openActivityCounter.decrementAndGet() == 0) onAppPaused()
4948
}
5049
}
5150
)

panel-core/src/main/kotlin/com/redmadrobot/debug/core/util/DebugPanelNotification.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ internal class DebugPanelNotification(private val context: Context) {
2727
}
2828

2929
fun show() {
30-
val isPermissionGranted = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
30+
val isPermissionDenied = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
3131
ContextCompat.checkSelfPermission(context, POST_NOTIFICATIONS) != PERMISSION_GRANTED
32-
if (isPermissionGranted) return
32+
if (isPermissionDenied) return
3333

3434
notificationManager = NotificationManagerCompat.from(context)
3535
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

0 commit comments

Comments
 (0)