Skip to content

Commit

Permalink
Task/recognize links inside map (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nailshaykhraziev authored Aug 10, 2021
1 parent 5c95fa9 commit aca5f07
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,35 @@ open class BlockRenderer(
viewGroup.addView(childView)
}
}
childNode is CDARichHyperLink && childNode.data is String -> {
val childLayout = childView.findViewById<ViewGroup>(R.id.rich_content)
if (childLayout.childCount > 0) {
val childTextView = childLayout.getChildAt(0) as TextView
val span = UrlSpan(childNode.data as String)
val text = lastTextView?.let {
SpannableStringBuilder(it.text).append(childTextView.text)
} ?: SpannableStringBuilder(childTextView.text)
childNode is CDARichHyperLink -> {
getDataUrl(childNode.data)?.let { data->
val childLayout = childView.findViewById<ViewGroup>(R.id.rich_content)
if (childLayout.childCount > 0) {
val childTextView = childLayout.getChildAt(0) as TextView
val span = UrlSpan(data)
val text = lastTextView?.let {
SpannableStringBuilder(it.text).append(childTextView.text)
} ?: SpannableStringBuilder(childTextView.text)

context.config?.linkColor?.let {
span.textColor = it
}
context.config?.linkColor?.let {
span.textColor = it
}

text.setSpan(
span,
lastTextView?.text?.length ?: 0,
text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
text.setSpan(
span,
lastTextView?.text?.length ?: 0,
text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)

lastTextView?.let {
it.movementMethod = LinkMovementMethod.getInstance()
it.text = text
} ?: run {
childTextView.text = text
lastTextView = childTextView
viewGroup.addView(childView)
lastTextView?.let {
it.movementMethod = LinkMovementMethod.getInstance()
it.text = text
} ?: run {
childTextView.text = text
lastTextView = childTextView
viewGroup.addView(childView)
}
}
}
}
Expand All @@ -91,4 +93,10 @@ open class BlockRenderer(
context: AndroidContext,
node: CDARichNode
): View = context.inflater.inflate(R.layout.rich_block_layout, null, false)

private fun getDataUrl(data: Any): String? = when {
data is String -> data
data is Map<*, *> && data["uri"] is String -> data["uri"] as? String
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.contentful.java.cda.CDAResource
import com.contentful.java.cda.LocalizedResource
import com.contentful.java.cda.rich.*
import com.contentful.java.cda.rich.CDARichMark.*
import com.google.gson.internal.LinkedTreeMap
import java.lang.reflect.Field

data class Page(val name: String, val document: CDARichDocument)
Expand Down Expand Up @@ -49,9 +50,21 @@ val PAGES = mutableListOf(
CDARichListItem().addAll(text("second item")),
CDARichListItem().addAll(text("third item")),
CDARichListItem().addAll(
CDARichHyperLink("https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=dogs")
.addAll(text("Hyperlink to first item")),
CDARichListItem().addAll(text("Normal Text"))
CDARichParagraph().addAll(
text("First Text"),
CDARichHyperLink(
LinkedTreeMap<String, String>().apply {
put(
"uri",
"https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=cats"
)
}
).addAll(text("Hyperlink to first item")),
text("Second Text")
),
CDARichListItem().addAll(
text("Normal Text")
)
),
CDARichListItem().addAll(
CDARichHyperLink("https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=cats")
Expand Down

0 comments on commit aca5f07

Please sign in to comment.