Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 상세 피드 / 댓글 생성 #139

Merged
merged 9 commits into from
Aug 21, 2023
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.android.go.sopt.winey.data.model.remote.response

import com.android.go.sopt.winey.domain.entity.Comment
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponsePostCommentDto(
@SerialName("commentId")
val commentId: Long,
@SerialName("commentCounter")
val commentCounter: Long,
@SerialName("authorId")
val authorId: Int,
@SerialName("author")
val author: String,
@SerialName("content")
val content: String,
@SerialName("authorId")
val authorId: Int,
@SerialName("authorLevel")
val authorLevel: Int
)
val authorLevel: Int,
@SerialName("createdAt")
val createdAt: LocalDateTime
) {
fun toComment() = Comment(
commentId = this.commentId,
author = this.author,
content = this.content,
authorLevel = this.authorLevel,
authorId = this.authorId
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.android.go.sopt.winey.data.model.remote.request.RequestPostCommentDto
import com.android.go.sopt.winey.data.model.remote.request.RequestPostLikeDto
import com.android.go.sopt.winey.data.model.remote.response.ResponsePostCommentDto
import com.android.go.sopt.winey.data.model.remote.response.ResponsePostWineyFeedDto
import com.android.go.sopt.winey.data.service.FeedService
import com.android.go.sopt.winey.data.source.FeedDataSource
import com.android.go.sopt.winey.data.source.paging.MyFeedPagingSource
import com.android.go.sopt.winey.data.source.paging.WineyFeedPagingSource
import com.android.go.sopt.winey.domain.entity.Comment
import com.android.go.sopt.winey.domain.entity.DetailFeed
import com.android.go.sopt.winey.domain.entity.Like
import com.android.go.sopt.winey.domain.entity.WineyFeed
Expand Down Expand Up @@ -61,11 +61,11 @@ class FeedRepositoryImpl @Inject constructor(
}

override suspend fun postComment(
feedId: Long,
feedId: Int,
requestPostCommentDto: RequestPostCommentDto
): Result<ResponsePostCommentDto?> =
): Result<Comment?> =
runCatching {
feedDataSource.postComment(feedId, requestPostCommentDto).data
feedDataSource.postComment(feedId, requestPostCommentDto).data?.toComment()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface FeedService {

@POST("comment/{feedId}")
suspend fun postComment(
@Path("feedId") feedId: Long,
@Path("feedId") feedId: Int,
@Body requestPostCommentDto: RequestPostCommentDto
): BaseResponse<ResponsePostCommentDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FeedDataSource @Inject constructor(
feedService.getFeedDetail(feedId)

suspend fun postComment(
feedId: Long,
feedId: Int,
requestPostCommentDto: RequestPostCommentDto
): BaseResponse<ResponsePostCommentDto> =
feedService.postComment(feedId, requestPostCommentDto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class DetailFeed(
val nickName: String,
val userId: Int,
val writerLevel: Int,
val comments: Long,
var comments: Long,
val timeAgo: String,
val commentList: List<Comment>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.android.go.sopt.winey.domain.repository
import androidx.paging.PagingData
import com.android.go.sopt.winey.data.model.remote.request.RequestPostCommentDto
import com.android.go.sopt.winey.data.model.remote.request.RequestPostLikeDto
import com.android.go.sopt.winey.data.model.remote.response.ResponsePostCommentDto
import com.android.go.sopt.winey.data.model.remote.response.ResponsePostWineyFeedDto
import com.android.go.sopt.winey.domain.entity.Comment
import com.android.go.sopt.winey.domain.entity.DetailFeed
import com.android.go.sopt.winey.domain.entity.Like
import com.android.go.sopt.winey.domain.entity.WineyFeed
Expand All @@ -29,7 +29,7 @@ interface FeedRepository {
suspend fun getFeedDetail(feedId: Int): Result<DetailFeed?>

suspend fun postComment(
feedId: Long,
feedId: Int,
requestPostCommentDto: RequestPostCommentDto
): Result<ResponsePostCommentDto?>
): Result<Comment?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import kotlinx.coroutines.flow.onEach

@AndroidEntryPoint
class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main) {
private val viewModel by viewModels<MainViewModel>()
private val mainViewModel by viewModels<MainViewModel>()
private val isUploadSuccess by lazy { intent.extras?.getBoolean(EXTRA_UPLOAD_KEY, false) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// 위니피드, 마이페이지 프래그먼트에서 getUserState 관찰
viewModel.getUser()
mainViewModel.getUser()

initFragment()
initBnvItemSelectedListener()
Expand Down Expand Up @@ -84,7 +84,7 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
}

private fun setupLogoutState() {
viewModel.logoutState.flowWithLifecycle(lifecycle).onEach { state ->
mainViewModel.logoutState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Loading -> {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WineyFeedAdapter(
private val binding: ItemWineyfeedPostBinding,
private val onLikeButtonClick: (feedId: Int, isLiked: Boolean) -> Unit,
private val showPopupMenu: (View, WineyFeed) -> Unit,
private val toFeedDetail: (feedId: Int, writerLevel: Int) -> Unit
private val toFeedDetail: (feedId: Int, writerId: Int) -> Unit
) : RecyclerView.ViewHolder(binding.root) {

fun onBind(data: WineyFeed?) {
Expand All @@ -43,7 +43,7 @@ class WineyFeedAdapter(
}

lWineyfeedPost.setOnSingleClickListener {
toFeedDetail(data.feedId, data.writerLevel)
toFeedDetail(data.feedId, data.userId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class WineyFeedFragment : BindingFragment<FragmentWineyFeedBinding>(R.layout.fra
initNotificationButtonClickListener()
}

// 상세 피드 갔다가 다시 돌아오면 갱신된 데이터가 보이도록
override fun onStart() {
super.onStart()
viewModel.getWineyFeed()
}

private fun initAdapter() {
wineyFeedHeaderAdapter = WineyFeedHeaderAdapter()
wineyFeedLoadAdapter = WineyFeedLoadAdapter()
Expand All @@ -81,7 +87,7 @@ class WineyFeedFragment : BindingFragment<FragmentWineyFeedBinding>(R.layout.fra
showPopupMenu = { view, wineyFeed ->
showPopupMenu(view, wineyFeed)
},
toFeedDetail = { feedId, writerLevel -> navigateToDetail(feedId, writerLevel) }
toFeedDetail = { feedId, writerId -> navigateToDetail(feedId, writerId) }
)
binding.rvWineyfeedPost.adapter = ConcatAdapter(
wineyFeedHeaderAdapter,
Expand Down Expand Up @@ -266,10 +272,10 @@ class WineyFeedFragment : BindingFragment<FragmentWineyFeedBinding>(R.layout.fra
startActivity(intent)
}

private fun navigateToDetail(feedId: Int, writerLevel: Int) {
private fun navigateToDetail(feedId: Int, writerId: Int) {
val intent = Intent(requireContext(), DetailActivity::class.java)
intent.putExtra(KEY_FEED_ID, feedId)
intent.putExtra(KEY_WRITER_LV, writerLevel)
intent.putExtra(KEY_FEED_WRITER_ID, writerId)
startActivity(intent)
}

Expand All @@ -279,6 +285,6 @@ class WineyFeedFragment : BindingFragment<FragmentWineyFeedBinding>(R.layout.fra
private const val TAG_DELETE_DIALOG = "DELETE_DIALOG"

private const val KEY_FEED_ID = "feedId"
private const val KEY_WRITER_LV = "writerLevel"
private const val KEY_FEED_WRITER_ID = "feedWriterId"
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.android.go.sopt.winey.databinding.ItemDetailCommentBinding
import com.android.go.sopt.winey.domain.entity.Comment
import com.android.go.sopt.winey.util.view.ItemDiffCallback

class CommentAdapter : ListAdapter<Comment, CommentAdapter.CommentViewHolder>(diffUtil) {
class CommentViewHolder(
class CommentAdapter(
private val onPopupMenuClicked: (View, Int) -> Unit
) : ListAdapter<Comment, CommentAdapter.CommentViewHolder>(diffUtil) {
inner class CommentViewHolder(
private val binding: ItemDetailCommentBinding
) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data: Comment) {
fun onBind(comment: Comment) {
binding.apply {
this.data = data
executePendingBindings()
this.data = comment

// 팝업 메뉴 버튼을 클릭하면, 해당 버튼의 뷰와 댓글 작성자의 아이디를 전달한다.
ivCommentMore.setOnClickListener { view ->
onPopupMenuClicked(view, comment.authorId)
}
}
}
}
Expand All @@ -31,6 +38,19 @@ class CommentAdapter : ListAdapter<Comment, CommentAdapter.CommentViewHolder>(di
holder.onBind(getItem(position))
}

fun addItem(item: Comment): Int {
val newList = currentList.toMutableList()
newList.add(item)
submitList(newList)
return newList.size
}

fun deleteItem(position: Int) {
val newList = currentList.toMutableList()
newList.removeAt(position)
submitList(newList)
}

companion object {
private val diffUtil = ItemDiffCallback<Comment>(
onItemsTheSame = { old, new -> old.commentId == new.commentId },
Expand Down
Loading