Skip to content

Commit

Permalink
[Feat]: 标签搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 10, 2023
1 parent c837370 commit 76e9372
Show file tree
Hide file tree
Showing 24 changed files with 386 additions and 90 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions app/src/main/java/com/xiaoyv/bangumi/helper/RouteHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ object RouteHelper {
titleLink.contains(BgmPathType.TYPE_PERSON) -> {
jumpPerson(id, false)
}
// 话题
titleLink.contains(BgmPathType.TYPE_TOPIC) -> {
when{
// 虚拟人物
titleLink.contains(TopicType.TYPE_CRT)-> {
jumpTopicDetail(id, TopicType.TYPE_CRT)
}
// 章节
titleLink.contains(TopicType.TYPE_EP)-> {
jumpTopicDetail(id, TopicType.TYPE_EP)
}
// 小组
titleLink.contains(TopicType.TYPE_GROUP)-> {
jumpTopicDetail(id, TopicType.TYPE_GROUP)
}
// 现实人物
titleLink.contains(TopicType.TYPE_PERSON)-> {
jumpTopicDetail(id, TopicType.TYPE_PERSON)
}
// 条目
titleLink.contains(TopicType.TYPE_SUBJECT)-> {
jumpTopicDetail(id, TopicType.TYPE_SUBJECT)
}
}
}
// 日志
titleLink.contains(BgmPathType.TYPE_BLOG) -> {
jumpBlogDetail(id)
}
}

debugLog { "Handle Url: $titleLink" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ class BlogActivity : BaseViewModelActivity<ActivityBlogBinding, BlogViewModel>()
item.initNavBack(this)
return super.onOptionsItemSelected(item)
}

