Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ https://github.com/pokeum/jsonviewer-xml/assets/102505472/e2f260f0-cc28-4607-9ec
- [Usage](#usage)
- [Basic](#basic)
- [Advance](#advance)
- [Custom Styles](#styles)


## <a id="installation"> Installation
Expand Down Expand Up @@ -150,3 +151,45 @@ implementation 'com.github.pokeum:jsonviewer-xml:0.0.0'
}
}
```

## <a id="styles"> Custom Styles

### Color

| | <img src="./image/screenshot/styles-color.png" width="160"> |
| -- | -- |
| Key | `"friends"`, `"0"`, `"name"`, `"age"` |
| Value | `"Alice"`, `28` |
| Splitter | `:` |
| Type | `ABC`, `123` |
| Arrow | `\/` |
| Bracket | `[ ]`, `{ }` |
| Divider | `│` |

#### Use JsonRecyclerView

```xml
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
...
app:keyColor="@color/key_color"
app:valueColor="@color/value_color"
app:splitterColor="@color/splitter_color"
app:typeColor="@color/type_color"
app:arrowColor="@color/arrow_color"
app:bracketColor="@color/bracket_color"
app:dividerColor="@color/divider_color" />
```

#### Use RecyclerView

```kotlin
recyclerView.adapter = JsonViewerAdapter(/* JsonElement */).apply {
setKeyColor(JVColor(/* Default Color[, Dark Mode Color] */))
setValueColor(JVColor(/* ... */))
setSplitterColor(JVColor(/* ... */))
setTypeColor(JVColor(/* ... */))
setArrowColor(JVColor(/* ... */))
setBracketColor(JVColor(/* ... */))
setDividerColor(JVColor(/* ... */))
}
```
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ dependencies {

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

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

implementation stdlib.kotlin
implementation androidx.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class JsonViewerActivity : AppCompatActivity() {
private fun initRecyclerView() {

jsonElement?.let {
binding.jsonRecyclerView.adapter = JsonViewerAdapter(it)
val adapter = JsonViewerAdapter(it)
binding.jsonRecyclerView.adapter = adapter
}
}

Expand Down
Binary file added image/screenshot/styles-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.adapter.JsonViewerAdapter
import kr.pokeum.jsonviewer_xml.util.JVColor

@Suppress("DEPRECATION")
class JsonRecyclerView
@JvmOverloads
constructor(
Expand All @@ -15,12 +17,28 @@ constructor(

private var text: String

private var keyColor: Int
private var valueColor: Int
private var splitterColor: Int
private var typeColor: Int
private var arrowColor: Int
private var bracketColor: Int
private var dividerColor: Int

init {
val typedArray = context.obtainStyledAttributes(
attrs, R.styleable.JsonRecyclerView, 0, 0
)
try {
text = typedArray.getString(R.styleable.JsonRecyclerView_text) ?: DEFAULT_TEXT

keyColor = typedArray.getColor(R.styleable.JsonRecyclerView_keyColor, resources.getColor(R.color.jv_key_color))
valueColor = typedArray.getColor(R.styleable.JsonRecyclerView_valueColor, resources.getColor(R.color.jv_value_color))
splitterColor = typedArray.getColor(R.styleable.JsonRecyclerView_splitterColor, resources.getColor(R.color.jv_splitter_color))
typeColor = typedArray.getColor(R.styleable.JsonRecyclerView_typeColor, resources.getColor(R.color.jv_type_color))
arrowColor = typedArray.getColor(R.styleable.JsonRecyclerView_arrowColor, resources.getColor(R.color.jv_arrow_color))
bracketColor = typedArray.getColor(R.styleable.JsonRecyclerView_bracketColor, resources.getColor(R.color.jv_bracket_color))
dividerColor = typedArray.getColor(R.styleable.JsonRecyclerView_dividerColor, resources.getColor(R.color.jv_divider_color))
} finally {
typedArray.recycle()
}
Expand All @@ -36,7 +54,15 @@ constructor(
jsonParser.parse(text)
} catch (_: Throwable) {
jsonParser.parse(DEFAULT_TEXT)
})
}).apply {
setKeyColor(JVColor(keyColor))
setValueColor(JVColor(valueColor))
setSplitterColor(JVColor(splitterColor))
setTypeColor(JVColor(typeColor))
setArrowColor(JVColor(arrowColor))
setBracketColor(JVColor(bracketColor))
setDividerColor(JVColor(dividerColor))
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kr.pokeum.jsonviewer_xml.adapter

import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.util.JVColor

abstract class BaseJsonViewerAdapter<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>()
{
// abstract fun expandAll()
// abstract fun collapseAll()

fun setKeyColor(color: JVColor) { KEY_COLOR = color }
fun setValueColor(color: JVColor) { VALUE_COLOR = color }
fun setSplitterColor(color: JVColor) { SPLITTER_COLOR = color }
fun setTypeColor(color: JVColor) { TYPE_COLOR = color }
fun setArrowColor(color: JVColor) { ARROW_COLOR = color }
fun setBracketColor(color: JVColor) { BRACKET_COLOR = color }
fun setDividerColor(color: JVColor) { DIVIDER_COLOR = color }

companion object {
internal var KEY_COLOR = JVColor(0xFF000000.toInt(), 0xFFFFFFFF.toInt())
internal var VALUE_COLOR = JVColor(0xFF888888.toInt())
internal var SPLITTER_COLOR = JVColor(0xFF000000.toInt(), 0xFFFFFFFF.toInt())
internal var TYPE_COLOR = JVColor(0xFF2196F3.toInt())
internal var ARROW_COLOR = JVColor(0xFFF44336.toInt())
internal var BRACKET_COLOR = JVColor(0xFF4CAF50.toInt())
internal var DIVIDER_COLOR = JVColor(0x1E000000.toInt(), 0x1EFFFFFF.toInt())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kr.pokeum.jsonviewer_xml.viewholder.JsonPrimitiveViewHolder
class JsonViewerAdapter(
jsonElement: JsonElement? = null,
recyclerViewPool: RecyclerView.RecycledViewPool? = null
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
) : BaseJsonViewerAdapter<RecyclerView.ViewHolder>() {

private val elements: MutableList<JsonElement>
private val recyclerViewPool: RecyclerView.RecycledViewPool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import android.text.Html.FROM_HTML_MODE_LEGACY
import android.text.Spanned
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import kr.pokeum.jsonviewer_xml.R
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.pokeum.jsonviewer_xml.util

import android.content.res.Configuration
import android.view.View
import androidx.annotation.ColorInt

class JVColor(
@ColorInt private val default: Int,
@ColorInt private val night: Int
) {
constructor(@ColorInt default: Int) : this(default, default)

internal fun getColor(view: View) = when (view.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> night
else -> default
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kr.pokeum.jsonviewer_xml.viewholder

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
import kr.pokeum.jsonviewer_xml.adapter.JsonViewerAdapter
import kr.pokeum.jsonviewer_xml.databinding.ItemJsonArrayBinding
import kr.pokeum.jsonviewer_xml.model.JsonArray
Expand All @@ -27,16 +29,27 @@ internal class JsonArrayViewHolder(
binding.childRecyclerView.setRecycledViewPool(recycledViewPool)
}

@SuppressLint("SetTextI18n")
fun bind(jsonArray: JsonArray) {
target = jsonArray
expandableUI(jsonArray.isExpanded())
binding.keyLabel.text = "\"${jsonArray.key}\""
childAdapter.setElements(jsonArray.elements)

setStyle()
}

private fun expandableUI(isExpanded: Boolean) {
val rotation = if (isExpanded) 90f else 0f
binding.arrowImage.rotation = rotation
binding.expandableLayout.visibility = if (isExpanded) View.VISIBLE else View.GONE
}

private fun setStyle() {
// COLOR
binding.arrowImage.setColorFilter(BaseJsonViewerAdapter.ARROW_COLOR.getColor(binding.root))
binding.keyDescriptionLabel.setTextColor(BaseJsonViewerAdapter.BRACKET_COLOR.getColor(binding.root))
binding.keyLabel.setTextColor(BaseJsonViewerAdapter.KEY_COLOR.getColor(binding.root))
binding.divider.setBackgroundColor(BaseJsonViewerAdapter.DIVIDER_COLOR.getColor(binding.root))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.pokeum.jsonviewer_xml.viewholder

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
import kr.pokeum.jsonviewer_xml.databinding.ItemJsonNullBinding
import kr.pokeum.jsonviewer_xml.model.JsonNull
import kr.pokeum.jsonviewer_xml.util.fromHtml
Expand All @@ -22,6 +23,13 @@ internal class JsonNullViewHolder(
view = binding.root
)
)

setStyle()
}

private fun setStyle() {
// COLOR
binding.keyDescriptionLabel.setTextColor(BaseJsonViewerAdapter.TYPE_COLOR.getColor(binding.root))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kr.pokeum.jsonviewer_xml.viewholder

import android.annotation.SuppressLint
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
import kr.pokeum.jsonviewer_xml.adapter.JsonViewerAdapter
import kr.pokeum.jsonviewer_xml.databinding.ItemJsonObjectBinding
import kr.pokeum.jsonviewer_xml.model.JsonObject
Expand All @@ -29,16 +31,27 @@ internal class JsonObjectViewHolder(
binding.childRecyclerView.setRecycledViewPool(recycledViewPool)
}

@SuppressLint("SetTextI18n")
fun bind(jsonObject: JsonObject) {
target = jsonObject
expandableUI(jsonObject.isExpanded())
binding.keyLabel.text = "\"${jsonObject.key}\""
childAdapter.setElements(jsonObject.elements)

setStyle()
}

private fun expandableUI(isExpanded: Boolean) {
val rotation = if (isExpanded) 90f else 0f
binding.arrowImage.rotation = rotation
binding.expandableLayout.visibility = if (isExpanded) VISIBLE else GONE
}

private fun setStyle() {
// COLOR
binding.arrowImage.setColorFilter(BaseJsonViewerAdapter.ARROW_COLOR.getColor(binding.root))
binding.keyDescriptionLabel.setTextColor(BaseJsonViewerAdapter.BRACKET_COLOR.getColor(binding.root))
binding.keyLabel.setTextColor(BaseJsonViewerAdapter.KEY_COLOR.getColor(binding.root))
binding.divider.setBackgroundColor(BaseJsonViewerAdapter.DIVIDER_COLOR.getColor(binding.root))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.pokeum.jsonviewer_xml.viewholder

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kr.pokeum.jsonviewer_xml.adapter.BaseJsonViewerAdapter
import kr.pokeum.jsonviewer_xml.databinding.ItemJsonPrimitiveBinding
import kr.pokeum.jsonviewer_xml.model.JsonPrimitive
import kr.pokeum.jsonviewer_xml.util.fromHtml
Expand Down Expand Up @@ -38,6 +39,13 @@ internal class JsonPrimitiveViewHolder(
view = binding.root
)
)

setStyle()
}

private fun setStyle() {
// COLOR
binding.keyDescriptionLabel.setTextColor(BaseJsonViewerAdapter.TYPE_COLOR.getColor(binding.root))
}

companion object {
Expand Down
7 changes: 4 additions & 3 deletions jsonviewer/src/main/res/layout/item_json_array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:layout_marginLeft="@dimen/json_key_margin_start"
android:layout_marginStart="@dimen/json_key_margin_start"
app:srcCompat="@drawable/ic_arrow_white_24dp"
app:tint="@color/red_500"
app:tint="@color/jv_arrow_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -41,7 +41,7 @@
android:layout_gravity="center"
android:text="@string/json_array_key_description_label"
android:textSize="@dimen/json_key_description_label_size"
android:textColor="@color/green_500"
android:textColor="@color/jv_bracket_color"
android:textStyle="bold" />

</FrameLayout>
Expand All @@ -53,7 +53,7 @@
android:layout_marginLeft="@dimen/json_key_margin_start"
android:layout_marginStart="@dimen/json_key_margin_start"
android:textSize="@dimen/json_key_label_size"
android:textColor="@color/jv_array_index_color"
android:textColor="@color/jv_key_color"
android:textStyle="bold"
tools:text="@string/json_key_label"
app:layout_constrainedWidth="true"
Expand All @@ -73,6 +73,7 @@
app:layout_constraintTop_toBottomOf="@+id/headerLayout" >

<View
android:id="@+id/divider"
android:layout_width="@dimen/json_divider_layout_width"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/json_divider_margin_start"
Expand Down
2 changes: 1 addition & 1 deletion jsonviewer/src/main/res/layout/item_json_null.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
android:layout_gravity="center"
android:text="@string/json_null_key_description_label"
android:textSize="@dimen/json_key_description_label_size"
android:textColor="@color/blue_500"
android:textColor="@color/jv_type_color"
android:textStyle="bold" />

</FrameLayout>
Expand Down
Loading