Skip to content

Commit

Permalink
Introduce multiple dagger components and modules
Browse files Browse the repository at this point in the history
This started off as a quick way to adding dagger to
:dribbble but then got a little out of hand due to dependencies
into :core and :app, especially within DataManager.

Also there are still issues with application wide singletons.
  • Loading branch information
keyboardsurfer authored and florina-muntenescu committed Oct 11, 2018
1 parent 3a78172 commit d15117d
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/java/io/plaidapp/core/dagger/DataManagerComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import dagger.Component

@Component(modules = [DataManagerModule::class])
interface DataManagerComponent
29 changes: 29 additions & 0 deletions core/src/main/java/io/plaidapp/core/dagger/DribbbleDataModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.dribbble.data.search.DribbbleSearchService
import retrofit2.Retrofit

@Module(includes = [DribbbleRetrofitModule::class])
class DribbbleDataModule {

@Provides fun provideDribbbleSearchService(retrofit: Retrofit): DribbbleSearchService =
retrofit.create(DribbbleSearchService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory
import dagger.Module
import dagger.Provides
import io.plaidapp.core.dribbble.data.search.DribbbleSearchConverter
import io.plaidapp.core.dribbble.data.search.DribbbleSearchService
import io.plaidapp.core.loggingInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
* Module to set up retrofit.
*/
@Module
class DribbbleRetrofitModule {

@Provides fun provideOkHttpClient(): OkHttpClient =
OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()

@Provides fun provideRetrofitInterface(client: OkHttpClient): Retrofit =
Retrofit.Builder()
.baseUrl(DribbbleSearchService.ENDPOINT)
.addConverterFactory(DribbbleSearchConverter.Factory())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(client)
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.dribbble.data.search.DribbbleSearchService
import io.plaidapp.core.dribbble.data.search.SearchRemoteDataSource

@Module(includes = [DribbbleDataModule::class])
class DribbleSearchServiceProvider {

@Provides fun provideDribbbleSearchService(dribbbleSearchService: DribbbleSearchService) =
SearchRemoteDataSource.getInstance(dribbbleSearchService)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.dagger

import dagger.Module
import dagger.Provides
import io.plaidapp.core.data.CoroutinesContextProvider
import io.plaidapp.core.dribbble.data.ShotsRepository
import io.plaidapp.core.dribbble.data.search.SearchRemoteDataSource

/**
* Provide [ShotsRepository].
*
* TODO: make this an app wide singleton.
*/
@Module(includes = [CoroutinesContextProviderModule::class, DribbleSearchServiceProvider::class])
class ShotsRepositoryModule {

@Provides fun provideShotsRepository(
remoteDataSource: SearchRemoteDataSource,
contextProvider: CoroutinesContextProvider
) = ShotsRepository.getInstance(remoteDataSource, contextProvider)
}

0 comments on commit d15117d

Please sign in to comment.