Skip to content

Instantly share code, notes, and snippets.

View JacquesSmuts's full-sized avatar
🙃
Recovering

Jacques Smuts JacquesSmuts

🙃
Recovering
View GitHub Profile
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
}
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 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")
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")
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>()
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
}
@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'
inline fun <reified Interface, Input, Result> callFunction(
apiCall: [THE INTERCHANGEABLE BIT],
input: Input //If there is an input
): Result?
fun saveUsername(username: String) {
StorageService.getInstance().saveUsername(username) { result ->
handleResult(result)
}
}
suspend fun saveUsername(username: String) {
val result = suspendedSaveUsername(username)
handleResult(result)
}
suspend fun suspendedSaveUsername(username: String) = suspendCoroutine<String> { continuation ->
StorageService.getInstance().saveUsername(username) { result ->
continuation.resume(result)