diff --git a/.idea/misc.xml b/.idea/misc.xml index cc51a7a..320b3df 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/ext/StringExt.kt b/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/ext/StringExt.kt new file mode 100644 index 0000000..600a956 --- /dev/null +++ b/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/ext/StringExt.kt @@ -0,0 +1,30 @@ +/** + * Designed and developed by Aidan Follestad (@afollestad) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.afollestad.nocknock.viewcomponents.ext + +fun String?.asSafeInt(defaultValue: Int = 0): Int { + if (this == null || this.trim().isEmpty()) { + return defaultValue + } + return this.toInt() +} + +fun String?.asSafeLong(defaultValue: Long = 0): Long { + if (this == null || this.trim().isEmpty()) { + return defaultValue + } + return this.toLong() +} diff --git a/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/livedata/LiveDataViewExt.kt b/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/livedata/LiveDataViewExt.kt index 9ab5e1f..ff717e9 100644 --- a/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/livedata/LiveDataViewExt.kt +++ b/viewcomponents/src/main/java/com/afollestad/nocknock/viewcomponents/livedata/LiveDataViewExt.kt @@ -26,6 +26,8 @@ import androidx.lifecycle.Observer import com.afollestad.nocknock.utilities.ext.onTextChanged import com.afollestad.nocknock.utilities.ext.setTextAndMaintainSelection import com.afollestad.nocknock.utilities.livedata.distinct +import com.afollestad.nocknock.viewcomponents.ext.asSafeInt +import com.afollestad.nocknock.viewcomponents.ext.asSafeLong import com.afollestad.nocknock.viewcomponents.ext.onItemSelected import com.afollestad.nocknock.viewcomponents.ext.showOrHide @@ -54,10 +56,10 @@ inline fun EditText.attachLiveData( if (pushOutChanges) { when { T::class == Int::class -> { - onTextChanged(debounce) { data.postValue(it.trim().toInt() as T) } + onTextChanged(debounce) { data.postValue(it.asSafeInt() as T) } } T::class == Long::class -> { - onTextChanged(debounce) { data.postValue(it.trim().toLong() as T) } + onTextChanged(debounce) { data.postValue(it.asSafeLong() as T) } } T::class == String::class -> { onTextChanged(debounce) { data.postValue(it as T) }