pull/32/head
kunfei 5 years ago
parent 2ce78ae4b2
commit 66ea00b41e
  1. 33
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt
  2. 20
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt
  3. 2
      app/src/main/res/layout/activity_book_info.xml
  4. 1
      app/src/main/res/values/strings.xml

@ -10,6 +10,7 @@ import io.legado.app.utils.getViewModel
import io.legado.app.utils.gone import io.legado.app.utils.gone
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.activity_book_info.* import kotlinx.android.synthetic.main.activity_book_info.*
import org.jetbrains.anko.sdk27.listeners.onClick
class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_book_info) { class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_book_info) {
override val viewModel: BookInfoViewModel override val viewModel: BookInfoViewModel
@ -17,14 +18,9 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.bookData.observe(this, Observer { showBook(it) }) viewModel.bookData.observe(this, Observer { showBook(it) })
viewModel.isLoadingData.observe(this, Observer { viewModel.isLoadingData.observe(this, Observer { upLoading(it) })
if (it) {
tv_loading.visible()
} else {
tv_loading.gone()
}
})
viewModel.loadBook(intent) viewModel.loadBook(intent)
initView()
} }
private fun showBook(book: Book) { private fun showBook(book: Book) {
@ -72,5 +68,28 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
} }
} }
private fun upLoading(isLoading: Boolean) {
if (isLoading) {
tv_loading.visible()
} else {
if (viewModel.inBookshelf) {
tv_shelf.text = getString(R.string.remove_from_bookshelf)
} else {
tv_shelf.text = getString(R.string.add_to_shelf)
}
tv_loading.gone()
}
}
private fun initView() {
tv_read.onClick {
} }
tv_shelf.onClick {
viewModel.saveBook {
tv_shelf.text = getString(R.string.remove_from_bookshelf)
}
}
tv_loading.onClick { }
}
}

@ -6,10 +6,12 @@ import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
class BookInfoViewModel(application: Application) : BaseViewModel(application) { class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val bookData = MutableLiveData<Book>() val bookData = MutableLiveData<Book>()
val chapterListData = MutableLiveData<List<BookChapter>>()
val isLoadingData = MutableLiveData<Boolean>() val isLoadingData = MutableLiveData<Boolean>()
var inBookshelf = false var inBookshelf = false
@ -23,7 +25,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} ?: intent.getStringExtra("searchBookUrl")?.let { } ?: intent.getStringExtra("searchBookUrl")?.let {
App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book ->
bookData.postValue(book) bookData.postValue(book)
if (book.tocUrl.isNullOrEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo() loadBookInfo()
} else { } else {
loadChapter() loadChapter()
@ -34,11 +36,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
fun loadBookInfo() { fun loadBookInfo() {
isLoadingData.postValue(false)
} }
fun loadChapter() { fun loadChapter() {
isLoadingData.postValue(false)
} }
fun saveBook(success: (() -> Unit)?) {
execute {
bookData.value?.let { book ->
App.db.bookDao().insert(book)
}
chapterListData.value?.let {
App.db.bookChapterDao().insert(*it.toTypedArray())
}
}.onSuccess {
success?.invoke()
}
}
} }

@ -224,7 +224,7 @@
android:focusable="true" android:focusable="true"
android:gravity="center" android:gravity="center"
android:includeFontPadding="false" android:includeFontPadding="false"
android:text="@string/start_read" android:text="@string/reading"
android:textColor="@color/tv_text_default" android:textColor="@color/tv_text_default"
android:textSize="15sp" /> android:textSize="15sp" />

@ -473,4 +473,5 @@
<string name="my">我的</string> <string name="my">我的</string>
<string name="dsm_start"><![CDATA[<]]></string> <string name="dsm_start"><![CDATA[<]]></string>
<string name="dsm_end"><![CDATA[>]]></string> <string name="dsm_end"><![CDATA[>]]></string>
<string name="reading">阅读</string>
</resources> </resources>

Loading…
Cancel
Save