This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun return1And2(): Pair<String, String> { | |
return "returnValue1" to "returnValue2" | |
} | |
val (r1, r2) = return1And2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer