pull/63/head
kunfei 5 years ago
parent 3339c0540c
commit 4d3c757f13
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 15
      app/src/main/java/io/legado/app/help/BookHelp.kt
  3. 2
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  4. 7
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt

@ -17,6 +17,9 @@ interface BookDao {
@Query("SELECT * FROM books WHERE origin = '${BookType.local}' order by durChapterTime desc")
fun observeLocal(): LiveData<List<Book>>
@Query("SELECT * FROM books WHERE origin <> '${BookType.local}' order by durChapterTime desc")
fun observeWeb(): LiveData<List<Book>>
@Query("SELECT * FROM books WHERE `group` = :group")
fun observeByGroup(group: Int): LiveData<List<Book>>

@ -35,8 +35,15 @@ object BookHelp {
FileHelp.getFolder(getBookCachePath())
}
@Synchronized
fun saveContent(book: Book, bookChapter: BookChapter, content: String) {
if (content.isEmpty()) return
FileHelp.getFolder(getBookFolder(book)).listFiles()?.forEach {
if (it.name.startsWith(String.format("%05d", bookChapter.index))) {
it.delete()
return@forEach
}
}
val filePath = getChapterPath(book, bookChapter)
val file = FileHelp.getFile(filePath)
file.writeText(content)
@ -74,11 +81,15 @@ object BookHelp {
}
}
private fun getChapterPath(book: Book, bookChapter: BookChapter): String {
private fun getBookFolder(book: Book): String {
val bookFolder = formatFolderName(book.name + book.bookUrl)
return "${getBookCachePath()}${File.separator}$bookFolder"
}
private fun getChapterPath(book: Book, bookChapter: BookChapter): String {
val chapterFile =
String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title))
return "${getBookCachePath()}${File.separator}$bookFolder${File.separator}$chapterFile.nb"
return "${getBookFolder(book)}${File.separator}$chapterFile.nb"
}
private fun formatFolderName(folderName: String): String {

@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseActivity
import io.legado.app.data.entities.Book
@ -28,6 +29,7 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) {
private fun initLiveData() {
bookshelfLiveData?.removeObservers(this)
bookshelfLiveData = App.db.bookDao().observeWeb()
bookshelfLiveData?.observe(this, Observer {
adapter.setItems(it)
})

@ -5,14 +5,17 @@ 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 kotlinx.android.synthetic.main.item_download.view.*
class DownloadAdapter(context: Context) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_download) {
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) {
tv_name.text = item.name
tv_author.text = item.author
}
}
}
Loading…
Cancel
Save