Last active
April 17, 2019 20:12
-
-
Save rongi/72fccf7a450c35bafb31fd2d4f5672ba to your computer and use it in GitHub Desktop.
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
import android.app.Activity | |
import android.os.Bundle | |
import io.reactivex.Observable | |
import okhttp3.OkHttpClient | |
class MyActivity : Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val getUser: (userId: String) -> Observable<User> = dependencies.provideGetUser() | |
val presenter = MyPresenter(getUser) | |
} | |
} | |
class MyPresenter( | |
private val getUser: (userId: String) -> Observable<User> | |
) | |
val dependencies: MyDependencies = MyDependenciesReal() | |
interface MyDependencies { | |
fun provideHttpClient(): OkHttpClient | |
fun provideGetUser(): (userId: String) -> Observable<User> | |
} | |
data class User( | |
val id: String, | |
val name: String | |
) | |
class MyDependenciesReal : MyDependencies { | |
private val okHttpClient: OkHttpClient by lazy(::OkHttpClient) | |
override fun provideHttpClient(): OkHttpClient = okHttpClient | |
override fun provideGetUser(): (userId: String) -> Observable<User> { | |
val okHttpClient = provideHttpClient() | |
// Given okHttpClient make an Observable that allows us to get the user. | |
return makeGetUser(okHttpClient) | |
} | |
} | |
fun makeGetUser(okHttpClient: OkHttpClient): (String) -> Observable<User> { | |
return { userId: String -> | |
// Just a fake implementation, real one should use the okHttpClient. | |
Observable.just(User(id = userId, name = "user name")) | |
} | |
} |
pakoito
commented
Apr 17, 2019
•
Thank you @pakoito! About this
extension class OkNetwork: Network<OkHttpClient> {
override val client: OkHttpClient by lazy { OkHttpClient() }
}
Should it be extension object
instead of extension class
if I want OkNetwork
to be a singleton?
Right. Fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment