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

章节增加字数统计字段,网络书籍增加字数统计 #4437

Merged
merged 1 commit into from
Dec 15, 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
1,918 changes: 1,918 additions & 0 deletions app/schemas/io.legado.app.data.AppDatabase/73.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/src/main/java/io/legado/app/data/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ val appDb by lazy {
}

@Database(
version = 72,
version = 73,
exportSchema = true,
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class,
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
Expand Down Expand Up @@ -100,6 +100,7 @@ val appDb by lazy {
AutoMigration(from = 69, to = 70),
AutoMigration(from = 70, to = 71),
AutoMigration(from = 71, to = 72),
AutoMigration(from = 72, to = 73),
]
)
abstract class AppDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data class BookChapter(
var isPay: Boolean = false, // 是否已购买
var resourceUrl: String? = null, // 音频真实URL
var tag: String? = null, // 更新时间或其他章节附加信息
var wordCount: String? = null, // 本章节字数
var start: Long? = null, // 章节起始位置
var end: Long? = null, // 章节终止位置
var startFragmentId: String? = null, //EPUB书籍当前章节的fragmentId
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/legado/app/help/book/BookHelp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ object BookHelp {
book.getFolderName(),
bookChapter.getFileName(),
).writeText(content)
bookChapter.wordCount = StringUtils.wordCountFormat(content.length)
appDb.bookChapterDao.update(bookChapter)
}

suspend fun saveImages(
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/io/legado/app/model/localBook/TextFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class TextFile(private var book: Book) {
* 按规则解析目录
*/
private fun analyze(pattern: Pattern?): Pair<ArrayList<BookChapter>, Int> {
if (pattern?.pattern().isNullOrEmpty()) {
if (pattern == null || pattern.pattern().isNullOrEmpty()) {
return analyze()
}
val toc = arrayListOf<BookChapter>()
Expand Down Expand Up @@ -227,7 +227,7 @@ class TextFile(private var book: Book) {
qyChapter.title = "前言"
qyChapter.start = curOffset
qyChapter.end = curOffset + chapterLength
qyChapter.tag = StringUtils.wordCountFormat(chapterContent.length)
qyChapter.wordCount = StringUtils.wordCountFormat(chapterContent.length)
toc.add(qyChapter)
book.intro = if (chapterContent.length <= 500) {
chapterContent
Expand All @@ -248,7 +248,7 @@ class TextFile(private var book: Book) {
//将当前段落添加上一章去
lastChapter.end = lastChapter.end!! + chapterLength
lastChapterWordCount += chapterContent.length
lastChapter.tag = StringUtils.wordCountFormat(lastChapterWordCount)
lastChapter.wordCount = StringUtils.wordCountFormat(lastChapterWordCount)
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
Expand All @@ -265,7 +265,7 @@ class TextFile(private var book: Book) {
chapterContent.substringAfter(lastChapter.title).isBlank()
lastChapter.end =
lastChapter.start!! + chapterLength
lastChapter.tag = StringUtils.wordCountFormat(chapterContent.length)
lastChapter.wordCount = StringUtils.wordCountFormat(chapterContent.length)
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
Expand All @@ -276,7 +276,7 @@ class TextFile(private var book: Book) {
curChapter.title = matcher.group()
curChapter.start = curOffset
curChapter.end = curOffset
curChapter.tag = StringUtils.wordCountFormat(chapterContent.length)
curChapter.wordCount = StringUtils.wordCountFormat(chapterContent.length)
toc.add(curChapter)
}
bookWordCount += chapterContent.length
Expand All @@ -293,7 +293,7 @@ class TextFile(private var book: Book) {
//设置上一章的结尾
toc.lastOrNull()?.let {
it.end = curOffset
it.tag = StringUtils.wordCountFormat(lastChapterWordCount)
it.wordCount = StringUtils.wordCountFormat(lastChapterWordCount)
}
}
toc.lastOrNull()?.let { chapter ->
Expand Down Expand Up @@ -383,7 +383,7 @@ class TextFile(private var book: Book) {
chapter.title = "第${blockPos}章($chapterPos)"
chapter.start = toc.lastOrNull()?.end ?: curOffset
chapter.end = chapter.start!! + end - chapterOffset
chapter.tag = StringUtils.wordCountFormat(content.length)
chapter.wordCount = StringUtils.wordCountFormat(content.length)
toc.add(chapter)
//减去已经被分配的长度
strLength -= (end - chapterOffset)
Expand All @@ -407,13 +407,13 @@ class TextFile(private var book: Book) {
chapter.title = "第${blockPos}章(${chapterPos})"
chapter.start = toc.lastOrNull()?.end ?: curOffset
chapter.end = chapter.start!! + bufferStart
chapter.tag = StringUtils.wordCountFormat(content.length)
chapter.wordCount = StringUtils.wordCountFormat(content.length)
toc.add(chapter)
} else {
val wordCount = lastChapterWordCount + content.length
toc.lastOrNull()?.let {
it.end = it.end!! + bufferStart
it.tag = StringUtils.wordCountFormat(wordCount)
it.wordCount = StringUtils.wordCountFormat(wordCount)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.text.TextUtils
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.R
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
Expand Down Expand Up @@ -162,6 +163,7 @@ object BookChapterList {
list.getOrElse(book.simulatedTotalChapterNum() - 1) { list.last() }
.getDisplayTitle(replaceRules, book.getUseReplaceRule())
coroutineContext.ensureActive()
getWordCount(list, book)
return list
}

Expand Down Expand Up @@ -267,4 +269,17 @@ object BookChapterList {
return Pair(chapterList, nextUrlList)
}

private fun getWordCount(list: ArrayList<BookChapter>, book: Book) {
val chapterList = appDb.bookChapterDao.getChapterList(book.bookUrl)
if (chapterList.isNotEmpty()){
val map = chapterList.associateBy({ it.getFileName() }, { it.wordCount })
for (bookChapter in list) {
val wordCount = map[bookChapter.getFileName()]
if(wordCount != null){
bookChapter.wordCount = wordCount
}
}
}
}

}
23 changes: 16 additions & 7 deletions app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,22 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
//普通章节 保持不变
tvChapterItem.background =
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
}
if (!item.tag.isNullOrEmpty() && !item.isVolume) {
//卷名不显示tag(更新时间规则)
tvTag.text = item.tag
tvTag.visible()
} else {
tvTag.gone()

//卷名不显示
if (!item.tag.isNullOrEmpty()) {
//更新时间规则
tvTag.text = item.tag
tvTag.visible()
} else {
tvTag.gone()
}
if (!item.wordCount.isNullOrEmpty()) {
//章节字数
tvWordCount.text = item.wordCount
tvWordCount.visible()
} else {
tvWordCount.gone()
}
}
upHasCache(binding, isDur, cached)
} else {
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/res/layout/item_chapter_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
android:singleLine="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/iv_checked"
app:layout_constraintBottom_toTopOf="@id/tv_tag" />
app:layout_constraintRight_toLeftOf="@+id/iv_checked" />

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

<TextView
android:id="@+id/tv_tag"
Expand All @@ -27,7 +38,7 @@
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_chapter_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_word_count"
app:layout_constraintRight_toLeftOf="@+id/iv_checked" />

<ImageView
Expand Down
Loading