Skip to content

Instantly share code, notes, and snippets.

View adrian110288's full-sized avatar
💭
I may be slow to respond.

Adrian L adrian110288

💭
I may be slow to respond.
  • London
View GitHub Profile
@adrian110288
adrian110288 / livedata-mutate-ext.kt
Created June 2, 2019 07:28
Mutating LiveData values
@MainThread
fun <T> MutableLiveData<T>.mutate(mutator: T.() -> Unit) {
this.value = this.value?.apply(mutator)
}
@MainThread
fun <T> MutableLiveData<T>.modifyValue(transform: T.() -> T) {
this.value = this.value?.run(transform)
}
@adrian110288
adrian110288 / dagger.txt
Last active April 25, 2019 12:46
Dagger tips
// Disable during development. It speeds up compilation time. Keep enabled in production or CI tests.
Adagger.formatGeneratedSource=disabled
// https://stackoverflow.com/questions/39136042/dagger-reusable-scope-vs-singleton
@Reusable
// Modules to be object with static providers
object xModule
@JvmStatic
@Provide
/**
* Copyright 2019 Coinbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
inline fun <T> LiveData<T>.observe(
owner: LifecycleOwner,
crossinline observer: (T?) -> Unit
) {
observe(owner, Observer<T> { v -> observer(v) })
}
inline fun <T> LiveData<T>.observeNonNull(
owner: LifecycleOwner,
crossinline observer: (T) -> Unit
@adrian110288
adrian110288 / ViewModelUtils.kt
Last active January 19, 2019 22:30
Convenience factory to handle ViewModels with one parameter.
/**
* Convenience factory to handle ViewModels with one parameter.
*
* Make a factory:
* ```
* // Make a factory
* val FACTORY = viewModelFactory(::MyViewModel)
* ```
*
* Use the generated factory:
@adrian110288
adrian110288 / Event.kt
Created January 19, 2019 20:54
Used as a wrapper for data that is exposed via a LiveData that represents an event.
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
@adrian110288
adrian110288 / EventObserver.kt
Last active January 19, 2019 20:53
To use with Event based handling of single live events.
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
}
}
}
public abstract class DataBindingBaseAdapter
extends RecyclerView.Adapter<MyViewHolder> {
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(
layoutInflater, viewType, parent, false);
return new MyViewHolder(binding);
}
public class DataBindingViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
public MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj) {
binding.setVariable(BR.obj, obj);
@adrian110288
adrian110288 / LoggingInterceptor.java
Created January 17, 2019 10:32
Retrofit Logging interceptor
import android.util.Log;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class LoggingInterceptor implements Interceptor {
@Override