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 / 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 / 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 / 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)
@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