Skip to content

Instantly share code, notes, and snippets.

View elihart's full-sized avatar

Eli Hart elihart

  • Airbnb
  • San Francisco
View GitHub Profile
@elihart
elihart / MvRxViewModelUnitTestSimpleExample.kt
Created November 20, 2019 20:22
MvRxViewModelUnitTestSimpleExample
@Test
fun setText() = TextViewModel::setText {
withParams("hello") expectState {
copy(text = "hello")
}
}
@elihart
elihart / CyclicAdapter.java
Last active February 11, 2022 14:21
Cyclic Adapter: An Android RecyclerView Adapter that supports endless cyclical scrolling
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.AdapterDataObserver;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.ViewGroup;
import java.util.List;
/**
* This Adapter class wraps another Adapter in order to support cyclical scrolling.
@elihart
elihart / EpoxyGlidePreloader.kt
Last active October 18, 2021 14:47
Utility to set up a RecyclerView scroll listener that enables preloading support with Glide in Epoxy library usages.
package com.airbnb.epoxy
import android.content.Context
import android.graphics.Bitmap
import android.support.annotation.IdRes
import android.support.annotation.Px
import android.support.v7.widget.RecyclerView
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
@elihart
elihart / MvRxCombinedMocksExample.kt
Created November 20, 2019 21:16
MvRxCombinedMocksExample
override fun provideMocks() = combineMocks(
"Marketplace" to marketplaceMocks(),
"Plus" to plusMocks(),
"Plus ProHost" to plusProHostMocks()
)
@elihart
elihart / FragmentDirectory.kt
Created December 5, 2019 04:32
Rough example code of a system for declaring routes for Fragments and their argument types. Allows for easily loading the Fragments individually or in a new activity.. Assumes usage with MvRx, but can be used with normal Fragments and Activities with basic modification.
// The tools below can be used to easily start fragments and activities across module boundaries.
// Example usage - a "directory" is declared like this object.
// Then you can use it to:
// Create a new fragment: Search.results().newInstance(fragmentArguments)
// Create an intent for the Fragment: Search.results().newIntent(context, fragmentArguments)
// Start an intent for the Fragment: Search.results().startActivity(context, fragmentArguments)
object Search : Fragments("com.example.android.search") {
fun results() = create<SearchArguments>("SearchResultsFragment")
@elihart
elihart / gist:be43575bf7e7447f67f08467a5604740
Created December 5, 2019 04:31
Rough example code of a system for declaring accessible Fragments, and then easily loading them.
// The tools below can be used to easily start fragments and activities across module boundaries.
// Example usage - a "directory" is declared like this object.
// Then you can use it to:
// Create a new fragment: Search.results().newInstance(fragmentArguments)
// Create an intent for the Fragment: Search.results().newIntent(context, fragmentArguments)
// Start an intent for the Fragment: Search.results().startActivity(context, fragmentArguments)
object Search : Fragments("com.example.android.search") {
fun results() = create<SearchArguments>("SearchResultsFragment")
@elihart
elihart / ktest.kts
Created November 27, 2019 16:08
Kotlin script that runs `gradle clean test` on other scripts to easily build and test them (using kscript)
import java.io.BufferedReader
import java.io.File
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess
/**
* This script takes the name of another kts script as an argument, builds that script with gradle, and runs
* its tests via "gradle clean test".
*/
@elihart
elihart / MvrxIntegrationTestsGeneratedClass.kt
Last active November 20, 2019 21:15
MvrxIntegrationTestsGeneratedClass
@LargeTest
class MvrxIntegrationTests : MvRxIntegrationTestBase() {
@Test
fun booking_fragments_BookingFragment_Screenshots() {
runScreenshots("com.airbnb.android.booking.fragments.BookingFragment")
}
@Test
fun booking_fragments_BookingFragment_Interactions() {
runInteractionTest("com.airbnb.android.booking.fragments.BookingFragment")
@elihart
elihart / ForceClearingAndroidResourceCache.kt
Created November 20, 2019 20:58
ForceClearingAndroidResourceCache
AppCompatDrawableManager.get().onConfigurationChanged(view.context)
val resources = view.resources
val currentConfig = resources.configuration
val currentDisplayMetrics = resources.displayMetrics
resources.updateConfiguration(null, currentDisplayMetrics)
resources.updateConfiguration(currentConfig, currentDisplayMetrics)
@elihart
elihart / TestContextFailureMessageExample.kt
Created November 20, 2019 20:52
TestContextFailureMessageExample
val testContextMessage = "Error while testing 'Default State' mock for BookingFragment -> Clicking 'book_button' view on thread 'AsyncTask #1'"
throw IllegalStateException(testContextMessage, originalException)