fun Long?.orZero(): Long = this ?: 0L
fun Long.toNegative(): Long = -abs(this)
fun Long.toPositive(): Long = abs(this)
fun Long.reverse(): Long = this.unaryMinus()
fun Long.takeIfZero(): Long? = this.takeIf { it == 0L }
fun Long.takeIfNotZero(): Long? = this.takeIf { it != 0L }
fun Long?.toStringOrEmpty() = this?.toString().orEmpty()
fun Long.takeIfZeroOrEmpty() = this.takeIf { it == 0L }?.toStringOrEmpty()
fun Double.toNegative() = -abs(this)
fun Double.toPositive() = abs(this)
fun Double.reverse() = this.unaryMinus()
fun Double.takeIfZero() = this.takeIf { it == 0.0 }
fun Double.takeIfNotZero() = this.takeIf { it != 0.0 }
fun Int?.toStringOrEmpty() = this?.toString().orEmpty()
fun Int.takeIfZeroOrEmpty() = this.takeIf { it == 0 }?.toStringOrEmpty()
fun <T, R> List<T>.mapAndFind(map: (T) -> R, find: (R) -> Boolean) = this.asSequence()
.map(map)
.find(find)
fun Boolean.doIfTrue(doFun: () -> Unit): Boolean {
if (this) {
doFun.invoke()
}
return this
}
fun Boolean.doIfFalse(doFun: () -> Unit): Boolean {
if (!this) {
doFun.invoke()
}
return this
}
fun ScrollView.smoothScrollToTop() {
smoothScrollTo(0, 0)
}
fun ScrollView.scrollToTop() {
scrollTo(0, 0)
}
fun ScrollView.smoothScrollToBottom() {
smoothScrollTo(this.right, this.bottom)
}
fun ScrollView.scrollToBottom() {
scrollTo(this.right, this.bottom)
}
fun BottomNavigationView.setChecked(numberItemChecked: Int) {
this.menu.getItem(numberItemChecked).isChecked = true
}
fun <T> Fragment.wakeUpLiveData(vararg liveData: LiveData<T>) {
liveData.forEach {
it.activate(viewLifecycleOwner)
}
}
inline fun <reified T> Fragment.argument(argumentKey: String): Lazy<T?> = unsafeLazy {
arguments?.get(argumentKey) as? T
}
inline fun <reified T> DialogFragment.argument(argumentKey: String): Lazy<T?> = unsafeLazy {
arguments?.get(argumentKey) as? T
}
inline fun <reified T> Fragment.argumentOrDefault(argumentKey: String, defaultValue: T): Lazy<T> =
unsafeLazy {
arguments?.get(argumentKey) as? T ?: defaultValue
}
inline fun <reified T> DialogFragment.argumentOrDefault(
argumentKey: String,
defaultValue: T
): Lazy<T> = unsafeLazy {
arguments?.get(argumentKey) as? T ?: defaultValue
}
fun Fragment.showShortToast(text: String) {
Toast.makeText(requireContext(), text, Toast.LENGTH_SHORT).show()
}
fun Fragment.showShortToast(text: () -> String) {
Toast.makeText(requireContext(), text.invoke(), Toast.LENGTH_SHORT).show()
}
fun Fragment.showLongToast(text: String) {
Toast.makeText(requireContext(), text, Toast.LENGTH_LONG).show()
}
fun Fragment.showLongToast(text: () -> String) {
Toast.makeText(requireContext(), text.invoke(), Toast.LENGTH_LONG).show()
}
Source code(tar.gz)
Source code(zip)