Skip to content

Instantly share code, notes, and snippets.

@nschwermann
Forked from mgranberry/DesignSupport.kt
Last active November 22, 2020 06:38
Show Gist options
  • Save nschwermann/0152caee426e247f90ce to your computer and use it in GitHub Desktop.
Save nschwermann/0152caee426e247f90ce to your computer and use it in GitHub Desktop.
collection of kotlin extensions for android development
package org.jetbrains.anko
import android.app.Activity
import android.support.annotation.StringRes
import android.support.design.widget.*
import android.support.v4.app.Fragment
import android.view.View
import android.view.ViewManager
import java.util.concurrent.atomic.AtomicInteger
fun ViewManager.appBarLayout(init: AppBarLayout.() -> Unit = {}) =
__dslAddView({ AppBarLayout(it) }, init, this)
fun ViewManager.collapsingToolbarLayout(init: CollapsingToolbarLayout.() -> Unit = {}) =
__dslAddView({ CollapsingToolbarLayout(it) }, init, this)
fun ViewManager.coordinatorLayout(init: CoordinatorLayout.() -> Unit = {}) =
__dslAddView({ CoordinatorLayout(it) }, init, this)
fun ViewManager.floatingActionButton(init: FloatingActionButton.() -> Unit = {}) =
__dslAddView({ FloatingActionButton(it) }, init, this)
fun ViewManager.navigationView(init: NavigationView.() -> Unit = {}) =
__dslAddView({ NavigationView(it) }, init, this)
fun ViewManager.tabLayout(init: TabLayout.() -> Unit = {}) =
__dslAddView({ TabLayout(it) }, init, this)
fun ViewManager.textInputLayout(init: TextInputLayout.() -> Unit = {}) =
__dslAddView({ TextInputLayout(it) }, init, this)
fun View.snackbar(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT, init: Snackbar.() -> Unit = {}): Snackbar {
val snack = Snackbar.make(this, text, duration)
snack.init()
snack.show()
return snack
}
fun View.snackbar(StringRes()text: Int, duration: Int = Snackbar.LENGTH_SHORT, init: Snackbar.() -> Unit = {}): Snackbar {
val snack = Snackbar.make(this, text, duration)
snack.init()
snack.show()
return snack
}
fun Fragment.snackbar(text : CharSequence, duration: Int = Snackbar.LENGTH_LONG, init : Snackbar.() -> Unit = {}) : Snackbar {
return getView()!!.snackbar(text, duration, init)
}
fun Fragment.snackbar(StringRes() text : Int, duration: Int = Snackbar.LENGTH_LONG, init : Snackbar.() -> Unit = {}) : Snackbar {
return getView()!!.snackbar(text, duration, init)
}
fun Activity.snackbar(view: View, text : CharSequence, duration: Int = Snackbar.LENGTH_LONG, init : Snackbar.() -> Unit = {}) : Snackbar {
return view.snackbar(text, duration, init)
}
fun Activity.snackbar(view: View, StringRes() text : Int, duration: Int = Snackbar.LENGTH_LONG, init : Snackbar.() -> Unit = {}) : Snackbar {
return view.snackbar(text, duration, init)
}
private object ViewCounter {
private val viewCounter = AtomicInteger(1)
public fun generateViewId(): Int {
while (true) {
val result = viewCounter.get()
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
var newValue = result + 1
if (newValue > 16777215) newValue = 1 // Roll over to 1, not 0.
if (viewCounter.compareAndSet(result, newValue)) {
return result
}
}
}
}
fun View.generateViewIdCompat(): Int {
if (android.os.Build.VERSION.SDK_INT >= 19)
return View.generateViewId()
else
return ViewCounter.generateViewId()
}
package android.support.v4.util
import java.util.*
import kotlin.Pair
/**
* Returns a new [ArrayMap] with the specified contents, given as a list of pairs
* where the first component is the key and the second is the value.
*
* @sample test.collections.MapTest.createUsingPairs
*/
public fun <K, V> arrayMapOf(vararg values: Pair<K, V>): ArrayMap<K, V> {
val answer = ArrayMap<K, V>(values.size())
answer.putAll(*values)
return answer
}
/**
* Returns a new [SimpleArrayMap] with the specified contents, given as a list of pairs
* where the first component is the key and the second is the value.
*
* @sample test.collections.MapTest.createUsingPairs
*/
public fun <K, V> simpleArrayMapOf(vararg values: Pair<K, V>): SimpleArrayMap<K, V> {
val answer = SimpleArrayMap<K, V>(values.size())
values.forEach { answer.put(it.first, it.second) }
return answer
}
@cheskos
Copy link

cheskos commented May 15, 2018

Could you please explain what's the purpose of this init: Snackbar.() -> Unit = {} parameter? How can I call these methods from Java if I only have the view, text, and duration?

@nschwermann
Copy link
Author

Could you please explain what's the purpose of this init: Snackbar.() -> Unit = {} parameter? How can I call these methods from Java if I only have the view, text, and duration?

Hey sorry two years late to reply! Maybe my response will be helpful to someone someday. This is a function literal with receiver. You can think of it as sort of an anonymous extension function that is passed by the caller. The receiver will create the snackbar and call the passed in function which is in the scope of the snackbar. To call kotlin extension functions form java it is a normal static call with additional argument being the receiver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment