Hello everyone, Can you please share what kind of ...
# android
v
Hello everyone, Can you please share what kind of Extensions (Android specific) are you using in your projects to make your day to day life easier and writing code shorter ๐Ÿ™‚
Copy code
val EditText.asString 
    get() = this.getText().toString()
t
I'd say that
androidx.core:core-ktx
contains most of the extensions that you'd use for any Android project, such as
String.toUri
(and many more)
๐Ÿ‘ 4
y
ContextExt
i
Copy code
fun 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()
}
๐Ÿ‘ 2
Not extension, but wonderful delegate inside all fragments, AmountInputFragmentArgs is just Parcelable data class:
Copy code
private var args by fragmentArgs<AmountInputFragmentArgs>()

  companion object {
    fun newInstance(args: AmountInputFragmentArgs) = AmountInputFragment().apply {
      this.args = args
    }
  }
Copy code
/**
 * 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>()
t
@ignus What do you think of the simpler
Copy code
val View.isVisible: Boolean
    get() = visibility == View.VISIBLE
    set(value) {
        visibility = if (value) View.VISIBLE else View.GONE
    }
i
@tseisel there are 3 states VISIBLE, INVISIBLE, GONE and I prefer functions to change visibility over vals with custom setters but sure only if it suits your project you should use simpler version ๐Ÿ˜‰
t
Also, IMHO this is missing from
core-ktx
:
Copy code
fun ViewGroup.inflate(@LayoutRes layoutResId: Int, attach: Boolean = false): View = LayoutInflater.from(context).inflate(layoutResId, this, attach)
๐Ÿ‘ 2
i
When I wanna keep tester away from spamming clicks actions ๐Ÿ˜„
Copy code
private 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)
}
๐Ÿ‘ 1
v
Copy code
fun Fragment.hideKeyboard() {
    requireActivity().getSystemService(InputMethodManager::class.java).hideSoftInputFromWindow(view!!.windowToken, 0)
}
v
Cool !