发现界面添加自定义样式

pull/1111/head
gedoor 3 years ago
parent c6c07234f8
commit 9deda74008
  1. 5
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  2. 31
      app/src/main/java/io/legado/app/data/entities/ExploreKind.kt
  3. 55
      app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt
  4. 6
      app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt

@ -171,11 +171,6 @@ data class BookSource(
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
data class ExploreKind(
var title: String,
var url: String? = null,
)
class Converters { class Converters {
@TypeConverter @TypeConverter
fun exploreRuleToString(exploreRule: ExploreRule?): String = GSON.toJson(exploreRule) fun exploreRuleToString(exploreRule: ExploreRule?): String = GSON.toJson(exploreRule)

@ -0,0 +1,31 @@
package io.legado.app.data.entities
data class ExploreKind(
val title: String,
val url: String? = null,
val style: Style = Style()
) {
data class Style(
val layout_flexGrow: Float = 0F,
val layout_flexShrink: Float = 1F,
val layout_alignSelf: String = "auto",
val layout_flexBasisPercent: Float = -1F,
val layout_wrapBefore: Boolean = false,
) {
fun alignSelf(): Int {
return when (layout_alignSelf) {
"auto" -> -1
"flex_start" -> 0
"flex_end" -> 1
"center" -> 2
"baseline" -> 3
"stretch" -> 4
else -> -1
}
}
}
}

@ -11,6 +11,7 @@ import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.ExploreKind
import io.legado.app.databinding.ItemFilletTextBinding import io.legado.app.databinding.ItemFilletTextBinding
import io.legado.app.databinding.ItemFindBookBinding import io.legado.app.databinding.ItemFindBookBinding
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
@ -56,10 +57,27 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca
Coroutine.async(scope) { Coroutine.async(scope) {
item.getExploreKinds() item.getExploreKinds()
}.onSuccess { kindList -> }.onSuccess { kindList ->
if (!kindList.isNullOrEmpty()) { upKindList(flexbox, item.bookSourceUrl, kindList)
}.onFinally {
rotateLoading.hide()
if (scrollTo >= 0) {
callBack.scrollTo(scrollTo)
scrollTo = -1
}
}
} else {
binding.ivStatus.setImageResource(R.drawable.ic_arrow_right)
binding.rotateLoading.hide()
binding.flexbox.gone()
}
}
}
private fun upKindList(flexbox: FlexboxLayout, sourceUrl: String, kinds: List<ExploreKind>) {
if (!kinds.isNullOrEmpty()) {
flexbox.visible() flexbox.visible()
flexbox.removeAllViews() flexbox.removeAllViews()
kindList.map { kind -> kinds.map { kind ->
val tv = ItemFilletTextBinding.inflate( val tv = ItemFilletTextBinding.inflate(
LayoutInflater.from(context), LayoutInflater.from(context),
flexbox, flexbox,
@ -67,32 +85,17 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca
).root ).root
flexbox.addView(tv) flexbox.addView(tv)
tv.text = kind.title tv.text = kind.title
if (!kind.url.isNullOrEmpty()) {
tv.setOnClickListener {
callBack.openExplore(
item.bookSourceUrl,
kind.title,
kind.url.toString()
)
}
}
val lp = tv.layoutParams as FlexboxLayout.LayoutParams val lp = tv.layoutParams as FlexboxLayout.LayoutParams
lp.let { kind.style.let { style ->
lp.flexGrow = style.layout_flexGrow
} lp.flexShrink = style.layout_flexShrink
} lp.alignSelf = style.alignSelf()
} lp.flexBasisPercent = style.layout_flexBasisPercent
}.onFinally { lp.isWrapBefore = style.layout_wrapBefore
rotateLoading.hide()
if (scrollTo >= 0) {
callBack.scrollTo(scrollTo)
scrollTo = -1
} }
tv.setOnClickListener {
callBack.openExplore(sourceUrl, kind.title, kind.url)
} }
} else {
binding.ivStatus.setImageResource(R.drawable.ic_arrow_right)
binding.rotateLoading.hide()
binding.flexbox.gone()
} }
} }
} }
@ -151,7 +154,7 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca
interface CallBack { interface CallBack {
fun scrollTo(pos: Int) fun scrollTo(pos: Int)
fun openExplore(sourceUrl: String, title: String, exploreUrl: String) fun openExplore(sourceUrl: String, title: String, exploreUrl: String?)
fun editSource(sourceUrl: String) fun editSource(sourceUrl: String)
fun toTop(source: BookSource) fun toTop(source: BookSource)
} }

@ -154,10 +154,8 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
(binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0) (binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0)
} }
override fun openExplore(sourceUrl: String, title: String, exploreUrl: String) { override fun openExplore(sourceUrl: String, title: String, exploreUrl: String?) {
if (exploreUrl.isBlank()) { if (exploreUrl.isNullOrBlank()) return
return
}
startActivity<ExploreShowActivity> { startActivity<ExploreShowActivity> {
putExtra("exploreName", title) putExtra("exploreName", title)
putExtra("sourceUrl", sourceUrl) putExtra("sourceUrl", sourceUrl)

Loading…
Cancel
Save