feat: 优化代码

pull/141/head
kunfei 5 years ago
parent 3821d7442e
commit 18b98307ec
  1. 23
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  2. 7
      app/src/main/java/io/legado/app/web/controller/BookshelfController.kt

@ -42,7 +42,7 @@ import kotlin.collections.ArrayList
class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: GroupViewModel private lateinit var viewModel: GroupViewModel
private lateinit var adapter: GroupAdapter private lateinit var adapter: GroupAdapter
private var callBack: CallBack? = null private val callBack: CallBack? get() = parentFragment as? CallBack
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -62,19 +62,12 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
callBack = parentFragment as? CallBack tool_bar.title = getString(R.string.group_manage)
initData() initData()
initMenu()
} }
private fun initData() { private fun initData() {
tool_bar.title = getString(R.string.group_manage)
tool_bar.inflateMenu(R.menu.book_group_manage)
tool_bar.menu.applyTint(requireContext(), Theme.getTheme())
tool_bar.setOnMenuItemClickListener(this)
tool_bar.menu.findItem(R.id.menu_group_local)
.isChecked = AppConst.bookGroupLocalShow
tool_bar.menu.findItem(R.id.menu_group_audio)
.isChecked = AppConst.bookGroupAudioShow
adapter = GroupAdapter(requireContext()) adapter = GroupAdapter(requireContext())
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(VerticalDivider(requireContext())) recycler_view.addItemDecoration(VerticalDivider(requireContext()))
@ -90,6 +83,16 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
} }
private fun initMenu() {
tool_bar.inflateMenu(R.menu.book_group_manage)
tool_bar.menu.applyTint(requireContext(), Theme.getTheme())
tool_bar.setOnMenuItemClickListener(this)
tool_bar.menu.findItem(R.id.menu_group_local)
.isChecked = AppConst.bookGroupLocalShow
tool_bar.menu.findItem(R.id.menu_group_audio)
.isChecked = AppConst.bookGroupAudioShow
}
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_add -> addGroup() R.id.menu_add -> addGroup()

@ -45,15 +45,16 @@ class BookshelfController {
if (book == null || chapter == null) { if (book == null || chapter == null) {
returnData.setErrorMsg("未找到") returnData.setErrorMsg("未找到")
} else { } else {
var content = BookHelp.getContent(book, chapter) val content: String? = BookHelp.getContent(book, chapter)
if (content != null) { if (content != null) {
returnData.setData(content) returnData.setData(content)
} else { } else {
App.db.bookSourceDao().getBookSource(book.origin)?.let { source -> App.db.bookSourceDao().getBookSource(book.origin)?.let { source ->
content = runBlocking { runBlocking {
WebBook(source).getContentSuspend(book, chapter) WebBook(source).getContentSuspend(book, chapter)
}.let {
returnData.setData(it)
} }
returnData.setErrorMsg(content)
} ?: returnData.setErrorMsg("未找到书源") } ?: returnData.setErrorMsg("未找到书源")
} }
} }

Loading…
Cancel
Save