feat: 优化代码

pull/98/head
kunfei 5 years ago
parent 2acf5ae253
commit eaaf0963e0
  1. 2
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 2
      app/src/main/java/io/legado/app/data/entities/BookChapter.kt
  3. 39
      app/src/main/java/io/legado/app/help/BookHelp.kt
  4. 18
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt
  5. 13
      app/src/main/java/io/legado/app/ui/widget/DetailSeekBar.kt

@ -86,6 +86,8 @@ data class Book(
@IgnoredOnParcel
override var tocHtml: String? = null
fun getRealAuthor() = author.replace("作者:", "")
fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0)
fun getDisplayCover() = if (customCoverUrl.isNullOrEmpty()) coverUrl else customCoverUrl

@ -33,7 +33,7 @@ data class BookChapter(
var tag: String? = null, //
var start: Long? = null, // 章节起始位置
var end: Long? = null, // 章节终止位置
var variable: String? = null
var variable: String? = null //变量
) : Parcelable {
@Ignore

@ -51,17 +51,6 @@ object BookHelp {
if (content.isEmpty()) return
if (downloadPath.isContentPath()) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
DocumentUtils.getDirDocument(
root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.uri?.let { uri ->
DocumentUtils.listFiles(App.INSTANCE, uri).forEach {
if (it.name.startsWith(String.format("%05d", bookChapter.index))) {
DocumentFile.fromSingleUri(App.INSTANCE, it.uri)?.delete()
return@forEach
}
}
}
DocumentUtils.createFileIfNotExist(
root,
"${bookChapterName(bookChapter)}.nb",
@ -69,15 +58,6 @@ object BookHelp {
)?.uri?.writeText(App.INSTANCE, content)
}
} else {
FileUtils.createFolderIfNotExist(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).listFiles()?.forEach {
if (it.name.startsWith(String.format("%05d", bookChapter.index))) {
it.delete()
return@forEach
}
}
FileUtils.createFileIfNotExist(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
@ -86,21 +66,28 @@ object BookHelp {
}
}
fun getChapterCount(book: Book): Int {
fun getChapterFiles(book: Book): List<String> {
val fileNameList = arrayListOf<String>()
if (downloadPath.isContentPath()) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { root ->
return DocumentUtils.createFolderIfNotExist(
DocumentUtils.createFolderIfNotExist(
root,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.listFiles()?.size ?: 0
)?.let { bookDoc ->
DocumentUtils.listFiles(App.INSTANCE, bookDoc.uri).forEach {
fileNameList.add(it.name)
}
}
}
} else {
return FileUtils.createFolderIfNotExist(
FileUtils.createFolderIfNotExist(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).list()?.size ?: 0
).list()?.let {
fileNameList.addAll(it)
}
}
return 0
return fileNameList
}
fun hasContent(book: Book, bookChapter: BookChapter): Boolean {

@ -1,12 +1,10 @@
package io.legado.app.ui.download
import android.content.Context
import android.view.View
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book
import io.legado.app.help.BookHelp
import kotlinx.android.synthetic.main.item_download.view.*
@ -17,20 +15,14 @@ class DownloadAdapter(context: Context) :
with(holder.itemView) {
if (payloads.isEmpty()) {
tv_name.text = item.name
tv_author.text = item.author
upDownloadCount(this, item)
tv_author.text = context.getString(R.string.author_show, item.getRealAuthor())
tv_download.setText(R.string.loading)
} else {
upDownloadCount(this, item)
val count = payloads[0] as Int
tv_download.text =
context.getString(R.string.download_count, count, item.totalChapterNum)
}
}
}
private fun upDownloadCount(view: View, book: Book) {
view.tv_download.text = context.getString(
R.string.download_count,
BookHelp.getChapterCount(book),
book.totalChapterNum
)
}
}

@ -29,21 +29,27 @@ class DetailSeekBar(context: Context, attrs: AttributeSet?) : LinearLayoutCompat
iv_seek_add.onClick {
seek_bar.progressAdd(1)
upValue()
onChanged?.invoke(seek_bar.progress)
}
iv_seek_remove.onClick {
seek_bar.progressAdd(-1)
upValue()
onChanged?.invoke(seek_bar.progress)
}
seek_bar.setOnSeekBarChangeListener(this)
}
private fun upValue() {
private fun upValue(progress: Int = seek_bar.progress) {
valueFormat?.let {
tv_seek_value.text = String.format(it, progress)
} ?: let {
tv_seek_value.text = progress.toString()
}
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
upValue(progress)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
@ -51,6 +57,7 @@ class DetailSeekBar(context: Context, attrs: AttributeSet?) : LinearLayoutCompat
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
upValue()
onChanged?.invoke(seek_bar.progress)
}

Loading…
Cancel
Save