From 4d3c757f13debb56e9a3d6a8e080da7ca9ee0a19 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 27 Dec 2019 09:39:26 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/data/dao/BookDao.kt | 3 +++ app/src/main/java/io/legado/app/help/BookHelp.kt | 15 +++++++++++++-- .../io/legado/app/ui/download/DownloadActivity.kt | 2 ++ .../io/legado/app/ui/download/DownloadAdapter.kt | 7 +++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 42e76ddd6..d7fa8e1cf 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -17,6 +17,9 @@ interface BookDao { @Query("SELECT * FROM books WHERE origin = '${BookType.local}' order by durChapterTime desc") fun observeLocal(): LiveData> + @Query("SELECT * FROM books WHERE origin <> '${BookType.local}' order by durChapterTime desc") + fun observeWeb(): LiveData> + @Query("SELECT * FROM books WHERE `group` = :group") fun observeByGroup(group: Int): LiveData> diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index e6f17134a..5070a74dc 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -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 { diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt index 2ae90c8a5..633d38ca9 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt @@ -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) }) diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt index 8407633ff..c6825041f 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt @@ -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(context, R.layout.item_download) { - override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList) { - + with(holder.itemView) { + tv_name.text = item.name + tv_author.text = item.author + } } } \ No newline at end of file