Skip to content

Commit

Permalink
[Feat]: 时间线吐槽详情和回复功能
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 23, 2023
1 parent 90562d0 commit 8e4224b
Show file tree
Hide file tree
Showing 18 changed files with 480 additions and 300 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.xiaoyv.bangumi.ui.timeline.page

import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.xiaoyv.bangumi.ui.timeline.page.binder.TimelineGridBinder
import com.xiaoyv.bangumi.ui.timeline.page.binder.TimelineMediaBinder
import com.xiaoyv.bangumi.ui.timeline.page.binder.TimelineTextBinder
import android.content.Context
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseDifferAdapter
import com.xiaoyv.bangumi.databinding.FragmentTimelinePageGridBinding
import com.xiaoyv.bangumi.databinding.FragmentTimelinePageMediaBinding
import com.xiaoyv.bangumi.databinding.FragmentTimelinePageTextBinding
import com.xiaoyv.common.api.parser.entity.TimelineEntity
import com.xiaoyv.common.config.annotation.TimelineAdapterType
import com.xiaoyv.common.helper.callback.IdDiffItemCallback
import com.xiaoyv.common.kts.inflater
import com.xiaoyv.common.kts.loadImageAnimate
import com.xiaoyv.common.widget.image.AnimeGridImageView
import com.xiaoyv.widget.binder.BaseQuickBindingHolder

/**
* Class: [TimelinePageAdapter]
Expand All @@ -15,12 +24,106 @@ import com.xiaoyv.common.widget.image.AnimeGridImageView
* @since 11/25/23
*/
class TimelinePageAdapter(
onGridItemClickListener: ((AnimeGridImageView.Image) -> Unit)? = null
) : BaseMultiItemAdapter<TimelineEntity>() {
init {
this.addItemType(TimelineAdapterType.TYPE_GRID, TimelineGridBinder(onGridItemClickListener))
.addItemType(TimelineAdapterType.TYPE_TEXT, TimelineTextBinder())
.addItemType(TimelineAdapterType.TYPE_MEDIA, TimelineMediaBinder())
.onItemViewType { position, list -> list[position].adapterType }
private val onGridItemClickListener: ((AnimeGridImageView.Image) -> Unit)? = null,
) : BaseDifferAdapter<TimelineEntity, RecyclerView.ViewHolder>(IdDiffItemCallback()) {
private val viewPool by lazy { RecyclerView.RecycledViewPool() }

override fun onBindViewHolder(
holder: RecyclerView.ViewHolder,
position: Int,
item: TimelineEntity?,
) {
item ?: return
when (val binding = (holder as BaseQuickBindingHolder<*>).binding) {
is FragmentTimelinePageGridBinding -> {
onBindGrid(binding, item, onGridItemClickListener)
}

is FragmentTimelinePageTextBinding -> {
onBindText(binding, item)
}

is FragmentTimelinePageMediaBinding -> {
onBindMedia(binding, item)
}

else -> throw IllegalArgumentException("error item")
}
}

override fun getItemViewType(position: Int, list: List<TimelineEntity>): Int {
return list[position].adapterType
}

override fun onCreateViewHolder(
context: Context,
parent: ViewGroup,
viewType: Int,
): RecyclerView.ViewHolder {
return when (viewType) {
TimelineAdapterType.TYPE_GRID -> BaseQuickBindingHolder(
FragmentTimelinePageGridBinding.inflate(context.inflater, parent, false)
.apply {
imageGrid.setRecycledViewPool(viewPool)
}
)

TimelineAdapterType.TYPE_TEXT -> BaseQuickBindingHolder(
FragmentTimelinePageTextBinding.inflate(context.inflater, parent, false)
)

TimelineAdapterType.TYPE_MEDIA -> BaseQuickBindingHolder(
FragmentTimelinePageMediaBinding.inflate(context.inflater, parent, false)
)

else -> throw IllegalArgumentException("error type: $viewType")
}
}

companion object {

private fun onBindGrid(
binding: FragmentTimelinePageGridBinding,
item: TimelineEntity,
onGridItemClickListener: ((AnimeGridImageView.Image) -> Unit)?,
) {
binding.ivAvatar.isInvisible = item.avatar.isBlank()
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.tvAction.text = item.title
binding.tvContent.text = item.content
binding.tvContent.isVisible = item.content.isNotBlank()
binding.tvTime.text = item.time

binding.imageGrid.images = item.gridCard
binding.imageGrid.onGridItemClickListener = onGridItemClickListener
}

private fun onBindMedia(binding: FragmentTimelinePageMediaBinding, item: TimelineEntity) {
binding.ivAvatar.isInvisible = item.avatar.isBlank()
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.tvAction.text = item.title
binding.tvContent.text = item.content
binding.tvContent.isVisible = item.content.isNotBlank()
binding.tvTime.text = item.time

binding.ivMediaScore.rating = item.mediaCard.score / 2f
binding.ivMediaScore.isVisible = item.mediaCard.score != 0f
binding.tvMediaName.text = item.mediaCard.title
binding.tvMediaDesc.text = item.mediaCard.info
binding.ivMediaCover.loadImageAnimate(item.mediaCard.cover)
}

internal fun onBindText(binding: FragmentTimelinePageTextBinding, item: TimelineEntity) {
binding.ivAvatar.isInvisible = item.avatar.isBlank()
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.tvAction.text = item.title
binding.tvContent.text = item.content
binding.tvContent.isVisible = item.content.isNotBlank()
binding.tvTime.text = item.time
binding.tvReply.isVisible = item.commentAble

binding.tvReply.text = if (item.commentCount == 0) "回复"
else String.format("回复:%d", item.commentCount)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TimelinePageFragment : BaseListFragment<TimelineEntity, TimelinePageViewMo
get() = !viewModel.hasMultiPage

override fun onCreateContentAdapter(): BaseDifferAdapter<TimelineEntity, *> {
return TimelinePageDiffAdapter {
return TimelinePageAdapter {
if (it is TimelineEntity.GridTimeline && it.id.isNotBlank()) {
when (it.pathType) {
// 跳转媒体详情
Expand Down Expand Up @@ -77,6 +77,13 @@ class TimelinePageFragment : BaseListFragment<TimelineEntity, TimelinePageViewMo
}

TimelineAdapterType.TYPE_TEXT -> {
// 是否为有回复的吐槽时间线
if (it.commentAble) {
TimelineReplyDialog.show(childFragmentManager, it)
return@setOnDebouncedChildClickListener
}

// 跳转 Title Id 对应的页面
if (it.titleId.isNotBlank()) when (it.titleType) {
// 跳转用户详情
BgmPathType.TYPE_USER -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class TimelinePageViewModel : BaseListViewModel<TimelineEntity>() {
require(UserHelper.isLogin) { "你还没有登录呢" }
BgmApiManager.bgmWebApi.queryFriendTimeline(
type = timelineType,
page = current
page = current,
ajax = 0
).parserTimelineForms()
}
// 自己的时间线
Expand Down
Loading

0 comments on commit 8e4224b

Please sign in to comment.