Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from gedoor:master #178

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions app/src/main/assets/web/help/md/ruleHelp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,29 @@
规则填写示范
[
{
name: "telephone",
type: "text"
"name": "telephone",
"type": "text"
},
{
name: "password",
type: "password"
"name": "password",
"type": "password"
},
{
name: "注册",
type: "button",
action: "http://www.yooike.com/xiaoshuo/#/register?title=%E6%B3%A8%E5%86%8C"
"name": "注册",
"type": "button",
"action": "http://www.yooike.com/xiaoshuo/#/register?title=%E6%B3%A8%E5%86%8C"
},
{
name: "获取验证码",
type: "button",
action: "getVerificationCode()"
"name": "获取验证码",
"type": "button",
"action": "getVerificationCode()",
"style": {
"layout_flexGrow": 0,
"layout_flexShrink": 1,
"layout_alignSelf": "auto",
"layout_flexBasisPercent": -1,
"layout_wrapBefore": false
}
}
]
```
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/legado/app/constant/PreferKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ object PreferKey {
const val checkSource = "checkSource"
const val uploadRule = "uploadRule"
const val tocUiUseReplace = "tocUiUseReplace"
const val tocCountWords = "tocCountWords"
const val enableReadRecord = "enableReadRecord"
const val localBookImportSort = "localBookImportSort"
const val customWelcome = "customWelcome"
Expand Down
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
6 changes: 6 additions & 0 deletions app/src/main/java/io/legado/app/help/config/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefBoolean(PreferKey.tocUiUseReplace, value)
}

var tocCountWords: Boolean
get() = appCtx.getPrefBoolean(PreferKey.tocCountWords, true)
set(value) {
appCtx.putPrefBoolean(PreferKey.tocCountWords, value)
}

var enableReadRecord: Boolean
get() = appCtx.getPrefBoolean(PreferKey.enableReadRecord, true)
set(value) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
&& oldItem.isPay == newItem.isPay
&& oldItem.title == newItem.title
&& oldItem.tag == newItem.tag
&& oldItem.wordCount == newItem.wordCount
&& oldItem.isVolume == newItem.isVolume
}

Expand Down Expand Up @@ -150,7 +151,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
} else {
tvTag.gone()
}
if (!item.wordCount.isNullOrEmpty() && !item.isVolume) {
if (AppConfig.tocCountWords && !item.wordCount.isNullOrEmpty() && !item.isVolume) {
//章节字数
tvWordCount.text = item.wordCount
tvWordCount.visible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
adapter.upDisplayTitles(mLayoutManager.findFirstVisibleItemPosition())
}

override fun upAdapter() {
adapter.notifyItemRangeChanged(0, adapter.itemCount)
}

override val scope: CoroutineScope
get() = lifecycleScope

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>(),
}
menu.findItem(R.id.menu_use_replace)?.isChecked =
AppConfig.tocUiUseReplace
menu.findItem(R.id.menu_load_word_count)?.isChecked =
AppConfig.tocCountWords
menu.findItem(R.id.menu_split_long_chapter)?.isChecked =
viewModel.bookData.value?.getSplitLongChapter() == true
return super.onMenuOpened(featureId, menu)
Expand Down Expand Up @@ -165,6 +167,11 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>(),
viewModel.chapterListCallBack?.upChapterList(searchView?.query?.toString())
}

R.id.menu_load_word_count -> {
AppConfig.tocCountWords = !item.isChecked
viewModel.upChapterListAdapter()
}

R.id.menu_export_bookmark -> exportDir.launch {
requestCode = 1
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
bookMarkCallBack?.upBookmark(newText)
}

fun upChapterListAdapter() {
chapterListCallBack?.upAdapter()
}

fun saveBookmark(treeUri: Uri) {
execute {
val book = bookData.value
Expand Down Expand Up @@ -119,6 +123,8 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
fun upChapterList(searchKey: String?)

fun clearDisplayTitle()

fun upAdapter()
}

interface BookmarkCallBack {
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
Loading
Loading