|
|
@ -16,8 +16,10 @@ import kotlinx.coroutines.Dispatchers |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
|
|
|
|
|
|
|
class AudioPlayViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class AudioPlayViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
var titleData = MutableLiveData<String>() |
|
|
|
|
|
|
|
var coverData = MutableLiveData<String>() |
|
|
|
|
|
|
|
var book: Book? = null |
|
|
|
var inBookshelf = false |
|
|
|
var inBookshelf = false |
|
|
|
var bookData = MutableLiveData<Book>() |
|
|
|
|
|
|
|
var chapterSize = 0 |
|
|
|
var chapterSize = 0 |
|
|
|
var callBack: CallBack? = null |
|
|
|
var callBack: CallBack? = null |
|
|
|
var durChapterIndex = 0 |
|
|
|
var durChapterIndex = 0 |
|
|
@ -30,16 +32,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
inBookshelf = intent.getBooleanExtra("inBookshelf", true) |
|
|
|
inBookshelf = intent.getBooleanExtra("inBookshelf", true) |
|
|
|
val bookUrl = intent.getStringExtra("bookUrl") |
|
|
|
val bookUrl = intent.getStringExtra("bookUrl") |
|
|
|
val book = if (!bookUrl.isNullOrEmpty()) { |
|
|
|
book = if (!bookUrl.isNullOrEmpty()) { |
|
|
|
App.db.bookDao().getBook(bookUrl) |
|
|
|
App.db.bookDao().getBook(bookUrl) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
App.db.bookDao().lastReadBook |
|
|
|
App.db.bookDao().lastReadBook |
|
|
|
} |
|
|
|
} |
|
|
|
book?.let { |
|
|
|
book?.let { book -> |
|
|
|
|
|
|
|
titleData.postValue(book.name) |
|
|
|
durChapterIndex = book.durChapterIndex |
|
|
|
durChapterIndex = book.durChapterIndex |
|
|
|
durPageIndex = book.durChapterPos |
|
|
|
durPageIndex = book.durChapterPos |
|
|
|
isLocalBook = book.origin == BookType.local |
|
|
|
isLocalBook = book.origin == BookType.local |
|
|
|
bookData.postValue(book) |
|
|
|
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
webBook = WebBook(it) |
|
|
|
webBook = WebBook(it) |
|
|
|
} |
|
|
|
} |
|
|
@ -57,7 +59,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
chapterSize = count |
|
|
|
chapterSize = count |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
saveRead(book) |
|
|
|
saveRead() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -148,23 +150,23 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun changeTo(book: Book) { |
|
|
|
fun changeTo(book1: Book) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
App.db.bookDao().insert(book) |
|
|
|
App.db.bookDao().insert(book1) |
|
|
|
bookData.postValue(book) |
|
|
|
book = book1 |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
App.db.bookSourceDao().getBookSource(book1.origin)?.let { |
|
|
|
webBook = WebBook(it) |
|
|
|
webBook = WebBook(it) |
|
|
|
} |
|
|
|
} |
|
|
|
if (book.tocUrl.isEmpty()) { |
|
|
|
if (book1.tocUrl.isEmpty()) { |
|
|
|
loadBookInfo(book) { upChangeDurChapterIndex(book, it) } |
|
|
|
loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loadChapterList(book) { upChangeDurChapterIndex(book, it) } |
|
|
|
loadChapterList(book1) { upChangeDurChapterIndex(book1, it) } |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -187,9 +189,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
fun moveToPrev() { |
|
|
|
fun moveToPrev() { |
|
|
|
if (durChapterIndex > 0) { |
|
|
|
if (durChapterIndex > 0) { |
|
|
|
durChapterIndex-- |
|
|
|
durChapterIndex-- |
|
|
|
bookData.value?.let { |
|
|
|
book?.durChapterIndex = durChapterIndex |
|
|
|
it.durChapterIndex = durChapterIndex |
|
|
|
saveRead() |
|
|
|
saveRead(it) |
|
|
|
book?.let { |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -198,9 +200,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
fun moveToNext() { |
|
|
|
fun moveToNext() { |
|
|
|
if (durChapterIndex < chapterSize - 1) { |
|
|
|
if (durChapterIndex < chapterSize - 1) { |
|
|
|
durChapterIndex++ |
|
|
|
durChapterIndex++ |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
it.durChapterIndex = durChapterIndex |
|
|
|
it.durChapterIndex = durChapterIndex |
|
|
|
saveRead(it) |
|
|
|
saveRead() |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -208,7 +210,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun saveRead(book: Book? = bookData.value) { |
|
|
|
fun saveRead() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
book.lastCheckCount = 0 |
|
|
|
book.lastCheckCount = 0 |
|
|
|