Skip to content

Commit 447d9c3

Browse files
committed
feat : 계속 flow 새로생성해서 반환 -> 리스트를 반환하고 viewModel에서 StateFlow로 관리
1 parent 1c14748 commit 447d9c3

File tree

6 files changed

+27
-36
lines changed

6 files changed

+27
-36
lines changed

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
@@ -5,7 +5,7 @@ import com.stop.data.model.nearplace.Place
55
interface NearPlaceRemoteDataSource {
66

77
suspend fun getNearPlaces(
8-
version : Int,
8+
version: Int,
99
searchKeyword: String,
1010
centerLon: Double,
1111
centerLat: Double,

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.stop.data.repository
33
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSource
44
import com.stop.domain.model.nearplace.Place
55
import com.stop.domain.repository.NearPlaceRepository
6-
import kotlinx.coroutines.flow.Flow
7-
import kotlinx.coroutines.flow.flow
86
import javax.inject.Inject
97

108
internal class NearPlaceRepositoryImpl @Inject constructor(
@@ -17,18 +15,14 @@ internal class NearPlaceRepositoryImpl @Inject constructor(
1715
centerLon: Double,
1816
centerLat: Double,
1917
appKey: String
20-
): Flow<List<Place>> = flow {
21-
emit(
22-
nearPlaceRemoteDataSource.getNearPlaces(
23-
version,
24-
searchKeyword,
25-
centerLon,
26-
centerLat,
27-
appKey
28-
).map {
29-
it.toUseCaseModel()
30-
}
31-
)
18+
): List<Place> = nearPlaceRemoteDataSource.getNearPlaces(
19+
version,
20+
searchKeyword,
21+
centerLon,
22+
centerLat,
23+
appKey
24+
).map {
25+
it.toUseCaseModel()
3226
}
3327

3428
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.stop.domain.repository
22

33
import com.stop.domain.model.nearplace.Place
4-
import kotlinx.coroutines.flow.Flow
54

65
interface NearPlaceRepository {
76

@@ -11,6 +10,6 @@ interface NearPlaceRepository {
1110
centerLon: Double,
1211
centerLat: Double,
1312
appKey: String
14-
): Flow<List<Place>>
13+
): List<Place>
1514

1615
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.stop.domain.usecase.nearplace
22

33
import com.stop.domain.model.nearplace.Place
4-
import kotlinx.coroutines.flow.Flow
54

65
interface GetNearPlacesUseCase {
76

@@ -11,6 +10,6 @@ interface GetNearPlacesUseCase {
1110
centerLon: Double,
1211
centerLat: Double,
1312
appKey: String
14-
) : Flow<List<Place>>
13+
): List<Place>
1514

1615
}

domain/src/main/java/com/stop/domain/usecase/nearplace/GetNearPlacesUseCaseImpl.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.stop.domain.usecase.nearplace
22

33
import com.stop.domain.model.nearplace.Place
44
import com.stop.domain.repository.NearPlaceRepository
5-
import kotlinx.coroutines.flow.Flow
65
import javax.inject.Inject
76

87
internal class GetNearPlacesUseCaseImpl @Inject constructor(
@@ -15,7 +14,7 @@ internal class GetNearPlacesUseCaseImpl @Inject constructor(
1514
centerLon: Double,
1615
centerLat: Double,
1716
appKey: String
18-
): Flow<List<Place>> =
17+
): List<Place> =
1918
nearPlaceRepository.getNearPlaces(
2019
version,
2120
searchKeyword,

presentation/src/main/java/com/stop/ui/placesearch/PlaceSearchViewModel.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import com.stop.model.Event
1212
import com.stop.model.Location
1313
import dagger.hilt.android.lifecycle.HiltViewModel
1414
import kotlinx.coroutines.channels.Channel
15-
import kotlinx.coroutines.flow.collectLatest
15+
import kotlinx.coroutines.flow.MutableStateFlow
16+
import kotlinx.coroutines.flow.StateFlow
1617
import kotlinx.coroutines.flow.receiveAsFlow
1718
import kotlinx.coroutines.launch
1819
import javax.inject.Inject
@@ -24,8 +25,8 @@ class PlaceSearchViewModel @Inject constructor(
2425

2526
var currentLocation = Location(0.0, 0.0)
2627

27-
private val _nearPlaceList = MutableLiveData<List<Place>>()
28-
val nearPlaceList: LiveData<List<Place>> = _nearPlaceList
28+
private val _nearPlaceList = MutableStateFlow<List<Place>>(emptyList())
29+
val nearPlaceList: StateFlow<List<Place>> = _nearPlaceList
2930

3031
private val errorMessageChannel = Channel<String>()
3132
val errorMessage = errorMessageChannel.receiveAsFlow()
@@ -36,7 +37,6 @@ class PlaceSearchViewModel @Inject constructor(
3637
private val clickCurrentLocationChannel = Channel<Boolean>()
3738
val clickCurrentLocation = clickCurrentLocationChannel.receiveAsFlow()
3839

39-
4040
fun afterTextChanged(s: Editable?) {
4141
getNearPlaces(
4242
s.toString(),
@@ -45,7 +45,7 @@ class PlaceSearchViewModel @Inject constructor(
4545
)
4646

4747
if (s.toString().isBlank()) {
48-
_nearPlaceList.postValue(emptyList())
48+
setNearPlaceListEmpty()
4949
}
5050
}
5151

@@ -56,15 +56,15 @@ class PlaceSearchViewModel @Inject constructor(
5656
) {
5757
viewModelScope.launch {
5858
try {
59-
getNearPlacesUseCase.getNearPlaces(
60-
TMAP_VERSION,
61-
searchKeyword,
62-
centerLon,
63-
centerLat,
64-
BuildConfig.TMAP_APP_KEY
65-
).collectLatest {
66-
_nearPlaceList.value = it
67-
}
59+
_nearPlaceList.emit(
60+
getNearPlacesUseCase.getNearPlaces(
61+
TMAP_VERSION,
62+
searchKeyword,
63+
centerLon,
64+
centerLat,
65+
BuildConfig.TMAP_APP_KEY
66+
)
67+
)
6868
} catch (e: Exception) {
6969
setNearPlaceListEmpty()
7070
errorMessageChannel.send(e.message ?: "something is wrong")
@@ -73,7 +73,7 @@ class PlaceSearchViewModel @Inject constructor(
7373
}
7474

7575
fun setNearPlaceListEmpty() {
76-
_nearPlaceList.postValue(emptyList())
76+
_nearPlaceList.value = emptyList()
7777
}
7878

7979
fun setClickPlace(place: Place) {

0 commit comments

Comments
 (0)