Skip to content

Instantly share code, notes, and snippets.

View pahill's full-sized avatar
👩‍🎤

Pamela Hill pahill

👩‍🎤
View GitHub Profile
abstract class NetworkBoundResource<ResultType, RequestType> (private val appExecutors: AppExecutors) {
//LiveData that represents the resource, implemented in the base class
val result: MutableLiveData<Resource<ResultType>> = MutableLiveData()
//Called to get the cached data from the database
@WorkerThread
abstract fun loadFromDb(): ResultType?
//Called with the data in the database to determine when it was fetched
//from the network
/**
* A generic class that describes data with a status
*/
class Resource<T> constructor(val status: Status, var data: T? = null, val message: String? = null, val date: Date? = null) {
companion object {
fun <T> success(data: T?, date: Date?): Resource<T> {
return Resource(status = Status.SUCCESS, data = data, date = date)
}
class UserRepository (private val webservice: UserApi, private val userDao: UserDao, private val appExecutors: AppExecutors){
fun loadUser(userId: Int) : LiveData<Resource<User>>{
return object : NetworkBoundResource<User, User> (appExecutors) {
override fun loadFromDb(): User? {
return userDao.load(userId)
}
override fun getDataFetchDate(data: User?): Date? {
return data?.fetchDate
class MoveAction {
operator fun invoke(units: Int, direction: String) {...}
  operator fun invoke(targetPoint: Point) {...}
}
val move = MoveAction()
val targetPoint = Point(90, 90)
move(5, "left")
move(targetPoint)
class SpaceShuttle {
operator fun invoke(units: Int, direction: String) {...}
operator fun invoke(targetPoint: Point) {...}
}
val spaceShuttle = SpaceShuttle()
val targetPoint = Point(90, 90)
spaceShuttle(5, "left")
spaceShuttle(targetPoint)
@pahill
pahill / Undo.kt
Last active November 27, 2018 10:04
class Memento(private val originalText: String) {
}
interface UndoCommand {
operator fun invoke()
}
class UndoTypingCommand(private val memento: Memento) : UndoCommand {
override operator fun invoke() {
//restore the state using the memento object
@pahill
pahill / Color.kt
Last active November 27, 2018 10:08
val translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f)
//Destructuring declarations operator extension functions - Color already exists in the android.graphics package
operator fun Color.component1() = this.red()
operator fun Color.component2() = this.green()
operator fun Color.component3() = this.blue()
operator fun Color.component4() = this.alpha()
//Assign variables using destructuring declaration
val (red, green, blue, alpha) = translucentRed
fun return1And2(): Pair<String, String> {
return "returnValue1" to "returnValue2"
}
val (r1, r2) = return1And2()
@pahill
pahill / Point.kt
Last active November 27, 2018 10:01
data class Point(private val x: Double,
private val y: Double) {
operator fun minus(other: Point): Double {
//Pythagorian algorithm - not important here!
return sqrt(pow(x - other.x, 2.0) + pow(y - other.y, 2.0))
}
}
val spaceStationPosition = Point(100.0, 100.0)
val spaceShuttlePosition = Point(10.0, 10.0)
data class Point(private val x: Double,
private val y: Double) {
operator fun minus(other: Point): Double {...}
operator fun compareTo(other: Point): Int {
val distanceToOrigin = this - Point(0.0, 0.0)
val otherDistanceToOrigin = other - Point(0.0, 0.0)
return when {
distanceToOrigin > otherDistanceToOrigin -> 1
distanceToOrigin == otherDistanceToOrigin -> 0
else -> -1