Skip to content

Commit cb043de

Browse files
committed
feat : NetworkResult에 대하여 Repository에서는 모르게 하기 위해서 Result 래퍼로 감싸서 repository에서 에러처리 변경
1 parent 09ee0ad commit cb043de

File tree

6 files changed

+45
-47
lines changed

6 files changed

+45
-47
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package com.stop.data.di
22

3+
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSource
4+
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSourceImpl
35
import com.stop.data.remote.source.route.RouteRemoteDataSource
46
import com.stop.data.remote.source.route.RouteRemoteDataSourceImpl
57
import dagger.Binds
68
import dagger.Module
79
import dagger.hilt.InstallIn
810
import dagger.hilt.components.SingletonComponent
11+
import javax.inject.Singleton
912

1013
@InstallIn(SingletonComponent::class)
1114
@Module
1215
internal interface DataSourceModule {
1316

1417
@Binds
18+
@Singleton
1519
fun provideRouteRemoteDataSource(
1620
routeRemoteDataSourceImpl: RouteRemoteDataSourceImpl
1721
): RouteRemoteDataSource
22+
23+
@Binds
24+
@Singleton
25+
fun provideNearPlaceDataSource(
26+
nearPlaceRemoteDataSourceImpl: NearPlaceRemoteDataSourceImpl
27+
) : NearPlaceRemoteDataSource
28+
1829
}

data/src/main/java/com/stop/data/di/RemoteDataModule.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

data/src/main/java/com/stop/data/di/RepositoryModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ internal interface RepositoryModule {
2222

2323
@Binds
2424
@Singleton
25-
abstract fun provideNearPlaceRepository(
25+
fun provideNearPlaceRepository(
2626
nearPlaceRepositoryImpl: NearPlaceRepositoryImpl
2727
): NearPlaceRepository
28+
2829
}

data/src/main/java/com/stop/data/remote/source/nearplace/NearPlaceRemoteDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ interface NearPlaceRemoteDataSource {
1010
centerLon: Double,
1111
centerLat: Double,
1212
appKey: String
13-
): List<Place>
13+
): Result<List<Place>>
1414

1515
}

data/src/main/java/com/stop/data/remote/source/nearplace/NearPlaceRemoteDataSourceImpl.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ internal class NearPlaceRemoteDataSourceImpl @Inject constructor(
1515
centerLon: Double,
1616
centerLat: Double,
1717
appKey: String
18-
): List<Place> {
18+
): Result<List<Place>> {
1919
val result = nearPlaceApiService.getNearPlaces(
2020
version,
2121
searchKeyword,
2222
centerLon,
2323
centerLat,
2424
appKey
2525
)
26-
27-
when (result) {
28-
is NetworkResult.Failure -> {
29-
throw Exception(result.message)
30-
}
31-
is NetworkResult.Success -> {
32-
return result.data.searchPoiInfo.pois.poi.map {
33-
it.toRepositoryModel()
26+
return runCatching {
27+
when (result) {
28+
is NetworkResult.Failure -> {
29+
throw Exception(result.message)
30+
}
31+
is NetworkResult.Success -> {
32+
result.data.searchPoiInfo.pois.poi.map {
33+
it.toRepositoryModel()
34+
}
35+
}
36+
is NetworkResult.NetworkError -> {
37+
throw result.exception
38+
}
39+
is NetworkResult.Unexpected -> {
40+
throw result.exception
3441
}
35-
}
36-
is NetworkResult.NetworkError -> {
37-
throw result.exception
38-
}
39-
is NetworkResult.Unexpected -> {
40-
throw result.exception
4142
}
4243
}
4344
}

data/src/main/java/com/stop/data/repository/NearPlaceRepositoryImpl.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ internal class NearPlaceRepositoryImpl @Inject constructor(
1515
centerLon: Double,
1616
centerLat: Double,
1717
appKey: String
18-
): List<Place> = nearPlaceRemoteDataSource.getNearPlaces(
19-
version,
20-
searchKeyword,
21-
centerLon,
22-
centerLat,
23-
appKey
24-
).map {
25-
it.toUseCaseModel()
18+
): List<Place> {
19+
nearPlaceRemoteDataSource.getNearPlaces(
20+
version,
21+
searchKeyword,
22+
centerLon,
23+
centerLat,
24+
appKey
25+
).onSuccess { places ->
26+
return places.map {
27+
it.toUseCaseModel()
28+
}
29+
}.onFailure {
30+
throw it
31+
}
32+
return emptyList()
2633
}
27-
2834
}

0 commit comments

Comments
 (0)