feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 5608c283ec
commit dcc5e19299
  1. 6
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  2. 26
      app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt
  3. 25
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  5. 49
      app/src/main/res/layout/dialog_book_group_picker.xml

@ -162,18 +162,18 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
GroupSelectDialog.show(supportFragmentManager, groupId, requestCode) GroupSelectDialog.show(supportFragmentManager, groupId, requestCode)
} }
override fun upGroup(requestCode: Int, group: BookGroup) { override fun upGroup(requestCode: Int, groupId: Int) {
when (requestCode) { when (requestCode) {
groupRequestCode -> { groupRequestCode -> {
val books = arrayListOf<Book>() val books = arrayListOf<Book>()
adapter.selectedBooks.forEach { adapter.selectedBooks.forEach {
books.add(it.copy(group = group.groupId)) books.add(it.copy(group = groupId))
} }
viewModel.updateBook(*books.toTypedArray()) viewModel.updateBook(*books.toTypedArray())
} }
adapter.groupRequestCode -> { adapter.groupRequestCode -> {
adapter.actionItem?.let { adapter.actionItem?.let {
viewModel.updateBook(it.copy(group = group.groupId)) viewModel.updateBook(it.copy(group = groupId))
} }
} }
} }

@ -27,12 +27,15 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.yesButton import io.legado.app.lib.dialogs.yesButton
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getVerticalDivider
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.requestInputMethod import io.legado.app.utils.requestInputMethod
import kotlinx.android.synthetic.main.dialog_book_group_picker.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.dialog_recycler_view.recycler_view
import kotlinx.android.synthetic.main.dialog_recycler_view.tool_bar
import kotlinx.android.synthetic.main.item_group_select.view.* import kotlinx.android.synthetic.main.item_group_select.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.* import java.util.*
@ -72,7 +75,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
viewModel = getViewModel(GroupViewModel::class.java) viewModel = getViewModel(GroupViewModel::class.java)
return inflater.inflate(R.layout.dialog_recycler_view, container) return inflater.inflate(R.layout.dialog_book_group_picker, container)
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -82,10 +85,11 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
groupId = it.getInt("groupId") groupId = it.getInt("groupId")
requestCode = it.getInt("requestCode", -1) requestCode = it.getInt("requestCode", -1)
} }
initView()
initData() initData()
} }
private fun initData() { private fun initView() {
tool_bar.title = getString(R.string.group_select) tool_bar.title = getString(R.string.group_select)
tool_bar.inflateMenu(R.menu.book_group_manage) tool_bar.inflateMenu(R.menu.book_group_manage)
tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.menu.applyTint(requireContext(), Theme.getTheme())
@ -96,13 +100,21 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(recycler_view.getVerticalDivider()) recycler_view.addItemDecoration(recycler_view.getVerticalDivider())
recycler_view.adapter = adapter recycler_view.adapter = adapter
App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
})
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
tv_cancel.onClick { dismiss() }
tv_ok.setTextColor(requireContext().accentColor)
tv_ok.onClick {
callBack?.upGroup(requestCode, groupId)
}
}
private fun initData() {
App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
})
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
@ -195,6 +207,6 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
} }
interface CallBack { interface CallBack {
fun upGroup(requestCode: Int, group: BookGroup) fun upGroup(requestCode: Int, groupId: Int)
} }
} }

@ -17,7 +17,6 @@ import io.legado.app.constant.BookType
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
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.BookGroup
import io.legado.app.help.BlurTransformation import io.legado.app.help.BlurTransformation
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
@ -57,13 +56,6 @@ class BookInfoActivity :
tv_intro.movementMethod = ScrollingMovementMethod.getInstance() tv_intro.movementMethod = ScrollingMovementMethod.getInstance()
viewModel.bookData.observe(this, Observer { showBook(it) }) viewModel.bookData.observe(this, Observer { showBook(it) })
viewModel.chapterListData.observe(this, Observer { upLoading(false, it) }) viewModel.chapterListData.observe(this, Observer { upLoading(false, it) })
viewModel.groupData.observe(this, Observer {
if (it == null) {
tv_group.text = getString(R.string.group_s, getString(R.string.no_group))
} else {
tv_group.text = getString(R.string.group_s, it.groupName)
}
})
viewModel.initData(intent) viewModel.initData(intent)
initOnClick() initOnClick()
} }
@ -147,6 +139,7 @@ class BookInfoActivity :
} }
} }
} }
upGroup(book.group)
} }
private fun showCover(book: Book) { private fun showCover(book: Book) {
@ -193,6 +186,16 @@ class BookInfoActivity :
} }
} }
private fun upGroup(groupId: Int) {
viewModel.loadGroup(groupId) {
if (it.isNullOrEmpty()) {
tv_group.text = getString(R.string.group_s, getString(R.string.no_group))
} else {
tv_group.text = getString(R.string.group_s, it)
}
}
}
private fun initOnClick() { private fun initOnClick() {
iv_cover.onClick { iv_cover.onClick {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
@ -314,9 +317,9 @@ class BookInfoActivity :
return viewModel.durChapterIndex return viewModel.durChapterIndex
} }
override fun upGroup(requestCode: Int, group: BookGroup) { override fun upGroup(requestCode: Int, groupId: Int) {
viewModel.groupData.postValue(group) upGroup(groupId)
viewModel.bookData.value?.group = group.groupId viewModel.bookData.value?.group = groupId
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {
viewModel.saveBook() viewModel.saveBook()
} }

@ -8,7 +8,6 @@ import io.legado.app.R
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 import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookGroup
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.model.localBook.AnalyzeTxtFile
@ -19,13 +18,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val chapterListData = MutableLiveData<List<BookChapter>>() val chapterListData = MutableLiveData<List<BookChapter>>()
var durChapterIndex = 0 var durChapterIndex = 0
var inBookshelf = false var inBookshelf = false
var groupData = MutableLiveData<BookGroup>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
intent.getStringExtra("bookUrl")?.let { intent.getStringExtra("bookUrl")?.let {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
groupData.postValue(App.db.bookGroupDao().getByID(book.group))
inBookshelf = true inBookshelf = true
setBook(book) setBook(book)
} ?: App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> } ?: App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book ->
@ -121,6 +118,20 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun loadGroup(groupId: Int, success: ((groupNames: String?) -> Unit)) {
execute {
val groupNames = arrayListOf<String>()
App.db.bookGroupDao().all.forEach {
if (groupId and it.groupId > 0) {
groupNames.add(it.groupName)
}
}
groupNames.joinToString(",")
}.onSuccess {
success.invoke(it)
}
}
fun changeTo(book: Book) { fun changeTo(book: Book) {
execute { execute {
if (inBookshelf) { if (inBookshelf) {

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
android:background="@color/background_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:background="@color/background_card"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:padding="12dp"
android:text="@string/cancel"
tools:ignore="RtlHardcoded" />
<TextView
android:id="@+id/tv_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:padding="12dp"
android:text="@string/ok"
tools:ignore="RtlHardcoded" />
</LinearLayout>
</LinearLayout>
Loading…
Cancel
Save