Skip to content

Commit

Permalink
[Feat]: 章节收藏优化
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 21, 2023
1 parent 0f09fdd commit 58418c7
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.xiaoyv.bangumi.ui.media.action

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SnackbarUtils
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.databinding.FragmentMediaActionEpCollectBinding
import com.xiaoyv.bangumi.helper.RouteHelper
import com.xiaoyv.blueprint.constant.NavKey
import com.xiaoyv.blueprint.kts.launchUI
import com.xiaoyv.common.api.BgmApiManager
import com.xiaoyv.common.api.parser.entity.MediaChapterEntity
import com.xiaoyv.common.config.annotation.BgmPathType
import com.xiaoyv.common.config.annotation.EpCollectType
import com.xiaoyv.common.config.annotation.InterestType
import com.xiaoyv.common.config.annotation.TopicType
import com.xiaoyv.common.helper.UserHelper
import com.xiaoyv.common.kts.hideSnackBar
import com.xiaoyv.common.kts.showSnackBar
import com.xiaoyv.widget.callback.setOnFastLimitClickListener
import com.xiaoyv.widget.kts.dpi
import com.xiaoyv.widget.kts.errorMsg
import com.xiaoyv.widget.kts.getParcelObj
import com.xiaoyv.widget.kts.updateWindowParams
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* Class: [MediaEpCollectDialog]
*
* @author why
* @since 12/18/23
*/
class MediaEpCollectDialog : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
) = FragmentMediaActionEpCollectBinding.inflate(inflater, container, false).root

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val entity = arguments?.getParcelObj<MediaChapterEntity>(NavKey.KEY_PARCELABLE) ?: return
initView(FragmentMediaActionEpCollectBinding.bind(view), entity)
}

private fun initView(binding: FragmentMediaActionEpCollectBinding, entity: MediaChapterEntity) {
binding.tvTitle.text = entity.titleNative.ifBlank { entity.titleCn }
binding.ivCancel.setOnClickListener {
dismissAllowingStateLoss()
}

// 设置状态
when (entity.collectType) {
InterestType.TYPE_WISH -> binding.gpButtons.check(R.id.btn_wish)
InterestType.TYPE_COLLECT -> binding.gpButtons.check(R.id.btn_collect)
InterestType.TYPE_DROPPED -> binding.gpButtons.check(R.id.btn_dropped)
}

binding.tvTitleCn.isVisible = entity.titleCn.isNotBlank()
binding.tvTitleCn.text = buildString {
append("中文名:")
append(entity.titleCn)
}
binding.tvDesc.text = entity.time
binding.tvComment.text = String.format("讨论:%d", entity.commentCount)

binding.tvComment.setOnFastLimitClickListener {
jumpDetail(entity)
}

binding.tvDesc.setOnFastLimitClickListener {
jumpDetail(entity)
}

binding.tvTitleCn.setOnFastLimitClickListener {
jumpDetail(entity)
}

binding.gpButtons.addOnButtonCheckedListener { _, i, checked ->
if (!checked) {
return@addOnButtonCheckedListener
}

val epCollectType = when (i) {
R.id.btn_wish -> {
entity.collectType = InterestType.TYPE_WISH
EpCollectType.TYPE_QUEUE
}

R.id.btn_collect -> {
entity.collectType = InterestType.TYPE_COLLECT
EpCollectType.TYPE_WATCHED
}

R.id.btn_dropped -> {
entity.collectType = InterestType.TYPE_DROPPED
EpCollectType.TYPE_DROP
}

else -> {
InterestType.TYPE_UNKNOWN
}
}

saveEpCollectStatus(entity, epCollectType)
}
}

/**
* 刷新章节进度
*/
private fun saveEpCollectStatus(
entity: MediaChapterEntity,
@EpCollectType epCollectType: String,
) {
launchUI(
error = {
it.printStackTrace()

showSnackBar(it.errorMsg, error = true)
},
block = {
showSnackBar("正在为你保存进度...", SnackbarUtils.LENGTH_INDEFINITE)
withContext(Dispatchers.IO) {
BgmApiManager.bgmWebApi.postEpCollect(
epId = entity.id,
epCollectType = epCollectType,
gh = UserHelper.formHash
)
}
hideSnackBar()

UserHelper.notifyActionChange(BgmPathType.TYPE_EP)

dismissAllowingStateLoss()
}
)
}

private fun jumpDetail(entity: MediaChapterEntity) {
dismissAllowingStateLoss()
RouteHelper.jumpTopicDetail(entity.id, TopicType.TYPE_EP)
}

override fun onStart() {
super.onStart()
val dialog = dialog ?: return
val window = dialog.window ?: return

window.setBackgroundDrawableResource(android.R.color.transparent)
window.setDimAmount(0.25f)
window.updateWindowParams {
width = ScreenUtils.getScreenWidth() - 32.dpi
gravity = Gravity.CENTER
}
}

