Skip to content

Commit

Permalink
[Feat]: 年份选取
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Mar 19, 2024
1 parent d7c489c commit 4c66ff6
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 29 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

11 changes: 0 additions & 11 deletions .idea/other.xml

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

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
applicationId = "com.xiaoyv.bangumi"
minSdk = 24
targetSdk = 34
versionCode = 6
versionCode = 7
versionName = "1.0.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.xiaoyv.bangumi.ui.media

import android.view.MenuItem
import androidx.core.view.GravityCompat
import androidx.core.view.ViewCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
Expand Down Expand Up @@ -88,6 +90,12 @@ class MediaFragment : BaseViewModelFragment<FragmentMediaBinding, MediaViewModel
binding.flOptions.offscreenPageLimit = optionAdapter.itemCount

tabLayoutMediator.attach()

// 底部空白适配
ViewCompat.setOnApplyWindowInsetsListener(binding.flOptions) { _, i ->
binding.flOptions.updatePadding(bottom = 0)
i
}
}

override fun initListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,22 @@ class MediaOptionFragment :
return@setOnDebouncedChildClickListener
}

// 如果没有选择年份时,选了月份则提示
if (it.isMonth && optionAdapter.selected.find { item -> item.isYear } == null) {
showToast("请先选择年份后再进行操作")
return@setOnDebouncedChildClickListener
}

if (optionAdapter.isSelected(it)) {
optionAdapter.unselectItem(it)

// 取消选取年份同时自动取消月份
if (it.isYear) {
val month = optionAdapter.selected.find { item -> item.isMonth }
if (month != null) {
optionAdapter.unselectItem(month)
}
}
} else {
optionAdapter.selectItem(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,20 @@ class MediaPageViewModel : BaseViewModel() {
items.remove(sortOption)
}

val monthOption = items.find { it.isMonth }
if (monthOption != null) {
items.remove(monthOption)
}

val path = items
.filter { it.value.orEmpty().isNotBlank() }
.map {
when {
it.isYear -> "/airtime/" + it.value
it.isYear -> {
if (monthOption != null) "/airtime/" + it.value + "-" + monthOption.value
else "/airtime/" + it.value
}

else -> it.value
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
android:layout_width="@dimen/ui_size_300"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/transparent"
android:background="?attr/colorSurface"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class MediaOptionConfig : ArrayList<MediaOptionConfig.Config>(), Parcelable {
var value: String? = null,
@SerializedName("isYear")
var isYear: Boolean = false,
@SerializedName("isMonth")
var isMonth: Boolean = false,

/**
* 下面的互斥
Expand Down Expand Up @@ -92,8 +94,9 @@ class MediaOptionConfig : ArrayList<MediaOptionConfig.Config>(), Parcelable {
val mediaType = it.mediaType.orEmpty()
val option = it.option.orEmpty().toMutableList()
option.removeIf { item -> item.generate }
option.add(buildTimeOption(mediaType, fromYear, yearCount))
option.add(buildSort(mediaType))
option.add(buildTimeOption(mediaType, fromYear, yearCount))
option.add(buildMonthOption(mediaType))
option.add(buildPinYinOption(mediaType))
it.option = option
}
Expand Down Expand Up @@ -151,7 +154,7 @@ class MediaOptionConfig : ArrayList<MediaOptionConfig.Config>(), Parcelable {
yearCount: Int,
): Config.Option {
val option = Config.Option()
option.title = "时间"
option.title = "年份"
option.pathIndex = 10
option.generate = true
option.items = arrayListOf<Config.Option.Item>().apply {
Expand All @@ -174,5 +177,30 @@ class MediaOptionConfig : ArrayList<MediaOptionConfig.Config>(), Parcelable {
}
return option
}

/**
* 构建月份选项
*/
private fun buildMonthOption(mediaType: String): Config.Option {
val option = Config.Option()
option.title = "月份"
option.pathIndex = 11
option.generate = true
option.items = arrayListOf<Config.Option.Item>().apply {
repeat(12) {
add(
Config.Option.Item(
groupTitle = option.title.orEmpty(),
pathIndex = option.pathIndex,
mediaType = mediaType,
title = (it + 1).toString() + "",
value = (it + 1).toString(),
isMonth = true,
)
)
}
}
return option
}
}
}
23 changes: 10 additions & 13 deletions lib-common/src/main/java/com/xiaoyv/common/kts/AdapterKt.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoyv.common.kts

import android.view.View
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
Expand All @@ -10,35 +11,31 @@ import com.chad.library.adapter.base.util.setOnDebouncedItemClick
* @author why
* @since 11/24/23
*/
inline fun <T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedItemClickListener(
inline fun <reified T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedItemClickListener(
time: Long = 500,
crossinline block: (T) -> Unit = {}
crossinline block: (T) -> Unit = {},
) {
setOnDebouncedItemClick(time) { adapter, _, position ->
val item = adapter.getItem(position)
if (item != null) block(item)
}
}

inline fun <T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedChildClickListener(
inline fun <reified T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedChildClickListener(
@IdRes childId: Int,
time: Long = 500,
crossinline block: (T) -> Unit = {}
crossinline block: (T) -> Unit = {},
) {
addOnDebouncedChildClick(childId, time) { adapter, _, position ->
val item = adapter.getItem(position)
if (item != null) block(item)
block(requireNotNull(adapter.getItem(position)))
}
}

@JvmName("setOnDebouncedChildClickKtListener")
inline fun <T, VH : RecyclerView.ViewHolder, reified CAST> BaseQuickAdapter<T, VH>.setOnDebouncedChildClickListener(
inline fun <reified T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnItemChildLongClickListener(
@IdRes childId: Int,
time: Long = 500,
crossinline block: (CAST) -> Unit = {}
crossinline block: (View, T) -> Boolean = { _, _ -> true },
) {
addOnDebouncedChildClick(childId, time) { adapter, _, position ->
val item = adapter.getItem(position)
if (item != null) block(item as CAST)
addOnItemChildLongClickListener(childId) { adapter, view, position ->
block(view, requireNotNull(adapter.getItem(position)))
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c66ff6

Please sign in to comment.