Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam McNeilly AdamMc331

View GitHub Profile
@AdamMc331
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
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
AdamMc331 / UniversityDAO_Impl.java
Created Apr 26, 2020
A copy of the generated code used in my blog post about Room database relationships.
View UniversityDAO_Impl.java
package com.adammcneilly.androiduniversity.room;
import android.database.Cursor;
import androidx.collection.LongSparseArray;
import androidx.lifecycle.LiveData;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.room.util.StringUtil;
@AdamMc331
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
AdamMc331 / RecyclerView Child Runnable.java
Last active Dec 19, 2019
Lifting code from RecyclerView to share
View RecyclerView Child Runnable.java
// 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 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
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
) {
SUNDAY(
index = 0,
dayName = "Sunday"
),
MONDAY(
index = 1,
@AdamMc331
AdamMc331 / .ideaNotes.md
Created Aug 26, 2019
My notes from a talk at Droidcon NYC about the .idea folder.
View .ideaNotes.md

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 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> = Transformations.map(state) {
(it as? QuestionState.Loaded)?.data
}
val questionTitle: String
get() = currentQuestion.value?.title.orEmpty
}
You can’t perform that action at this time.