Skip to content

Commit 68233d2

Browse files
authored
Merge pull request #2 from pokeum/develop
Release v0.0.1
2 parents 2707054 + 9609c18 commit 68233d2

File tree

20 files changed

+192
-27
lines changed

20 files changed

+192
-27
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ https://github.com/pokeum/jsonviewer-xml/assets/102505472/e2f260f0-cc28-4607-9ec
3737
- [Usage](#usage)
3838
- [Basic](#basic)
3939
- [Advance](#advance)
40+
- [Custom Styles](#styles)
4041

4142

4243
## <a id="installation"> Installation
@@ -150,3 +151,45 @@ implementation 'com.github.pokeum:jsonviewer-xml:0.0.0'
150151
}
151152
}
152153
```
154+
155+
## <a id="styles"> Custom Styles
156+
157+
### Color
158+
159+
| | <img src="./image/screenshot/styles-color.png" width="160"> |
160+
| -- | -- |
161+
| Key | `"friends"`, `"0"`, `"name"`, `"age"` |
162+
| Value | `"Alice"`, `28` |
163+
| Splitter | `:` |
164+
| Type | `ABC`, `123` |
165+
| Arrow | `\/` |
166+
| Bracket | `[ ]`, `{ }` |
167+
| Divider | `` |
168+
169+
#### Use JsonRecyclerView
170+
171+
```xml
172+
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
173+
...
174+
app:keyColor="@color/key_color"
175+
app:valueColor="@color/value_color"
176+
app:splitterColor="@color/splitter_color"
177+
app:typeColor="@color/type_color"
178+
app:arrowColor="@color/arrow_color"
179+
app:bracketColor="@color/bracket_color"
180+
app:dividerColor="@color/divider_color" />
181+
```
182+
183+
#### Use RecyclerView
184+
185+
```kotlin
186+
recyclerView.adapter = JsonViewerAdapter(/* JsonElement */).apply {
187+
setKeyColor(JVColor(/* Default Color[, Dark Mode Color] */))
188+
setValueColor(JVColor(/* ... */))
189+
setSplitterColor(JVColor(/* ... */))
190+
setTypeColor(JVColor(/* ... */))
191+
setArrowColor(JVColor(/* ... */))
192+
setBracketColor(JVColor(/* ... */))
193+
setDividerColor(JVColor(/* ... */))
194+
}
195+
```

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ dependencies {
5959

6060
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
6161

62-
implementation 'com.github.pokeum:jsonviewer-xml:0.0.0'
62+
//implementation 'com.github.pokeum:jsonviewer-xml:0.0.0'
63+
implementation(project(":jsonviewer"))
6364

6465
implementation stdlib.kotlin
6566
implementation androidx.core

app/src/main/java/kr/pokeum/app/presentation/activity/JsonViewerActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class JsonViewerActivity : AppCompatActivity() {
4141
private fun initRecyclerView() {
4242

4343
jsonElement?.let {
44-
binding.jsonRecyclerView.adapter = JsonViewerAdapter(it)
44+
val adapter = JsonViewerAdapter(it)
45+
binding.jsonRecyclerView.adapter = adapter
4546
}
4647
}
4748

image/screenshot/styles-color.png

17.8 KB
Loading

jsonviewer/src/main/java/kr/pokeum/jsonviewer_xml/JsonRecyclerView.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import android.util.AttributeSet
55
import androidx.recyclerview.widget.LinearLayoutManager
66
import androidx.recyclerview.widget.RecyclerView
77
import kr.pokeum.jsonviewer_xml.adapter.JsonViewerAdapter
8+
import kr.pokeum.jsonviewer_xml.util.JVColor
89

10+
@Suppress("DEPRECATION")
911
class JsonRecyclerView
1012
@JvmOverloads
1113
constructor(
@@ -15,12 +17,28 @@ constructor(
1517

1618
private var text: String
1719

20+
private var keyColor: Int
21+
private var valueColor: Int
22+
private var splitterColor: Int
23+
private var typeColor: Int
24+
private var arrowColor: Int
25+
private var bracketColor: Int
26+
private var dividerColor: Int
27+
1828
init {
1929
val typedArray = context.obtainStyledAttributes(
2030
attrs, R.styleable.JsonRecyclerView, 0, 0
2131
)
2232
try {
2333
text = typedArray.getString(R.styleable.JsonRecyclerView_text) ?: DEFAULT_TEXT
34+
35+
keyColor = typedArray.getColor(R.styleable.JsonRecyclerView_keyColor, resources.getColor(R.color.jv_key_color))
36+
valueColor = typedArray.getColor(R.styleable.JsonRecyclerView_valueColor, resources.getColor(R.color.jv_value_color))
37+
splitterColor = typedArray.getColor(R.styleable.JsonRecyclerView_splitterColor, resources.getColor(R.color.jv_splitter_color))
38+
typeColor = typedArray.getColor(R.styleable.JsonRecyclerView_typeColor, resources.getColor(R.color.jv_type_color))
39+
arrowColor = typedArray.getColor(R.styleable.JsonRecyclerView_arrowColor, resources.getColor(R.color.jv_arrow_color))
40+
bracketColor = typedArray.getColor(R.styleable.JsonRecyclerView_bracketColor, resources.getColor(R.color.jv_bracket_color))
41+
dividerColor = typedArray.getColor(R.styleable.JsonRecyclerView_dividerColor, resources.getColor(R.color.jv_divider_color))
2442
} finally {
2543
typedArray.recycle()
2644
}
@@ -36,7 +54,15 @@ constructor(
3654
jsonParser.parse(text)
3755
} catch (_: Throwable) {
3856
jsonParser.parse(DEFAULT_TEXT)
39-
})
57+
}).apply {
58+
setKeyColor(JVColor(keyColor))
59+
setValueColor(JVColor(valueColor))
60+
setSplitterColor(JVColor(splitterColor))
61+
setTypeColor(JVColor(typeColor))
62+
setArrowColor(JVColor(arrowColor))
63+
setBracketColor(JVColor(bracketColor))
64+
setDividerColor(JVColor(dividerColor))
65+
}
4066
}
4167

4268
companion object {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package kr.pokeum.jsonviewer_xml.adapter
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import kr.pokeum.jsonviewer_xml.util.JVColor
5+
6+
abstract class BaseJsonViewerAdapter<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>()
7+
{
8+
// abstract fun expandAll()
9+
// abstract fun collapseAll()
10+
11+
fun setKeyColor(color: JVColor) { KEY_COLOR = color }
12+
fun setValueColor(color: JVColor) { VALUE_COLOR = color }
13+
fun setSplitterColor(color: JVColor) { SPLITTER_COLOR = color }
14+
fun setTypeColor(color: JVColor) { TYPE_COLOR = color }
15+
fun setArrowColor(color: JVColor) { ARROW_COLOR = color }
16+
fun setBracketColor(color: JVColor) { BRACKET_COLOR = color }
17+
fun setDividerColor(color: JVColor) { DIVIDER_COLOR = color }
18+
19+
companion object {
20+
internal var KEY_COLOR = JVColor(0xFF000000.toInt(), 0xFFFFFFFF.toInt())
21+
internal var VALUE_COLOR = JVColor(0xFF888888.toInt())
22+
internal var SPLITTER_COLOR = JVColor(0xFF000000.toInt(), 0xFFFFFFFF.toInt())
23+
internal var TYPE_COLOR = JVColor(0xFF2196F3.toInt())
24+
internal var ARROW_COLOR = JVColor(0xFFF44336.toInt())
25+
internal var BRACKET_COLOR = JVColor(0xFF4CAF50.toInt())
26+
internal var DIVIDER_COLOR = JVColor(0x1E000000.toInt(), 0x1EFFFFFF.toInt())
27+
}
28+
}
29+

jsonviewer/src/main/java/kr/pokeum/jsonviewer_xml/adapter/JsonViewerAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kr.pokeum.jsonviewer_xml.viewholder.JsonPrimitiveViewHolder
1919
class JsonViewerAdapter(
2020
jsonElement: JsonElement? = null,
2121
recyclerViewPool: RecyclerView.RecycledViewPool? = null
22-
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
22+
) : BaseJsonViewerAdapter<RecyclerView.ViewHolder>() {
2323

2424
private val elements: MutableList<JsonElement>
2525
private val recyclerViewPool: RecyclerView.RecycledViewPool

jsonviewer/src/main/java/kr/pokeum/jsonviewer_xml/util/SpansUtils.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import android.text.Html.FROM_HTML_MODE_LEGACY
66
import android.text.Spanned
77
import android.view.View
88
import androidx.annotation.ColorInt
9-
import androidx.core.content.ContextCompat
10-
import kr.pokeum.jsonviewer_xml.R
9+
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
1110

1211
internal fun fromHtml(htmlText: String): Spanned {
1312
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@@ -21,9 +20,9 @@ internal fun keyValueHtmlGenerator(
2120
splitter: String,
2221
view: View, /* Support dark mode */
2322
): String {
24-
return textColorHtmlGenerator(key, ContextCompat.getColor(view.context, R.color.jv_key_color)) +
25-
textColorHtmlGenerator(splitter, ContextCompat.getColor(view.context, R.color.jv_splitter_color)) +
26-
textColorHtmlGenerator(value, ContextCompat.getColor(view.context, R.color.jv_value_color))
23+
return textColorHtmlGenerator(key, BaseJsonViewerAdapter.KEY_COLOR.getColor(view)) +
24+
textColorHtmlGenerator(splitter, BaseJsonViewerAdapter.SPLITTER_COLOR.getColor(view)) +
25+
textColorHtmlGenerator(value, BaseJsonViewerAdapter.VALUE_COLOR.getColor(view))
2726
}
2827

2928
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kr.pokeum.jsonviewer_xml.util
2+
3+
import android.content.res.Configuration
4+
import android.view.View
5+
import androidx.annotation.ColorInt
6+
7+
class JVColor(
8+
@ColorInt private val default: Int,
9+
@ColorInt private val night: Int
10+
) {
11+
constructor(@ColorInt default: Int) : this(default, default)
12+
13+
internal fun getColor(view: View) = when (view.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
14+
Configuration.UI_MODE_NIGHT_YES -> night
15+
else -> default
16+
}
17+
}

jsonviewer/src/main/java/kr/pokeum/jsonviewer_xml/viewholder/JsonArrayViewHolder.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package kr.pokeum.jsonviewer_xml.viewholder
22

3+
import android.annotation.SuppressLint
34
import android.view.View
45
import androidx.recyclerview.widget.RecyclerView
6+
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
57
import kr.pokeum.jsonviewer_xml.adapter.JsonViewerAdapter
68
import kr.pokeum.jsonviewer_xml.databinding.ItemJsonArrayBinding
79
import kr.pokeum.jsonviewer_xml.model.JsonArray
@@ -27,16 +29,27 @@ internal class JsonArrayViewHolder(
2729
binding.childRecyclerView.setRecycledViewPool(recycledViewPool)
2830
}
2931

32+
@SuppressLint("SetTextI18n")
3033
fun bind(jsonArray: JsonArray) {
3134
target = jsonArray
3235
expandableUI(jsonArray.isExpanded())
3336
binding.keyLabel.text = "\"${jsonArray.key}\""
3437
childAdapter.setElements(jsonArray.elements)
38+
39+
setStyle()
3540
}
3641

3742
private fun expandableUI(isExpanded: Boolean) {
3843
val rotation = if (isExpanded) 90f else 0f
3944
binding.arrowImage.rotation = rotation
4045
binding.expandableLayout.visibility = if (isExpanded) View.VISIBLE else View.GONE
4146
}
47+
48+
private fun setStyle() {
49+
// COLOR
50+
binding.arrowImage.setColorFilter(BaseJsonViewerAdapter.ARROW_COLOR.getColor(binding.root))
51+
binding.keyDescriptionLabel.setTextColor(BaseJsonViewerAdapter.BRACKET_COLOR.getColor(binding.root))
52+
binding.keyLabel.setTextColor(BaseJsonViewerAdapter.KEY_COLOR.getColor(binding.root))
53+
binding.divider.setBackgroundColor(BaseJsonViewerAdapter.DIVIDER_COLOR.getColor(binding.root))
54+
}
4255
}

0 commit comments

Comments
 (0)