diff --git a/app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt b/app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt index 9023ba29..4f215e9e 100644 --- a/app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt +++ b/app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt @@ -25,6 +25,13 @@ private val USER_NAME_KEY = stringPreferencesKey("user_name") private val PET_ID_KEY = intPreferencesKey("pet_id") private val PET_NAME_KEY = stringPreferencesKey("pet_name") +// 위치 정보를 위한 새로운 키들 +private val SELECTED_GU_ID_KEY = intPreferencesKey("selected_gu_id") +private val SELECTED_DONG_ID_KEY = intPreferencesKey("selected_dong_id") +private val SELECTED_GU_NAME_KEY = stringPreferencesKey("selected_gu_name") +private val SELECTED_DONG_NAME_KEY = stringPreferencesKey("selected_dong_name") +private val ACTIVE_REGION_KEY = stringPreferencesKey("active_region") + private fun List.toPreferenceString(): String = joinToString(";") { "${it.latitude},${it.longitude}" } @@ -48,7 +55,7 @@ object PreferenceDataStore { private val summaryStore get() = appContext.summaryStore - + // 기존 함수들... suspend fun saveWalkSummary( points: List, totalDistance: Float, @@ -174,6 +181,125 @@ object PreferenceDataStore { } } + // ===== 위치 정보 관련 새로운 함수들 ===== + + /** + * 선택된 위치 정보를 저장합니다 + */ + suspend fun saveLocationInfo( + guId: Int, + dongId: Int, + guName: String, + dongName: String + ) { + summaryStore.edit { preferences -> + preferences[SELECTED_GU_ID_KEY] = guId + preferences[SELECTED_DONG_ID_KEY] = dongId + preferences[SELECTED_GU_NAME_KEY] = guName + preferences[SELECTED_DONG_NAME_KEY] = dongName + } + } + + /** + * 구 정보만 저장합니다 (동 선택 전) + */ + suspend fun saveGuInfo(guId: Int, guName: String) { + summaryStore.edit { preferences -> + preferences[SELECTED_GU_ID_KEY] = guId + preferences[SELECTED_GU_NAME_KEY] = guName + // 구가 변경되면 기존 동 정보 초기화 + preferences.remove(SELECTED_DONG_ID_KEY) + preferences.remove(SELECTED_DONG_NAME_KEY) + } + } + + /** + * 동 정보만 저장합니다 + */ + suspend fun saveDongInfo(dongId: Int, dongName: String) { + summaryStore.edit { preferences -> + preferences[SELECTED_DONG_ID_KEY] = dongId + preferences[SELECTED_DONG_NAME_KEY] = dongName + } + } + + /** + * 활동 지역 정보를 저장합니다 (activeRegion) + */ + suspend fun saveActiveRegion(activeRegion: String) { + summaryStore.edit { preferences -> + preferences[ACTIVE_REGION_KEY] = activeRegion + } + } + + // 개별 조회 함수들 + fun getSelectedGuId(): Flow = summaryStore.data.map { + it[SELECTED_GU_ID_KEY] ?: 0 + } + + fun getSelectedDongId(): Flow = summaryStore.data.map { + it[SELECTED_DONG_ID_KEY] ?: 0 + } + + fun getSelectedGuName(): Flow = summaryStore.data.map { + it[SELECTED_GU_NAME_KEY] ?: "" + } + + fun getSelectedDongName(): Flow = summaryStore.data.map { + it[SELECTED_DONG_NAME_KEY] ?: "" + } + + fun getActiveRegion(): Flow = summaryStore.data.map { + it[ACTIVE_REGION_KEY] ?: "" + } + + // 위치 정보 통합 조회 + data class LocationInfo( + val guId: Int, + val dongId: Int, + val guName: String, + val dongName: String, + val activeRegion: String + ) { + val displayLocation: String + get() = if (guName.isNotEmpty() && dongName.isNotEmpty()) { + "$guName $dongName" + } else if (guName.isNotEmpty()) { + guName + } else { + "위치를 선택해주세요" + } + + val isLocationSelected: Boolean + get() = guId != 0 && dongId != 0 + } + + /** + * 모든 위치 정보를 한번에 조회합니다 + */ + fun getLocationInfo(): Flow = summaryStore.data.map { preferences -> + LocationInfo( + guId = preferences[SELECTED_GU_ID_KEY] ?: 0, + dongId = preferences[SELECTED_DONG_ID_KEY] ?: 0, + guName = preferences[SELECTED_GU_NAME_KEY] ?: "", + dongName = preferences[SELECTED_DONG_NAME_KEY] ?: "", + activeRegion = preferences[ACTIVE_REGION_KEY] ?: "" + ) + } + + /** + * 위치 정보를 초기화합니다 + */ + suspend fun clearLocationInfo() { + summaryStore.edit { preferences -> + preferences.remove(SELECTED_GU_ID_KEY) + preferences.remove(SELECTED_DONG_ID_KEY) + preferences.remove(SELECTED_GU_NAME_KEY) + preferences.remove(SELECTED_DONG_NAME_KEY) + preferences.remove(ACTIVE_REGION_KEY) + } + } + suspend fun clearAllData() { summaryStore.edit { it.clear() } } diff --git a/app/src/main/java/com/paw/key/data/di/RepositoryModule.kt b/app/src/main/java/com/paw/key/data/di/RepositoryModule.kt index b8beeb11..42735e1f 100644 --- a/app/src/main/java/com/paw/key/data/di/RepositoryModule.kt +++ b/app/src/main/java/com/paw/key/data/di/RepositoryModule.kt @@ -15,6 +15,7 @@ import com.paw.key.data.repositoryimpl.WalkSharedResultRepositoryImpl import com.paw.key.data.repositoryimpl.filter.FilterOptionRepositoryImpl import com.paw.key.data.repositoryimpl.sharedwalk.SharedWalkRepositoryImpl import com.paw.key.data.repositoryimpl.home.HomeRegionRepositoryImpl +import com.paw.key.data.repositoryimpl.home.RegionCurrentRepositoryImpl import com.paw.key.data.repositoryimpl.list.PostsListRepositoryImpl import com.paw.key.data.repositoryimpl.walklist.WalkListDetailRepositoryImpl import com.paw.key.data.repositoryimpl.walkreview.WalkReviewRepositoryImpl @@ -30,6 +31,7 @@ import com.paw.key.domain.repository.WalkSharedResultRepository import com.paw.key.domain.repository.filter.FilterOptionRepository import com.paw.key.domain.repository.sharedwalk.SharedWalkRepository import com.paw.key.domain.repository.home.HomeRegionRepository +import com.paw.key.domain.repository.home.RegionCurrentRepository import com.paw.key.domain.repository.list.PostsListRepository import com.paw.key.domain.repository.petprofile.PetProfileRepository import com.paw.key.domain.repository.userprofile.UserProfileRepository @@ -157,4 +159,10 @@ interface RepositoryModule { fun bindPostsListRepository( impl: PostsListRepositoryImpl ) : PostsListRepository + + @Binds + @Singleton + fun bindRegionCurrentRepository( + impl: RegionCurrentRepositoryImpl + ) : RegionCurrentRepository } \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/data/di/ServiceModule.kt b/app/src/main/java/com/paw/key/data/di/ServiceModule.kt index 731acc24..eecd92db 100644 --- a/app/src/main/java/com/paw/key/data/di/ServiceModule.kt +++ b/app/src/main/java/com/paw/key/data/di/ServiceModule.kt @@ -13,6 +13,7 @@ import com.paw.key.data.service.UserProfileService import com.paw.key.data.service.filter.FilterOptionService import com.paw.key.data.service.sharedwalk.SharedWalkService import com.paw.key.data.service.home.HomeRegionService +import com.paw.key.data.service.home.RegionCurrentService import com.paw.key.data.service.list.PostsListService import com.paw.key.data.service.walkcourse.WalkCourseService import com.paw.key.data.service.walklist.WalkListDetailService @@ -115,4 +116,9 @@ object ServiceModule { @Singleton fun providePostsListService(retrofit: Retrofit): PostsListService = retrofit.create() + + @Provides + @Singleton + fun provideRegionCurrentService(retrofit: Retrofit): RegionCurrentService = + retrofit.create() } \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/data/dto/response/home/RegionCurrentResponseDto.kt b/app/src/main/java/com/paw/key/data/dto/response/home/RegionCurrentResponseDto.kt new file mode 100644 index 00000000..5583b420 --- /dev/null +++ b/app/src/main/java/com/paw/key/data/dto/response/home/RegionCurrentResponseDto.kt @@ -0,0 +1,18 @@ +package com.paw.key.data.dto.response.home + +import com.paw.key.domain.model.entity.home.RegionCurrentDataEntity +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class RegionCurrentResponseDto( + @SerialName("currentRegionId") + val currentRegionId: Int, + @SerialName("fullRegionName") + val fullRegionName: String +) { + fun toEntity() = RegionCurrentDataEntity( + currentRegionId = currentRegionId, + fullRegionName = fullRegionName + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/data/remote/datasource/home/RegionCurrentDataSource.kt b/app/src/main/java/com/paw/key/data/remote/datasource/home/RegionCurrentDataSource.kt new file mode 100644 index 00000000..573f7408 --- /dev/null +++ b/app/src/main/java/com/paw/key/data/remote/datasource/home/RegionCurrentDataSource.kt @@ -0,0 +1,14 @@ +package com.paw.key.data.remote.datasource.home + +import com.paw.key.data.dto.request.home.HomeRegionRequest +import com.paw.key.data.service.home.HomeRegionService +import com.paw.key.data.service.home.RegionCurrentService +import javax.inject.Inject + +class RegionCurrentDataSource @Inject constructor( + private val service: RegionCurrentService +) { + suspend fun RegionCurrent(userId: Int) = + service.RegionCurrent(userId) +} + diff --git a/app/src/main/java/com/paw/key/data/repositoryimpl/home/RegionCurrentRepositoryImpl.kt b/app/src/main/java/com/paw/key/data/repositoryimpl/home/RegionCurrentRepositoryImpl.kt new file mode 100644 index 00000000..81035d5a --- /dev/null +++ b/app/src/main/java/com/paw/key/data/repositoryimpl/home/RegionCurrentRepositoryImpl.kt @@ -0,0 +1,23 @@ +package com.paw.key.data.repositoryimpl.home + +import com.paw.key.data.remote.datasource.home.RegionCurrentDataSource +import com.paw.key.domain.model.entity.home.RegionCurrentDataEntity +import com.paw.key.domain.repository.home.RegionCurrentRepository +import javax.inject.Inject + +class RegionCurrentRepositoryImpl @Inject constructor( + private val dataSource: RegionCurrentDataSource, +) : RegionCurrentRepository { + + override suspend fun RegionCurrent(userId: Int): Result { + return runCatching { + val response = dataSource.RegionCurrent(userId) + if (response.code == "S000") { + // 올바른 타입 반환 (RegionCurrentDataEntity) + response.data.toEntity() // DTO에서 Entity로 변환 + } else { + throw Exception(response.message) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/data/service/home/RegionCurrentService.kt b/app/src/main/java/com/paw/key/data/service/home/RegionCurrentService.kt new file mode 100644 index 00000000..a76e8294 --- /dev/null +++ b/app/src/main/java/com/paw/key/data/service/home/RegionCurrentService.kt @@ -0,0 +1,13 @@ +package com.paw.key.data.service.home + +import com.paw.key.data.dto.response.BaseResponse +import com.paw.key.data.dto.response.home.RegionCurrentResponseDto +import retrofit2.http.GET +import retrofit2.http.Header + +interface RegionCurrentService { + @GET("regions/current") + suspend fun RegionCurrent( + @Header("X-USER-ID") userId: Int + ):BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/domain/model/entity/home/HomeEntity.kt b/app/src/main/java/com/paw/key/domain/model/entity/home/HomeEntity.kt index 58f229f0..7dcf031c 100644 --- a/app/src/main/java/com/paw/key/domain/model/entity/home/HomeEntity.kt +++ b/app/src/main/java/com/paw/key/domain/model/entity/home/HomeEntity.kt @@ -2,4 +2,9 @@ package com.paw.key.domain.model.entity.home data class HomeRegionDataEntity( val success: Boolean = true +) + +data class RegionCurrentDataEntity( + val currentRegionId: Int, + val fullRegionName: String ) \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/domain/repository/home/RegionCurrentRepository.kt b/app/src/main/java/com/paw/key/domain/repository/home/RegionCurrentRepository.kt new file mode 100644 index 00000000..db5c32d2 --- /dev/null +++ b/app/src/main/java/com/paw/key/domain/repository/home/RegionCurrentRepository.kt @@ -0,0 +1,8 @@ +package com.paw.key.domain.repository.home + +import com.paw.key.domain.model.entity.home.HomeRegionDataEntity +import com.paw.key.domain.model.entity.home.RegionCurrentDataEntity + +interface RegionCurrentRepository { + suspend fun RegionCurrent(userId: Int): Result +} \ No newline at end of file diff --git a/app/src/main/java/com/paw/key/presentation/ui/course/entire/tab/map/List/TabListScreen.kt b/app/src/main/java/com/paw/key/presentation/ui/course/entire/tab/map/List/TabListScreen.kt index 20067d05..952581c0 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/course/entire/tab/map/List/TabListScreen.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/course/entire/tab/map/List/TabListScreen.kt @@ -49,7 +49,10 @@ fun TapListRoute( viewModel = viewModel, onClickLike = { postId, isLiked -> - viewModel.toggleLike(postId = postId, isLiked = isLiked) + viewModel.toggleLike( + userId = 2, + postId = postId + ) } ) } @@ -118,12 +121,20 @@ fun TabListScreen( title = post.title, petName = post.writer.petName, createdAt = post.createdAt, + isLiked = post.isLike, representativeImageUrl = post.representativeImageUrl, petProfileImageUrl = post.writer.petProfileImageUrl, descriptionTags = post.descriptionTags, - isLiked = post.isLike, onClickLike = { isLiked -> - viewModel.toggleLike(post.postId, isLiked) + viewModel.toggleLike( + userId = 2, + postId = post.postId + ) + onClickLike( + post.postId, + isLiked + + ) }, onClickItem = { navigateToDetail() } diff --git a/app/src/main/java/com/paw/key/presentation/ui/home/HomeLocationSettingScreen.kt b/app/src/main/java/com/paw/key/presentation/ui/home/HomeLocationSettingScreen.kt index ea4631b9..ebab64b4 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/home/HomeLocationSettingScreen.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/home/HomeLocationSettingScreen.kt @@ -1,6 +1,5 @@ package com.paw.key.presentation.ui.home -import android.util.Log import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -14,7 +13,6 @@ import androidx.compose.foundation.layout.width import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -31,12 +29,9 @@ import com.paw.key.core.designsystem.theme.PawKeyTheme import com.paw.key.core.util.noRippleClickable import com.paw.key.presentation.ui.home.viewmodel.HomeViewModel import com.paw.key.presentation.ui.signup.component.FormField -import com.paw.key.presentation.ui.signup.component.LocationButton import com.paw.key.presentation.ui.signup.component.LocationItem import com.paw.key.presentation.ui.signup.component.LocationItemList import com.paw.key.presentation.ui.signup.component.LocationList -import com.paw.key.presentation.ui.signup.component.SignUpHeader -import com.paw.key.presentation.ui.signup.viewmodel.SignUpViewModel @Preview(showBackground = true) @Composable @@ -81,8 +76,9 @@ fun HomeLocationSettingScreen( val state by viewModel.state.collectAsStateWithLifecycle() val regionList by viewModel.regionList.collectAsStateWithLifecycle() - val selectedGu = state.selectedGu - val selectedDong = state.selectedDong + // 올바른 필드 접근 + val selectedGu = state.selectedLocation.selectedGu + val selectedDong = state.selectedLocation.selectedDong val guOptions = regionList.map { it.gu.name } @@ -168,17 +164,8 @@ fun HomeLocationSettingScreen( enabled = isFormValid, onClick = { if (isFormValid) { - /*viewModel.patchRegion( - userId = 2, - onSuccess = { - val selectedDongId = state.selectedDongId - navigateNext(selectedDongId) - }, - onFailure = { message -> - Log.e("HomeScreen", "지역 설정 실패: $message") - } - )*/ - navigateNext(state.selectedDongId) + // 올바른 필드 접근 + navigateNext(state.selectedLocation.selectedDongId) } } ) diff --git a/app/src/main/java/com/paw/key/presentation/ui/home/HomeScreen.kt b/app/src/main/java/com/paw/key/presentation/ui/home/HomeScreen.kt index bf298271..527c1b79 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/home/HomeScreen.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/home/HomeScreen.kt @@ -112,7 +112,7 @@ fun HomeScreen( .fillMaxSize() ) { HomeTopBar( - location = state.selectedLocation.displayLocation, + location = state.currentRegion.currentName, onLocationClick = { viewModel.toggleLocationMenu() } ) diff --git a/app/src/main/java/com/paw/key/presentation/ui/home/state/HomeContract.kt b/app/src/main/java/com/paw/key/presentation/ui/home/state/HomeContract.kt index fbcda2da..451c954e 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/home/state/HomeContract.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/home/state/HomeContract.kt @@ -1,9 +1,11 @@ - package com.paw.key.presentation.ui.home.state import androidx.compose.runtime.Immutable +import com.paw.key.core.util.PreferenceDataStore import com.paw.key.domain.model.entity.archivedlist.ArchivedListEntity import com.paw.key.domain.model.entity.list.ListEntity +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking class HomeContract { @@ -14,10 +16,13 @@ class HomeContract { val selectedLocation: LocationInfo = LocationInfo(), val courseList: List = emptyList(), val uiState: HomeUiState = HomeUiState(), - val selectedGuId: Int = 0, - val selectedDongId: Int = 0, - val selectedGu: String = "", - val selectedDong: String = "" + val currentRegion: CurrentRegionInfo = CurrentRegionInfo() + ) + + @Immutable + data class CurrentRegionInfo( + val currentId: Int = 0, + val currentName: String = "" ) @Immutable @@ -28,12 +33,27 @@ class HomeContract { val selectedDong: String = "" ) { val displayLocation: String - get() = if (selectedGu.isNotEmpty() && selectedDong.isNotEmpty()) { - "$selectedGu $selectedDong" - } else if (selectedGu.isNotEmpty()) { - selectedGu - } else { - "위치를 선택해주세요" + get() = when { + // 구/동이 모두 선택된 경우 + selectedGu.isNotEmpty() && selectedDong.isNotEmpty() -> { + "$selectedGu $selectedDong" + } + // 구만 선택된 경우 + selectedGu.isNotEmpty() -> { + selectedGu + } + // 아무것도 선택되지 않은 경우 - activeRegion 사용 + else -> { + try { + // 코루틴 블로킹 호출 (UI에서는 이미 로드된 상태여야 함) + runBlocking { + val activeRegion = PreferenceDataStore.getActiveRegion().first() + activeRegion.ifEmpty { "위치를 선택해주세요" } + } + } catch (e: Exception) { + "위치를 선택해주세요" + } + } } val isLocationSelected: Boolean diff --git a/app/src/main/java/com/paw/key/presentation/ui/home/viewmodel/HomeViewModel.kt b/app/src/main/java/com/paw/key/presentation/ui/home/viewmodel/HomeViewModel.kt index 189c2df7..a56e27a0 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/home/viewmodel/HomeViewModel.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/home/viewmodel/HomeViewModel.kt @@ -5,6 +5,7 @@ import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.paw.key.core.util.PreferenceDataStore +import com.paw.key.domain.repository.home.RegionCurrentRepository import com.paw.key.domain.repository.onboarding.OnboardingRegionRepository import com.paw.key.presentation.ui.home.state.HomeContract import dagger.hilt.android.lifecycle.HiltViewModel @@ -19,6 +20,7 @@ import javax.inject.Inject @HiltViewModel class HomeViewModel @Inject constructor( private val regionRepository: OnboardingRegionRepository, + private val regionCurrentRepository: RegionCurrentRepository ) : ViewModel() { private val _state = MutableStateFlow(HomeContract.HomeState()) @@ -31,6 +33,51 @@ class HomeViewModel @Inject constructor( init { fetchRegion() +// loadSavedLocationInfo() + regionCurrent() // 현재 지역 정보 가져오기 추가 + } + + /** + * 저장된 위치 정보를 불러와서 상태에 반영 + */ + private fun loadSavedLocationInfo() { + viewModelScope.launch { + try { + // LocationInfo와 ActiveRegion을 모두 가져오기 + val locationInfo = PreferenceDataStore.getLocationInfo().first() + val activeRegion = PreferenceDataStore.getActiveRegion().first() + + Log.d("HomeViewModel", "저장된 위치 정보 불러오기:") + Log.d("HomeViewModel", " - 구: ${locationInfo.guName} (ID: ${locationInfo.guId})") + Log.d("HomeViewModel", " - 동: ${locationInfo.dongName} (ID: ${locationInfo.dongId})") + Log.d("HomeViewModel", " - 활동지역: $activeRegion") + + _state.update { currentState -> + currentState.copy( + selectedLocation = HomeContract.LocationInfo( + selectedGuId = locationInfo.guId, + selectedDongId = locationInfo.dongId, + selectedGu = locationInfo.guName, + selectedDong = locationInfo.dongName + ) + ) + } + + // 위치 정보가 있으면 표시용 로그 + val displayLocation = if (locationInfo.guName.isNotEmpty() && locationInfo.dongName.isNotEmpty()) { + "${locationInfo.guName} ${locationInfo.dongName}" + } else if (activeRegion.isNotEmpty()) { + activeRegion + } else { + "위치를 선택해주세요" + } + + Log.d("HomeViewModel", "TopBar 표시 위치: $displayLocation") + + } catch (e: Exception) { + Log.e("HomeViewModel", "저장된 위치 정보 불러오기 실패: ${e.message}") + } + } } fun toggleLocationMenu() { @@ -42,29 +89,113 @@ class HomeViewModel @Inject constructor( } fun onGuSelected(guName: String, guId: Int) { - _state.update { currentState -> - currentState.copy( - selectedLocation = currentState.selectedLocation.copy( - selectedGu = guName, - selectedGuId = guId, - // 구를 새로 선택하면 기존 동 선택 초기화 - selectedDong = "", - selectedDongId = 0 - ), - // 구 선택 후 메뉴 닫기 - isLocationMenuVisible = false - ) + viewModelScope.launch { + try { + // DataStore에 구 정보 저장 + PreferenceDataStore.saveGuInfo(guId, guName) + + // 상태 업데이트 + _state.update { currentState -> + currentState.copy( + selectedLocation = currentState.selectedLocation.copy( + selectedGu = guName, + selectedGuId = guId, + // 구를 새로 선택하면 기존 동 선택 초기화 + selectedDong = "", + selectedDongId = 0 + ), + // 구 선택 후 메뉴 닫기 + isLocationMenuVisible = false + ) + } + + Log.d("HomeViewModel", "구 선택 완료: $guName (ID: $guId)") + } catch (e: Exception) { + Log.e("HomeViewModel", "구 선택 저장 실패: ${e.message}") + } } } fun onDongSelected(dongName: String, dongId: Int) { - _state.update { currentState -> - currentState.copy( - selectedLocation = currentState.selectedLocation.copy( - selectedDong = dongName, - selectedDongId = dongId + viewModelScope.launch { + try { + // 현재 구 정보와 함께 전체 위치 정보 저장 + val currentLocation = _state.value.selectedLocation + PreferenceDataStore.saveLocationInfo( + guId = currentLocation.selectedGuId, + dongId = dongId, + guName = currentLocation.selectedGu, + dongName = dongName ) - ) + + // 상태 업데이트 + _state.update { currentState -> + currentState.copy( + selectedLocation = currentState.selectedLocation.copy( + selectedDong = dongName, + selectedDongId = dongId + ) + ) + } + + Log.d("HomeViewModel", "동 선택 완료: $dongName (ID: $dongId)") + Log.d("HomeViewModel", "전체 위치: ${currentLocation.selectedGu} $dongName") + } catch (e: Exception) { + Log.e("HomeViewModel", "동 선택 저장 실패: ${e.message}") + } + } + } + + private fun regionCurrent() { + _state.update { it.copy(uiState = it.uiState.copy(isLoading = true)) } + + viewModelScope.launch { + try { + val result = regionCurrentRepository.RegionCurrent(userId.first()) + result.onSuccess { response -> + Log.d("HomeViewModel", "RegionCurrent 성공: ${response.fullRegionName}") + + _state.update { currentState -> + currentState.copy( + currentRegion = HomeContract.CurrentRegionInfo( + currentId = response.currentRegionId, + currentName = response.fullRegionName + ), + uiState = currentState.uiState.copy( + isLoading = false, + error = null + ) + ) + } + try { + PreferenceDataStore.saveActiveRegion(response.fullRegionName) + Log.d("HomeViewModel", "activeRegion 저장 완료: ${response.fullRegionName}") + } catch (e: Exception) { + Log.e("HomeViewModel", "activeRegion 저장 실패: ${e.message}") + } + + }.onFailure { exception -> + Log.e("HomeViewModel", "RegionCurrent 실패: ${exception.message}") + _state.update { currentState -> + currentState.copy( + uiState = currentState.uiState.copy( + isLoading = false, + error = exception.message + ) + ) + } + } + } catch (e: Exception) { + Log.e("HomeViewModel", "RegionCurrent Exception: ${e.message}") + _state.update { currentState -> + currentState.copy( + uiState = currentState.uiState.copy( + isLoading = false, + error = e.message + ) + ) + } + } } } @@ -125,19 +256,8 @@ class HomeViewModel @Inject constructor( ) } -// val updatedCourseList = currentState.courseList.map { course -> -// if (course.postId == postId) { -// // ArchivedListEntity의 실제 프로퍼티명에 맞게 수정 -// // isLiked 대신 isLike 또는 liked 등의 프로퍼티를 확인하고 사용 -// course.copy(isLike = isLiked) // 또는 course.copy(liked = isLiked) -// } else { -// course -// } -// } - currentState.copy( - postsResult = updatedPostsResult, -// courseList = updatedCourseList + postsResult = updatedPostsResult ) } } diff --git a/app/src/main/java/com/paw/key/presentation/ui/mypage/viewmodel/UserProfileViewModel.kt b/app/src/main/java/com/paw/key/presentation/ui/mypage/viewmodel/UserProfileViewModel.kt index 87ba8c09..c64ff693 100644 --- a/app/src/main/java/com/paw/key/presentation/ui/mypage/viewmodel/UserProfileViewModel.kt +++ b/app/src/main/java/com/paw/key/presentation/ui/mypage/viewmodel/UserProfileViewModel.kt @@ -3,8 +3,8 @@ package com.paw.key.presentation.ui.mypage.viewmodel import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.paw.key.core.util.PreferenceDataStore import com.paw.key.domain.repository.userprofile.UserProfileRepository -import com.paw.key.presentation.ui.mypage.state.PetProfileSideEffect import com.paw.key.presentation.ui.mypage.state.UserProfileState import com.paw.key.presentation.ui.mypage.state.UserProfileSideEffect import dagger.hilt.android.lifecycle.HiltViewModel @@ -42,6 +42,13 @@ class UserProfileViewModel @Inject constructor( activeRegion = result.activeRegion ) } + + try { + PreferenceDataStore.saveActiveRegion(result.activeRegion) + Log.d("UserProfileViewModel", "activeRegion 저장 완료: ${result.activeRegion}") + } catch (e: Exception) { + Log.e("UserProfileViewModel", "activeRegion 저장 실패: ${e.message}") + } } .onFailure { e -> Log.e("UserProfileViewModel", "유저 프로필 불러오기 실패", e)