Skip to content

Instantly share code, notes, and snippets.

@pbochenski
Last active October 24, 2017 11:47
Show Gist options
  • Save pbochenski/55ca6a881530f2e6131cd38aa19caa80 to your computer and use it in GitHub Desktop.
Save pbochenski/55ca6a881530f2e6131cd38aa19caa80 to your computer and use it in GitHub Desktop.
learning_dagger
@Component
interface AppComponent
class App : Application() {
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder().build()
}
}
public final class DaggerAppComponent implements AppComponent {
private DaggerAppComponent(Builder builder) {}
public static Builder builder() {
return new Builder();
}
public static AppComponent create() {
return new Builder().build();
}
public static final class Builder {
private Builder() {}
public AppComponent build() {
return new DaggerAppComponent(this);
}
}
}
@Singleton
@Component(modules = arrayOf(RestDModule::class))
interface AppComponent {
fun inject(fragment: MainActivityAH)
}
@Singleton
@Component(modules = arrayOf(RestDModule::class))
interface AppComponent {
fun inject(fragment: MainActivityAH)
fun inject(fragment: LoginActivityAH)
}
@Component(modules = arrayOf(RestDModule::class))
interface AppComponent {
}
component = DaggerAppComponent.builder()
.restDModule(RestDModule("https://ig1oxjwy74.execute-api.us-east-1.amazonaws.com/dev/"))
.build()
class MainActivityAH : ActionHolder<MainActivityCommands, MainActivityState>() {
@Inject lateinit var repo: Repo
...
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity.application as App).component.inject(this)
}
@Module
class RestDModule(private val baseUrl: String){
@Provides
@Singleton
fun provideRepo() :Repo {
val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
.apply { level = HttpLoggingInterceptor.Level.BODY })
.build()
val api = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(LoginDojoApi::class.java)
return Repo(api)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment