Skip to content

Instantly share code, notes, and snippets.

View AdamMc331's full-sized avatar

Adam McNeilly AdamMc331

View GitHub Profile
@AdamMc331
AdamMc331 / RecyclerView Child Runnable.java
Last active December 19, 2019 19:10
Lifting code from RecyclerView to share
// This is from Android's RecyclerView.Java file
/**
* Note: this Runnable is only ever posted if:
* 1) We've been through first layout
* 2) We know we have a fixed size (mHasFixedSize)
* 3) We're attached
*/
final Runnable mUpdateChildViewsRunnable = new Runnable() {
@Override
@AdamMc331
AdamMc331 / PokemonListState.kt
Created November 22, 2019 18:04
The files used in a blog post about unit testing.
sealed class PokemonListState {
object Loading : PokemonListState()
object Empty : PokemonListState()
class Loaded(val data: List<Pokemon>) : PokemonListState()
class Error(val error: Throwable?) : PokemonListState()
}
@AdamMc331
AdamMc331 / AfterFormat.kt
Created September 30, 2019 14:33
A quick gist that shows a KtLint formatting issue with enum classes.
enum class DayEnum(
val index: Int,
val dayName: String
) {
SUNDAY(
index = 0,
dayName = "Sunday"
),
MONDAY(
index = 1,
@AdamMc331
AdamMc331 / .ideaNotes.md
Created August 26, 2019 17:32
My notes from a talk at Droidcon NYC about the .idea folder.

git rm -r —cached .idea? - Harry Grillo

What is this folder?

Comes from the IDE Intellij, which AS is forked from and that’s why we have it.

Projects are directory based, which is what the .idea/ folder is for.

The gitignore file is often kind of spotty about what’s ignored and what isn’t. But now there’s a fixed set of lines (that are also what the presenter recommends).

libraries/

Meta data around libraries in the project. We don’t want to include this because dependency management tools are what’s responsible for setting this up (like gradle).

@AdamMc331
AdamMc331 / DependencyExample.kt
Created July 19, 2019 15:47
Quick example that shows how to use Kotlin default params to help with DI.
class HumanTimeHelper(private val clock: IClock = SystemClock()) {
// This allows me to update my helper class, _without_ changing the call site!
}
class QuestionViewModel {
private val state = MutableLiveData<QuestionState>()
private val currentQuestion: LiveData<Question> = Transformations.map(state) {
(it as? QuestionState.Loaded)?.data
}
val questionTitle: String
get() = currentQuestion.value?.title.orEmpty
}
// In Java, I can do this
public class MainClass {
interface MyInterface {
public void printName();
}
}
public class SubClass extends MainClass {
}
@AdamMc331
AdamMc331 / ErrorHandling.kt
Last active January 25, 2019 02:16
Shows a way to use function types to handle errors.
// Create a custom error class that holds both the error that occured,
// and a function to invoke if it retries.
data class MyCustomError(
val error: Throwable? = null,
val retry: (() -> Unit)? = null
)
// Create a state class that represents the current state of your screen.
// This is where you'd wanna put data you have to display, like a list of items
// or maybe an error, and a function to call if you want to retry something
@AdamMc331
AdamMc331 / AccountDAO_Impl.java
Created May 30, 2018 18:40
Shows an implementation of a Room database DAO
public class AccountDAO_Impl implements AccountDAO {
private final RoomDatabase __db;
private final EntityInsertionAdapter __insertionAdapterOfAccount;
private final EntityDeletionOrUpdateAdapter __deletionAdapterOfAccount;
private final SharedSQLiteStatement __preparedStmtOfDeleteAll;
public AccountDAO_Impl(RoomDatabase __db) {
// NoClassDef
Caused by: java.lang.NoClassDefFoundError: com/android/ide/common/res2/ResourceSet
at org.jetbrains.kotlin.gradle.plugin.Android25ProjectHandler.getResDirectories(Android25ProjectHandler.kt:185)
at org.jetbrains.kotlin.gradle.plugin.Android25ProjectHandler.getResDirectories(Android25ProjectHandler.kt:21)
// Full
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app-refresh'.
at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:94)
at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:89)