Skip to content

Commit

Permalink
[Feat]: 人物角色、作品Tab
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 5, 2023
1 parent 6092446 commit ca28e4a
Show file tree
Hide file tree
Showing 33 changed files with 578 additions and 188 deletions.
101 changes: 101 additions & 0 deletions app/src/main/java/com/xiaoyv/bangumi/base/BaseListStubFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.xiaoyv.bangumi.base

import androidx.annotation.CallSuper
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseDifferAdapter
import com.chad.library.adapter.base.QuickAdapterHelper
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
import com.xiaoyv.bangumi.databinding.FragmentListBinding
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModelFragment
import com.xiaoyv.blueprint.kts.toJson
import com.xiaoyv.common.kts.GoogleAttr
import com.xiaoyv.common.kts.debugLog
import com.xiaoyv.common.widget.scroll.AnimeLinearLayoutManager
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter
import com.xiaoyv.widget.kts.getAttrColor

/**
* Class: [BaseListStubFragment]
*
* @author why
* @since 11/29/23
*/
abstract class BaseListStubFragment<T, VM : BaseListViewModel<T>> :
BaseViewModelFragment<FragmentListBinding, VM>() {

abstract val isOnlyOnePage: Boolean

internal val contentAdapter: BaseDifferAdapter<T, *> by lazy {
onCreateContentAdapter()
}

private val adapterHelper by lazy {
QuickAdapterHelper.Builder(contentAdapter)
.setTrailingLoadStateAdapter(object : TrailingLoadStateAdapter.OnTrailingListener {
override fun isAllowLoading(): Boolean {
return binding.srlRefresh.isRefreshing.not() && isOnlyOnePage.not()
}

override fun onFailRetry() {
viewModel.loadMore()
}

override fun onLoad() {
viewModel.loadMore()
}
})
.build()
}

internal open val hasFixedSize = false

internal open val layoutManager: LinearLayoutManager?
get() = binding.rvContent.layoutManager as? LinearLayoutManager

abstract fun onCreateContentAdapter(): BaseQuickDiffBindingAdapter<T, *>

@CallSuper
override fun initView() {
binding.rvContent.setHasFixedSize(hasFixedSize)
binding.srlRefresh.initRefresh { viewModel.isRefresh }
binding.srlRefresh.setColorSchemeColors(hostActivity.getAttrColor(GoogleAttr.colorPrimary))
}

@CallSuper
override fun initData() {
binding.rvContent.layoutManager = onCreateLayoutManager()
if (isOnlyOnePage) {
binding.rvContent.adapter = contentAdapter
} else {
binding.rvContent.adapter = adapterHelper.adapter
}
viewModel.refresh()
}

open fun onCreateLayoutManager(): LinearLayoutManager {
return AnimeLinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
}

@CallSuper
override fun initListener() {
binding.srlRefresh.setOnRefreshListener {
viewModel.refresh()
}
}

@CallSuper
override fun LifecycleOwner.initViewObserver() {
viewModel.onListLiveData.observe(this) {
debugLog { "List:\n " + it.toJson(true) }

contentAdapter.submitList(it.orEmpty()) {
if (viewModel.isRefresh) {
layoutManager?.scrollToPositionWithOffset(0, 0)
}

adapterHelper.trailingLoadState = viewModel.loadingMoreState
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.xiaoyv.bangumi.ui.feature.person.character

import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import com.xiaoyv.bangumi.databinding.FragmentMediaBoardItemBinding
import com.xiaoyv.common.api.parser.entity.MediaBoardEntity
import com.xiaoyv.bangumi.databinding.FragmentMediaPageItemBinding
import com.xiaoyv.common.api.parser.entity.CharacterEntity
import com.xiaoyv.common.kts.loadImageAnimate
import com.xiaoyv.widget.binder.BaseQuickBindingHolder
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter

Expand All @@ -12,27 +14,27 @@ import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter
* @author why
* @since 12/5/23
*/
class PersonCharacterAdapter : BaseQuickDiffBindingAdapter<MediaBoardEntity,
FragmentMediaBoardItemBinding>(ItemDiffItemCallback) {
class PersonCharacterAdapter : BaseQuickDiffBindingAdapter<CharacterEntity,
FragmentMediaPageItemBinding>(ItemDiffItemCallback) {

override fun BaseQuickBindingHolder<FragmentMediaBoardItemBinding>.converted(item: MediaBoardEntity) {
binding.tvTitle.text = String.format("用户:%s", item.userName)
binding.tvContent.text = item.content
binding.tvReplay.text = item.replies
binding.tvTime.text = item.time
override fun BaseQuickBindingHolder<FragmentMediaPageItemBinding>.converted(item: CharacterEntity) {
binding.ivCover.loadImageAnimate(item.avatar)
binding.tvTitle.text = item.nameCn.ifBlank { item.nameNative }
binding.tvTag.text = String.format("x%d", item.from.size)
binding.tvSource.isVisible = false
}

private object ItemDiffItemCallback : DiffUtil.ItemCallback<MediaBoardEntity>() {
private object ItemDiffItemCallback : DiffUtil.ItemCallback<CharacterEntity>() {
override fun areItemsTheSame(
oldItem: MediaBoardEntity,
newItem: MediaBoardEntity
oldItem: CharacterEntity,
newItem: CharacterEntity
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: MediaBoardEntity,
newItem: MediaBoardEntity
oldItem: CharacterEntity,
newItem: CharacterEntity
): Boolean {
return oldItem == newItem
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
package com.xiaoyv.bangumi.ui.feature.person.character

import android.os.Bundle
import androidx.core.os.bundleOf
import com.xiaoyv.bangumi.databinding.FragmentPersonCharacterBinding
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModelFragment
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.layoutmanager.QuickGridLayoutManager
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.base.BaseListFragment
import com.xiaoyv.bangumi.helper.RouteHelper
import com.xiaoyv.blueprint.constant.NavKey
import com.xiaoyv.common.api.parser.entity.CharacterEntity
import com.xiaoyv.common.kts.setOnDebouncedChildClickListener
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter
import com.xiaoyv.widget.kts.dpi

/**
* Class: [PersonCharacterFragment]
*
* @author why
* @since 12/4/23
*/
class PersonCharacterFragment :
BaseViewModelFragment<FragmentPersonCharacterBinding, PersonCharacterViewModel>() {
class PersonCharacterFragment : BaseListFragment<CharacterEntity, PersonCharacterViewModel>() {

override fun initArgumentsData(arguments: Bundle) {
viewModel.personId = arguments.getString(NavKey.KEY_STRING).orEmpty()
viewModel.isVirtual = arguments.getBoolean(NavKey.KEY_BOOLEAN)
}

override val isOnlyOnePage: Boolean
get() = true

override fun initView() {
super.initView()
binding.rvContent.updatePadding(8.dpi, 8.dpi, 8.dpi, 8.dpi)
}

override fun initListener() {
super.initListener()

contentAdapter.setOnDebouncedChildClickListener(R.id.iv_cover) {
RouteHelper.jumpPerson(it.id, true)
}
}

override fun initData() {
override fun onCreateContentAdapter(): BaseQuickDiffBindingAdapter<CharacterEntity, *> {
return PersonCharacterAdapter()
}

override fun onCreateLayoutManager(): LinearLayoutManager {
return QuickGridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.xiaoyv.bangumi.ui.feature.person.character

import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModel
import com.xiaoyv.bangumi.base.BaseListViewModel
import com.xiaoyv.common.api.BgmApiManager
import com.xiaoyv.common.api.parser.entity.CharacterEntity
import com.xiaoyv.common.api.parser.impl.parserPersonVoices

/**
* Class: [PersonCharacterViewModel]
*
* @author why
* @since 12/4/23
*/
class PersonCharacterViewModel : BaseViewModel() {
class PersonCharacterViewModel : BaseListViewModel<CharacterEntity>() {
/**
* 人物ID和是否为虚拟人物
*/
internal var personId: String = ""
internal var isVirtual: Boolean = false

override suspend fun onRequestListImpl(): List<CharacterEntity> {
return BgmApiManager.bgmWebApi.queryPersonWorkVoices(personId).parserPersonVoices()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PersonCollectFragment :
}

override fun onCreateLayoutManager(): LinearLayoutManager {
return QuickGridLayoutManager(requireContext(), 6, GridLayoutManager.VERTICAL, false)
return QuickGridLayoutManager(requireContext(), 5, GridLayoutManager.VERTICAL, false)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.xiaoyv.bangumi.ui.feature.person.collect
import com.xiaoyv.bangumi.base.BaseListViewModel
import com.xiaoyv.common.api.BgmApiManager
import com.xiaoyv.common.api.parser.entity.MediaDetailEntity
import com.xiaoyv.common.api.parser.impl.parserPersonCollect
import com.xiaoyv.common.api.parser.impl.parserPersonCollector

/**
* Class: [PersonCollectViewModel]
Expand All @@ -25,12 +25,12 @@ class PersonCollectViewModel : BaseListViewModel<MediaDetailEntity.MediaWho>() {
BgmApiManager.bgmWebApi.queryCharacterCollectUser(
personId = personId,
page = current
).parserPersonCollect()
).parserPersonCollector()
} else {
BgmApiManager.bgmWebApi.queryPersonCollectUser(
personId = personId,
page = current
).parserPersonCollect()
).parserPersonCollector()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xiaoyv.bangumi.ui.feature.person.cooperate

import androidx.recyclerview.widget.DiffUtil
import com.xiaoyv.bangumi.databinding.FragmentPersonCollectItemBinding
import com.xiaoyv.bangumi.databinding.FragmentPersonCooperateItemBinding
import com.xiaoyv.common.api.parser.entity.PersonEntity
import com.xiaoyv.common.kts.loadImageAnimate
Expand All @@ -21,6 +20,7 @@ class PersonCooperateAdapter : BaseQuickDiffBindingAdapter<PersonEntity.RecentCo
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.tvName.text = item.name
binding.tvTimes.text = String.format("x%d", item.times)
binding.tvJobs.text = item.jobs.joinToString("")
}

private object ItemDiffItemCallback : DiffUtil.ItemCallback<PersonEntity.RecentCooperate>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class PersonCooperateFragment :
}

override fun onCreateLayoutManager(): LinearLayoutManager {
return QuickGridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
return QuickGridLayoutManager(requireContext(), 3, GridLayoutManager.VERTICAL, false)
}

companion object {
fun newInstance(personId: String, isVirtual: Boolean): PersonCooperateFragment {
return PersonCooperateFragment().apply {
arguments = bundleOf(
NavKey.KEY_STRING to "17491",
NavKey.KEY_STRING to personId,
NavKey.KEY_BOOLEAN to isVirtual
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.xiaoyv.bangumi.ui.feature.person.opus

import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import com.xiaoyv.bangumi.databinding.FragmentPersonCollectItemBinding
import com.xiaoyv.bangumi.databinding.FragmentMediaPageItemBinding
import com.xiaoyv.common.api.parser.entity.PersonEntity
import com.xiaoyv.common.kts.loadImageAnimate
import com.xiaoyv.widget.binder.BaseQuickBindingHolder
Expand All @@ -13,25 +14,28 @@ import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter
* @author why
* @since 12/5/23
*/
class PersonOpusAdapter : BaseQuickDiffBindingAdapter<PersonEntity.RecentCooperate,
FragmentPersonCollectItemBinding>(ItemDiffItemCallback) {
class PersonOpusAdapter : BaseQuickDiffBindingAdapter<PersonEntity.RecentlyOpus,
FragmentMediaPageItemBinding>(ItemDiffItemCallback) {

override fun BaseQuickBindingHolder<FragmentPersonCollectItemBinding>.converted(item: PersonEntity.RecentCooperate) {
binding.ivAvatar.loadImageAnimate(item.avatar)
binding.tvTip.text = item.name
override fun BaseQuickBindingHolder<FragmentMediaPageItemBinding>.converted(item: PersonEntity.RecentlyOpus) {
binding.ivCover.loadImageAnimate(item.cover)
binding.tvTitle.text = item.titleCn
binding.tvTag.text = item.jobs.joinToString("")
binding.tvSource.text = String.format("%.1f", item.rateInfo.rate)
binding.tvSource.isVisible = item.rateInfo.rate != 0f
}

private object ItemDiffItemCallback : DiffUtil.ItemCallback<PersonEntity.RecentCooperate>() {
private object ItemDiffItemCallback : DiffUtil.ItemCallback<PersonEntity.RecentlyOpus>() {
override fun areItemsTheSame(
oldItem: PersonEntity.RecentCooperate,
newItem: PersonEntity.RecentCooperate
oldItem: PersonEntity.RecentlyOpus,
newItem: PersonEntity.RecentlyOpus
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: PersonEntity.RecentCooperate,
newItem: PersonEntity.RecentCooperate
oldItem: PersonEntity.RecentlyOpus,
newItem: PersonEntity.RecentlyOpus
): Boolean {
return oldItem == newItem
}
Expand Down
Loading

0 comments on commit ca28e4a

Please sign in to comment.