companion object {

fun show(fragmentManager: FragmentManager, chapterEntity: MediaChapterEntity) {
MediaEpCollectDialog().apply {
arguments = bundleOf(NavKey.KEY_PARCELABLE to chapterEntity)
}.show(fragmentManager, "MediaEpCollectDialog")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ class MediaChapterAdapter : BaseQuickDiffBindingAdapter<MediaChapterEntity,
// 已经播出
item.isAired -> {
binding.vAired.text = item.airedStateText
binding.vAired.backgroundTintList =
context.getAttrColor(GoogleAttr.colorPrimarySurface).tint
binding.vAired.setTextColor(context.getAttrColor(GoogleAttr.colorOnPrimarySurface))
binding.vAired.backgroundTintList = context.getColor(CommonColor.state_aired).tint
binding.vAired.setTextColor(context.getColor(CommonColor.state_aired_text))
}
// 未播出
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.xiaoyv.bangumi.ui.media.detail.chapter

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.base.BaseListFragment
import com.xiaoyv.bangumi.helper.RouteHelper
import com.xiaoyv.bangumi.ui.media.action.MediaEpCollectDialog
import com.xiaoyv.blueprint.constant.NavKey
import com.xiaoyv.common.api.parser.entity.MediaChapterEntity
import com.xiaoyv.common.config.annotation.TopicType
import com.xiaoyv.common.config.annotation.BgmPathType
import com.xiaoyv.common.helper.UserHelper
import com.xiaoyv.common.kts.setOnDebouncedChildClickListener
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter

Expand All @@ -34,9 +36,17 @@ class MediaChapterFragment : BaseListFragment<MediaChapterEntity, MediaChapterVi

override fun initListener() {
super.initListener()

contentAdapter.setOnDebouncedChildClickListener(R.id.item_ep) {
RouteHelper.jumpTopicDetail(it.id, TopicType.TYPE_EP)
MediaEpCollectDialog.show(childFragmentManager, it)
}
}

override fun LifecycleOwner.initViewObserverExt() {
UserHelper.observeAction(this) {
if (it == BgmPathType.TYPE_EP) {
viewModel.refresh()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class MediaChapterViewModel : BaseListViewModel<MediaChapterEntity>() {
override suspend fun onRequestListImpl(): List<MediaChapterEntity> {
require(mediaId.isNotBlank()) { "媒体ID不存在" }
return BgmApiManager.bgmWebApi.queryMediaDetail(mediaId, MediaDetailType.TYPE_CHAPTER)
.parserMediaChapters()
.parserMediaChapters(mediaId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class OverviewAdapter(
item.entity = media.apply {
myProgress = progress
progressList.filterNot { it.isNotEp }.forEachIndexed { index, mediaProgress ->
mediaProgress.isWatched = (index + 1) <= progress
// mediaProgress.collectType = (index + 1) <= progress
}
}
set(targetIndex, item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xiaoyv.bangumi.ui.media.detail.overview.binder

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Paint
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseMultiItemAdapter
Expand All @@ -18,11 +19,13 @@ import com.xiaoyv.common.kts.GoogleAttr
import com.xiaoyv.common.kts.forceCast
import com.xiaoyv.common.kts.inflater
import com.xiaoyv.common.kts.setOnDebouncedChildClickListener
import com.xiaoyv.common.kts.tint
import com.xiaoyv.widget.binder.BaseQuickBindingHolder
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter
import com.xiaoyv.widget.kts.getAttrColor
import com.xiaoyv.widget.kts.subListLimit


/**
* Class: [OverviewEpBinder]
*
Expand Down Expand Up @@ -103,21 +106,39 @@ class OverviewEpBinder(

override fun BaseQuickBindingHolder<FragmentOverviewEpItemBinding>.converted(item: MediaDetailEntity.MediaProgress) {
binding.tvEp.text = item.number
binding.tvEp.paintFlags = binding.tvEp.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
when {
item.isWatched -> {
binding.tvEp.setTextColor(context.getAttrColor(GoogleAttr.colorOnPrimarySurface))
binding.tvEp.backgroundTintList = ColorStateList.valueOf(
context.getColor(CommonColor.save_collect)
)
// 看过
item.collectType == InterestType.TYPE_COLLECT -> {
binding.tvEp.backgroundTintList =
context.getColor(CommonColor.save_collect).tint
binding.tvEp.setTextColor(context.getColor(CommonColor.save_collect_text))
}

item.isRelease -> {
binding.tvEp.setTextColor(context.getAttrColor(GoogleAttr.colorOnPrimaryContainer))
binding.tvEp.backgroundTintList = ColorStateList.valueOf(
context.getAttrColor(GoogleAttr.colorPrimaryContainer)
)
// 想看
item.collectType == InterestType.TYPE_WISH -> {
binding.tvEp.backgroundTintList = context.getColor(CommonColor.save_wish).tint
binding.tvEp.setTextColor(context.getColor(CommonColor.save_wish_text))
}

// 抛弃
item.collectType == InterestType.TYPE_DROPPED -> {
binding.tvEp.backgroundTintList =
context.getColor(CommonColor.save_dropped).tint
binding.tvEp.setTextColor(context.getColor(CommonColor.save_dropped_text))
binding.tvEp.paintFlags = binding.tvEp.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
}
// 放送中
item.isAiring -> {
binding.tvEp.setTextColor(context.getColor(CommonColor.state_airing_text))
binding.tvEp.backgroundTintList =
context.getColor(CommonColor.state_airing).tint
}
// 已播出
item.isAired -> {
binding.tvEp.backgroundTintList =
context.getColor(CommonColor.state_aired).tint
binding.tvEp.setTextColor(context.getColor(CommonColor.state_aired_text))
}
// 未播出
else -> {
binding.tvEp.setTextColor(context.getAttrColor(GoogleAttr.colorOnSurface))
binding.tvEp.backgroundTintList = ColorStateList.valueOf(
Expand Down
Loading

0 comments on commit 58418c7

Please sign in to comment.