Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 26, 2024
1 parent f1a99fd commit 5477756
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 84 deletions.
32 changes: 3 additions & 29 deletions app/src/main/java/io/legado/app/data/entities/rule/ExploreKind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,11 @@ package io.legado.app.data.entities.rule
data class ExploreKind(
val title: String = "",
val url: String? = null,
val style: Style? = null
val style: FlexChildStyle? = null
) {

companion object {
val defaultStyle = Style()
}

fun style(): Style {
return style ?: defaultStyle
}

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
}
}

fun style(): FlexChildStyle {
return style ?: FlexChildStyle.defaultStyle
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.legado.app.data.entities.rule

import android.view.View
import com.google.android.flexbox.FlexboxLayout

data class FlexChildStyle(
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
}
}

fun apply(view: View) {
val lp = view.layoutParams as FlexboxLayout.LayoutParams
lp.flexGrow = layout_flexGrow
lp.flexShrink = layout_flexShrink
lp.alignSelf = alignSelf()
lp.flexBasisPercent = layout_flexBasisPercent
lp.isWrapBefore = layout_wrapBefore
}

companion object {
val defaultStyle = FlexChildStyle()
}

}
14 changes: 8 additions & 6 deletions app/src/main/java/io/legado/app/data/entities/rule/RowUi.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.legado.app.data.entities.rule

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class RowUi(
var name: String,
var type: String = "text",
var action: String? = null
) : Parcelable {
var action: String? = null,
var style: FlexChildStyle? = null
) {

@Suppress("ConstPropertyName")
object Type {

const val text = "text"
Expand All @@ -18,4 +16,8 @@ data class RowUi(

}

fun style(): FlexChildStyle {
return style ?: FlexChildStyle.defaultStyle
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/help/book/BookHelp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ object BookHelp {
book.getFolderName(),
bookChapter.getFileName(),
).writeText(content)
if (book.isOnLineTxt) {
if (book.isOnLineTxt && AppConfig.tocCountWords) {
bookChapter.wordCount = StringUtils.wordCountFormat(content.length)
appDb.bookChapterDao.update(bookChapter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,15 @@ object BookChapterList {
}

private fun getWordCount(list: ArrayList<BookChapter>, book: Book) {
if (!AppConfig.tocCountWords) {
return
}
val chapterList = appDb.bookChapterDao.getChapterList(book.bookUrl)
if (chapterList.isNotEmpty()){
if (chapterList.isNotEmpty()) {
val map = chapterList.associateBy({ it.getFileName() }, { it.wordCount })
for (bookChapter in list) {
val wordCount = map[bookChapter.getFileName()]
if(wordCount != null){
if (wordCount != null) {
bookChapter.wordCount = wordCount
}
}
Expand Down
51 changes: 25 additions & 26 deletions app/src/main/java/io/legado/app/ui/login/SourceLoginDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import splitties.views.onClick
import kotlin.collections.HashMap
import kotlin.collections.List
import kotlin.collections.forEachIndexed
import kotlin.collections.hashMapOf
import kotlin.collections.set


class SourceLoginDialog : BaseDialogFragment(R.layout.dialog_login, true) {
Expand Down Expand Up @@ -70,6 +65,7 @@ class SourceLoginDialog : BaseDialogFragment(R.layout.dialog_login, true) {
it.textInputLayout.hint = rowUi.name
it.editText.setText(loginInfo?.get(rowUi.name))
}

RowUi.Type.password -> ItemSourceEditBinding.inflate(
layoutInflater,
binding.root,
Expand All @@ -82,37 +78,19 @@ class SourceLoginDialog : BaseDialogFragment(R.layout.dialog_login, true) {
InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
it.editText.setText(loginInfo?.get(rowUi.name))
}

RowUi.Type.button -> ItemFilletTextBinding.inflate(
layoutInflater,
binding.root,
false
).let {
binding.flexbox.addView(it.root)
rowUi.style().apply(it.root)
it.root.id = index + 1000
it.textView.text = rowUi.name
it.textView.setPadding(16.dpToPx())
it.root.onClick {
Coroutine.async {
if (rowUi.action.isAbsUrl()) {
context?.openUrl(rowUi.action!!)
} else {
// JavaScript
rowUi.action?.let { buttonFunctionJS ->
kotlin.runCatching {
source.getLoginJs()?.let { loginJS ->
source.evalJS("$loginJS\n$buttonFunctionJS") {
put("result", getLoginData(loginUi))
}
}
}.onFailure { e ->
AppLog.put(
"LoginUI Button ${rowUi.name} JavaScript error",
e
)
}
}
}
}
handleButtonClick(source, rowUi, loginUi)
}
}
}
Expand All @@ -125,19 +103,40 @@ class SourceLoginDialog : BaseDialogFragment(R.layout.dialog_login, true) {
val loginData = getLoginData(loginUi)
login(source, loginData)
}

R.id.menu_show_login_header -> alert {
setTitle(R.string.login_header)
source.getLoginHeader()?.let { loginHeader ->
setMessage(loginHeader)
}
}

R.id.menu_del_login_header -> source.removeLoginHeader()
R.id.menu_log -> showDialogFragment<AppLogDialog>()
}
return@setOnMenuItemClickListener true
}
}

private fun handleButtonClick(source: BaseSource, rowUi: RowUi, loginUi: List<RowUi>) {
Coroutine.async {
if (rowUi.action.isAbsUrl()) {
context?.openUrl(rowUi.action!!)
} else if (rowUi.action != null) {
// JavaScript
val buttonFunctionJS = rowUi.action!!
val loginJS = source.getLoginJs() ?: return@async
kotlin.runCatching {
source.evalJS("$loginJS\n$buttonFunctionJS") {
put("result", getLoginData(loginUi))
}
}.onFailure { e ->
AppLog.put("LoginUI Button ${rowUi.name} JavaScript error", e)
}
}
}
}

private fun getLoginData(loginUi: List<RowUi>?): HashMap<String, String> {
val loginData = hashMapOf<String, String>()
loginUi?.forEachIndexed { index, rowUi ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
val tv = getFlexboxChild(flexbox)
flexbox.addView(tv)
tv.text = kind.title
val lp = tv.layoutParams as FlexboxLayout.LayoutParams
kind.style().let { style ->
lp.flexGrow = style.layout_flexGrow
lp.flexShrink = style.layout_flexShrink
lp.alignSelf = style.alignSelf()
lp.flexBasisPercent = style.layout_flexBasisPercent
lp.isWrapBefore = style.layout_wrapBefore
}
kind.style().apply(tv)
if (kind.url.isNullOrBlank()) {
tv.setOnClickListener(null)
} else {
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/res/layout/item_chapter_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,59 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/tv_chapter_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical"
android:padding="12dp">

<TextView
android:id="@+id/tv_chapter_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/tv_word_count"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/iv_checked" />
app:layout_constraintRight_toLeftOf="@+id/iv_checked"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_word_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="18dp"
android:singleLine="true"
android:textSize="12sp"
android:visibility="gone"
android:singleLine="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_chapter_name"
tools:ignore="RtlSymmetry" />

<TextView
android:id="@+id/tv_tag"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="12sp"
android:visibility="gone"
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_chapter_name"
app:layout_constraintLeft_toRightOf="@id/tv_word_count"
app:layout_constraintRight_toLeftOf="@+id/iv_checked" />
app:layout_constraintRight_toLeftOf="@+id/iv_checked"
app:layout_constraintTop_toBottomOf="@+id/tv_chapter_name" />

<ImageView
android:id="@+id/iv_checked"
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@string/success"
android:padding="4dp"
android:src="@drawable/ic_check"
android:visibility="invisible"
android:contentDescription="@string/success"
app:tint="@color/secondaryText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/secondaryText" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 5477756

Please sign in to comment.