override fun onDestroy() {
super.onDestroy()
binding.webView.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class NotifyActivity : BaseListActivity<NotifyEntity, NotifyViewModel>() {
contentAdapter.setOnDebouncedChildClickListener(R.id.item_notify) {
if (it.titleLink.isNotBlank()) {
RouteHelper.handleUrl(it.titleLink)
} else {
RouteHelper.jumpUserDetail(it.userId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ class PostBlogActivity : BaseViewModelActivity<ActivityPostBlogBinding, PostBlog
item.initNavBack(this)
return super.onOptionsItemSelected(item)
}

override fun onDestroy() {
super.onDestroy()
binding.webView.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xiaoyv.bangumi.ui.feature.search

import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
import com.blankj.utilcode.util.KeyboardUtils
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.databinding.ActivitySearchBinding
Expand All @@ -21,16 +22,27 @@ class SearchActivity : BaseViewModelActivity<ActivitySearchBinding, SearchViewMo

private val subjectItemAdapter by lazy { SearchAdapter(false) }
private val personItemAdapter by lazy { SearchAdapter(false) }
private val tagItemAdapter by lazy { SearchAdapter(false) }
private val recentlyItemAdapter by lazy { SearchAdapter(true) }

private val viewPool by lazy { RecycledViewPool() }

override fun initView() {
binding.rvSubject.adapter = subjectItemAdapter
binding.rvPerson.adapter = personItemAdapter
binding.rvRecently.adapter = recentlyItemAdapter
binding.rvSubject.hasFixedSize()
binding.rvSubject.setRecycledViewPool(viewPool)
binding.rvPerson.hasFixedSize()
binding.rvPerson.setRecycledViewPool(viewPool)
binding.rvTag.hasFixedSize()
binding.rvTag.setRecycledViewPool(viewPool)
binding.rvRecently.hasFixedSize()
binding.rvRecently.setRecycledViewPool(viewPool)
}

override fun initData() {

binding.rvSubject.adapter = subjectItemAdapter
binding.rvPerson.adapter = personItemAdapter
binding.rvTag.adapter = tagItemAdapter
binding.rvRecently.adapter = recentlyItemAdapter
}

override fun initListener() {
Expand All @@ -51,12 +63,17 @@ class SearchActivity : BaseViewModelActivity<ActivitySearchBinding, SearchViewMo
}

subjectItemAdapter.addOnItemChildClickListener(R.id.item_search) { _, _, position ->
viewModel.currentSearchItem.value = subjectItemAdapter.getItem(position)
viewModel.switchSearchType(subjectItemAdapter.getItem(position))
KeyboardUtils.showSoftInput(binding.searchBar.etKeyword)
}

personItemAdapter.addOnItemChildClickListener(R.id.item_search) { _, _, position ->
viewModel.currentSearchItem.value = personItemAdapter.getItem(position)
viewModel.switchSearchType(personItemAdapter.getItem(position))
KeyboardUtils.showSoftInput(binding.searchBar.etKeyword)
}

tagItemAdapter.addOnItemChildClickListener(R.id.item_search) { _, _, position ->
viewModel.switchSearchType(tagItemAdapter.getItem(position))
KeyboardUtils.showSoftInput(binding.searchBar.etKeyword)
}

Expand All @@ -73,22 +90,27 @@ class SearchActivity : BaseViewModelActivity<ActivitySearchBinding, SearchViewMo
}

viewModel.onSearchPersonLiveData.observe(this) {
binding.rvRecently.isVisible = it.isNotEmpty()
binding.tvTitleRecently.isVisible = it.isNotEmpty()
binding.dividerRecently.isVisible = it.isNotEmpty()
personItemAdapter.submitList(it)
}

viewModel.onSearchTagLiveData.observe(this) {
tagItemAdapter.submitList(it)
}

viewModel.onSearchRecentlyLiveData.observe(this) {
binding.rvRecently.isVisible = it.isNullOrEmpty().not()
binding.tvTitleRecently.isVisible = it.isNullOrEmpty().not()
binding.dividerRecently.isVisible = it.isNullOrEmpty().not()
recentlyItemAdapter.submitList(it)
}

viewModel.currentSearchItem.observe(this) {
val item = it ?: return@observe
val hint = buildString {
append("搜索:")
append(BgmPathType.string(it.pathType))
append(BgmPathType.string(item.pathType))
append(" - ")
append(it.label)
append(item.label)
}
binding.searchBar.etKeyword.hint = hint

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModel
import com.xiaoyv.blueprint.kts.launchUI
import com.xiaoyv.common.config.annotation.BgmPathType
import com.xiaoyv.common.config.annotation.MediaType
import com.xiaoyv.common.config.annotation.SearchCatType
import com.xiaoyv.common.config.bean.SearchItem
import com.xiaoyv.common.helper.CacheHelper
Expand All @@ -17,10 +18,11 @@ import kotlinx.coroutines.withContext
* @since 12/9/23
*/
class SearchViewModel : BaseViewModel() {
internal val currentSearchItem = MutableLiveData<SearchItem>()
internal val currentSearchItem = MutableLiveData<SearchItem?>()

internal val onSearchSubjectLiveData = MutableLiveData<List<SearchItem>>()
internal val onSearchPersonLiveData = MutableLiveData<List<SearchItem>>()
internal val onSearchTagLiveData = MutableLiveData<List<SearchItem>>()
internal val onSearchRecentlyLiveData = MutableLiveData<List<SearchItem>?>()

override fun onViewCreated() {
Expand Down Expand Up @@ -59,7 +61,35 @@ class SearchViewModel : BaseViewModel() {
label = "三次元",
pathType = BgmPathType.TYPE_SEARCH_SUBJECT,
id = SearchCatType.TYPE_REAL,
)
)

onSearchTagLiveData.value = listOf(
SearchItem(
label = "动画",
pathType = BgmPathType.TYPE_SEARCH_TAG,
id = MediaType.TYPE_ANIME
),
SearchItem(
label = "书籍",
pathType = BgmPathType.TYPE_SEARCH_TAG,
id = MediaType.TYPE_BOOK
),
SearchItem(
label = "音乐",
pathType = BgmPathType.TYPE_SEARCH_TAG,
id = MediaType.TYPE_MUSIC
),
SearchItem(
label = "游戏",
pathType = BgmPathType.TYPE_SEARCH_TAG,
id = MediaType.TYPE_GAME
),
SearchItem(
label = "三次元",
pathType = BgmPathType.TYPE_SEARCH_TAG,
id = MediaType.TYPE_REAL
)
)

onSearchPersonLiveData.value = listOf(
Expand All @@ -85,7 +115,15 @@ class SearchViewModel : BaseViewModel() {
}

// 默认搜索 条目-全部
currentSearchItem.value = onSearchSubjectLiveData.value?.firstOrNull()
switchSearchType(onSearchSubjectLiveData.value?.firstOrNull())
}
}


/**
* 切换搜索类型
*/
fun switchSearchType(searchItem: SearchItem?) {
currentSearchItem.value = searchItem
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.QuickAdapterHelper
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
import com.xiaoyv.bangumi.R
import com.xiaoyv.bangumi.databinding.ActivitySearchDetailBinding
import com.xiaoyv.bangumi.helper.RouteHelper
Expand All @@ -16,6 +18,7 @@ import com.xiaoyv.common.config.annotation.BgmPathType
import com.xiaoyv.common.config.annotation.SearchCatType
import com.xiaoyv.common.kts.GoogleAttr
import com.xiaoyv.common.kts.setOnDebouncedChildClickListener
import com.xiaoyv.common.widget.scroll.AnimeLinearLayoutManager
import com.xiaoyv.widget.callback.setOnFastLimitClickListener
import com.xiaoyv.widget.kts.getAttrColor
import com.xiaoyv.widget.kts.getParcelObj
Expand All @@ -29,10 +32,21 @@ import com.xiaoyv.widget.kts.useNotNull
*/
class SearchDetailActivity :
BaseViewModelActivity<ActivitySearchDetailBinding, SearchDetailViewModel>() {
private val contentAdapter by lazy {
SearchDetailAdapter()
private val contentItemAdapter by lazy {
SearchDetailItemAdapter()
}

private val contentTagAdapter by lazy {
SearchDetailTagAdapter()
}

/**
* 适配器
*/
private val contentAdapter
get() = if (viewModel.isSearchTag) contentTagAdapter else contentItemAdapter


private val adapterHelper by lazy {
QuickAdapterHelper.Builder(contentAdapter)
.setTrailingLoadStateAdapter(object : TrailingLoadStateAdapter.OnTrailingListener {
Expand All @@ -51,21 +65,25 @@ class SearchDetailActivity :
.build()
}

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

override fun initIntentData(intent: Intent, bundle: Bundle, isNewIntent: Boolean) {
viewModel.currentSearchItem.value = bundle.getParcelObj(NavKey.KEY_PARCELABLE)
}

override fun initView() {
binding.srlRefresh.initRefresh { false }
binding.srlRefresh.setColorSchemeColors(getAttrColor(GoogleAttr.colorPrimary))

binding.rvContent.adapter = adapterHelper.adapter
}

override fun initData() {
if (viewModel.isSearchTag) {
binding.rvContent.layoutManager = FlexboxLayoutManager(this, FlexDirection.ROW)
binding.rvContent.adapter = contentAdapter
} else {
binding.rvContent.layoutManager =
AnimeLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
binding.rvContent.adapter = adapterHelper.adapter
}

viewModel.refresh()
}

Expand All @@ -89,7 +107,7 @@ class SearchDetailActivity :
true
}

contentAdapter.setOnDebouncedChildClickListener(R.id.item_search) {
contentItemAdapter.setOnDebouncedChildClickListener(R.id.item_search) {
useNotNull(viewModel.currentSearchItem.value) {
if (pathType == BgmPathType.TYPE_SEARCH_SUBJECT) {
RouteHelper.jumpMediaDetail(it.id)
Expand Down Expand Up @@ -126,9 +144,14 @@ class SearchDetailActivity :

contentAdapter.submitList(it) {
if (viewModel.isRefresh) {
layoutManager?.scrollToPositionWithOffset(0, 0)
if (viewModel.isSearchTag) {
(binding.rvContent.layoutManager as? FlexboxLayoutManager)
?.scrollToPosition(0)
} else {
(binding.rvContent.layoutManager as? LinearLayoutManager)
?.scrollToPositionWithOffset(0, 0)
}
}

adapterHelper.trailingLoadState = viewModel.loadingMoreState
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import com.xiaoyv.widget.binder.BaseQuickBindingHolder
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter

/**
* Class: [SearchDetailAdapter]
* Class: [SearchDetailItemAdapter]
*
* @author why
* @since 12/8/23
*/
class SearchDetailAdapter : BaseQuickDiffBindingAdapter<SearchResultEntity,
class SearchDetailItemAdapter : BaseQuickDiffBindingAdapter<SearchResultEntity,
ActivitySearchDetailItemBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<ActivitySearchDetailItemBinding>.converted(item: SearchResultEntity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.xiaoyv.bangumi.ui.feature.search.detail

import com.xiaoyv.bangumi.databinding.ActivitySearchDetailTagBinding
import com.xiaoyv.common.api.parser.entity.SearchResultEntity
import com.xiaoyv.common.helper.callback.IdDiffItemCallback
import com.xiaoyv.widget.binder.BaseQuickBindingHolder
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter

/**
* Class: [SearchDetailTagAdapter]
*
* @author why
* @since 12/8/23
*/
class SearchDetailTagAdapter : BaseQuickDiffBindingAdapter<SearchResultEntity,
ActivitySearchDetailTagBinding>(IdDiffItemCallback()) {

override fun BaseQuickBindingHolder<ActivitySearchDetailTagBinding>.converted(item: SearchResultEntity) {
binding.tvTitleTag.text = item.title
binding.tvCommentCount.text = item.count
}
}
Loading

0 comments on commit 76e9372

Please sign in to comment.