This file contains hidden or 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
| // Project-level build.gradle: | |
| buildscript { | |
| dependencies { | |
| classpath 'com.novoda:bintray-release:0.9' | |
| } | |
| } | |
| // Library module build.gradle: | |
| apply plugin: 'com.novoda.bintray-release' |
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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
| 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>() |
This file contains hidden or 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
| 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") |
This file contains hidden or 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 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") |
This file contains hidden or 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
| 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") |
This file contains hidden or 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 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 | |
| } |
This file contains hidden or 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
| 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> |
This file contains hidden or 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 database: DeviceDatabase) { | |
| val liveUsers: LiveData<List<User>> = Transformations.map(database.userDao().liveUsers) { | |
| it.map { | |
| it.toUser() | |
| } | |
| } | |
| } |
This file contains hidden or 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
| // 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") |