Skip to content

Instantly share code, notes, and snippets.

View AdamMc331's full-sized avatar

Adam McNeilly AdamMc331

View GitHub Profile
@AdamMc331
AdamMc331 / PrescriptionCardListController.kt
Created December 11, 2020 01:34
Demonstrating an EpoxyGroupModel implementation and what I would like to do if possible.
/**
* This class is the current implementation.
*
* Notice that in [addPendingPrescriptionCard] and [addActivePrescriptionCard],
* we have relatively identical code:
*
* 1. Create the group.
* 2. Create all the models and add them to the group.
* 3. Add the group to the main controller.
*
@AdamMc331
AdamMc331 / Patient.kt
Created October 4, 2020 17:42
Shows how we can map a network response object to a UI model & manually check for required fields.
data class PatientDTO(
@field:Json(name = "id")
val id: String? = null,
@field:Json(name = "firstName")
val firstName: String? = null,
@field:Json(name = "lastName")
val lastName: String? = null
) {
fun toPatient(): Patient {
val id = this.id.orEmpty()
@AdamMc331
AdamMc331 / DateTesting.kt
Created September 25, 2020 15:59
Gives an example of how I take a date that is GMT and convert it to the TimeZone that I want.
/**
* Given a supplied [dateString] that matches our [RFC_3339_FORMAT], convert it to a
* [Calendar] instance for the corresponding [timeZoneString].
*
* The only way I've been able to do this is:
* 1. Take the date string, which is GMT, and convert it to a Date.
* 2. Update the TZ on our SimpleDateFormat, to get the string in the desired TimeZone.
* 3. Reverse engineer that back into a date.
* 4. Return this calendar.
*
@AdamMc331
AdamMc331 / EpoxyController.kt
Last active September 16, 2020 16:00
Shows a possible example for using a TypedEpoxyController with the currentData property. Not sure if this is best pracitce.
class MyEpoxyController : TypedEpoxyController<ViewState> {
/**
* This buildModels method takes in the current ViewState and builds all of the Epoxy Models
* that need to be displayed for this state.
*/
override fun buildModels(data: ViewState) {
data.usersAndItems.forEach { userItems ->
addUserItemsModels(userItems)
}
}
@AdamMc331
AdamMc331 / User.kt
Created August 25, 2020 14:39
Showing the idea of separating UI items with data layer items.
// Easy to do this
@Entity
data class User(
@PrimaryKey(autoGenerate = false)
@field:Json(name = "id")
val id: String = "",
@field:Json(name = "display_name")
val displayName: String? = null
)
@AdamMc331
AdamMc331 / Dangerfile
Created August 22, 2020 21:55
Demonstrates how to have a custom DangerFile that gets the lines of code changed excluding a certain file.
# If you want to include deletions, `+ git.deleted_files`
modifiedOrNewFiles = (git.added_files + git.modified_files)
diffWithoutPackageLock = modifiedOrNewFiles.select { |file|
file != "package-lock.json"
}.map { |file|
git.info_for_file(file)
}.map { |info|
info[:insertions] + info[:deletions]
}.sum()
@AdamMc331
AdamMc331 / AuthorizationProvider.kt
Created August 19, 2020 17:01
Mockk unit fun example
interface AuthorizationProvider {
fun getAuthorizationToken(): String?
fun storeAuthorizationToken(token: String)
}
@AdamMc331
AdamMc331 / FakePreferences.kt
Created July 23, 2020 02:49
Shows an example of wrapping shared preferences and how we can use a fake to test the value.
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 June 11, 2020 01:20
Demonstrates an Observer for LiveData to be used during unit testing.
/**
* 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 April 26, 2020 18:49
A copy of the generated code used in my blog post about Room database relationships.
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;