forked from contentful/rich-text-renderer-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename .java to .kt * Add parsing links inside of block * Add colored url spannable
- Loading branch information
1 parent
2f8f9ce
commit ee4f4ff
Showing
10 changed files
with
406 additions
and
323 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
android/src/main/java/com/contentful/rich/android/AndroidConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.contentful.rich.android.renderer.views | ||
|
||
import android.text.SpannableStringBuilder | ||
import android.text.Spanned | ||
import android.text.method.LinkMovementMethod | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import com.contentful.java.cda.rich.CDARichBlock | ||
import com.contentful.java.cda.rich.CDARichHyperLink | ||
import com.contentful.java.cda.rich.CDARichNode | ||
import com.contentful.rich.android.AndroidContext | ||
import com.contentful.rich.android.AndroidProcessor | ||
import com.contentful.rich.android.AndroidRenderer | ||
import com.contentful.rich.android.R | ||
import com.contentful.rich.android.renderer.views.span.UrlSpan | ||
|
||
open class BlockRenderer( | ||
processor: AndroidProcessor<View> | ||
) : AndroidRenderer<AndroidContext, View>(processor) { | ||
|
||
override fun canRender(context: AndroidContext?, node: CDARichNode): Boolean = node is CDARichBlock | ||
|
||
override fun render(context: AndroidContext, node: CDARichNode): View? { | ||
val block = node as CDARichBlock | ||
val result = inflateRichLayout(context, node) | ||
val content = result.findViewById<ViewGroup>(R.id.rich_content) | ||
var lastTextView: TextView? = null | ||
block.content.forEach { childNode -> | ||
val childView = processor.process(context, childNode!!) | ||
if (childView != null) { | ||
when { | ||
childView is TextView -> { | ||
lastTextView?.let { | ||
it.text = SpannableStringBuilder(it.text).append(" ").append(childView.text) | ||
} ?: run { | ||
lastTextView = childView | ||
content.addView(childView) | ||
} | ||
} | ||
childNode is CDARichHyperLink -> { | ||
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(" ").append(childTextView.text) | ||
} ?: SpannableStringBuilder(childTextView.text) | ||
|
||
context.config?.linkColor?.let { | ||
span.textColor = it | ||
} | ||
|
||
text.setSpan( | ||
span, | ||
lastTextView?.text?.length?.plus(1) ?: 0, | ||
text.length, | ||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | ||
) | ||
|
||
lastTextView?.let { | ||
it.movementMethod = LinkMovementMethod.getInstance() | ||
it.text = text | ||
} ?: run { | ||
childTextView.text = text | ||
lastTextView = childTextView | ||
content.addView(childView) | ||
} | ||
} | ||
} | ||
context.path?.size ?: 0 > 1 -> { | ||
val indented = context.inflater.inflate(R.layout.rich_indention_layout, null, false) | ||
(indented.findViewById<View>(R.id.rich_content) as ViewGroup).addView(childView) | ||
content.addView(indented) | ||
} | ||
else -> content.addView(childView) | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
protected open fun inflateRichLayout( | ||
context: AndroidContext, | ||
node: CDARichNode | ||
): View = context.inflater.inflate(R.layout.rich_block_layout, null, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
android/src/main/java/com/contentful/rich/android/renderer/views/span/UrlSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.contentful.rich.android.renderer.views.span | ||
|
||
import android.text.TextPaint | ||
import android.text.style.URLSpan | ||
import androidx.annotation.ColorInt | ||
|
||
class UrlSpan(url: String) : URLSpan(url) { | ||
|
||
@ColorInt | ||
var textColor: Int? = null | ||
|
||
override fun updateDrawState(ds: TextPaint) { | ||
textColor?.also { | ||
ds.linkColor = it | ||
} | ||
super.updateDrawState(ds) | ||
} | ||
} |
Oops, something went wrong.