Another couple of kotlin utils

Replacement of @BindView from ButterKnife

I will admit. I’m not a fan of code generation. That’s why we made a replacement of one code generation (from ButterKnife library) with another one (from kotlin itself) 😅

private val someTextView: TextView by lazy { findViewById(R.id.some_text_view) }
fun <T : View> View.bindable(@IdRes id: Int): Lazy<T> = 
lazyUnsafe { findViewById<T>(id) }
fun <T> lazyUnsafe(initializer: () -> T): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE, initializer)

Useful utils for context and view

Note that here we used some unknown form of inflate method. This is also the extension method. With we will also use another extension for context:

More handy dimension extensions for view

In the previous post I wrote how you can provide extensions that will allow you to write something like:

val p = view.padding.start
view.padding
view.margin
  • The second pair will get the width/height of only the drawable part of the view. The mentioned view.height returns the height of the view with padding. But sometimes it is useful to know the height of the content area — an area without paddings.

Coroutine properties — properties that can be suspended

Well, they are not quite properties we want. Moreover, it is worth mentioning that properties are designed initially in a way to not allow suspend keywords for a specific reason (here is a discussion about that).

prop set 6

Afterwords

Unfortunately lately I write rarely. This is a tough topic but there are some reasons for that, including emotional burnout, lack of interesting thoughts to share, and the current situation with my job right now.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store