Skip to content

Commit

Permalink
[fix/#144] 이미지 오류 처리 및 api 성공 시, 좋아요 스크랩 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokwanhee committed Aug 27, 2024
1 parent 0bbcc1c commit 2992d30
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fun StadiumReviewContent(
)
)
),
error = painterResource(id = com.depromeet.designsystem.R.drawable.ic_default_profile),
modifier = Modifier
.size(20.dp)
.clip(CircleShape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.BrushPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
Expand Down Expand Up @@ -140,6 +141,7 @@ fun DetailContentHeader(
)
)
),
error = painterResource(id = com.depromeet.designsystem.R.drawable.ic_default_profile),
modifier = Modifier
.size(20.dp)
.clip(CircleShape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

enum class Sort {
Expand Down Expand Up @@ -67,120 +68,132 @@ class StadiumDetailViewModel @Inject constructor(

fun updateLike(id: Long) {
viewModelScope.launch {
viewfinderRepository.updateLike(id.toInt())
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.reviews.map { review ->
if (review.id == id) {
if (review.isLike) {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount - 1
)
viewfinderRepository.updateLike(id.toInt()).onSuccess {
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.reviews.map { review ->
if (review.id == id) {
if (review.isLike) {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount - 1
)
} else {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount + 1
)
}
} else {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount + 1
)
review
}
} else {
review
}
currentState.copy(reviews = updatedReviews)
}
currentState.copy(reviews = updatedReviews)
}

else -> currentState
else -> currentState
}
}.onFailure {
Timber.e("error : ${it.message}")
}
}
}

fun updateTopReviewLike(id: Long) {
viewModelScope.launch {
viewfinderRepository.updateLike(id.toInt())
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.topReviewImages.map { review ->
if (review.id == id) {
if (review.isLike) {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount - 1
)
viewfinderRepository.updateLike(id.toInt()).onSuccess {
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.topReviewImages.map { review ->
if (review.id == id) {
if (review.isLike) {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount - 1
)
} else {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount + 1
)
}
} else {
review.copy(
isLike = !review.isLike,
likesCount = review.likesCount + 1
)
review
}
} else {
review
}
currentState.copy(topReviewImages = updatedReviews)
}
currentState.copy(topReviewImages = updatedReviews)
}

else -> currentState
else -> currentState
}
}.onFailure {
Timber.e("error : ${it.message}")
}
}
}

fun updateScrap(id: Long) {
viewModelScope.launch {
viewfinderRepository.updateScrap(id.toInt())
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.reviews.map { review ->
if (review.id == id) {
if (review.isScrap) {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount - 1
)
viewfinderRepository.updateScrap(id.toInt()).onSuccess {
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.reviews.map { review ->
if (review.id == id) {
if (review.isScrap) {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount - 1
)
} else {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount + 1
)
}
} else {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount + 1
)
review
}
} else {
review
}
currentState.copy(reviews = updatedReviews)
}
currentState.copy(reviews = updatedReviews)
}

else -> currentState
else -> currentState
}
}.onFailure {
Timber.e("error : ${it.message}")
}
}
}

fun updateTopReviewScrap(id: Long) {
viewModelScope.launch {
viewfinderRepository.updateScrap(id.toInt())
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.topReviewImages.map { review ->
if (review.id == id) {
if (review.isScrap) {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount - 1
)
viewfinderRepository.updateScrap(id.toInt()).onSuccess {
_detailUiState.value = when (val currentState = _detailUiState.value) {
is StadiumDetailUiState.ReviewsData -> {
val updatedReviews = currentState.topReviewImages.map { review ->
if (review.id == id) {
if (review.isScrap) {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount - 1
)
} else {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount + 1
)
}
} else {
review.copy(
isScrap = !review.isScrap,
scrapsCount = review.scrapsCount + 1
)
review
}
} else {
review
}
currentState.copy(topReviewImages = updatedReviews)
}
currentState.copy(topReviewImages = updatedReviews)
}

else -> currentState
else -> currentState
}
}.onFailure {
Timber.e("error : ${it.message}")
}
}
}
Expand Down

0 comments on commit 2992d30

Please sign in to comment.