From 3a6298fd85ccc9ca0ae863052f73cab2cb313fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Tue, 7 Feb 2023 15:28:06 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20chore:=20data-remote=20?= =?UTF-8?q?=EB=AA=A8=EB=93=88=EB=A1=9C=20=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 9 ++--- data-remote/build.gradle.kts | 31 +++++++++++++++ .../data/remote/api/KakaoApiService.kt | 6 +-- .../lighthouse/data/remote/di/ApiModule.kt | 22 +++++++++++ .../lighthouse/data/remote/di/DataModule.kt | 19 ++++++++++ .../data/remote}/di/NetworkModule.kt | 23 ++++------- .../BrandPlaceInfoDataContainerMapper.kt | 8 ++-- .../model/BrandPlaceInfoDataContainer.kt | 4 +- .../repository/BrandRemoteRepositoryImpl.kt | 38 +++++++++++++++++++ .../remote/utils}/HTTPRequestInterceptor.kt | 6 +-- .../datasource/brand/BrandRemoteDataSource.kt | 14 ------- .../brand/BrandRemoteDataSourceImpl.kt | 30 --------------- .../java/com/lighthouse/mapper/ErrorMapper.kt | 8 ---- .../com/lighthouse/model/BeepErrorData.kt | 9 ----- .../repository/brand/BrandRemoteRepository.kt | 13 +++++++ settings.gradle.kts | 1 + 16 files changed, 148 insertions(+), 93 deletions(-) create mode 100644 data-remote/build.gradle.kts rename data/src/main/java/com/lighthouse/network/NetworkApiService.kt => data-remote/src/main/java/com/lighthouse/data/remote/api/KakaoApiService.kt (76%) create mode 100644 data-remote/src/main/java/com/lighthouse/data/remote/di/ApiModule.kt create mode 100644 data-remote/src/main/java/com/lighthouse/data/remote/di/DataModule.kt rename {app/src/main/java/com/lighthouse => data-remote/src/main/java/com/lighthouse/data/remote}/di/NetworkModule.kt (62%) rename {data/src/main/java/com/lighthouse => data-remote/src/main/java/com/lighthouse/data/remote}/mapper/BrandPlaceInfoDataContainerMapper.kt (71%) rename {data/src/main/java/com/lighthouse => data-remote/src/main/java/com/lighthouse/data/remote}/model/BrandPlaceInfoDataContainer.kt (94%) create mode 100644 data-remote/src/main/java/com/lighthouse/data/remote/repository/BrandRemoteRepositoryImpl.kt rename {app/src/main/java/com/lighthouse/di => data-remote/src/main/java/com/lighthouse/data/remote/utils}/HTTPRequestInterceptor.kt (74%) delete mode 100644 data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSource.kt delete mode 100644 data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSourceImpl.kt delete mode 100644 data/src/main/java/com/lighthouse/mapper/ErrorMapper.kt delete mode 100644 data/src/main/java/com/lighthouse/model/BeepErrorData.kt create mode 100644 data/src/main/java/com/lighthouse/repository/brand/BrandRemoteRepository.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6e9a040f0..eb5352e65 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,8 +17,8 @@ android { versionCode = 1 versionName = "1.0.0" - buildConfigField("String", "kakaoSearchId", getApiKey("kakao_search_id")) - manifestPlaceholders["naver_map_api_id"] = getApiKey("naver_map_api_id") + val naverMapApiId = gradleLocalProperties(rootDir).getProperty("naver_map_api_id") + manifestPlaceholders["naver_map_api_id"] = naverMapApiId } buildFeatures { @@ -40,6 +40,7 @@ dependencies { implementation(projects.domain) implementation(projects.presentation) implementation(projects.data) + implementation(projects.dataRemote) implementation(libs.androidX.hilt.work) implementation(libs.androidX.work.runtime.ktx) @@ -69,7 +70,3 @@ dependencies { kapt { useBuildCache = true } - -fun getApiKey(propertyKey: String): String { - return gradleLocalProperties(rootDir).getProperty(propertyKey) -} diff --git a/data-remote/build.gradle.kts b/data-remote/build.gradle.kts new file mode 100644 index 000000000..fed4774ee --- /dev/null +++ b/data-remote/build.gradle.kts @@ -0,0 +1,31 @@ +import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties + +@Suppress("DSL_SCOPE_VIOLATION") +plugins { + id("beep.android.library") + id("beep.android.hilt") + alias(libs.plugins.ksp) +} + +android { + namespace = "com.lighthouse.data.remote" + + defaultConfig { + val kakaoSearchId = gradleLocalProperties(rootDir).getProperty("kakao_search_id") + buildConfigField("String", "kakaoSearchId", kakaoSearchId) + } +} + +dependencies { + implementation(projects.core) + implementation(projects.coreAndroid) + implementation(projects.model) + implementation(projects.common) + implementation(projects.commonAndroid) + implementation(projects.data) + + implementation(libs.squareup.retrofit2) + implementation(libs.squareup.retrofit2.converter.moshi) + + implementation(libs.timber) +} diff --git a/data/src/main/java/com/lighthouse/network/NetworkApiService.kt b/data-remote/src/main/java/com/lighthouse/data/remote/api/KakaoApiService.kt similarity index 76% rename from data/src/main/java/com/lighthouse/network/NetworkApiService.kt rename to data-remote/src/main/java/com/lighthouse/data/remote/api/KakaoApiService.kt index e64e179fc..28066f3fa 100644 --- a/data/src/main/java/com/lighthouse/network/NetworkApiService.kt +++ b/data-remote/src/main/java/com/lighthouse/data/remote/api/KakaoApiService.kt @@ -1,10 +1,10 @@ -package com.lighthouse.network +package com.lighthouse.data.remote.api -import com.lighthouse.model.BrandPlaceInfoDataContainer +import com.lighthouse.data.remote.model.BrandPlaceInfoDataContainer import retrofit2.http.GET import retrofit2.http.Query -interface NetworkApiService { +internal interface KakaoApiService { @GET("v2/local/search/keyword.json") suspend fun getAllBrandPlaceInfo( diff --git a/data-remote/src/main/java/com/lighthouse/data/remote/di/ApiModule.kt b/data-remote/src/main/java/com/lighthouse/data/remote/di/ApiModule.kt new file mode 100644 index 000000000..bb031e7f6 --- /dev/null +++ b/data-remote/src/main/java/com/lighthouse/data/remote/di/ApiModule.kt @@ -0,0 +1,22 @@ +package com.lighthouse.data.remote.di + +import com.lighthouse.data.remote.api.KakaoApiService +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import retrofit2.Retrofit +import javax.inject.Singleton + +@Suppress("unused") +@Module +@InstallIn(SingletonComponent::class) +internal object ApiModule { + @Provides + @Singleton + fun provideApiService( + retrofit: Retrofit + ): KakaoApiService { + return retrofit.create(KakaoApiService::class.java) + } +} diff --git a/data-remote/src/main/java/com/lighthouse/data/remote/di/DataModule.kt b/data-remote/src/main/java/com/lighthouse/data/remote/di/DataModule.kt new file mode 100644 index 000000000..9585be349 --- /dev/null +++ b/data-remote/src/main/java/com/lighthouse/data/remote/di/DataModule.kt @@ -0,0 +1,19 @@ +package com.lighthouse.data.remote.di + +import com.lighthouse.data.remote.repository.BrandRemoteRepositoryImpl +import com.lighthouse.repository.brand.BrandRemoteRepository +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Suppress("unused") +@Module +@InstallIn(SingletonComponent::class) +internal abstract class DataModule { + + @Binds + abstract fun bindsRemoteRepository( + repository: BrandRemoteRepositoryImpl + ): BrandRemoteRepository +} diff --git a/app/src/main/java/com/lighthouse/di/NetworkModule.kt b/data-remote/src/main/java/com/lighthouse/data/remote/di/NetworkModule.kt similarity index 62% rename from app/src/main/java/com/lighthouse/di/NetworkModule.kt rename to data-remote/src/main/java/com/lighthouse/data/remote/di/NetworkModule.kt index 9496f60ae..5139f36e6 100644 --- a/app/src/main/java/com/lighthouse/di/NetworkModule.kt +++ b/data-remote/src/main/java/com/lighthouse/data/remote/di/NetworkModule.kt @@ -1,8 +1,7 @@ -package com.lighthouse.di +package com.lighthouse.data.remote.di -import com.lighthouse.network.NetworkApiService +import com.lighthouse.data.remote.utils.HTTPRequestInterceptor import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -12,16 +11,13 @@ import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import javax.inject.Singleton +@Suppress("unused") @Module @InstallIn(SingletonComponent::class) -object NetworkModule { +internal object NetworkModule { private const val KAKAO_URL = "https://dapi.kakao.com/" - private val moshi = Moshi.Builder() - .addLast(KotlinJsonAdapterFactory()) - .build() - @Provides @Singleton fun provideOkHttpClient(): OkHttpClient { @@ -32,17 +28,14 @@ object NetworkModule { @Provides @Singleton - fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { + fun provideRetrofit( + okHttpClient: OkHttpClient, + moshi: Moshi + ): Retrofit { return Retrofit.Builder() .client(okHttpClient) .baseUrl(KAKAO_URL) .addConverterFactory(MoshiConverterFactory.create(moshi)) .build() } - - @Provides - @Singleton - fun provideApiService(retrofit: Retrofit): NetworkApiService { - return retrofit.create(NetworkApiService::class.java) - } } diff --git a/data/src/main/java/com/lighthouse/mapper/BrandPlaceInfoDataContainerMapper.kt b/data-remote/src/main/java/com/lighthouse/data/remote/mapper/BrandPlaceInfoDataContainerMapper.kt similarity index 71% rename from data/src/main/java/com/lighthouse/mapper/BrandPlaceInfoDataContainerMapper.kt rename to data-remote/src/main/java/com/lighthouse/data/remote/mapper/BrandPlaceInfoDataContainerMapper.kt index ce3be3521..e849351d5 100644 --- a/data/src/main/java/com/lighthouse/mapper/BrandPlaceInfoDataContainerMapper.kt +++ b/data-remote/src/main/java/com/lighthouse/data/remote/mapper/BrandPlaceInfoDataContainerMapper.kt @@ -1,9 +1,11 @@ -package com.lighthouse.mapper +package com.lighthouse.data.remote.mapper import com.lighthouse.beep.model.brand.BrandPlaceInfo -import com.lighthouse.model.BrandPlaceInfoDataContainer +import com.lighthouse.data.remote.model.BrandPlaceInfoDataContainer -internal fun List.toDomain(brandName: String): List { +internal fun List.toDomain( + brandName: String +): List { return this.map { BrandPlaceInfo( addressName = it.addressName, diff --git a/data/src/main/java/com/lighthouse/model/BrandPlaceInfoDataContainer.kt b/data-remote/src/main/java/com/lighthouse/data/remote/model/BrandPlaceInfoDataContainer.kt similarity index 94% rename from data/src/main/java/com/lighthouse/model/BrandPlaceInfoDataContainer.kt rename to data-remote/src/main/java/com/lighthouse/data/remote/model/BrandPlaceInfoDataContainer.kt index b08db0f26..93bd2a2e5 100644 --- a/data/src/main/java/com/lighthouse/model/BrandPlaceInfoDataContainer.kt +++ b/data-remote/src/main/java/com/lighthouse/data/remote/model/BrandPlaceInfoDataContainer.kt @@ -1,10 +1,10 @@ -package com.lighthouse.model +package com.lighthouse.data.remote.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class BrandPlaceInfoDataContainer( +internal data class BrandPlaceInfoDataContainer( val documents: List, val meta: Meta ) { diff --git a/data-remote/src/main/java/com/lighthouse/data/remote/repository/BrandRemoteRepositoryImpl.kt b/data-remote/src/main/java/com/lighthouse/data/remote/repository/BrandRemoteRepositoryImpl.kt new file mode 100644 index 000000000..68224b3ce --- /dev/null +++ b/data-remote/src/main/java/com/lighthouse/data/remote/repository/BrandRemoteRepositoryImpl.kt @@ -0,0 +1,38 @@ +package com.lighthouse.data.remote.repository + +import com.lighthouse.beep.model.brand.BrandPlaceInfo +import com.lighthouse.beep.model.error.BeepError +import com.lighthouse.beep.model.location.Dms +import com.lighthouse.common.utils.geography.LocationConverter +import com.lighthouse.data.remote.api.KakaoApiService +import com.lighthouse.data.remote.mapper.toDomain +import com.lighthouse.repository.brand.BrandRemoteRepository +import java.net.UnknownHostException +import javax.inject.Inject + +internal class BrandRemoteRepositoryImpl @Inject constructor( + private val kakaoApiService: KakaoApiService +) : BrandRemoteRepository { + + override suspend fun getBrandPlaceInfo( + brandName: String, + x: Dms, + y: Dms, + size: Int + ): Result> { + val vertex = LocationConverter.getVertex(x, y) + + return try { + val list = kakaoApiService.getAllBrandPlaceInfo( + brandName, + vertex, + size + ).documents.toDomain(brandName) + Result.success(list) + } catch (unknownHostException: UnknownHostException) { + Result.failure(BeepError.NetworkFailure) + } catch (e: Exception) { + Result.failure(e) + } + } +} diff --git a/app/src/main/java/com/lighthouse/di/HTTPRequestInterceptor.kt b/data-remote/src/main/java/com/lighthouse/data/remote/utils/HTTPRequestInterceptor.kt similarity index 74% rename from app/src/main/java/com/lighthouse/di/HTTPRequestInterceptor.kt rename to data-remote/src/main/java/com/lighthouse/data/remote/utils/HTTPRequestInterceptor.kt index ec61f3480..b116b790a 100644 --- a/app/src/main/java/com/lighthouse/di/HTTPRequestInterceptor.kt +++ b/data-remote/src/main/java/com/lighthouse/data/remote/utils/HTTPRequestInterceptor.kt @@ -1,10 +1,10 @@ -package com.lighthouse.di +package com.lighthouse.data.remote.utils -import com.lighthouse.beep.BuildConfig +import com.lighthouse.data.remote.BuildConfig import okhttp3.Interceptor import okhttp3.Response -class HTTPRequestInterceptor : Interceptor { +internal class HTTPRequestInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val origin = chain.request() diff --git a/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSource.kt b/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSource.kt deleted file mode 100644 index 3ee2a3ccd..000000000 --- a/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSource.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.lighthouse.datasource.brand - -import com.lighthouse.beep.model.location.Dms -import com.lighthouse.model.BrandPlaceInfoDataContainer - -interface BrandRemoteDataSource { - - suspend fun getBrandPlaceInfo( - brandName: String, - x: Dms, - y: Dms, - size: Int - ): Result> -} diff --git a/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSourceImpl.kt b/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSourceImpl.kt deleted file mode 100644 index 0c4f457ce..000000000 --- a/data/src/main/java/com/lighthouse/datasource/brand/BrandRemoteDataSourceImpl.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.lighthouse.datasource.brand - -import com.lighthouse.beep.model.location.Dms -import com.lighthouse.common.utils.geography.LocationConverter -import com.lighthouse.model.BeepErrorData -import com.lighthouse.model.BrandPlaceInfoDataContainer -import com.lighthouse.network.NetworkApiService -import java.net.UnknownHostException -import javax.inject.Inject - -class BrandRemoteDataSourceImpl @Inject constructor( - private val networkApiService: NetworkApiService -) : BrandRemoteDataSource { - - override suspend fun getBrandPlaceInfo( - brandName: String, - x: Dms, - y: Dms, - size: Int - ): Result> { - val vertex = LocationConverter.getVertex(x, y) - - val result = runCatching { networkApiService.getAllBrandPlaceInfo(brandName, vertex, size).documents } - return when (val exception = result.exceptionOrNull()) { - null -> result - is UnknownHostException -> Result.failure(BeepErrorData.NetworkFailure) - else -> Result.failure(exception) - } - } -} diff --git a/data/src/main/java/com/lighthouse/mapper/ErrorMapper.kt b/data/src/main/java/com/lighthouse/mapper/ErrorMapper.kt deleted file mode 100644 index 2e986cf7f..000000000 --- a/data/src/main/java/com/lighthouse/mapper/ErrorMapper.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.lighthouse.mapper - -import com.lighthouse.beep.model.error.BeepError -import com.lighthouse.model.BeepErrorData - -internal fun BeepErrorData.toDomain(): BeepError = when (this) { - is BeepErrorData.NetworkFailure -> BeepError.NetworkFailure -} diff --git a/data/src/main/java/com/lighthouse/model/BeepErrorData.kt b/data/src/main/java/com/lighthouse/model/BeepErrorData.kt deleted file mode 100644 index 16d876e89..000000000 --- a/data/src/main/java/com/lighthouse/model/BeepErrorData.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.lighthouse.model - -sealed class BeepErrorData( - override val message: String? = null, - override val cause: Throwable? = null -) : Exception(message, cause) { - - object NetworkFailure : BeepErrorData() -} diff --git a/data/src/main/java/com/lighthouse/repository/brand/BrandRemoteRepository.kt b/data/src/main/java/com/lighthouse/repository/brand/BrandRemoteRepository.kt new file mode 100644 index 000000000..2c89b6d28 --- /dev/null +++ b/data/src/main/java/com/lighthouse/repository/brand/BrandRemoteRepository.kt @@ -0,0 +1,13 @@ +package com.lighthouse.repository.brand + +import com.lighthouse.beep.model.brand.BrandPlaceInfo +import com.lighthouse.beep.model.location.Dms + +interface BrandRemoteRepository { + suspend fun getBrandPlaceInfo( + brandName: String, + x: Dms, + y: Dms, + size: Int + ): Result> +} diff --git a/settings.gradle.kts b/settings.gradle.kts index fa689b4d4..b004316c2 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -37,5 +37,6 @@ includeProject(":common-android", "commons") includeProject(":common-location", "commons") includeProject(":common-recognizer", "commons") includeProject(":data") +includeProject(":data-remote") includeProject(":presentation") includeProject(":domain")