Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
⚡️ :: 수정하기 데이터 불러오기
Browse files Browse the repository at this point in the history
  • Loading branch information
Tmdhoon2 committed Nov 16, 2023
1 parent 17760c2 commit 0a93509
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ internal fun CreatePost(
val details = state.postDetailsEntity

var imagePreview: Uri? by remember { mutableStateOf(null) }

LaunchedEffect(Unit) {
if (feedId != -1L) {
feedViewModel.setFeedId(feedId)
feedViewModel.fetchPostDetails()
with(feedViewModel) {
setFeedId(feedId)
fetchPostDetails()
}
}
}

Expand All @@ -83,7 +85,11 @@ internal fun CreatePost(
attachmentViewModel.sideEffect.collect {
when (it) {
is AttachmentSideEffect.Success -> {
feedViewModel.createPost(imageUrl = fileState.imageUrl)
if (feedId == -1L) {
feedViewModel.createPost(imageUrl = fileState.imageUrl)
} else {
feedViewModel.editPost(imageUrl = fileState.imageUrl)
}
}

is AttachmentSideEffect.Failure -> {
Expand Down Expand Up @@ -113,14 +119,20 @@ internal fun CreatePost(
.padding(horizontal = 16.dp),
) {
Header(
title = stringResource(id = R.string.create_post_header_title),
title = stringResource(
id = if (feedId == -1L) {
R.string.create_post_header_title
} else {
R.string.edit_post_header_title
},
),
onLeadingClicked = moveToBack,
)
Spacer(modifier = Modifier.height(4.dp))
SignalTextField(
value = state.title,
onValueChange = feedViewModel::setTitle,
hint = details.title.ifEmpty { stringResource(id = R.string.create_post_title_hint) },
hint = stringResource(id = R.string.create_post_title_hint),
title = stringResource(id = R.string.create_post_title),
showLength = true,
maxLength = 20,
Expand All @@ -130,7 +142,7 @@ internal fun CreatePost(
modifier = Modifier.fillMaxHeight(0.5f),
value = state.content,
onValueChange = feedViewModel::setContent,
hint = details.content.ifEmpty { stringResource(id = R.string.create_post_content_hint) },
hint = stringResource(id = R.string.create_post_content_hint),
title = stringResource(id = R.string.create_post_content),
alignment = Alignment.Top,
showLength = true,
Expand All @@ -139,7 +151,7 @@ internal fun CreatePost(
)
PostImage(
uri = { imagePreview },
image = { details.image }
imageUrl = { details.image },
) {
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
Expand All @@ -148,7 +160,11 @@ internal fun CreatePost(
text = stringResource(id = R.string.my_page_secession_check),
onClick = {
if (imagePreview == null) {
feedViewModel.createPost()
if (feedId == -1L) {
feedViewModel.createPost()
} else {
feedViewModel.editPost()
}
} else {
attachmentViewModel.uploadFile()
}
Expand All @@ -162,7 +178,7 @@ internal fun CreatePost(
@Composable
private fun PostImage(
uri: () -> Uri?,
image: () -> String?,
imageUrl: () -> String?,
onClick: () -> Unit,
) {
Box(
Expand All @@ -185,7 +201,8 @@ private fun PostImage(
)
AsyncImage(
modifier = Modifier.fillMaxSize(),
model = image() ?: uri(),
model = if (imageUrl().isNullOrBlank()) uri()
else imageUrl(),
contentDescription = stringResource(id = R.string.feed_image),
contentScale = ContentScale.Crop,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.signal.signal_android.feature.main.feed

import android.util.Log
import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.viewModelScope
import com.signal.domain.entity.PostCommentsEntity
Expand Down Expand Up @@ -66,6 +67,8 @@ internal class FeedViewModel(
profile = it.profile,
isMine = it.isMine,
),
title = it.title,
content = it.content,
),
)
}
Expand Down Expand Up @@ -119,17 +122,22 @@ internal class FeedViewModel(
}
}

internal fun editPost() {
internal fun editPost(imageUrl: String? = null) {
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.editPost(
feedId = feedId,
title = title,
image = image,
image = imageUrl ?: image.ifEmpty { postDetailsEntity.image },
content = content,
tag = tag,
).onSuccess {

postSideEffect(FeedSideEffect.PostSuccess)
fetchPosts()
}.onFailure {
if (it is KotlinNullPointerException) {
postSideEffect(FeedSideEffect.PostSuccess)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@

<string name="create_post_image">게시글 이미지</string>

<string name="edit_post_header_title">게시글 수정하기</string>

<!--report-->
<string name="report_header_title">게시글 신고하기</string>
<string name="report_reason">신고 사유</string>
Expand Down

0 comments on commit 0a93509

Please sign in to comment.