pull/47/head
kunfei 5 years ago
parent 68a5889ff2
commit 37feed6768
  1. 20
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 50
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -74,7 +74,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
initView() initView()
viewModel.callBack = this viewModel.callBack = this
viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) viewModel.titleDate.observe(this, Observer { title_bar.title = it })
viewModel.initData(intent) viewModel.initData(intent)
} }
@ -141,12 +141,12 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
when (item.itemId) { when (item.itemId) {
R.id.menu_change_source -> { R.id.menu_change_source -> {
read_menu.runMenuOut() read_menu.runMenuOut()
viewModel.bookData.value?.let { viewModel.book?.let {
ChangeSourceDialog.show(supportFragmentManager, it.name, it.author) ChangeSourceDialog.show(supportFragmentManager, it.name, it.author)
} }
} }
R.id.menu_refresh -> { R.id.menu_refresh -> {
viewModel.bookData.value?.let { viewModel.book?.let {
viewModel.curTextChapter = null viewModel.curTextChapter = null
page_view.upContent() page_view.upContent()
viewModel.refreshContent(it) viewModel.refreshContent(it)
@ -255,7 +255,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 加载章节内容 * 加载章节内容
*/ */
override fun loadContent() { override fun loadContent() {
viewModel.bookData.value?.let { viewModel.book?.let {
viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex)
viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(it, viewModel.durChapterIndex + 1)
viewModel.loadContent(it, viewModel.durChapterIndex - 1) viewModel.loadContent(it, viewModel.durChapterIndex - 1)
@ -266,7 +266,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 加载章节内容, index章节序号 * 加载章节内容, index章节序号
*/ */
override fun loadContent(index: Int) { override fun loadContent(index: Int) {
viewModel.bookData.value?.let { viewModel.book?.let {
viewModel.loadContent(it, index) viewModel.loadContent(it, index)
} }
} }
@ -337,10 +337,10 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override val curOrigin: String? override val curOrigin: String?
get() = viewModel.bookData.value?.origin get() = viewModel.book?.origin
override val oldBook: Book? override val oldBook: Book?
get() = viewModel.bookData.value get() = viewModel.book
override fun changeTo(book: Book) { override fun changeTo(book: Book) {
viewModel.changeTo(book) viewModel.changeTo(book)
@ -436,7 +436,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun openChapterList() { override fun openChapterList() {
viewModel.bookData.value?.let { viewModel.book?.let {
startActivityForResult<ChapterListActivity>( startActivityForResult<ChapterListActivity>(
requestCodeChapterList, requestCodeChapterList,
Pair("bookUrl", it.bookUrl) Pair("bookUrl", it.bookUrl)
@ -475,7 +475,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 朗读 * 朗读
*/ */
private fun readAloud(play: Boolean = true) { private fun readAloud(play: Boolean = true) {
val book = viewModel.bookData.value val book = viewModel.book
val textChapter = viewModel.curTextChapter val textChapter = viewModel.curTextChapter
if (book != null && textChapter != null) { if (book != null && textChapter != null) {
val key = IntentDataHelp.putData(textChapter) val key = IntentDataHelp.putData(textChapter)
@ -523,7 +523,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun finish() { override fun finish() {
viewModel.bookData.value?.let { viewModel.book?.let {
if (!viewModel.inBookshelf) { if (!viewModel.inBookshelf) {
this.alert(title = getString(R.string.add_to_shelf)) { this.alert(title = getString(R.string.add_to_shelf)) {
message = getString(R.string.check_add_bookshelf, it.name) message = getString(R.string.check_add_bookshelf, it.name)

@ -21,8 +21,9 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var titleDate = 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
@ -38,22 +39,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
inBookshelf = intent.getBooleanExtra("inBookshelf", true) inBookshelf = intent.getBooleanExtra("inBookshelf", true)
IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let {
setBook(it) initBook(it)
} ?: intent.getStringExtra("bookUrl")?.let { } ?: intent.getStringExtra("bookUrl")?.let {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
setBook(book) initBook(book)
} }
} ?: App.db.bookDao().lastReadBook?.let { } ?: App.db.bookDao().lastReadBook?.let {
setBook(it) initBook(it)
} }
} }
} }
private fun setBook(book: Book) { private fun initBook(book: Book) {
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) this.book = book
titleDate.postValue(book.name)
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) webBook = WebBook(it)
} }
@ -72,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
callBack?.loadContent() callBack?.loadContent()
} }
if (inBookshelf) { if (inBookshelf) {
saveRead(book) saveRead()
} }
} }
@ -117,7 +119,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
prevTextChapter = curTextChapter prevTextChapter = curTextChapter
curTextChapter = nextTextChapter curTextChapter = nextTextChapter
nextTextChapter = null nextTextChapter = null
bookData.value?.let { book?.let {
if (curTextChapter == null) { if (curTextChapter == null) {
loadContent(it, durChapterIndex) loadContent(it, durChapterIndex)
} else if (upContent) { } else if (upContent) {
@ -127,7 +129,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
launch(IO) { launch(IO) {
for (i in 2..10) { for (i in 2..10) {
delay(100) delay(100)
bookData.value?.let { book -> book?.let { book ->
download(book, durChapterIndex + i) download(book, durChapterIndex + i)
} }
} }
@ -140,7 +142,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
nextTextChapter = curTextChapter nextTextChapter = curTextChapter
curTextChapter = prevTextChapter curTextChapter = prevTextChapter
prevTextChapter = null prevTextChapter = null
bookData.value?.let { book?.let {
if (curTextChapter == null) { if (curTextChapter == null) {
loadContent(it, durChapterIndex) loadContent(it, durChapterIndex)
} else if (upContent) { } else if (upContent) {
@ -150,7 +152,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
launch(IO) { launch(IO) {
for (i in -5..-2) { for (i in -5..-2) {
delay(100) delay(100)
bookData.value?.let { book -> book?.let { book ->
download(book, durChapterIndex + i) download(book, durChapterIndex + i)
} }
} }
@ -225,19 +227,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
val c = BookHelp.disposeContent( val c = BookHelp.disposeContent(
chapter.title, chapter.title,
bookData.value!!.name, book!!.name,
webBook?.bookSource?.bookSourceUrl, webBook?.bookSource?.bookSourceUrl,
content, content,
bookData.value!!.useReplaceRule book!!.useReplaceRule
) )
callBack?.contentLoadFinish(chapter, c) callBack?.contentLoadFinish(chapter, c)
} }
} }
} }
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)
} }
prevTextChapter = null prevTextChapter = null
@ -246,15 +248,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
withContext(Main) { withContext(Main) {
callBack?.upContent() callBack?.upContent()
} }
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) }
} }
} }
} }
@ -291,7 +293,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
callBack?.loadContent() callBack?.loadContent()
} }
fun saveRead(book: Book? = bookData.value) { fun saveRead() {
execute { execute {
book?.let { book -> book?.let { book ->
book.lastCheckCount = 0 book.lastCheckCount = 0
@ -308,7 +310,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun removeFromBookshelf(success: (() -> Unit)?) { fun removeFromBookshelf(success: (() -> Unit)?) {
execute { execute {
bookData.value?.let { book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
}.onSuccess { }.onSuccess {
@ -318,7 +320,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun upBookSource() { fun upBookSource() {
execute { execute {
bookData.value?.let { book -> book?.let { book ->
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) webBook = WebBook(it)
} }

Loading…
Cancel
Save