|
|
@ -21,7 +21,6 @@ import kotlinx.coroutines.launch |
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
var inBookshelf = false |
|
|
|
var inBookshelf = false |
|
|
|
var bookData = MutableLiveData<Book>() |
|
|
|
var bookData = MutableLiveData<Book>() |
|
|
|
val chapterListFinish = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
var chapterSize = 0 |
|
|
|
var chapterSize = 0 |
|
|
|
var bookSource: BookSource? = null |
|
|
|
var bookSource: BookSource? = null |
|
|
|
var callBack: CallBack? = null |
|
|
|
var callBack: CallBack? = null |
|
|
@ -61,7 +60,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
durChapterIndex = count - 1 |
|
|
|
durChapterIndex = count - 1 |
|
|
|
} |
|
|
|
} |
|
|
|
chapterSize = count |
|
|
|
chapterSize = count |
|
|
|
chapterListFinish.postValue(true) |
|
|
|
loadChapterListFinish() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -85,7 +84,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
if (!cList.isNullOrEmpty()) { |
|
|
|
if (!cList.isNullOrEmpty()) { |
|
|
|
App.db.bookChapterDao().insert(*cList.toTypedArray()) |
|
|
|
App.db.bookChapterDao().insert(*cList.toTypedArray()) |
|
|
|
chapterSize = cList.size |
|
|
|
chapterSize = cList.size |
|
|
|
chapterListFinish.postValue(true) |
|
|
|
loadChapterListFinish() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toast(R.string.error_load_toc) |
|
|
|
toast(R.string.error_load_toc) |
|
|
|
} |
|
|
|
} |
|
|
@ -95,6 +94,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun loadChapterListFinish() { |
|
|
|
|
|
|
|
bookData.value?.let { |
|
|
|
|
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
|
|
|
|
loadContent(it, durChapterIndex + 1) |
|
|
|
|
|
|
|
loadContent(it, durChapterIndex - 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun loadContent(book: Book, index: Int) { |
|
|
|
fun loadContent(book: Book, index: Int) { |
|
|
|
synchronized(loadingLock) { |
|
|
|
synchronized(loadingLock) { |
|
|
|
if (loadingChapters.contains(index)) return |
|
|
|
if (loadingChapters.contains(index)) return |
|
|
@ -193,18 +200,22 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
durPageIndex = 0 |
|
|
|
durPageIndex = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
saveRead() |
|
|
|
saveRead() |
|
|
|
chapterListFinish.postValue(true) |
|
|
|
loadChapterListFinish() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun saveRead() { |
|
|
|
fun saveRead() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookData.value?.let { book -> |
|
|
|
bookData.value?.let { book -> |
|
|
|
|
|
|
|
book.durChapterTime = System.currentTimeMillis() |
|
|
|
book.durChapterIndex = durChapterIndex |
|
|
|
book.durChapterIndex = durChapterIndex |
|
|
|
book.durChapterPos = durPageIndex |
|
|
|
book.durChapterPos = durPageIndex |
|
|
|
|
|
|
|
curTextChapter?.let { |
|
|
|
|
|
|
|
book.durChapterTitle = it.title |
|
|
|
App.db.bookDao().update(book) |
|
|
|
App.db.bookDao().update(book) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
@ -233,7 +244,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
loadContent(it, durChapterIndex.plus(1)) |
|
|
|
loadContent(it, durChapterIndex.plus(1)) |
|
|
|
launch(IO) { |
|
|
|
launch(IO) { |
|
|
|
for (i in 2..10) { |
|
|
|
for (i in 2..10) { |
|
|
|
delay(5000L) |
|
|
|
delay(1000L) |
|
|
|
bookData.value?.let { book -> |
|
|
|
bookData.value?.let { book -> |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
} |
|
|
|
} |
|
|
|