Skip to content

Instantly share code, notes, and snippets.


Adam McNeilly AdamMc331

View GitHub Profile
AdamMc331 / FakePreferences.kt
Created Jul 23, 2020
Shows an example of wrapping shared preferences and how we can use a fake to test the value.
View FakePreferences.kt
class FakePreferences : Preferences {
var storedLongCallCount = 0
private set
var getLongCallCount = 0
private set
* In this fake, we're just incrementing the number of calls to get long, but we could be more
* thorough and track the key that was called.
AdamMc331 / ProfileRepository.kt
Last active Jun 11, 2020
Demonstrates an Observer for LiveData to be used during unit testing.
View ProfileRepository.kt
* This defines the contract for communicating with some data source to request profile information.
interface ProfileRepository {
fun fetchProfile(userId: String): Single<User>
AdamMc331 /
Created Apr 26, 2020
A copy of the generated code used in my blog post about Room database relationships.
import android.database.Cursor;
import androidx.collection.LongSparseArray;
import androidx.lifecycle.LiveData;
AdamMc331 / PokemonDetailViewModel.kt
Last active Mar 1, 2020
This is part of my blog post on TDD for Android demonstrating some tests we may write.
View PokemonDetailViewModel.kt
class PokemonDetailViewModel {
private var pokemon: Pokemon? = null
fun setPokemon(pokemon: Pokemon) {
val typeCount = pokemon.types.size
if (typeCount <= 0 || typeCount > 2) {
throw IllegalArgumentException("Pokemon has an invalid number of types.")
AdamMc331 / RecyclerView Child
Last active Dec 19, 2019
Lifting code from RecyclerView to share
View RecyclerView Child
// 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() {
AdamMc331 / PokemonListState.kt
Created Nov 22, 2019
The files used in a blog post about unit testing.
View PokemonListState.kt
sealed class PokemonListState {
object Loading : PokemonListState()
object Empty : PokemonListState()
class Loaded(val data: List<Pokemon>) : PokemonListState()
class Error(val error: Throwable?) : PokemonListState()
AdamMc331 / AfterFormat.kt
Created Sep 30, 2019
A quick gist that shows a KtLint formatting issue with enum classes.
View AfterFormat.kt
enum class DayEnum(
val index: Int,
val dayName: String
) {
index = 0,
dayName = "Sunday"
index = 1,
AdamMc331 /
Created Aug 26, 2019
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).


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 / DependencyExample.kt
Created Jul 19, 2019
Quick example that shows how to use Kotlin default params to help with DI.
View DependencyExample.kt
class HumanTimeHelper(private val clock: IClock = SystemClock()) {
// This allows me to update my helper class, _without_ changing the call site!
View TestBug.kt
class QuestionViewModel {
private val state = MutableLiveData<QuestionState>()
private val currentQuestion: LiveData<Question> = {
(it as? QuestionState.Loaded)?.data
val questionTitle: String
get() = currentQuestion.value?.title.orEmpty
You can’t perform that action at this time.