Skip to content

Commit

Permalink
Update LeakCanaryModule.kt
Browse files Browse the repository at this point in the history
Provide Api to configure defaults for heap dump instead of making a primary constructor
  • Loading branch information
akshay253101 authored Jan 5, 2023
1 parent 253760a commit f0f2088
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import android.widget.Switch
import au.com.gridstone.debugdrawer.DebugDrawerModule
import leakcanary.LeakCanary

class LeakCanaryModule(
private val enableHeapDumpsDefault: Boolean
) : DebugDrawerModule {
object LeakCanaryModule : DebugDrawerModule {

private const val SHARED_PREFS_NAME = "DebugDrawer_LeakCanary"
private const val KEY_ENABLE_HEAP_DUMPS = "enableHeapDumps"

private var enableHeapDumpsByDefault: Boolean = true

override fun onAttach(context: Context) {
val sharedPrefs = context.getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE)
val enableHeapDumps = sharedPrefs.getBoolean(KEY_ENABLE_HEAP_DUMPS, enableHeapDumpsDefault)
val enableHeapDumps = sharedPrefs.getBoolean(KEY_ENABLE_HEAP_DUMPS, enableHeapDumpsByDefault)
LeakCanary.config = LeakCanary.config.copy(dumpHeap = enableHeapDumps)
}

@SuppressLint("UseSwitchCompatOrMaterialCode")
override fun onCreateView(parent: ViewGroup): View {
val inflater = LayoutInflater.from(parent.context)
val view: View = inflater.inflate(R.layout.drawer_leakcanary, parent, false)

val sharedPrefs = parent.context.getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE)
val enableHeapDumps = sharedPrefs.getBoolean(KEY_ENABLE_HEAP_DUMPS, enableHeapDumpsDefault)
val enableHeapDumps = sharedPrefs.getBoolean(KEY_ENABLE_HEAP_DUMPS, enableHeapDumpsByDefault)

val toggle: Switch = view.findViewById(R.id.drawer_leakcanaryToggle)
toggle.isChecked = enableHeapDumps
Expand All @@ -42,9 +44,8 @@ class LeakCanaryModule(

return view
}

companion object {
private const val SHARED_PREFS_NAME = "DebugDrawer_LeakCanary"
private const val KEY_ENABLE_HEAP_DUMPS = "enableHeapDumps"

fun setEnableHeapDumpsByDefault(enable: Boolean) {
enableHeapDumpsByDefault = enable
}
}

0 comments on commit f0f2088

Please sign in to comment.