From 0d6f013f9b0a11b20e0a425534dcc9dcffaefd6d Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 16 Aug 2019 15:21:47 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/help/BookHelp.kt | 6 +++- .../app/ui/readbook/ReadBookViewModel.kt | 32 +++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) 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 69c95140d..b71c3dfe9 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -12,7 +12,11 @@ import java.nio.charset.StandardCharsets object BookHelp { - var downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) + private var downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) + + fun upDownloadPath() { + downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) + } fun saveContent(book: Book, bookChapter: BookChapter, content: String) { if (content.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index d793c0cb0..029be993e 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -7,13 +7,16 @@ import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource +import io.legado.app.help.BookHelp import io.legado.app.model.WebBook +import kotlinx.coroutines.Dispatchers.IO class ReadBookViewModel(application: Application) : BaseViewModel(application) { var book: Book? = null var bookSource: BookSource? = null var chapterMaxIndex = MediatorLiveData() + var webBook: WebBook? = null fun initData(intent: Intent) { val bookUrl = intent.getStringExtra("bookUrl") @@ -21,20 +24,20 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { book = App.db.bookDao().getBook(bookUrl) book?.let { book -> - val count = App.db.bookChapterDao().getChapterCount(bookUrl) - if (count == 0) { - App.db.bookSourceDao().getBookSource(book.origin)?.let { - WebBook(it).getChapterList(book) - .onSuccess { cList -> + bookSource = App.db.bookSourceDao().getBookSource(book.origin) + bookSource?.let { + webBook = WebBook(it) + val count = App.db.bookChapterDao().getChapterCount(bookUrl) + if (count == 0) { + webBook?.getChapterList(book) + ?.onSuccess { cList -> cList?.let { App.db.bookChapterDao().insert(*cList.toTypedArray()) } } - } ?: let { - + } else { + chapterMaxIndex.postValue(count) } - } else { - chapterMaxIndex.postValue(count) } } @@ -42,4 +45,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + + fun download(book: Book, index: Int) { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + webBook?.getContent(book, chapter) + ?.onSuccess(IO) { content -> + content?.let { + BookHelp.saveContent(book, chapter, it) + } + } + } + } } \ No newline at end of file