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 / gist:dd1815e2aa18656120fd
Last active August 29, 2015 14:16
RequestManager
public class TestFragment extends AirFragment {
private static final int REQUEST_MANAGER_ID = 1;
@Inject RequestManager mRequestManager;
private RequestListener mListener = new RequestListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
@elihart
elihart / gist:e85c782356373040c1c6
Created February 25, 2015 22:43
Compare upload phrases methods
require 'time'
require 'json'
require "rexml/document"
# Get the secret from monorail's encrypted databag
# (e.g. /srv/airbnb/on/config/configatron/secure.yml)
# under `api_private_translate`, `secret`.
SECRET = ARGV[0]
URL = 'https://www.airbnb.com'
@elihart
elihart / FileEditor.kt
Created May 10, 2019 03:23
Kotlin File extension for easily editing the contents of a file
/**
* Edit the contents of a File with a [FileEditor]. The new contents will override the old.
*
* @param lineTransform Called with each line of the file in order.
* Return the new value you would like written in the file, or null to delete the line.
* You can also call [FileEditor.insertNext] to insert a line immediately above the given line, or
* [FileEditor.insertPrevious] to insert a line immediately after the given line.
*/
fun File.edit(lineTransform: FileEditor.(line: String) -> String) {
FileEditor(this).edit(lineTransform)
@elihart
elihart / MvrxEpoxyFormExample.kt
Last active October 29, 2019 06:51
Example edit text simple form with MvRx and Epoxy
// MvRx setup
data class MyState(
val text: String? = null
)
class MyViewModel : MvRxViewModel() {
fun setText(newText: String) {
setState {
copy(text = newText)
@elihart
elihart / MvRxHelloWorld.kt
Created November 20, 2019 17:19
MvRx Hello World Example
data class MyState(val text: String = "Hello World") : MvRxState
class MyViewModel(state: MyState) : BaseMvRxViewModel<MyState>(state)
class MyFragment : MvRxFragment() {
val viewModel: MyViewModel by fragmentViewModel()
fun epoxyController() = simpleController(viewModel) { state ->
textRow {
@elihart
elihart / MvRxMockDeclarationExample.kt
Created November 20, 2019 17:25
MvRx Mock Declaration Example
class DadJokeFragment : MvRxFragment() {
val viewModel: DadJokeViewModel by fragmentViewModel()
override fun provideMocks() = mockSingleViewModel(
viewModelReference = DadJokeFragment::viewModel,
defaultState = mockDadJokeState
)
}
@elihart
elihart / MvRxDadJokeMockExample.kt
Created November 20, 2019 17:28
MvRx Mock State Example
val mockDadJokeState = DadJokeState(
jokes = Success(
value = JokesResponse(
nextPage = 3,
results = listOf(
Joke(
id = "0LuXvkq4Muc",
joke = "I'm tired of following my dreams. I'm just going to ask them where they are going and meet up with them later."
),
Joke(
@elihart
elihart / MvRxMockVariantDslExample.kt
Created November 20, 2019 17:30
MvRx Mock Variant DSL Example
override fun provideMocks() = mockSingleViewModel(
viewModelReference = DadJokeFragment::viewModel,
defaultState = mockDadJokeState
) {
state("Loading") {
copy(jokes = Loading())
}
state("Empty results") {
copy(jokes = Success(jokes.copy(results = emptyList())))
@elihart
elihart / MvRxNestedSetSyntaxExample.kt
Created November 20, 2019 17:32
MvRx Nested Set Syntax Example
state("Empty results") {
// Nested copying - gross!
copy(request = Success(request.copy(results = emptyList())))
// A cleaner way to specify the same thing
set {  ::request { success {  ::results } } }.with { emptyList() }
}
@elihart
elihart / MvRxSetSyntaxShortcutsExample.kt
Created November 20, 2019 17:33
MvRx Set Syntax Shortcuts Example
state("Show cancellation policy subtitle") {
set {  ::bookingDetails { success {  ::cancellationSection {  ::showSubtitle } } } }.with { true }
// Can also be written more simply as:
setTrue {  ::bookingDetails { success {  ::cancellationSection {  ::showSubtitle } } } }
}