Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeerus committed May 26, 2020
1 parent 6ad1aa0 commit 8a30ed6
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ allprojects {
```
```
dependencies {
implementation 'com.github.lndmflngs:font-span-view:1.0.0'
implementation 'com.github.lndmflngs:font-span-view:1.0.1'
}
```
## Usage
Expand Down
48 changes: 9 additions & 39 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "com.lockwood.robotospandemo"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

buildTypes {
debug {
applicationIdSuffix ".debug"
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
apply from: rootProject.file('gradle/android/app.gradle')
apply from: rootProject.file('gradle/deps.gradle')

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation "com.google.android.material:material:1.2.0-alpha06"

implementation(
deps.kotlin.stdlib,
deps.android.appcompat,
deps.android.material
)

implementation project(':font-library')
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/com/lockwood/fontspandemo/extensions/Attrs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lockwood.fontspandemo.extensions

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes

internal inline fun fetchAttrs(
context: Context,
attrs: IntArray,
set: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0,
fetch: TypedArray .() -> Unit = {}
) {
val typedArray = context.theme.obtainStyledAttributes(
set,
attrs,
defStyleAttr,
defStyleRes
)
with(typedArray) {
try {
fetch()
} finally {
recycle()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.lockwood.fontspandemo.extensions

import android.content.res.TypedArray
import androidx.annotation.ColorInt

internal fun TypedArray.getStringOrEmpty(
index: Int
): String {
return getString(index) ?: ""
}

internal fun TypedArray.getTextSizeOrDefault(
index: Int,
default: Int
): Int {
return getDimensionPixelSize(index, default)
}

internal fun TypedArray.getTextColorOrDefault(
index: Int,
@ColorInt default: Int
): Int {
return getColor(index, default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import android.text.style.UnderlineSpan
import android.util.AttributeSet
import android.util.Log
import com.lockwood.fontspandemo.R
import com.lockwood.multispan.extensions.fetchAttrs
import com.lockwood.multispan.extensions.getStringOrEmpty
import com.lockwood.multispan.extensions.getTextColorOrDefault
import com.lockwood.fontspandemo.extensions.fetchAttrs
import com.lockwood.fontspandemo.extensions.getStringOrEmpty
import com.lockwood.fontspandemo.extensions.getTextColorOrDefault
import com.lockwood.multispan.font.FontThreeSpanView
import com.lockwood.multispan.font.span.FontSpan
import java.util.regex.PatternSyntaxException
Expand Down Expand Up @@ -42,16 +42,13 @@ class HighlightTextView @JvmOverloads constructor(
}

init {
fetchAttrs(R.styleable.HighlightTextView, context, attrs) {
highlightPattern =
getStringOrEmpty(R.styleable.HighlightTextView_highlightPattern)
highlightFont =
getFontOrDefault(R.styleable.HighlightTextView_highlightFont)
highlightSpanColor =
getTextColorOrDefault(
R.styleable.HighlightTextView_highlightColor,
defaultTextColor
)
fetchAttrs(context, R.styleable.HighlightTextView, attrs) {
highlightPattern = getStringOrEmpty(R.styleable.HighlightTextView_highlightPattern)
highlightFont = getFontOrDefault(R.styleable.HighlightTextView_highlightFont)
highlightSpanColor = getTextColorOrDefault(
R.styleable.HighlightTextView_highlightColor,
defaultTextColor
)
isHighlight = getBoolean(
R.styleable.HighlightTextView_isHighlight,
DEFAULT_IS_HIGHLIGHT
Expand All @@ -61,14 +58,17 @@ class HighlightTextView @JvmOverloads constructor(
DEFAULT_IS_USE_UNDERLINE
)
}

updateSpanStyles()
}

override fun setSpanOnResult(resultSpans: SpannableString): CharSequence {
return if (isHighlight && !highlightPattern.isNullOrEmpty()) {

try {
val regexPattern = highlightPattern!!.toRegex()
val highlightMatches = regexPattern.findAll(resultSpans).map { it.value }

highlightMatches.forEach { item ->
val start = resultSpans.indexOf(item)
val end = start + item.length
Expand All @@ -90,6 +90,7 @@ class HighlightTextView @JvmOverloads constructor(
}
}
resultSpans

} catch (e: PatternSyntaxException) {
Log.e(TAG, e.message.toString())
super.setSpanOnResult(resultSpans)
Expand Down
43 changes: 10 additions & 33 deletions font-library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.github.lndmflngs'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
consumerProguardFiles 'consumer-rules.pro'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
apply from: rootProject.file('gradle/android/library.gradle')
apply from: rootProject.file('gradle/kotlin/target.gradle')
apply from: rootProject.file('gradle/publish.gradle')
apply from: rootProject.file('gradle/deps.gradle')

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.core:core-ktx:1.2.0"
api "com.github.lndmflngs:multispan-view:1.0.1"
}
implementation(
deps.kotlin.stdlib,
deps.android.appcompat,
deps.android.ktx
)

repositories {
mavenCentral()
api deps.lndmflngs.multispan
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.lockwood.multispan.font

import android.content.Context
import android.util.AttributeSet
import com.lockwood.multispan.extensions.fetchAttrs
import com.lockwood.multispan.extensions.fetchFourSpanAttrs
import com.lockwood.multispan.font.extensions.fetchAttrs
import com.lockwood.multispan.font.spannable.FontFourSpan
import com.lockwood.multispan.spannable.FourSpan.Companion.FOUR_ITEMS_COUNT
import com.lockwood.multispan.spannable.FourSpan.Companion.ITEM_FOURTH
Expand All @@ -42,7 +42,7 @@ open class FontFourSpanView @JvmOverloads constructor(
init {
fetchFourSpanAttrs(context, attrs, defaultTextSize, defaultTextColor)

fetchAttrs(R.styleable.FontFourSpanView, context, attrs) {
fetchAttrs(context, R.styleable.FontFourSpanView, attrs) {
fourthFont = getFontOrDefault(R.styleable.FontFourSpanView_fourthFont)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package com.lockwood.multispan.font

import android.content.Context
import android.util.AttributeSet
import com.lockwood.multispan.extensions.fetchAttrs
import com.lockwood.multispan.extensions.fetchSingleSpanAttrs
import com.lockwood.multispan.font.base.FontMultiSpanView
import com.lockwood.multispan.font.extensions.fetchAttrs
import com.lockwood.multispan.font.spannable.FontSingleSpan
import com.lockwood.multispan.spannable.SingleSpan.Companion.ITEM_FIRST
import com.lockwood.multispan.spannable.SingleSpan.Companion.ONE_ITEM_COUNT
Expand All @@ -43,7 +43,7 @@ open class FontSingleSpanView @JvmOverloads constructor(
init {
fetchSingleSpanAttrs(context, attrs, defaultTextSize, defaultTextColor)

fetchAttrs(R.styleable.FontSingleSpanView, context, attrs) {
fetchAttrs(context, R.styleable.FontSingleSpanView, attrs) {
firstFont = getFontOrDefault(R.styleable.FontSingleSpanView_firstFont)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.lockwood.multispan.font

import android.content.Context
import android.util.AttributeSet
import com.lockwood.multispan.extensions.fetchAttrs
import com.lockwood.multispan.extensions.fetchThreeSpanAttrs
import com.lockwood.multispan.font.extensions.fetchAttrs
import com.lockwood.multispan.font.spannable.FontThreeSpan
import com.lockwood.multispan.spannable.ThreeSpan.Companion.ITEM_THIRD
import com.lockwood.multispan.spannable.ThreeSpan.Companion.THREE_ITEMS_COUNT
Expand All @@ -42,7 +42,7 @@ open class FontThreeSpanView @JvmOverloads constructor(
init {
fetchThreeSpanAttrs(context, attrs, defaultTextSize, defaultTextColor)

fetchAttrs(R.styleable.FontThreeSpanView, context, attrs) {
fetchAttrs(context, R.styleable.FontThreeSpanView, attrs) {
thirdFont = getFontOrDefault(R.styleable.FontThreeSpanView_thirdFont)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.lockwood.multispan.font

import android.content.Context
import android.util.AttributeSet
import com.lockwood.multispan.extensions.fetchAttrs
import com.lockwood.multispan.extensions.fetchTwoSpanAttrs
import com.lockwood.multispan.font.extensions.fetchAttrs
import com.lockwood.multispan.font.spannable.FontTwoSpan
import com.lockwood.multispan.spannable.TwoSpan.Companion.ITEM_SECOND
import com.lockwood.multispan.spannable.TwoSpan.Companion.TWO_ITEMS_COUNT
Expand All @@ -42,7 +42,7 @@ open class FontTwoSpanView @JvmOverloads constructor(
init {
fetchTwoSpanAttrs(context, attrs, defaultTextSize, defaultTextColor)

fetchAttrs(R.styleable.FontTwoSpanView, context, attrs) {
fetchAttrs(context, R.styleable.FontTwoSpanView, attrs) {
secondFont = getFontOrDefault(R.styleable.FontTwoSpanView_secondFont)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import com.lockwood.multispan.MultiSpanView
import com.lockwood.multispan.extensions.getStringOrEmpty
import com.lockwood.multispan.font.delegate.SpanFontDelegate
import com.lockwood.multispan.font.extensions.getStringOrEmpty
import com.lockwood.multispan.font.item.FontSpanItem
import com.lockwood.multispan.font.spannable.FontSpannable

Expand All @@ -33,6 +33,11 @@ abstract class FontMultiSpanView @JvmOverloads constructor(
defStyleAttr: Int = android.R.attr.textViewStyle
) : MultiSpanView<FontSpanItem>(context, attrs, defStyleAttr), FontSpannable {

companion object {

private val TAG = FontMultiSpanView::class.java.simpleName
}

override fun initSpan(): FontSpanItem {
return FontSpanItem(
text = "",
Expand Down Expand Up @@ -70,10 +75,5 @@ abstract class FontMultiSpanView @JvmOverloads constructor(
}
// endregion

companion object {

private val TAG = FontMultiSpanView::class.java.simpleName
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lockwood.multispan.font.extensions

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes

internal inline fun fetchAttrs(
context: Context,
attrs: IntArray,
set: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0,
fetch: TypedArray .() -> Unit = {}
) {
val typedArray = context.theme.obtainStyledAttributes(
set,
attrs,
defStyleAttr,
defStyleRes
)
with(typedArray) {
try {
fetch()
} finally {
recycle()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lockwood.multispan.font.extensions

import android.content.res.TypedArray

internal fun TypedArray.getStringOrEmpty(
index: Int
): String {
return getString(index) ?: ""
}
3 changes: 3 additions & 0 deletions gradle/android/app.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apply plugin: 'com.android.application'
apply from: rootProject.file('gradle/android/base.gradle')
apply from: rootProject.file('gradle/android/build.gradle')
Loading

0 comments on commit 8a30ed6

Please sign in to comment.