This file contains 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
val toastPub:PublishSubject<ToastData> = PublishSubject.create() |
This file contains 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
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() | |
} | |
} |
This file contains 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
/** | |
* 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() |
This file contains 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
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 | |
/** |
This file contains 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
/** | |
* 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 |
This file contains 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
@BindingAdapter("isHeaderVisible") | |
fun setHeaderVisibility(view: View, isVisible: Boolean) { | |
if (view.visibility == View.VISIBLE) { | |
if (isVisible) { | |
return | |
} | |
view.animate() | |
.alpha(0f) | |
.translationY(-1f * view.height) |
This file contains 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
@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 |
This file contains 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
// 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)) |
This file contains 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
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) | |
} | |
} |
This file contains 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
inline fun <T : ViewDataBinding> T.bindData(block: T.() -> Unit): T { block(); return this } |