Fix a crash

This commit is contained in:
Aidan Follestad 2019-01-07 12:52:34 -08:00
parent 6daebe46eb
commit 19f58ef2e6
3 changed files with 35 additions and 3 deletions

2
.idea/misc.xml generated
View file

@ -5,7 +5,7 @@
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -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()
}

View file

@ -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 <reified T> 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) }