Created
May 23, 2016 12:08
-
-
Save SupaHam/501402b350282238d1983ae2f7413350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:Suppress("NOTHING_TO_INLINE") | |
package org.jetbrains.anko | |
import android.app.Fragment | |
import android.view.View | |
/* | |
* This class must be named Dialogs2 to not override Anko. | |
*/ | |
/* Snackbars */ | |
inline fun AnkoContext<*>.snackbar(resId: Int, | |
noinline init: (SnackbarBuilder.() -> Unit)? = null) = view.snackbar(resId, init) | |
inline fun Fragment.snackbar(resId: Int, | |
noinline init: (SnackbarBuilder.() -> Unit)? = null) = view.snackbar(resId, init) | |
fun View.snackbar(resId: Int, init: (SnackbarBuilder.() -> Unit)?) = | |
SnackbarBuilder(this, resId).apply { init?.invoke(this) } | |
inline fun AnkoContext<*>.snackbar(text: CharSequence, | |
noinline init: (SnackbarBuilder.() -> Unit)? = null) = view.snackbar(text, init) | |
inline fun Fragment.snackbar(text: CharSequence, | |
noinline init: (SnackbarBuilder.() -> Unit)? = null) = view.snackbar(text, init) | |
fun View.snackbar(text: CharSequence, init: (SnackbarBuilder.() -> Unit)?) = | |
SnackbarBuilder(this, text).apply { init?.invoke(this) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supaham.shyer.activities | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import org.jetbrains.anko.* | |
class SignInActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
SignInActivityUI().setContentView(this) | |
} | |
} | |
class SignInActivityUI : AnkoComponent<SignInActivity> { | |
override fun createView(ui: AnkoContext<SignInActivity>) = with(ui) { | |
coordinatorLayout { | |
verticalLayout { | |
lparams(width = matchParent, height = matchParent) | |
button { | |
onClick { | |
snackbar("t".repeat(100)) { | |
action("Close") { | |
toast("CLOSED!") | |
} | |
actionTextColor(0xFFCC0000.toInt()) | |
}.show() | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jetbrains.anko | |
import android.content.res.ColorStateList | |
import android.support.design.widget.Snackbar | |
import android.view.View | |
class SnackbarBuilder { | |
val view: View | |
val builder: Snackbar | |
constructor(view: View, text: CharSequence) { | |
this.view = view | |
this.builder = Snackbar.make(view, text, Snackbar.LENGTH_SHORT) | |
} | |
constructor(view: View, resId: Int) { | |
this.view = view | |
this.builder = Snackbar.make(view, resId, Snackbar.LENGTH_SHORT) | |
} | |
fun action(resId: Int, f: (v: android.view.View?) -> Unit) { | |
builder.setAction(resId, f) | |
} | |
fun action(text: CharSequence, f: (v: android.view.View?) -> Unit) { | |
builder.setAction(text, f) | |
} | |
fun actionTextColor(colors: ColorStateList) { | |
builder.setActionTextColor(colors) | |
} | |
fun actionTextColor(color: Int) { | |
builder.setActionTextColor(color) | |
} | |
fun text(title: CharSequence) { | |
builder.setText(title) | |
} | |
fun text(resId: Int) { | |
builder.setText(resId) | |
} | |
fun duration(duration: Int) { | |
builder.duration = duration | |
} | |
fun show(){ | |
builder.show() | |
} | |
fun dismiss(){ | |
builder.dismiss() | |
} | |
// TODO kotlinify this usage | |
fun callback(f: Snackbar.Callback) { | |
builder.setCallback(f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment