Skip to content

Instantly share code, notes, and snippets.

View JacquesSmuts's full-sized avatar
🙃
Recovering

Jacques Smuts JacquesSmuts

🙃
Recovering
View GitHub Profile
@JacquesSmuts
JacquesSmuts / build.gradle
Last active May 4, 2019 09:30
for creating and deploying a library
// Project-level build.gradle:
buildscript {
dependencies {
classpath 'com.novoda:bintray-release:0.9'
}
}
// Library module build.gradle:
apply plugin: 'com.novoda.bintray-release'
import kotlin.reflect.KSuspendFunction1 // You have to import this manually
import kotlin.reflect.KSuspendFunction2 // You have to import this manually
inline suspend fun <reified Service: RetroService, Result> doSuspendedApiCall(
services: List<RetroService>,
apiCall: KSuspendFunction1<Service, Result>
): Result? {
// identical to doApiCall
}
import kotlin.reflect.KFunction2 // You have to import this manually
inline fun <reified Service: RetroService, Input, Result> doApiCallWithArg(
services: List<RetroService>,
apiCall: KFunction2<Service, Input, Result>,
input: Input
): Result? {
Log.v(TAG, "Attempting to do \$apiCall on \$services with argument \$input")
services.filterIsInstance<Service>()
inline fun <reified Service: RetroService, Result> doApiCall(
services: List<RetroService>,
apiCall: KFunction1<Service, Result>
): Result? {
Log.v(TAG, "Attempting to do \$apiCall on \$services")
services.filterIsInstance<Service>()
.forEach { service ->
val result = apiCall(service)
Log.i(TAG, "Returned result $result")
fun doAwsApiCall(
services: List<RetroService>,
apiCall: KFunction1<AwsCodeCommitService, List<AwsRepo>>
): List<AwsRepo>? {
Log.v(TAG, "Attempting to do \$apiCall on \$services")
services.filterIsInstance<AwsCodeCommitService>()
.forEach { service ->
val result = apiCall(service) // This is the only real change
Log.i(TAG, "Returned result $result")
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion" // In your gradle
import kotlin.reflect.KFunction1 // You have to import this manually
// The IDE generates this automatically
fun doAwsApiCall(
services: List<RetroService>,
apiCall: KFunction1<AwsCodeCommitService, List<AwsRepo>>
): List<AwsRepo>? {
TODO("Not implemented yet")
fun listAwsRepos(services: List<RetroService>): List<AwsRepo>? {
Log.v(TAG, "Attempting to get awsRepos from \$services")
services.filterIsInstance<AwsCodeCommitService>()
.forEach { service ->
val result = service.listAwsRepos()
Log.i(TAG, "Returned result $result")
return result
}
interface RetroService
interface GitHubService: RetroService {
@GET("users/{user}/repos")
fun listGitHubRepos(@Path("user") user: String): List<GitHubRepo>
}
interface AwsCodeCommitService: RetroService {
@GET("ListRepositories")
fun listAwsRepos(): List<AwsRepo>
class UserRepository(private val database: DeviceDatabase) {
val liveUsers: LiveData<List<User>> = Transformations.map(database.userDao().liveUsers) {
it.map {
it.toUser()
}
}
}
// In your shared domain/core module
data class User(
val id: String,
val name: String,
val surname: String,
val age: Int
)
// In your database module ONLY
@Entity(tableName = "UserTable")