Skip to content

Instantly share code, notes, and snippets.

View CDRussell's full-sized avatar

Craig Russell CDRussell

View GitHub Profile
@CDRussell
CDRussell / SneakyRange.kt
Last active August 8, 2017 06:36
Kotlin - Range Example with Silent Cast
val i = 10.5
if (i in 1..10) {
println("$i is within the range of 1-10")
} else {
println("$i is not in range of 1-10")
}
// outputs "10.5 is within the range of 1-10"
val i = 10.5
if (i in 1..10) {
println("$i is within the range of 1-10")
} else {
println("$i is not in range of 1-10")
}
// outputs "10.5 is not in range of 1-10"
@kotlin.jvm.JvmName("intRangeContains")
public operator fun ClosedRange<Int>.contains(value: Float): Boolean {
return start <= value && value <= endInclusive
}
@kotlin.jvm.JvmName("intRangeContains")
public operator fun ClosedRange<Int>.contains(value: Float): Boolean {
return value.toIntExactOrNull().let { if (it != null) contains(it) else false }
}
internal fun Float.toIntExactOrNull(): Int? {
return if (this in Int.MIN_VALUE.toFloat()..Int.MAX_VALUE.toFloat()) this.toInt() else null
}
Log.v(TAG, "verbose");
Log.d(TAG, "debug");
Log.i(TAG, "info");
Log.w(TAG, "warning");
Log.e(TAG, "error");
Timber.v("verbose");
Timber.d("debug");
Timber.i("info");
Timber.w("warning");
public class ProductionLogger extends Timber.Tree {
@Override
protected void log(int priority, String tag, String message, Throwable t) {
if(priority >= Log.WARN) {
// do your thing
}
}
}
# This will strip `Log.v`, `Log.d`, and `Log.i` statements and will leave `Log.w` and `Log.e` statements intact.
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int d(...);
public static int i(...);
}
-assumenosideeffects class timber.log.Timber* {
public static *** v(...);
public static *** d(...);
public static *** i(...);
}
data class ViewState(
val isLoading: Boolean = false,
val progress: Int = 0,
val url: String? = null,
val isEditing: Boolean = false,
val browserShowing: Boolean = false,
val showClearButton: Boolean = false
)
val viewState: MutableLiveData<ViewState> = MutableLiveData()
init {
viewState.value = ViewState()
}
private fun currentViewState(): ViewState = viewState.value!!