diff --git a/changelog.md b/changelog.md index 90ef1ba..ff69963 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ## Changelog +### Version 2.0.1: +- Kotlin cleanup + ### Version 2.0.0: - Built on Kotlin - Extension method support for `TextView#applyLinks` to avoid boilerplate of creating the `LinkBuilder` diff --git a/example/src/main/java/com/klinker/android/link_builder_example/HtmlLinkExampleActivity.kt b/example/src/main/java/com/klinker/android/link_builder_example/HtmlLinkExampleActivity.kt index 9c2c2c6..77a6461 100644 --- a/example/src/main/java/com/klinker/android/link_builder_example/HtmlLinkExampleActivity.kt +++ b/example/src/main/java/com/klinker/android/link_builder_example/HtmlLinkExampleActivity.kt @@ -31,7 +31,6 @@ class HtmlLinkExampleActivity : AppCompatActivity() { setSupportActionBar(toolbar) toolbar.setTitle(R.string.app_name) - val demo1 = findViewById(R.id.demo1) as TextView demo1.text = Html.fromHtml(TEXT) demo1.applyLinks(getLinks()) @@ -43,22 +42,14 @@ class HtmlLinkExampleActivity : AppCompatActivity() { private fun getLinks(): List { val google = Link("www.google.com") - google.setTextColor(Color.parseColor("#00BCD4")) - google.setHighlightAlpha(.4f) - google.setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - showToast("clicked: $clickedText") - } - }) + .setTextColor(Color.parseColor("#00BCD4")) + .setHighlightAlpha(.4f) + .setOnClickListener { showToast("clicked: $it") } val exampleText = Link("this") - exampleText.setTextColor(Color.parseColor("#00BCD4")) - exampleText.setHighlightAlpha(.4f) - exampleText.setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - showToast("clicked the example text") - } - }) + .setTextColor(Color.parseColor("#00BCD4")) + .setHighlightAlpha(.4f) + .setOnClickListener { showToast("clicked the example text") } return listOf(google, exampleText) } @@ -68,7 +59,8 @@ class HtmlLinkExampleActivity : AppCompatActivity() { } companion object { - - private const val TEXT = "Here is an example link www.google.com." + "To show it alongside other LinkBuilder functionality, lets highlight this." + private const val TEXT = + "Here is an example link www.google.com." + + "To show it alongside other LinkBuilder functionality, lets highlight this." } } diff --git a/example/src/main/java/com/klinker/android/link_builder_example/MainActivity.kt b/example/src/main/java/com/klinker/android/link_builder_example/MainActivity.kt index 8111c4d..f75b611 100644 --- a/example/src/main/java/com/klinker/android/link_builder_example/MainActivity.kt +++ b/example/src/main/java/com/klinker/android/link_builder_example/MainActivity.kt @@ -47,65 +47,41 @@ class MainActivity : AppCompatActivity() { private fun getLinks(): List { val github = Link("TextView-LinkBuilder") - github.setTypeface(Typeface.DEFAULT_BOLD) - .setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - openLink(GITHUB_LINK) - } - }) + .setTypeface(Typeface.DEFAULT_BOLD) + .setOnClickListener { openLink(GITHUB_LINK) } val mentions = Link(Pattern.compile("@\\w{1,15}")) - mentions.setTextColor(Color.parseColor("#00BCD4")) - mentions.setHighlightAlpha(.4f) - mentions.setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - openLink(TWITTER_PROFILE + clickedText.replace("@", "")) - } - }) + .setTextColor(Color.parseColor("#00BCD4")) + .setHighlightAlpha(.4f) + .setOnClickListener { clickedText -> + openLink(TWITTER_PROFILE + clickedText.replace("@", "")) + } val numbers = Link(Pattern.compile("[0-9]+")) - numbers.setTextColor(Color.parseColor("#FF9800")) - numbers.setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - showToast("Clicked: $clickedText") - } - }) + .setTextColor(Color.parseColor("#FF9800")) + .setOnClickListener { showToast("Clicked: $it") } val longClickHere = Link("here") - longClickHere.setTextColor(Color.parseColor("#259B24")) - longClickHere.setOnLongClickListener(object : Link.OnLongClickListener { - override fun onLongClick(clickedText: String) { - showToast("You long clicked. Nice job.") - } - }) + .setTextColor(Color.parseColor("#259B24")) + .setOnLongClickListener { showToast("You long clicked. Nice job.") } - val yes = Link("Yes") - yes.setUnderlined(true) - yes.setTextColor(Color.parseColor("#FFEB3B")) + val yes = Link("Yes").setUnderlined(true) + .setTextColor(Color.parseColor("#FFEB3B")) - val no = Link("No") - no.setUnderlined(false) - no.setTextColor(Color.parseColor("#FFEB3B")) + val no = Link("No").setUnderlined(false) + .setTextColor(Color.parseColor("#FFEB3B")) - val bold = Link("bold") - bold.setBold(true) - bold.setTextColor(Color.parseColor("#FF0000")) + val bold = Link("bold").setBold(true) + .setTextColor(Color.parseColor("#FF0000")) - val prepend = Link("prepended") - prepend.setPrependedText("(!)") - - val appended = Link("appended") - appended.setAppendedText("(!)") + val prepend = Link("prepended").setPrependedText("(!)") + val appended = Link("appended").setAppendedText("(!)") val playStore = Link("Play Store") - playStore.setTextColor(Color.parseColor("#FF9800")) - playStore.setTextColorOfHighlightedLink(Color.parseColor("#FF6600")) - playStore.setHighlightAlpha(0f) - playStore.setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - openLink(PLAY_STORE) - } - }) + .setTextColor(Color.parseColor("#FF9800")) + .setTextColorOfHighlightedLink(Color.parseColor("#FF6600")) + .setHighlightAlpha(0f) + .setOnClickListener { openLink(PLAY_STORE) } return listOf(github, mentions, numbers, longClickHere, yes, no, bold, prepend, appended, playStore) } diff --git a/example/src/main/java/com/klinker/android/link_builder_example/list_view_example/SampleAdapter.kt b/example/src/main/java/com/klinker/android/link_builder_example/list_view_example/SampleAdapter.kt index dc31e03..45f4d2c 100644 --- a/example/src/main/java/com/klinker/android/link_builder_example/list_view_example/SampleAdapter.kt +++ b/example/src/main/java/com/klinker/android/link_builder_example/list_view_example/SampleAdapter.kt @@ -36,18 +36,8 @@ class SampleAdapter(private val mContext: Context) : BaseAdapter() { val textView = convertView as LinkConsumableTextView? textView!!.text = String.format(TEXT, position) - // Add 2 Links - val link1 = Link(LINK1).setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - Log.d(TAG, LINK1) - } - }) - - val link2 = Link(LINK2).setOnClickListener(object : Link.OnClickListener { - override fun onClick(clickedText: String) { - Log.d(TAG, LINK2) - } - }) + val link1 = Link(LINK1).setOnClickListener { Log.d(TAG, LINK1) } + val link2 = Link(LINK2).setOnClickListener { Log.d(TAG, LINK2) } textView.applyLinks(link1, link2) return convertView @@ -60,5 +50,6 @@ class SampleAdapter(private val mContext: Context) : BaseAdapter() { private const val LINK1 = "First link" private const val LINK2 = "Second link" private const val TEXT = "This is item %d. $LINK1 $LINK2" + } } diff --git a/gradle.properties b/gradle.properties index 8f3db22..8d8acdc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,7 +14,7 @@ # limitations under the License. # -VERSION_NAME=2.0.0 +VERSION_NAME=2.0.1 VERSION_CODE=19 MIN_SDK=14 diff --git a/library/src/main/java/com/klinker/android/link_builder/Link.kt b/library/src/main/java/com/klinker/android/link_builder/Link.kt index 32334cf..a5b9906 100644 --- a/library/src/main/java/com/klinker/android/link_builder/Link.kt +++ b/library/src/main/java/com/klinker/android/link_builder/Link.kt @@ -22,30 +22,30 @@ import java.util.regex.Pattern @Suppress("MemberVisibilityCanBePrivate") class Link { - internal var text: String? = null - private set - internal var prependedText: String? = null - private set - internal var appendedText: String? = null - private set - internal var pattern: Pattern? = null - private set - internal var textColor = 0 - private set - internal var textColorOfHighlightedLink = 0 - private set - internal var highlightAlpha = DEFAULT_ALPHA - private set - internal var underlined = true - private set - internal var bold = false - private set - internal var typeface: Typeface? = null - private set - internal var clickListener: OnClickListener? = null - private set - internal var longClickListener: OnLongClickListener? = null - private set + var text: String? = null + set(value) { setText(value!!) } + var prependedText: String? = null + set(value) { setPrependedText(value!!) } + var appendedText: String? = null + set(value) { setAppendedText(value!!) } + var pattern: Pattern? = null + set(value) { setPattern(value!!) } + var textColor = 0 + set(value) { setTextColor(value) } + var textColorOfHighlightedLink = 0 + set(value) { setTextColorOfHighlightedLink(value) } + var highlightAlpha = DEFAULT_ALPHA + set(value) { setHighlightAlpha(value) } + var underlined = true + set(value) { setUnderlined(value) } + var bold = false + set(value) { setBold(value) } + var typeface: Typeface? = null + set(value) { setTypeface(value!!) } + var clickListener: OnClickListener? = null + set(value) { setOnClickListener(value!!) } + var longClickListener: OnLongClickListener? = null + set(value) { setOnLongClickListener(value!!) } /** * Copy Constructor. @@ -136,6 +136,16 @@ class Link { return this } + fun setOnClickListener(listener: (String) -> Unit): Link { + this.clickListener = object : OnClickListener { + override fun onClick(clickedText: String) { + listener(clickedText) + } + } + + return this + } + /** * Specify what happens with a long click. * @param longClickListener action for the long click. @@ -146,6 +156,16 @@ class Link { return this } + fun setOnLongClickListener(listener: (String) -> Unit): Link { + this.longClickListener = object : OnLongClickListener { + override fun onLongClick(clickedText: String) { + listener(clickedText) + } + } + + return this + } + /** * Specify the text color for the linked text. * @param color as an integer (not resource). @@ -209,6 +229,7 @@ class Link { /** * Interface to manage the single clicks. */ + @FunctionalInterface interface OnClickListener { fun onClick(clickedText: String) } @@ -216,6 +237,7 @@ class Link { /** * Interface to manage the long clicks. */ + @FunctionalInterface interface OnLongClickListener { fun onLongClick(clickedText: String) }