|
@@ -11,10 +11,10 @@ import org.koin.core.context.GlobalContext
|
|
|
import io.legado.app.constant.AppLog
|
|
import io.legado.app.constant.AppLog
|
|
|
import io.legado.app.constant.BookType
|
|
import io.legado.app.constant.BookType
|
|
|
import io.legado.app.constant.EventBus
|
|
import io.legado.app.constant.EventBus
|
|
|
-import io.legado.app.data.appDb
|
|
|
|
|
import io.legado.app.data.entities.Book
|
|
import io.legado.app.data.entities.Book
|
|
|
import io.legado.app.data.entities.BookChapter
|
|
import io.legado.app.data.entities.BookChapter
|
|
|
import io.legado.app.data.entities.BookSource
|
|
import io.legado.app.data.entities.BookSource
|
|
|
|
|
+import io.legado.app.data.repository.BookRepository
|
|
|
import io.legado.app.help.book.getBookSource
|
|
import io.legado.app.help.book.getBookSource
|
|
|
import io.legado.app.help.book.removeType
|
|
import io.legado.app.help.book.removeType
|
|
|
import io.legado.app.help.book.simulatedTotalChapterNum
|
|
import io.legado.app.help.book.simulatedTotalChapterNum
|
|
@@ -23,7 +23,10 @@ import io.legado.app.model.webBook.WebBook
|
|
|
import io.legado.app.utils.postEvent
|
|
import io.legado.app.utils.postEvent
|
|
|
import io.legado.app.utils.toastOnUi
|
|
import io.legado.app.utils.toastOnUi
|
|
|
|
|
|
|
|
-class AudioPlayViewModel(application: Application) : BaseViewModel(application) {
|
|
|
|
|
|
|
+class AudioPlayViewModel(
|
|
|
|
|
+ application: Application,
|
|
|
|
|
+ private val bookRepository: BookRepository,
|
|
|
|
|
+) : BaseViewModel(application) {
|
|
|
private val otherSettingsGateway get() = GlobalContext.get().get<OtherSettingsGateway>()
|
|
private val otherSettingsGateway get() = GlobalContext.get().get<OtherSettingsGateway>()
|
|
|
private val readSettingsGateway get() = GlobalContext.get().get<ReadSettingsGateway>()
|
|
private val readSettingsGateway get() = GlobalContext.get().get<ReadSettingsGateway>()
|
|
|
val titleData = MutableLiveData<String>()
|
|
val titleData = MutableLiveData<String>()
|
|
@@ -32,7 +35,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|
|
fun initData(intent: Intent) = AudioPlay.apply {
|
|
fun initData(intent: Intent) = AudioPlay.apply {
|
|
|
execute {
|
|
execute {
|
|
|
val bookUrl = intent.getStringExtra("bookUrl") ?: book?.bookUrl ?: return@execute
|
|
val bookUrl = intent.getStringExtra("bookUrl") ?: book?.bookUrl ?: return@execute
|
|
|
- val book = appDb.bookDao.getBook(bookUrl) ?: return@execute
|
|
|
|
|
|
|
+ val book = bookRepository.getBook(bookUrl) ?: return@execute
|
|
|
inBookshelf = intent.getBooleanExtra("inBookshelf", true)
|
|
inBookshelf = intent.getBooleanExtra("inBookshelf", true)
|
|
|
initBook(book)
|
|
initBook(book)
|
|
|
}.onFinally {
|
|
}.onFinally {
|
|
@@ -74,12 +77,12 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|
|
val oldBook = book.copy()
|
|
val oldBook = book.copy()
|
|
|
val cList = WebBook.getChapterListAwait(bookSource, book).getOrThrow()
|
|
val cList = WebBook.getChapterListAwait(bookSource, book).getOrThrow()
|
|
|
if (oldBook.bookUrl == book.bookUrl) {
|
|
if (oldBook.bookUrl == book.bookUrl) {
|
|
|
- appDb.bookDao.update(book)
|
|
|
|
|
|
|
+ bookRepository.update(book)
|
|
|
} else {
|
|
} else {
|
|
|
- appDb.bookDao.replace(oldBook, book)
|
|
|
|
|
|
|
+ bookRepository.replace(oldBook, book)
|
|
|
}
|
|
}
|
|
|
- appDb.bookChapterDao.delByBook(book.bookUrl)
|
|
|
|
|
- appDb.bookChapterDao.insert(*cList.toTypedArray())
|
|
|
|
|
|
|
+ bookRepository.deleteChaptersByBook(book.bookUrl)
|
|
|
|
|
+ bookRepository.insertChapters(*cList.toTypedArray())
|
|
|
AudioPlay.chapterSize = cList.size
|
|
AudioPlay.chapterSize = cList.size
|
|
|
AudioPlay.simulatedChapterSize = book.simulatedTotalChapterNum()
|
|
AudioPlay.simulatedChapterSize = book.simulatedTotalChapterNum()
|
|
|
AudioPlay.upDurChapter()
|
|
AudioPlay.upDurChapter()
|
|
@@ -107,10 +110,10 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|
|
)
|
|
)
|
|
|
book.removeType(BookType.updateError)
|
|
book.removeType(BookType.updateError)
|
|
|
AudioPlay.book?.delete()
|
|
AudioPlay.book?.delete()
|
|
|
- appDb.bookDao.insert(book)
|
|
|
|
|
|
|
+ bookRepository.insert(book)
|
|
|
AudioPlay.book = book
|
|
AudioPlay.book = book
|
|
|
AudioPlay.bookSource = source
|
|
AudioPlay.bookSource = source
|
|
|
- appDb.bookChapterDao.insert(*toc.toTypedArray())
|
|
|
|
|
|
|
+ bookRepository.insertChapters(*toc.toTypedArray())
|
|
|
AudioPlay.upDurChapter()
|
|
AudioPlay.upDurChapter()
|
|
|
}.onFinally {
|
|
}.onFinally {
|
|
|
postEvent(EventBus.SOURCE_CHANGED, book.bookUrl)
|
|
postEvent(EventBus.SOURCE_CHANGED, book.bookUrl)
|
|
@@ -120,7 +123,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) {
|
|
fun removeFromBookshelf(success: (() -> Unit)?) {
|
|
|
execute {
|
|
execute {
|
|
|
AudioPlay.book?.let {
|
|
AudioPlay.book?.let {
|
|
|
- appDb.bookDao.delete(it)
|
|
|
|
|
|
|
+ bookRepository.delete(it)
|
|
|
}
|
|
}
|
|
|
}.onSuccess {
|
|
}.onSuccess {
|
|
|
success?.invoke()
|
|
success?.invoke()
|
|
@@ -131,11 +134,11 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|
|
execute {
|
|
execute {
|
|
|
book.removeType(BookType.notShelf)
|
|
book.removeType(BookType.notShelf)
|
|
|
if (book.order == 0) {
|
|
if (book.order == 0) {
|
|
|
- book.order = appDb.bookDao.minOrder - 1
|
|
|
|
|
|
|
+ book.order = bookRepository.getMinOrder() - 1
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- appDb.bookDao.insert(book)
|
|
|
|
|
- appDb.bookChapterDao.insert(*toc.toTypedArray())
|
|
|
|
|
|
|
+ bookRepository.insert(book)
|
|
|
|
|
+ bookRepository.insertChapters(*toc.toTypedArray())
|
|
|
}.onSuccess {
|
|
}.onSuccess {
|
|
|
success?.invoke()
|
|
success?.invoke()
|
|
|
}.onError {
|
|
}.onError {
|