vapoyan
07/24/2019, 6:43 AMvapoyan
07/24/2019, 6:45 AMval EditText.asString
get() = this.getText().toString()
tseisel
07/24/2019, 6:59 AMandroidx.core:core-ktx
contains most of the extensions that you'd use for any Android project, such as String.toUri
(and many more)YongSik Jung
07/24/2019, 7:18 AMignus
07/24/2019, 7:23 AMfun View.visibleOrInvisible(visible: Boolean) {
if (visible) {
visible()
} else {
invisible()
}
}
fun View.visibleOrGone(visible: Boolean) {
if (visible) {
visible()
} else {
gone()
}
}
fun View.visible() {
visibility = View.VISIBLE
}
fun View.invisible() {
visibility = View.INVISIBLE
}
fun View.gone() {
visibility = View.GONE
}
fun TextView.setTextOrGone(newText: String?) {
newText?.let { text = newText; visible() } ?: gone()
}
ignus
07/24/2019, 7:30 AMprivate var args by fragmentArgs<AmountInputFragmentArgs>()
companion object {
fun newInstance(args: AmountInputFragmentArgs) = AmountInputFragment().apply {
this.args = args
}
}
/**
* Eases the Fragment.newInstance ceremony by marking the fragment's args with this delegate
* Just write the property in newInstance and read it like any other property after the fragment has been created
*
* Inspired by Adam Powell, he mentioned it during his IO/17 talk about Kotlin
*
* @see <https://gist.github.com/yanngx/efdfbf777d21d6f0e73fab4efe47e924>
*/
class FragmentArgumentDelegate<T : Any> : kotlin.properties.ReadWriteProperty<Fragment, T> {
var value: T? = null
override operator fun getValue(thisRef: Fragment, property: kotlin.reflect.KProperty<*>): T {
if (value == null) {
val args = thisRef.arguments ?: throw IllegalStateException(
"Cannot read property ${property.name} if no arguments have been set"
)
@Suppress("UNCHECKED_CAST")
value = args.get(property.name) as T
}
return value ?: throw IllegalStateException("Property ${property.name} could not be read")
}
override operator fun setValue(thisRef: Fragment, property: kotlin.reflect.KProperty<*>, value: T) {
val key = property.name
val args = thisRef.arguments ?: Bundle()
args.putAll(bundleOf(key to value))
thisRef.arguments = args
}
}
@Suppress("unused")
fun <T : Any> LifecycleOwner.fragmentArgs() = FragmentArgumentDelegate<T>()
tseisel
07/24/2019, 7:32 AMval View.isVisible: Boolean
get() = visibility == View.VISIBLE
set(value) {
visibility = if (value) View.VISIBLE else View.GONE
}
ignus
07/24/2019, 7:34 AMtseisel
07/24/2019, 7:37 AMcore-ktx
:
fun ViewGroup.inflate(@LayoutRes layoutResId: Int, attach: Boolean = false): View = LayoutInflater.from(context).inflate(layoutResId, this, attach)
ignus
07/24/2019, 7:48 AMprivate const val CLICK_THROTTLE_FIRST_TIME_MS = 1000L
@SuppressLint("CheckResult")
fun View.onClickWithThrottle(action: (Any) -> Unit) {
RxView.clicks(this)
.throttleFirst(CLICK_THROTTLE_FIRST_TIME_MS, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(action)
}
voben
07/24/2019, 7:44 PMfun Fragment.hideKeyboard() {
requireActivity().getSystemService(InputMethodManager::class.java).hideSoftInputFromWindow(view!!.windowToken, 0)
}
vapoyan
07/25/2019, 6:52 AM