Skip to content

Commit

Permalink
release 1.0.0 and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
iamageo committed Feb 2, 2022
1 parent 3345cf5 commit e06ceb9
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 7 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion app/src/main/java/com/iamageo/another_read_more/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package com.iamageo.another_read_more

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.iamageo.library.AnotherReadMore


class MainActivity : AppCompatActivity() {

private var anotherReadMore: AnotherReadMore? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val mtv = findViewById<TextView>(R.id.tv)

anotherReadMore = AnotherReadMore.Builder(this).build()

val readMoreOption: AnotherReadMore = AnotherReadMore.Builder(this)
.textLength(100, AnotherReadMore.TYPE_LINE)
.moreLabel("mais")
.lessLabel("menos")
.build()

readMoreOption.addReadMoreTo(
mtv,
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Anotherreadmore" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Anotherreadmore" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Anotherreadmore" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Anotherreadmore" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
21 changes: 17 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'maven-publish'
}

group = 'com.github.iamageo'
version = '1.0.0'

android {
compileSdkVersion 31
buildToolsVersion "31.0.0"
Expand Down Expand Up @@ -33,12 +37,21 @@ android {
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'com.github.iamageo'
artifactId = 'another_read_more'
version = '1.0.0'
}
}
}
}
129 changes: 129 additions & 0 deletions library/src/main/java/com/iamageo/library/AnotherReadMore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.iamageo.library

import android.animation.LayoutTransition
import android.content.Context
import android.os.Handler
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.TextView

class AnotherReadMore(
var context: Context? = null,
val textLength: Int = 0,
val textLengthType: Int = 0,
val moreLabel: String? = null,
val lessLabel: String? = null,
) {

private constructor(builder: Builder) : this(
builder.context,
builder.textLength,
builder.textLengthType,
builder.moreLabel,
builder.lessLabel,
)

fun addReadMoreTo(textView: TextView, text: CharSequence) {
if (textLengthType == AnotherReadMore.TYPE_CHARACTER) {
if (text.length <= textLength) {
textView.text = text
return
}
} else {
// If TYPE_LINE
textView.setLines(textLength)
textView.text = text
}
textView.post(Runnable {
var textLengthNew = textLength
if (textLengthType == AnotherReadMore.TYPE_LINE) {
if (textView.layout.lineCount <= textLength) {
textView.text = text
return@Runnable
}
val lp = textView.layoutParams as MarginLayoutParams
val subString = text.toString().substring(
textView.layout.getLineStart(0),
textView.layout.getLineEnd(textLength - 1)
)
textLengthNew = subString.length - (moreLabel!!.length + 4 + lp.rightMargin / 6)
}
val spannableStringBuilder = SpannableStringBuilder(text.subSequence(0, textLengthNew))
.append("...")
.append(moreLabel)
val ss = SpannableString.valueOf(spannableStringBuilder)
val clickableSpan: ClickableSpan = object : ClickableSpan() {
override fun onClick(view: View) {
addReadLess(textView, text)
}

}
ss.setSpan(
clickableSpan,
ss.length - moreLabel!!.length,
ss.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
val layoutTransition = LayoutTransition()
layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
(textView.parent as ViewGroup).layoutTransition = layoutTransition
textView.text = ss
textView.movementMethod = LinkMovementMethod.getInstance()
})
}

private fun addReadLess(textView: TextView, text: CharSequence) {
textView.maxLines = Int.MAX_VALUE
val spannableStringBuilder = SpannableStringBuilder(text)
.append(lessLabel)
val ss = SpannableString.valueOf(spannableStringBuilder)
val clickableSpan: ClickableSpan = object : ClickableSpan() {
override fun onClick(view: View) {
Handler().post { addReadMoreTo(textView, text) }
}

}
ss.setSpan(
clickableSpan,
ss.length - lessLabel!!.length,
ss.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
textView.text = ss
textView.movementMethod = LinkMovementMethod.getInstance()
}

class Builder(c: Context) {
val context: Context = c

var textLength = 100
private set
var textLengthType = AnotherReadMore.TYPE_CHARACTER
private set
var moreLabel = "mais"
private set
var lessLabel = "menos"
private set


fun textLength(length: Int?, typeLine: Int) = apply { textLength = length ?: 100 }
fun textLengthType(type: Int) = apply { textLengthType = type }
fun moreLabel(more: String) = apply { moreLabel = more }
fun lessLabel(less: String) = apply { lessLabel = less }

fun build() = AnotherReadMore(this)
}

companion object {
private val TAG = AnotherReadMore::class.java.simpleName
const val TYPE_LINE = 1
const val TYPE_CHARACTER = 2
}

}

0 comments on commit e06ceb9

Please sign in to comment.