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 / gist:16212d1351f8623a1913
Created August 13, 2015 16:57
RxJava vs Callback
/*
* Standard way
*/
// MarvelAndroid.getInstance().getCharacters(queryParams,
// new ResponseCallback<com.n8.marveldroid.EntityModelObjects.Character>() {
// @Override
// public void onRequestComplete(List<Character> responseEntities, Throwable error) {
// if (error != null) {
// mResultTextView.setText(error.getLocalizedMessage());
// return;
@n8ebel
n8ebel / gist:13dbad32a3eec4315ab5
Last active August 29, 2015 14:27
RxJava vs Standard 2

This code demonstrates 2 approaches to completing the same task, and performs the following actions:

  1. The task is to hit the Marvel rest endpoint, and return a list of characters.
  2. Take the 4th character in that list, fetch its comics, and then load the image for that comic.
  3. Once those requests are done, the image and list of character names are bound to the ui.
    /*
     * Standard way
     */

Simple illustration of responding to text changed events on an EditText.

/*
 *  RxJava
 */
RxTextView.textChangeEvents(mEditText)
    .subscribe(event -> mResultTextView.setText(event.text()));

/*
@n8ebel
n8ebel / MainActivity.kt
Last active September 14, 2017 01:03
Simple use of databinding to display a String and Int
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main).apply {
message = "hello"
number = 5
}
@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 }
@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
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)
/**
* 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()
@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()
}
}
@n8ebel
n8ebel / BaseViewModel.kt
Created January 25, 2018 03:42
ToastData PublishSubject on a BaseViewModel
val toastPub:PublishSubject<ToastData> = PublishSubject.create()