https://kotlinlang.org logo
#android
Title
# android
v

vapoyan

07/24/2019, 6:43 AM
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

tseisel

07/24/2019, 6:59 AM
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

YongSik Jung

07/24/2019, 7:18 AM
ContextExt
i

ignus

07/24/2019, 7:23 AM
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

tseisel

07/24/2019, 7:32 AM
@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

ignus

07/24/2019, 7:34 AM
@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

tseisel

07/24/2019, 7:37 AM
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

ignus

07/24/2019, 7:48 AM
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

voben

07/24/2019, 7:44 PM
Copy code
fun Fragment.hideKeyboard() {
    requireActivity().getSystemService(InputMethodManager::class.java).hideSoftInputFromWindow(view!!.windowToken, 0)
}
v

vapoyan

07/25/2019, 6:52 AM
Cool !