From 33211885176ca9adefc1aa3580925c58ac5cc6a0 Mon Sep 17 00:00:00 2001 From: gaeulzzang Date: Fri, 8 Nov 2024 20:26:32 +0900 Subject: [PATCH] =?UTF-8?q?#30=20[FEAT]=20=EC=A7=80=EB=8F=84=20api=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/di/DataSourceModule.kt | 6 + .../app/di/RepositoryModule.kt | 6 + .../alddeul_babsang/app/di/ServiceModule.kt | 7 + .../data/datasource/MapDataSource.kt | 10 ++ .../data/datasourceimpl/MapDataSourceImpl.kt | 20 +++ .../dto/response/ResponseMapStoreDetailDto.kt | 15 ++ .../data/dto/response/ResponseMapStoresDto.kt | 15 ++ .../data/repositoryimpl/MapRepositoryImpl.kt | 23 +++ .../data/service/ApiKeyStorage.kt | 1 + .../data/service/MapApiService.kt | 20 +++ .../domain/repository/MapRepository.kt | 9 + .../presentation/map/screen/MapScreen.kt | 162 +++++++++++------- .../presentation/map/screen/MapViewModel.kt | 45 ++++- 13 files changed, 276 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/datasource/MapDataSource.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/MapDataSourceImpl.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoreDetailDto.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoresDto.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/repositoryimpl/MapRepositoryImpl.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/data/service/MapApiService.kt create mode 100644 app/src/main/java/com/hackathon/alddeul_babsang/domain/repository/MapRepository.kt diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt index 99fa87a..e071bad 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt @@ -3,11 +3,13 @@ package com.hackathon.alddeul_babsang.app.di import com.hackathon.alddeul_babsang.data.datasource.BabsangDataSource import com.hackathon.alddeul_babsang.data.datasource.DetailDataSource import com.hackathon.alddeul_babsang.data.datasource.ExampleDataSource +import com.hackathon.alddeul_babsang.data.datasource.MapDataSource import com.hackathon.alddeul_babsang.data.datasource.ProfileDataSource import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource import com.hackathon.alddeul_babsang.data.datasourceimpl.BabsangDataSourceImpl import com.hackathon.alddeul_babsang.data.datasourceimpl.DetailDataSourceImpl import com.hackathon.alddeul_babsang.data.datasourceimpl.ExampleDataSourceImpl +import com.hackathon.alddeul_babsang.data.datasourceimpl.MapDataSourceImpl import com.hackathon.alddeul_babsang.data.datasourceimpl.ProfileDataSourceImpl import com.hackathon.alddeul_babsang.data.datasourceimpl.UserPreferencesDataSourceImpl import dagger.Binds @@ -39,4 +41,8 @@ abstract class DataSourceModule { @Binds @Singleton abstract fun bindDetailDataSource(detailDataSourceImpl: DetailDataSourceImpl): DetailDataSource + + @Binds + @Singleton + abstract fun bindMapDataSource(mapDataSourceImpl: MapDataSourceImpl): MapDataSource } \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt index 8fd7c27..1f76552 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt @@ -3,11 +3,13 @@ package com.hackathon.alddeul_babsang.app.di import com.hackathon.alddeul_babsang.data.repositoryimpl.BabsangRepositoryImpl import com.hackathon.alddeul_babsang.data.repositoryimpl.DetailRepositoryImpl import com.hackathon.alddeul_babsang.data.repositoryimpl.ExampleRepositoryImpl +import com.hackathon.alddeul_babsang.data.repositoryimpl.MapRepositoryImpl import com.hackathon.alddeul_babsang.data.repositoryimpl.ProfileRepositoryImpl import com.hackathon.alddeul_babsang.data.repositoryimpl.UserPreferencesRepositoryImpl import com.hackathon.alddeul_babsang.domain.repository.BabsangRepository import com.hackathon.alddeul_babsang.domain.repository.DetailRepository import com.hackathon.alddeul_babsang.domain.repository.ExampleRepository +import com.hackathon.alddeul_babsang.domain.repository.MapRepository import com.hackathon.alddeul_babsang.domain.repository.ProfileRepository import com.hackathon.alddeul_babsang.domain.repository.UserPreferencesRepository import dagger.Binds @@ -39,4 +41,8 @@ abstract class RepositoryModule { @Binds @Singleton abstract fun bindDetailRepository(detailRepositoryImpl: DetailRepositoryImpl): DetailRepository + + @Binds + @Singleton + abstract fun bindMapRepository(mapRepositoryImpl: MapRepositoryImpl): MapRepository } \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt index e10276a..048292c 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt @@ -2,6 +2,7 @@ package com.hackathon.alddeul_babsang.app.di import com.hackathon.alddeul_babsang.data.service.BabsangApiService import com.hackathon.alddeul_babsang.data.service.DetailApiService +import com.hackathon.alddeul_babsang.data.service.MapApiService import com.hackathon.alddeul_babsang.data.service.ProfileApiService import com.sopt.data.service.ExampleApiService import dagger.Module @@ -38,4 +39,10 @@ object ServiceModule { fun provideDetailService( @AlddeulRetrofit retrofit: Retrofit ): DetailApiService = retrofit.create(DetailApiService::class.java) + + @Provides + @Singleton + fun provideMapService( + @AlddeulRetrofit retrofit: Retrofit + ): MapApiService = retrofit.create(MapApiService::class.java) } diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/datasource/MapDataSource.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasource/MapDataSource.kt new file mode 100644 index 0000000..817ea3e --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasource/MapDataSource.kt @@ -0,0 +1,10 @@ +package com.hackathon.alddeul_babsang.data.datasource + +import com.hackathon.alddeul_babsang.data.dto.BaseResponse +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto + +interface MapDataSource { + suspend fun getMapStores(): BaseResponse> + suspend fun getMapStoreDetail(id: Long): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/MapDataSourceImpl.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/MapDataSourceImpl.kt new file mode 100644 index 0000000..cc27ceb --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/MapDataSourceImpl.kt @@ -0,0 +1,20 @@ +package com.hackathon.alddeul_babsang.data.datasourceimpl + +import com.hackathon.alddeul_babsang.data.datasource.MapDataSource +import com.hackathon.alddeul_babsang.data.dto.BaseResponse +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto +import com.hackathon.alddeul_babsang.data.service.MapApiService +import javax.inject.Inject + +class MapDataSourceImpl @Inject constructor( + private val mapApiService: MapApiService +) : MapDataSource { + override suspend fun getMapStores(): BaseResponse> { + return mapApiService.getMapStores() + } + + override suspend fun getMapStoreDetail(id: Long): BaseResponse { + return mapApiService.getMapStoreDetail(id) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoreDetailDto.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoreDetailDto.kt new file mode 100644 index 0000000..3646b9b --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoreDetailDto.kt @@ -0,0 +1,15 @@ +package com.hackathon.alddeul_babsang.data.dto.response + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ResponseMapStoreDetailDto ( + @SerialName("id") val id: Long, + @SerialName("name") val name: String, + @SerialName("category") val category: String, + @SerialName("address") val address: String, + @SerialName("contact") val contact: String, + @SerialName("imageUrl") val imageUrl: String? = null, + @SerialName("favorite") val favorite: Boolean, +) \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoresDto.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoresDto.kt new file mode 100644 index 0000000..1456d6c --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/dto/response/ResponseMapStoresDto.kt @@ -0,0 +1,15 @@ +package com.hackathon.alddeul_babsang.data.dto.response + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ResponseMapStoresDto ( + @SerialName("storeId") val storeId: Long, + @SerialName("name") val name: String, + @SerialName("category") val category: String, + @SerialName("address") val address: String, + @SerialName("region") val region: String, + @SerialName("latitude") val latitude: Double, + @SerialName("longitude") val longitude: Double, +) \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/repositoryimpl/MapRepositoryImpl.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/repositoryimpl/MapRepositoryImpl.kt new file mode 100644 index 0000000..26e04c6 --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/repositoryimpl/MapRepositoryImpl.kt @@ -0,0 +1,23 @@ +package com.hackathon.alddeul_babsang.data.repositoryimpl + +import com.hackathon.alddeul_babsang.data.datasource.MapDataSource +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto +import com.hackathon.alddeul_babsang.domain.repository.MapRepository +import javax.inject.Inject + +class MapRepositoryImpl @Inject constructor( + private val mapDataSource: MapDataSource +) : MapRepository { + override suspend fun getMapStores(): Result> { + return runCatching { + mapDataSource.getMapStores().result ?: emptyList() + } + } + + override suspend fun getMapStoreDetail(id: Long): Result { + return runCatching { + mapDataSource.getMapStoreDetail(id).result + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/service/ApiKeyStorage.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/service/ApiKeyStorage.kt index 20eec7a..12ae1cf 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/data/service/ApiKeyStorage.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/service/ApiKeyStorage.kt @@ -20,4 +20,5 @@ object ApiKeyStorage { const val LOGOUT = "logout" const val USER_ID = "userId" const val STORE_ID = "storeId" + const val ID = "id" } \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/service/MapApiService.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/service/MapApiService.kt new file mode 100644 index 0000000..8cae160 --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/service/MapApiService.kt @@ -0,0 +1,20 @@ +package com.hackathon.alddeul_babsang.data.service + +import com.hackathon.alddeul_babsang.data.dto.BaseResponse +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto +import com.sopt.data.service.ApiKeyStorage.ID +import com.sopt.data.service.ApiKeyStorage.MAP +import com.sopt.data.service.ApiKeyStorage.STORES +import retrofit2.http.GET +import retrofit2.http.Path + +interface MapApiService { + @GET("/$MAP/$STORES") + suspend fun getMapStores(): BaseResponse> + + @GET("/$MAP/$STORES/{$ID}") + suspend fun getMapStoreDetail( + @Path("id") id: Long + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/domain/repository/MapRepository.kt b/app/src/main/java/com/hackathon/alddeul_babsang/domain/repository/MapRepository.kt new file mode 100644 index 0000000..05485bd --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/domain/repository/MapRepository.kt @@ -0,0 +1,9 @@ +package com.hackathon.alddeul_babsang.domain.repository + +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto + +interface MapRepository { + suspend fun getMapStores(): Result> + suspend fun getMapStoreDetail(id: Long): Result +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapScreen.kt b/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapScreen.kt index 7f059c9..80254d4 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapScreen.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapScreen.kt @@ -18,6 +18,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.SideEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -35,6 +36,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.accompanist.systemuicontroller.rememberSystemUiController import com.hackathon.alddeul_babsang.R import com.hackathon.alddeul_babsang.core_ui.theme.Blue @@ -45,7 +47,9 @@ import com.hackathon.alddeul_babsang.core_ui.theme.White import com.hackathon.alddeul_babsang.core_ui.theme.body2Regular import com.hackathon.alddeul_babsang.core_ui.theme.body4Regular import com.hackathon.alddeul_babsang.core_ui.theme.head4Bold +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto import com.hackathon.alddeul_babsang.presentation.map.navigation.MapNavigator +import com.hackathon.alddeul_babsang.util.UiState import com.naver.maps.geometry.LatLng import com.naver.maps.map.CameraAnimation import com.naver.maps.map.CameraPosition @@ -70,6 +74,7 @@ import com.naver.maps.map.overlay.Marker import com.naver.maps.map.overlay.Overlay import com.naver.maps.map.overlay.OverlayImage import com.naver.maps.map.util.MarkerIcons +import timber.log.Timber @Composable fun MapRoute( @@ -77,6 +82,8 @@ fun MapRoute( ) { val mapViewModel: MapViewModel = hiltViewModel() val systemUiController = rememberSystemUiController() + val getMapStoresState by mapViewModel.getMapStoresState.collectAsStateWithLifecycle(UiState.Empty) + var data = emptyList() SideEffect { systemUiController.setStatusBarColor( @@ -84,7 +91,22 @@ fun MapRoute( ) } + LaunchedEffect(Unit) { + mapViewModel.getMapStores() + } + + when (getMapStoresState) { + is UiState.Success -> { + data = (getMapStoresState as UiState.Success).data + } + is UiState.Failure -> { + Timber.e((getMapStoresState as UiState.Failure).msg) + } + else -> {} + } + MapScreen( + mapEntityList = data, mapViewModel = mapViewModel, onDetailClick = { id -> navigator.navigateDetail(id) } ) @@ -93,6 +115,7 @@ fun MapRoute( @OptIn(ExperimentalNaverMapApi::class) @Composable fun MapScreen( + mapEntityList: List = emptyList(), mapViewModel: MapViewModel, onDetailClick: (Long) -> Unit, ) { @@ -110,13 +133,13 @@ fun MapScreen( MapUiSettings(isLocationButtonEnabled = true) ) } - val mapEntityList = mapViewModel.mockMapList val cameraPositionState = rememberCameraPositionState { position = CameraPosition(NaverMapConstants.DefaultCameraPosition.target, 10.0) } var showBabsangDetail by remember { mutableStateOf(false) } var selectedBabsangDetail by remember { mutableStateOf(null) } val context = LocalContext.current + val getMapStoreDetailState by mapViewModel.getMapStoreDetailState.collectAsStateWithLifecycle(UiState.Empty) Box( modifier = Modifier @@ -162,7 +185,7 @@ fun MapScreen( if (cluster.maxZoom <= 9) { null } else { - MapData(-1, "", "", (cluster.children.first().tag as MapData).gu, "", "") + MapData(-1, "", "", (cluster.children.first().tag as MapData).gu, "") } } .markerManager(object : DefaultMarkerManager() { @@ -221,13 +244,12 @@ fun MapScreen( // MapEntity 데이터를 Clusterer에 추가 val keyTagMap = mapEntityList.associate { - ItemKey(it.id.toInt(), LatLng(it.latitude, it.longitude)) to MapData( - id = it.id, + ItemKey(it.storeId.toInt(), LatLng(it.latitude, it.longitude)) to MapData( + id = it.storeId, name = it.name, - codeName = it.codeName, - gu = it.gu, - address = it.address, - phone = it.phone + codeName = it.category, + gu = it.region, + address = it.address ) } clusterManager?.addAll(keyTagMap) @@ -239,73 +261,89 @@ fun MapScreen( } if (showBabsangDetail && selectedBabsangDetail != null) { val data = selectedBabsangDetail ?: throw IllegalStateException("Data is null") - Surface( - modifier = Modifier - .padding(horizontal = 18.dp, vertical = 12.dp) - .align(Alignment.BottomCenter) - .shadow( - elevation = 4.dp, - shape = RoundedCornerShape(14.dp) - ) - .clickable { onDetailClick(data.id) } - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .background( - color = White, - shape = RoundedCornerShape(14.dp) - ) - .padding(20.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Image( + mapViewModel.getMapStoreDetail(data.id) + + when (getMapStoreDetailState) { + is UiState.Success -> { + val detailData = (getMapStoreDetailState as UiState.Success).data ?: return + Surface( modifier = Modifier - .size(70.dp) - .clip(CircleShape), - painter = painterResource(id = R.drawable.ic_launcher_background), - contentDescription = null, - contentScale = ContentScale.FillBounds - ) - Spacer(modifier = Modifier.width(14.dp)) - Column { + .padding(horizontal = 18.dp, vertical = 12.dp) + .align(Alignment.BottomCenter) + .shadow( + elevation = 4.dp, + shape = RoundedCornerShape(14.dp) + ) + .clickable { onDetailClick(data.id) } + ) { Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.Bottom + modifier = Modifier + .fillMaxWidth() + .background( + color = White, + shape = RoundedCornerShape(14.dp) + ) + .padding(20.dp), + verticalAlignment = Alignment.CenterVertically ) { - Text( - text = data.name, - style = head4Bold, - color = Orange900 - ) - Spacer(modifier = Modifier.weight(1f)) - Text( - text = data.codeName, - style = body2Regular, - color = Orange800 + Image( + modifier = Modifier + .size(70.dp) + .clip(CircleShape), + painter = painterResource(id = R.drawable.ic_launcher_background), + contentDescription = null, + contentScale = ContentScale.FillBounds ) + Spacer(modifier = Modifier.width(14.dp)) + Column { + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.Bottom + ) { + Text( + text = detailData.name, + style = head4Bold, + color = Orange900 + ) + Spacer(modifier = Modifier.weight(1f)) + Text( + text = when(detailData.category) { + "KOREAN" -> "한식" + "CHINESE" -> "중식" + "WESTERN_JAPANESE" -> "경양식/일식" + else -> "기타외식업" + }, + style = body2Regular, + color = Orange800 + ) + } + Spacer(modifier = Modifier.height(4.dp)) + Text( + text = detailData.address, + style = body4Regular, + color = Gray300 + ) + Text( + modifier = Modifier.padding(top = 4.dp), + text = detailData.contact, + style = body4Regular, + color = Gray300 + ) + } } - Spacer(modifier = Modifier.height(4.dp)) - Text( - text = data.address, - style = body4Regular, - color = Gray300 - ) - Text( - modifier = Modifier.padding(top = 4.dp), - text = data.phone, - style = body4Regular, - color = Gray300 - ) } } + is UiState.Failure -> { + Timber.e((getMapStoreDetailState as UiState.Failure).msg) + } + else -> {} } } } } -private class MapData(val id: Long, val name: String, val codeName: String, val gu: String, val address: String, val phone: String) +private class MapData(val id: Long, val name: String, val codeName: String, val gu: String, val address: String) @Preview(showBackground = true) @Composable diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapViewModel.kt b/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapViewModel.kt index 93edef9..d9e77f6 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapViewModel.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/presentation/map/screen/MapViewModel.kt @@ -1,12 +1,55 @@ package com.hackathon.alddeul_babsang.presentation.map.screen import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoreDetailDto +import com.hackathon.alddeul_babsang.data.dto.response.ResponseMapStoresDto import com.hackathon.alddeul_babsang.domain.entity.MapEntity +import com.hackathon.alddeul_babsang.domain.repository.MapRepository +import com.hackathon.alddeul_babsang.util.UiState import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel -class MapViewModel @Inject constructor() : ViewModel() { +class MapViewModel @Inject constructor( + private val mapRepository: MapRepository +) : ViewModel() { + private val _getMapStoresState = + MutableStateFlow>>(UiState.Empty) + val getMapStoresState: StateFlow>> = _getMapStoresState + + private val _getMapStoreDetailState = + MutableStateFlow>(UiState.Empty) + val getMapStoreDetailState: StateFlow> = + _getMapStoreDetailState + + fun getMapStores() = viewModelScope.launch { + _getMapStoresState.emit(UiState.Loading) + mapRepository.getMapStores().fold( + onSuccess = { + _getMapStoresState.emit(UiState.Success(it)) + }, + onFailure = { + _getMapStoresState.emit(UiState.Failure(it.message.toString())) + } + ) + } + + fun getMapStoreDetail(id: Long) = viewModelScope.launch { + _getMapStoreDetailState.emit(UiState.Loading) + mapRepository.getMapStoreDetail(id).fold( + onSuccess = { + _getMapStoreDetailState.emit(UiState.Success(it)) + }, + onFailure = { + _getMapStoreDetailState.emit(UiState.Failure(it.message.toString())) + } + ) + } + val mockMapList = listOf( MapEntity( id = 1,