Skip to content

Commit

Permalink
cleanup for the kotlin conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
klinker24 committed Mar 5, 2018
1 parent 26765fd commit 1a86b44
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 101 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class HtmlLinkExampleActivity : AppCompatActivity() {
setSupportActionBar(toolbar)
toolbar.setTitle(R.string.app_name)


val demo1 = findViewById<View>(R.id.demo1) as TextView
demo1.text = Html.fromHtml(TEXT)
demo1.applyLinks(getLinks())
Expand All @@ -43,22 +42,14 @@ class HtmlLinkExampleActivity : AppCompatActivity() {

private fun getLinks(): List<Link> {
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)
}
Expand All @@ -68,7 +59,8 @@ class HtmlLinkExampleActivity : AppCompatActivity() {
}

companion object {

private const val TEXT = "Here is an example link <a href=\"www.google.com\">www.google.com</a>." + "To show it alongside other LinkBuilder functionality, lets highlight this."
private const val TEXT =
"Here is an example link <a href=\"www.google.com\">www.google.com</a>." +
"To show it alongside other LinkBuilder functionality, lets highlight this."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,65 +47,41 @@ class MainActivity : AppCompatActivity() {

private fun getLinks(): List<Link> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

VERSION_NAME=2.0.0
VERSION_NAME=2.0.1
VERSION_CODE=19

MIN_SDK=14
Expand Down
70 changes: 46 additions & 24 deletions library/src/main/java/com/klinker/android/link_builder/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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).
Expand Down Expand Up @@ -209,13 +229,15 @@ class Link {
/**
* Interface to manage the single clicks.
*/
@FunctionalInterface
interface OnClickListener {
fun onClick(clickedText: String)
}

/**
* Interface to manage the long clicks.
*/
@FunctionalInterface
interface OnLongClickListener {
fun onLongClick(clickedText: String)
}
Expand Down

0 comments on commit 1a86b44

Please sign in to comment.