Skip to content

Instantly share code, notes, and snippets.

View n8ebel's full-sized avatar
🏠
Working from home

Nate Ebel n8ebel

🏠
Working from home
View GitHub Profile
@n8ebel
n8ebel / BaseViewModel.kt
Created January 25, 2018 03:42
ToastData PublishSubject on a BaseViewModel
val toastPub:PublishSubject<ToastData> = PublishSubject.create()
@n8ebel
n8ebel / BaseActivity.kt
Last active January 25, 2018 03:39
Activity subscription to viewmodel ToastState
viewModel.toastPub.subscribe { toastData ->
when(toastData) {
is ShortToast -> Toast.makeText(this, toastData.msgId, Toast.LENGTH_SHORT).show()
is LongToast -> Toast.makeText(this, toastData.msgId, Toast.LENGTH_LONG).show()
}
}
/**
* Representation of [Toast] data that can be exposed by a view model, observed, and bound to
* the current UI
*/
sealed class ToastData
data class ShortToast(@StringRes val msgId:Int) : ToastData()
data class LongToast(@StringRes val msgId:Int) : ToastData()
typealias SnackFinishedListener = () -> Unit
typealias SnackActionListener = () -> Unit
/**
* Representation of [Snackbar] data that can be exposed by a view model, observed, and bound to
* the current UI
*/
sealed class SnackData
/**
@n8ebel
n8ebel / ActivityBindingProvider.kt
Created November 22, 2017 07:10
A custom Kotlin delegate for initializing ViewDataBinding objects for Android databinding
/**
* Provides a [ViewDataBinding] object of the declared type by
* calling [DataBindingUtil.setContentView] with the property owning [Activity] and the specified
* layout resource id
*/
class ActivityBindingProvider<out T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int) : ReadOnlyProperty<Activity, T> {
private var binding : T? = null
@n8ebel
n8ebel / BindingAdapters.kt
Created September 19, 2017 02:35
Animations with BindingAdapters
@BindingAdapter("isHeaderVisible")
fun setHeaderVisibility(view: View, isVisible: Boolean) {
if (view.visibility == View.VISIBLE) {
if (isVisible) {
return
}
view.animate()
.alpha(0f)
.translationY(-1f * view.height)
@n8ebel
n8ebel / BindingAdapters.kt
Created September 19, 2017 02:00
Image loading binding adapter
@BindingAdapter("imageUrl")
fun setProgress(view: ImageView, url: String) {
Glide.with(view.context)
.load(url)
.error(R.drawable.ic_broken_image_black)
.listener(object : RequestListener<String, GlideDrawable> {
override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
// use this to ensure placeholder icon doesn't stretch to fill too much space
view.scaleType = ImageView.ScaleType.CENTER_INSIDE
return false
@n8ebel
n8ebel / BindingAdapters.kt
Last active July 8, 2021 18:40
BindingAdapters for separation of viewmodels and Android framework for resource loading
// I would typically place this adapter in main/_BindingAdapters.kt
// The gist tool won't allow that filename structure
//
@BindingAdapter("mainSubtitleText")
fun setText(view: TextView, value:Int) {
view.text = view.context.getString(R.string.main_subtitle_format, value)
}
@BindingAdapter("textColor")
fun setTextColor(view: TextView, @ColorRes resId: Int) = view.setTextColor(ContextCompat.getColor(view.context, resId))
@n8ebel
n8ebel / MainActivity.kt
Last active February 27, 2023 22:31
Basic example of using databinding to handle button clicks
class MainActivity : AppCompatActivity(), ViewModelListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main).bindData {
viewModel = MainViewModel("hello", 5, this@MainActivity)
}
}
@n8ebel
n8ebel / Extensions.kt
Last active September 14, 2017 01:26
ViewDataBinding extension to provide a more readable api for setting data into a binding object
inline fun <T : ViewDataBinding> T.bindData(block: T.() -> Unit): T { block(); return this }