Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<LatLng>.toPreferenceString(): String =
joinToString(";") { "${it.latitude},${it.longitude}" }

Expand All @@ -48,7 +55,7 @@ object PreferenceDataStore {
private val summaryStore
get() = appContext.summaryStore


// 기존 함수들...
suspend fun saveWalkSummary(
points: List<LatLng>,
totalDistance: Float,
Expand Down Expand Up @@ -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<Int> = summaryStore.data.map {
it[SELECTED_GU_ID_KEY] ?: 0
}

fun getSelectedDongId(): Flow<Int> = summaryStore.data.map {
it[SELECTED_DONG_ID_KEY] ?: 0
}

fun getSelectedGuName(): Flow<String> = summaryStore.data.map {
it[SELECTED_GU_NAME_KEY] ?: ""
}

fun getSelectedDongName(): Flow<String> = summaryStore.data.map {
it[SELECTED_DONG_NAME_KEY] ?: ""
}

fun getActiveRegion(): Flow<String> = 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<LocationInfo> = 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() }
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/paw/key/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -157,4 +159,10 @@ interface RepositoryModule {
fun bindPostsListRepository(
impl: PostsListRepositoryImpl
) : PostsListRepository

@Binds
@Singleton
fun bindRegionCurrentRepository(
impl: RegionCurrentRepositoryImpl
) : RegionCurrentRepository
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/paw/key/data/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,4 +116,9 @@ object ServiceModule {
@Singleton
fun providePostsListService(retrofit: Retrofit): PostsListService =
retrofit.create()

@Provides
@Singleton
fun provideRegionCurrentService(retrofit: Retrofit): RegionCurrentService =
retrofit.create()
}
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
@@ -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)
}

Original file line number Diff line number Diff line change
@@ -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<RegionCurrentDataEntity> {
return runCatching {
val response = dataSource.RegionCurrent(userId)
if (response.code == "S000") {
// 올바른 타입 반환 (RegionCurrentDataEntity)
response.data.toEntity() // DTO에서 Entity로 변환
} else {
throw Exception(response.message)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<RegionCurrentResponseDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
@@ -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<RegionCurrentDataEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ fun TapListRoute(
viewModel = viewModel,
onClickLike = {
postId, isLiked ->
viewModel.toggleLike(postId = postId, isLiked = isLiked)
viewModel.toggleLike(
userId = 2,
postId = postId
)
}
)
}
Expand Down Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun HomeScreen(
.fillMaxSize()
) {
HomeTopBar(
location = state.selectedLocation.displayLocation,
location = state.currentRegion.currentName,
onLocationClick = { viewModel.toggleLocationMenu() }
)

Expand Down
Loading