<lambda>null1 package leakcanary.internal.activity.screen
2 
3 import android.view.View
4 import android.view.View.GONE
5 import android.view.View.VISIBLE
6 import android.view.ViewGroup
7 import android.widget.ListView
8 import android.widget.TextView
9 import com.squareup.leakcanary.core.R
10 import leakcanary.internal.activity.db.HeapAnalysisTable
11 import leakcanary.internal.activity.db.LeakTable
12 import leakcanary.internal.activity.db.LeakTable.AllLeaksProjection
13 import leakcanary.internal.activity.db.executeOnDb
14 import leakcanary.internal.activity.ui.SimpleListAdapter
15 import leakcanary.internal.activity.ui.TimeFormatter
16 import leakcanary.internal.navigation.NavigatingActivity
17 import leakcanary.internal.navigation.Screen
18 import leakcanary.internal.navigation.activity
19 import leakcanary.internal.navigation.goTo
20 import leakcanary.internal.navigation.inflate
21 import leakcanary.internal.navigation.onScreenExiting
22 
23 internal class LeaksScreen : Screen() {
24   override fun createView(container: ViewGroup) =
25     container.inflate(R.layout.leak_canary_list).apply {
26 
27       val unsubscribeRefresh = HeapAnalysisTable.onUpdate {
28         activity<NavigatingActivity>().refreshCurrentScreen()
29       }
30 
31       onScreenExiting { unsubscribeRefresh() }
32 
33       executeOnDb {
34         val projections = LeakTable.retrieveAllLeaks(db)
35         updateUi { onGroupsRetrieved(projections) }
36       }
37     }
38 
39   private fun View.onGroupsRetrieved(projections: List<AllLeaksProjection>) {
40     activity.title = resources.getQuantityString(
41       R.plurals.leak_canary_distinct_leaks,
42       projections.size, projections.size
43     )
44 
45     val listView = findViewById<ListView>(R.id.leak_canary_list)
46 
47     listView.adapter =
48       SimpleListAdapter(R.layout.leak_canary_leak_row, projections) { view, position ->
49         val countView = view.findViewById<TextView>(R.id.leak_canary_count_text)
50         val descriptionView = view.findViewById<TextView>(R.id.leak_canary_leak_text)
51         val timeView = view.findViewById<TextView>(R.id.leak_canary_time_text)
52         val newChipView = view.findViewById<TextView>(R.id.leak_canary_chip_new)
53         val libraryLeakChipView = view.findViewById<TextView>(R.id.leak_canary_chip_library_leak)
54 
55         val projection = projections[position]
56         countView.isEnabled = projection.isNew
57 
58         newChipView.visibility = if (projection.isNew) VISIBLE else GONE
59         libraryLeakChipView.visibility = if (projection.isLibraryLeak) VISIBLE else GONE
60 
61         countView.text = projection.leakTraceCount.toString()
62         descriptionView.text = projection.shortDescription
63 
64         val formattedDate =
65           TimeFormatter.formatTimestamp(view.context, projection.createdAtTimeMillis)
66         timeView.text =
67           resources.getString(R.string.leak_canary_group_list_time_label, formattedDate)
68       }
69 
70     listView.setOnItemClickListener { _, _, position, _ ->
71       goTo(LeakScreen(projections[position].signature))
72     }
73   }
74 }