-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: retrofit 의존성 주입 라이브러리 적용 (#353)
* feat: DataSource Module 추가 * feat: Service Module 추가 * refactor: Repository에 의존성 주입을 자동화하기 위해 수정 * refactor: Repository에 Service 주입 자동화 * refactor: lint check
- Loading branch information
Showing
8 changed files
with
115 additions
and
39 deletions.
There are no files selected for viewing
16 changes: 0 additions & 16 deletions
16
android/app/src/main/java/com/now/naaga/data/remote/retrofit/ServicePool.kt
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
android/app/src/main/java/com/now/naaga/di/ServiceModule.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.now.naaga.di | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.now.naaga.BuildConfig | ||
import com.now.naaga.NaagaApplication | ||
import com.now.naaga.data.remote.retrofit.service.AdventureService | ||
import com.now.naaga.data.remote.retrofit.service.AuthService | ||
import com.now.naaga.data.remote.retrofit.service.PlaceService | ||
import com.now.naaga.data.remote.retrofit.service.RankService | ||
import com.now.naaga.data.remote.retrofit.service.StatisticsService | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class ServiceModule { | ||
private val BASE_URL = BuildConfig.BASE_URL | ||
|
||
@Singleton | ||
@Provides | ||
fun createInterceptor(): Interceptor = Interceptor { chain -> | ||
val token: String = NaagaApplication.authDataSource.getAccessToken() ?: "" | ||
with(chain) { | ||
val newRequest = request().newBuilder() | ||
.addHeader("Authorization", "Bearer $token") | ||
.addHeader("Content-Type", "application/json") | ||
.build() | ||
proceed(newRequest) | ||
} | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun createOkHttpClient(interceptor: Interceptor): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
addInterceptor(interceptor) | ||
}.build() | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.client(okHttpClient) | ||
.build() | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRankService(retrofit: Retrofit): RankService = retrofit.create(RankService::class.java) | ||
|
||
@Singleton | ||
@Provides | ||
fun provideStatisticsService(retrofit: Retrofit): StatisticsService = retrofit.create(StatisticsService::class.java) | ||
|
||
@Singleton | ||
@Provides | ||
fun provideAdventureService(retrofit: Retrofit): AdventureService = retrofit.create(AdventureService::class.java) | ||
|
||
@Singleton | ||
@Provides | ||
fun providePlaceService(retrofit: Retrofit): PlaceService = retrofit.create(PlaceService::class.java) | ||
|
||
@Singleton | ||
@Provides | ||
fun provideAuthService(retrofit: Retrofit): AuthService = retrofit.create(AuthService::class.java) | ||
} |