diff --git a/build.gradle b/build.gradle index e8617be..5cf90d4 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,12 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.4.21" + ext.kotlin_version = "1.5.10" repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // Add this line classpath "org.jacoco:org.jacoco.core:0.8.5" diff --git a/currency-formatter/build.gradle b/currency-formatter/build.gradle index 0bcd631..14af308 100644 --- a/currency-formatter/build.gradle +++ b/currency-formatter/build.gradle @@ -4,12 +4,12 @@ apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'jacoco' android { - compileSdkVersion 30 + compileSdkVersion 31 buildToolsVersion "30.0.2" defaultConfig { minSdkVersion 16 - targetSdkVersion 30 + targetSdkVersion 31 versionCode 1 versionName "1.0.0" @@ -30,6 +30,12 @@ android { kotlinOptions { jvmTarget = "1.8" } + + compileOptions { + sourceCompatibility 1.8 + targetCompatibility 1.8 + } + } jacoco { @@ -42,16 +48,16 @@ tasks.withType(Test) { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.appcompat:appcompat:1.3.1' - implementation "androidx.core:core-ktx:1.3.2" + implementation "androidx.core:core-ktx:1.6.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test.ext:junit:1.1.2' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' - androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2' - androidTestImplementation 'androidx.test:core-ktx:1.3.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3' + androidTestImplementation 'androidx.test:core-ktx:1.4.0' } repositories { diff --git a/currency-formatter/src/androidTest/java/com/jacoballenwood/formatter/MultiCurrencyFormatterTest.kt b/currency-formatter/src/androidTest/java/com/jacoballenwood/formatter/MultiCurrencyFormatterTest.kt index 39d704e..9e1984c 100644 --- a/currency-formatter/src/androidTest/java/com/jacoballenwood/formatter/MultiCurrencyFormatterTest.kt +++ b/currency-formatter/src/androidTest/java/com/jacoballenwood/formatter/MultiCurrencyFormatterTest.kt @@ -28,11 +28,10 @@ class MultiCurrencyFormatterTest { return Pair(scenario, act!!) } - private fun setUpFormatter(activity: FragmentActivity): MultiCurrencyFormatter = - MultiCurrencyFormatter.newInstance( - activity, - EditText(activity) - ) + private fun setUpFormatter(activity: FragmentActivity): MultiCurrencyFormatter = MultiCurrencyFormatter.newInstance( + activity, + EditText(activity) + ) @Test fun setting_and_getting_amount() { diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/MultiCurrencyFormatter.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/MultiCurrencyFormatter.kt index 80b6843..1f98c1f 100644 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/MultiCurrencyFormatter.kt +++ b/currency-formatter/src/main/java/com/jacoballenwood/formatter/MultiCurrencyFormatter.kt @@ -1,5 +1,7 @@ package com.jacoballenwood.formatter +import android.os.Handler +import android.os.Looper import android.widget.EditText import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleObserver @@ -11,6 +13,7 @@ import com.jacoballenwood.formatter.main.CurrencyTextWatcher import com.jacoballenwood.formatter.main.TextChangeListener import com.jacoballenwood.formatter.main.impl.CurrencyFormatterImpl import java.math.BigDecimal +import java.text.DecimalFormatSymbols import java.util.* class MultiCurrencyFormatter private constructor( @@ -28,15 +31,15 @@ class MultiCurrencyFormatter private constructor( @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun destroy() { currencyTextWatcher.destroy() - lifecycleOwner.lifecycle.removeObserver( - this - ) + lifecycleOwner.lifecycle.removeObserver(this) lifecycleObserver = null } } init { - lifecycleOwner.lifecycle.addObserver(lifecycleObserver!!) + Handler(Looper.getMainLooper()).post { + lifecycleOwner.lifecycle.addObserver(lifecycleObserver!!) + } } /** @@ -150,4 +153,7 @@ class MultiCurrencyFormatter private constructor( ) } -} \ No newline at end of file +} + +val MultiCurrencyFormatter.decimalFormatSymbols: DecimalFormatSymbols + get() = this.currencyTextWatcher.formatter.underlyingDecimalFormat.decimalFormatSymbols diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/ext/StringExt.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/ext/StringExt.kt index e1028a7..11b6ef6 100644 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/ext/StringExt.kt +++ b/currency-formatter/src/main/java/com/jacoballenwood/formatter/ext/StringExt.kt @@ -18,4 +18,14 @@ fun String.withSuperscript(startIdx: Int, endIdx: Int): Spannable = SpannableStr endIdx, 0 ) +} + +fun String.indexOfLastDigit(): Int { + var result = 0 + for (i in this.indices) { + if (Character.isDigit(this[i])) { + result = i + } + } + return result } \ No newline at end of file diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/CurrencyFormatter.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/CurrencyFormatter.kt index 207ae39..02d4073 100644 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/CurrencyFormatter.kt +++ b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/CurrencyFormatter.kt @@ -5,8 +5,6 @@ import java.math.BigDecimal import java.text.DecimalFormat import java.util.* -private var instance: CurrencyFormatterImpl? = null - interface CurrencyFormatter : CurrencyAttrs { val underlyingDecimalFormat: DecimalFormat fun parse(currency: String): BigDecimal @@ -14,6 +12,7 @@ interface CurrencyFormatter : CurrencyAttrs { fun format(currency: BigDecimal, decimals: Boolean): String companion object { + private var instance: CurrencyFormatterImpl? = null /** * Returns a [CurrencyFormatterImpl] for the specified locale. This is equivalent to diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyFormatterImpl.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyFormatterImpl.kt index 529f09c..038a8ca 100644 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyFormatterImpl.kt +++ b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyFormatterImpl.kt @@ -78,9 +78,7 @@ class CurrencyFormatterImpl internal constructor( /** * */ - override fun format(currency: String, decimals: Boolean): String { - return format(parse(currency), decimals) - } + override fun format(currency: String, decimals: Boolean): String = format(parse(currency), decimals) /** * diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyTextWatcherImpl.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyTextWatcherImpl.kt index 33d83a1..cc6e2e6 100644 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyTextWatcherImpl.kt +++ b/currency-formatter/src/main/java/com/jacoballenwood/formatter/main/impl/CurrencyTextWatcherImpl.kt @@ -5,9 +5,9 @@ import android.text.Spannable import android.text.TextWatcher import android.widget.EditText import com.jacoballenwood.formatter.ext.fitText +import com.jacoballenwood.formatter.ext.indexOfLastDigit import com.jacoballenwood.formatter.ext.withSuperscript import com.jacoballenwood.formatter.main.* -import com.jacoballenwood.formatter.util.StringUtil.indexOfLastDigit import java.lang.ref.WeakReference import java.math.BigDecimal @@ -80,7 +80,7 @@ class CurrencyTextWatcherImpl internal constructor( val sel = if (currentIndexOfCents > -1) { (indexOfDecimalPoint + currentIndexOfCents) + if (isDeleting) 0 else 1 } else - indexOfLastDigit(requireEditText.text?.toString()) + 1 + (requireEditText.text?.toString()?.indexOfLastDigit() ?: 0) + 1 if (sel <= requireEditText.length()) requireEditText.setSelection(sel) else diff --git a/currency-formatter/src/main/java/com/jacoballenwood/formatter/util/StringUtil.kt b/currency-formatter/src/main/java/com/jacoballenwood/formatter/util/StringUtil.kt deleted file mode 100644 index a3bb383..0000000 --- a/currency-formatter/src/main/java/com/jacoballenwood/formatter/util/StringUtil.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.jacoballenwood.formatter.util - -object StringUtil { - fun indexOfLastDigit(str: String?): Int { - var result = 0 - str ?: return result - for (i in str.indices) { - if (Character.isDigit(str[i])) { - result = i - } - } - return result - } -} \ No newline at end of file