Skip to content

Commit e13385b

Browse files
authored
Merge pull request #158 from AppDevNext/LinceColorThemeable
Allow specifying colors for LogListAdapter in Theme
2 parents f8efc71 + eccc9f6 commit e13385b

3 files changed

Lines changed: 67 additions & 20 deletions

File tree

LogcatCoreLib/src/main/java/info/hannes/logcat/base/LogListAdapter.kt

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package info.hannes.logcat.base
22

33
import android.content.Context
44
import android.content.res.ColorStateList
5-
import android.content.res.TypedArray
5+
import android.content.res.Resources
66
import android.graphics.Color
77
import android.view.LayoutInflater
88
import android.view.View
@@ -87,32 +87,54 @@ class LogListAdapter(private var completeLogs: MutableList<String>, filter: Stri
8787
* @param position
8888
*/
8989
override fun onBindViewHolder(holder: LogViewHolder, position: Int) {
90-
holder.logContent.text = filterLogs[position].also {
91-
if (it.contains(" ${LogBaseFragment.ERROR_LINE}") || it.startsWith(LogBaseFragment.ERROR_LINE)) {
92-
holder.logContent.setTextColor(Color.RED)
93-
} else if (it.contains(" ${LogBaseFragment.ASSERT_LINE}") || it.startsWith(LogBaseFragment.ASSERT_LINE)) {
94-
holder.logContent.setTextColor(Color.RED)
95-
} else if (it.contains(" ${LogBaseFragment.INFO_LINE}") || it.startsWith(LogBaseFragment.INFO_LINE)) {
96-
holder.logContent.setTextColor(Color.BLACK)
97-
} else if (it.contains(" ${LogBaseFragment.WARNING_LINE}") || it.startsWith(LogBaseFragment.WARNING_LINE)) {
98-
holder.logContent.setTextColor(Color.MAGENTA)
99-
} else if (it.contains(" ${LogBaseFragment.VERBOSE_LINE}") || it.startsWith(LogBaseFragment.VERBOSE_LINE)) {
100-
holder.logContent.setTextColor(Color.GRAY)
101-
} else {
102-
holder.logContent.setTextColor(getColorAttr(holder.logContent.context, android.R.attr.textColorSecondary))
103-
}
90+
val text = filterLogs[position]
91+
holder.logContent.let {
92+
it.text = text
93+
94+
when {
95+
text.contains(" ${LogBaseFragment.ERROR_LINE}") || text.startsWith(LogBaseFragment.ERROR_LINE) -> {
96+
getAttrColorStateList(it.context, R.attr.colorErrorLine) ?: ColorStateList.valueOf(Color.RED)
97+
}
98+
text.contains(" ${LogBaseFragment.ASSERT_LINE}") || text.startsWith(LogBaseFragment.ASSERT_LINE) -> {
99+
getAttrColorStateList(it.context, R.attr.colorAssertLine) ?: ColorStateList.valueOf(Color.RED)
100+
}
101+
text.contains(" ${LogBaseFragment.INFO_LINE}") || text.startsWith(LogBaseFragment.INFO_LINE) -> {
102+
getAttrColorStateList(it.context, R.attr.colorInfoLine) ?: getAttrColorStateList(it.context, android.R.attr.textColorPrimary)
103+
}
104+
text.contains(" ${LogBaseFragment.WARNING_LINE}") || text.startsWith(LogBaseFragment.WARNING_LINE) -> {
105+
getAttrColorStateList(it.context, R.attr.colorWarningLine) ?: ColorStateList.valueOf(Color.MAGENTA)
106+
}
107+
text.contains(" ${LogBaseFragment.VERBOSE_LINE}") || text.startsWith(LogBaseFragment.VERBOSE_LINE) -> {
108+
getAttrColorStateList(it.context, R.attr.colorVerboseLine) ?: ColorStateList.valueOf(Color.GRAY)
109+
}
110+
else -> {
111+
getAttrColorStateList(it.context, R.attr.colorDebugLine) ?: getAttrColorStateList(it.context, android.R.attr.textColorSecondary)
112+
}
113+
}?.let { colors: ColorStateList -> it.setTextColor(colors) }
104114
}
105115
}
106116

107117
override fun getItemCount(): Int = filterLogs.size
108118

109119
companion object {
110-
fun getColorAttr(context: Context, attr: Int): ColorStateList? {
111-
val ta: TypedArray = context.obtainStyledAttributes(intArrayOf(attr))
112-
return try {
113-
ta.getColorStateList(0)
114-
} finally {
120+
121+
/**
122+
* Retrieve the ColorStateList for the given attribute. The value may be either a single solid
123+
* color or a reference to a color or complex [android.content.res.ColorStateList]
124+
* description.
125+
*
126+
* @param context
127+
* @param attr
128+
* @return
129+
*/
130+
fun getAttrColorStateList(context: Context, attr: Int): ColorStateList? {
131+
try {
132+
val ta = context.obtainStyledAttributes(intArrayOf(attr))
133+
val colorStateList = ta.getColorStateList(0)
115134
ta.recycle()
135+
return colorStateList
136+
} catch (e: Resources.NotFoundException) {
137+
return null
116138
}
117139
}
118140
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="Theme">
4+
<attr name="colorAssertLine" format="color" />
5+
6+
<attr name="colorDebugLine" format="color" />
7+
8+
<attr name="colorErrorLine" format="color" />
9+
10+
<attr name="colorInfoLine" format="color" />
11+
12+
<attr name="colorVerboseLine" format="color" />
13+
14+
<attr name="colorWarningLine" format="color" />
15+
</declare-styleable>
16+
</resources>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
<resources>
22

33
<style name="DetailTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4+
<item name="colorPrimary">@color/colorPrimary</item>
5+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
6+
<item name="colorAccent">@color/colorAccent</item>
47

8+
<item name="colorAssertLine">#ff0000</item>
9+
<item name="colorDebugLine">?android:attr/textColorSecondary</item>
10+
<item name="colorErrorLine">#ff0000</item>
11+
<item name="colorInfoLine">?android:attr/textColorPrimary</item>
12+
<item name="colorVerboseLine">#888888</item>
13+
<item name="colorWarningLine">#ff00ff</item>
514
</style>
615

716
</resources>

0 commit comments

Comments
 (0)