From 9a2976118494b393e29229d9406fd0bdfd0b960c Mon Sep 17 00:00:00 2001 From: krrong Date: Wed, 20 Sep 2023 21:34:02 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20DataSource=20Module=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/di/DataSourceModule.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 android/app/src/main/java/com/now/naaga/di/DataSourceModule.kt diff --git a/android/app/src/main/java/com/now/naaga/di/DataSourceModule.kt b/android/app/src/main/java/com/now/naaga/di/DataSourceModule.kt new file mode 100644 index 000000000..4765ecf64 --- /dev/null +++ b/android/app/src/main/java/com/now/naaga/di/DataSourceModule.kt @@ -0,0 +1,19 @@ +package com.now.naaga.di + +import android.content.Context +import com.now.naaga.data.local.AuthDataSource +import com.now.naaga.data.local.DefaultAuthDataSource +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +class DataSourceModule { + @Singleton + @Provides + fun provideAuthDatasource(@ApplicationContext context: Context): AuthDataSource = DefaultAuthDataSource(context) +} From 84a2015f0d13ddd7c0c69cfc9fdacaee122612f5 Mon Sep 17 00:00:00 2001 From: krrong Date: Wed, 20 Sep 2023 21:34:10 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20Service=20Module=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/di/ServiceModule.kt | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 android/app/src/main/java/com/now/naaga/di/ServiceModule.kt diff --git a/android/app/src/main/java/com/now/naaga/di/ServiceModule.kt b/android/app/src/main/java/com/now/naaga/di/ServiceModule.kt new file mode 100644 index 000000000..17f77cd3a --- /dev/null +++ b/android/app/src/main/java/com/now/naaga/di/ServiceModule.kt @@ -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) +} From 3f8f19f1111b1ab1c0b81d6717700b50faa12d74 Mon Sep 17 00:00:00 2001 From: krrong Date: Wed, 20 Sep 2023 21:34:43 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20Repository=EC=97=90=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=A3=BC=EC=9E=85=EC=9D=84=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=ED=99=94=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=B4=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/di/RepositoryModule.kt | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt b/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt index 611daee28..e7beb9286 100644 --- a/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt +++ b/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt @@ -5,7 +5,12 @@ import com.now.domain.repository.AuthRepository import com.now.domain.repository.PlaceRepository import com.now.domain.repository.RankRepository import com.now.domain.repository.StatisticsRepository -import com.now.naaga.NaagaApplication +import com.now.naaga.data.local.AuthDataSource +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 com.now.naaga.data.repository.DefaultAdventureRepository import com.now.naaga.data.repository.DefaultAuthRepository import com.now.naaga.data.repository.DefaultPlaceRepository @@ -22,21 +27,24 @@ import javax.inject.Singleton class RepositoryModule { @Singleton @Provides - fun provideRankRepository(): RankRepository = DefaultRankRepository() + fun provideRankRepository(rankService: RankService): RankRepository = DefaultRankRepository(rankService) @Singleton @Provides - fun provideAuthRepository(): AuthRepository = DefaultAuthRepository(NaagaApplication.authDataSource) + fun provideAuthRepository(authDataSource: AuthDataSource, authService: AuthService): AuthRepository = + DefaultAuthRepository(authDataSource, authService) @Singleton @Provides - fun provideAdventureRepository(): AdventureRepository = DefaultAdventureRepository() + fun provideAdventureRepository(adventureService: AdventureService): AdventureRepository = + DefaultAdventureRepository(adventureService) @Singleton @Provides - fun providePlaceRepository(): PlaceRepository = DefaultPlaceRepository() + fun providePlaceRepository(placeService: PlaceService): PlaceRepository = DefaultPlaceRepository(placeService) @Singleton @Provides - fun provideStatisticsRepository(): StatisticsRepository = DefaultStatisticsRepository() + fun provideStatisticsRepository(statisticsService: StatisticsService): StatisticsRepository = + DefaultStatisticsRepository(statisticsService) } From fe58d47460dcb00c3e2cc1295d2bf5b278be937f Mon Sep 17 00:00:00 2001 From: krrong Date: Wed, 20 Sep 2023 21:35:08 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20Repository=EC=97=90=20Service?= =?UTF-8?q?=20=EC=A3=BC=EC=9E=85=20=EC=9E=90=EB=8F=99=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naaga/data/remote/retrofit/ServicePool.kt | 16 ------------- .../repository/DefaultAdventureRepository.kt | 24 ++++++++++--------- .../data/repository/DefaultAuthRepository.kt | 3 ++- .../data/repository/DefaultPlaceRepository.kt | 6 +++-- .../data/repository/DefaultRankRepository.kt | 6 +++-- .../repository/DefaultStatisticsRepository.kt | 6 +++-- 6 files changed, 27 insertions(+), 34 deletions(-) delete mode 100644 android/app/src/main/java/com/now/naaga/data/remote/retrofit/ServicePool.kt diff --git a/android/app/src/main/java/com/now/naaga/data/remote/retrofit/ServicePool.kt b/android/app/src/main/java/com/now/naaga/data/remote/retrofit/ServicePool.kt deleted file mode 100644 index 74ca786a3..000000000 --- a/android/app/src/main/java/com/now/naaga/data/remote/retrofit/ServicePool.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.now.naaga.data.remote.retrofit - -import com.now.naaga.data.remote.retrofit.RetrofitFactory.retrofit -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 - -object ServicePool { - val adventureService = retrofit.create(AdventureService::class.java) - val rankService = retrofit.create(RankService::class.java) - val statisticsService = retrofit.create(StatisticsService::class.java) - val placeService = retrofit.create(PlaceService::class.java) - val authService = retrofit.create(AuthService::class.java) -} diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAdventureRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAdventureRepository.kt index 75bec02ee..ff0815e2e 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAdventureRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAdventureRepository.kt @@ -12,27 +12,29 @@ import com.now.domain.repository.AdventureRepository import com.now.naaga.data.mapper.toDomain import com.now.naaga.data.mapper.toDto import com.now.naaga.data.remote.dto.FinishGameDto -import com.now.naaga.data.remote.retrofit.ServicePool +import com.now.naaga.data.remote.retrofit.service.AdventureService import com.now.naaga.util.getValueOrThrow -class DefaultAdventureRepository : AdventureRepository { +class DefaultAdventureRepository( + private val adventureService: AdventureService, +) : AdventureRepository { override suspend fun fetchMyAdventures(): List { - val response = ServicePool.adventureService.getMyGames().getValueOrThrow() + val response = adventureService.getMyGames().getValueOrThrow() return response.map { it.toDomain() } } override suspend fun fetchAdventure(adventureId: Long): Adventure { - val response = ServicePool.adventureService.getGame(adventureId).getValueOrThrow() + val response = adventureService.getGame(adventureId).getValueOrThrow() return response.toDomain() } override suspend fun fetchAdventureByStatus(status: AdventureStatus): List { - val response = ServicePool.adventureService.getGamesByStatus(status.name).getValueOrThrow() + val response = adventureService.getGamesByStatus(status.name).getValueOrThrow() return response.map { it.toDomain() } } override suspend fun beginAdventure(coordinate: Coordinate): Adventure { - val response = ServicePool.adventureService.beginGame(coordinate.toDto()).getValueOrThrow() + val response = adventureService.beginGame(coordinate.toDto()).getValueOrThrow() return response.toDomain() } @@ -42,12 +44,12 @@ class DefaultAdventureRepository : AdventureRepository { coordinate: Coordinate, ): AdventureStatus { val finishGameDto = FinishGameDto(endType.name, coordinate.toDto()) - val response = ServicePool.adventureService.endGame(adventureId, finishGameDto).getValueOrThrow() + val response = adventureService.endGame(adventureId, finishGameDto).getValueOrThrow() return AdventureStatus.getStatus(response.gameStatus) } override suspend fun fetchAdventureResult(adventureId: Long): AdventureResult { - val response = ServicePool.adventureService.getGameResult(adventureId).getValueOrThrow() + val response = adventureService.getGameResult(adventureId).getValueOrThrow() return response.toDomain() } @@ -55,17 +57,17 @@ class DefaultAdventureRepository : AdventureRepository { sortBy: SortType, order: OrderType, ): List { - val response = ServicePool.adventureService.getMyGameResults(sortBy.name, order.name).getValueOrThrow() + val response = adventureService.getMyGameResults(sortBy.name, order.name).getValueOrThrow() return response.map { it.toDomain() } } override suspend fun fetchHint(adventureId: Long, hintId: Long): Hint { - val response = ServicePool.adventureService.getHint(adventureId, hintId).getValueOrThrow() + val response = adventureService.getHint(adventureId, hintId).getValueOrThrow() return response.toDomain() } override suspend fun makeHint(adventureId: Long, coordinate: Coordinate): Hint { - val response = ServicePool.adventureService.requestHint(adventureId, coordinate.toDto()).getValueOrThrow() + val response = adventureService.requestHint(adventureId, coordinate.toDto()).getValueOrThrow() return response.toDomain() } } diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt index 9f577f85a..429643c00 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt @@ -4,7 +4,7 @@ import com.now.domain.model.PlatformAuth import com.now.domain.repository.AuthRepository import com.now.naaga.data.local.AuthDataSource import com.now.naaga.data.mapper.toDto -import com.now.naaga.data.remote.retrofit.ServicePool.authService +import com.now.naaga.data.remote.retrofit.service.AuthService import com.now.naaga.util.getValueOrThrow import com.now.naaga.util.unlinkWithKakao import kotlinx.coroutines.CoroutineDispatcher @@ -13,6 +13,7 @@ import kotlinx.coroutines.withContext class DefaultAuthRepository( private val authDataSource: AuthDataSource, + private val authService: AuthService, private val dispatcher: CoroutineDispatcher = Dispatchers.IO, ) : AuthRepository { diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultPlaceRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultPlaceRepository.kt index c4c9223ad..76a77d1b9 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultPlaceRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultPlaceRepository.kt @@ -4,7 +4,7 @@ import com.now.domain.model.Coordinate import com.now.domain.model.Place import com.now.domain.repository.PlaceRepository import com.now.naaga.data.mapper.toDomain -import com.now.naaga.data.remote.retrofit.ServicePool.placeService +import com.now.naaga.data.remote.retrofit.service.PlaceService import com.now.naaga.util.getValueOrThrow import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody @@ -13,7 +13,9 @@ import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.toRequestBody import java.io.File -class DefaultPlaceRepository : PlaceRepository { +class DefaultPlaceRepository( + private val placeService: PlaceService, +) : PlaceRepository { override suspend fun fetchMyPlaces( sortBy: String, order: String, diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultRankRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultRankRepository.kt index 0e4e2d7fc..ac447c91b 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultRankRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultRankRepository.kt @@ -3,10 +3,12 @@ package com.now.naaga.data.repository import com.now.domain.model.Rank import com.now.domain.repository.RankRepository import com.now.naaga.data.mapper.toDomain -import com.now.naaga.data.remote.retrofit.ServicePool.rankService +import com.now.naaga.data.remote.retrofit.service.RankService import com.now.naaga.util.getValueOrThrow -class DefaultRankRepository : RankRepository { +class DefaultRankRepository( + private val rankService: RankService, +) : RankRepository { override suspend fun getAllRanks( sortBy: String, order: String, diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultStatisticsRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultStatisticsRepository.kt index 6d64d75a9..7f1d70332 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultStatisticsRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultStatisticsRepository.kt @@ -3,10 +3,12 @@ package com.now.naaga.data.repository import com.now.domain.model.Statistics import com.now.domain.repository.StatisticsRepository import com.now.naaga.data.mapper.toDomain -import com.now.naaga.data.remote.retrofit.ServicePool.statisticsService +import com.now.naaga.data.remote.retrofit.service.StatisticsService import com.now.naaga.util.getValueOrThrow -class DefaultStatisticsRepository : StatisticsRepository { +class DefaultStatisticsRepository( + private val statisticsService: StatisticsService, +) : StatisticsRepository { override suspend fun getMyStatistics(): Statistics { val response = statisticsService.getMyStatistics() return response.getValueOrThrow().toDomain() From 06dce51fb9dc3853087aec3e8df3feaa68c0dcd6 Mon Sep 17 00:00:00 2001 From: krrong Date: Thu, 21 Sep 2023 20:45:41 +0900 Subject: [PATCH 5/5] refactor: lint check --- android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt b/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt index 6fcba03a7..e7beb9286 100644 --- a/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt +++ b/android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt @@ -6,7 +6,6 @@ import com.now.domain.repository.PlaceRepository import com.now.domain.repository.RankRepository import com.now.domain.repository.StatisticsRepository import com.now.naaga.data.local.AuthDataSource -import com.now.naaga.data.local.AuthDataSource 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