pull/36/head
kunfei 5 years ago
parent bff4043607
commit 6bed8c39c2
  1. 10
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 38
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 23
      app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt
  4. 1
      app/src/main/res/values/strings.xml

@ -38,11 +38,7 @@ object AppConst {
)
}
val defaultBookGroups by lazy {
listOf(
BookGroup(-1, "全部"),
BookGroup(-2, "本地"),
BookGroup(-3, "音频")
)
}
val bookGroupAll = BookGroup(-1, App.INSTANCE.getString(R.string.all))
val bookGroupLocal = BookGroup(-2, App.INSTANCE.getString(R.string.local))
val bookGroupAudio = BookGroup(-3, App.INSTANCE.getString(R.string.audio))
}

@ -17,6 +17,7 @@ import io.legado.app.data.entities.BookGroup
import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getViewModel
import io.legado.app.utils.putPrefInt
import io.legado.app.utils.startActivity
@ -27,13 +28,14 @@ import org.jetbrains.anko.startActivity
class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_bookshelf),
SearchView.OnQueryTextListener,
GroupManageDialog.CallBack,
BookshelfAdapter.CallBack {
override val viewModel: BookshelfViewModel
get() = getViewModel(BookshelfViewModel::class.java)
private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private val bookGroups = mutableListOf<BookGroup>().apply { addAll(AppConst.defaultBookGroups) }
private val bookGroups = mutableListOf<BookGroup>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar)
@ -50,9 +52,8 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
when (item.itemId) {
R.id.menu_search -> startActivity<SearchActivity>()
R.id.menu_bookshelf_layout -> selectBookshelfLayout()
R.id.menu_group_manage -> fragmentManager?.let {
GroupManageDialog().show(it, "groupManageDialog")
}
R.id.menu_group_manage -> GroupManageDialog()
.show(childFragmentManager, "groupManageDialog")
R.id.menu_add_local -> {
}
R.id.menu_add_url -> {
@ -83,11 +84,18 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
for (index in AppConst.defaultBookGroups.size until bookGroups.size) {
bookGroups.removeAt(AppConst.defaultBookGroups.size)
synchronized(this) {
bookGroups.clear()
bookGroups.add(AppConst.bookGroupAll)
if (getPrefBoolean("bookGroupLocal", true)) {
bookGroups.add(AppConst.bookGroupLocal)
}
if (getPrefBoolean("bookGroupAudio", true)) {
bookGroups.add(AppConst.bookGroupAudio)
}
bookGroups.addAll(it)
view_pager_bookshelf.adapter?.notifyDataSetChanged()
}
bookGroups.addAll(it)
view_pager_bookshelf.adapter?.notifyDataSetChanged()
})
}
@ -100,6 +108,20 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
return false
}
override fun upGroup() {
synchronized(this) {
bookGroups.remove(AppConst.bookGroupLocal)
bookGroups.remove(AppConst.bookGroupAudio)
if (getPrefBoolean("bookGroupAudio", true)) {
bookGroups.add(1, AppConst.bookGroupAudio)
}
if (getPrefBoolean("bookGroupLocal", true)) {
bookGroups.add(1, AppConst.bookGroupLocal)
}
view_pager_bookshelf.adapter?.notifyDataSetChanged()
}
}
private fun selectBookshelfLayout() {
selector(
title = "选择书架布局",

@ -24,9 +24,7 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.yesButton
import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel
import io.legado.app.utils.requestInputMethod
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_group_manage.view.*
@ -35,6 +33,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: BookshelfViewModel
private lateinit var adapter: GroupAdapter
private var callBack: CallBack? = null
override fun onStart() {
super.onStart()
@ -54,6 +53,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
callBack = parentFragment as? CallBack
initData()
}
@ -62,6 +62,10 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
tool_bar.inflateMenu(R.menu.book_group_manage)
tool_bar.menu.applyTint(requireContext(), false)
tool_bar.setOnMenuItemClickListener(this)
tool_bar.menu.findItem(R.id.menu_group_local)
.isChecked = getPrefBoolean("bookGroupLocal", true)
tool_bar.menu.findItem(R.id.menu_group_audio)
.isChecked = getPrefBoolean("bookGroupAudio", true)
adapter = GroupAdapter(requireContext())
recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(
@ -76,6 +80,16 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_add -> addGroup()
R.id.menu_group_local -> {
item.isChecked = !item.isChecked
putPrefBoolean("bookGroupLocal", item.isChecked)
callBack?.upGroup()
}
R.id.menu_group_audio -> {
item.isChecked = !item.isChecked
putPrefBoolean("bookGroupAudio", item.isChecked)
callBack?.upGroup()
}
}
return true
}
@ -133,4 +147,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
}
}
interface CallBack {
fun upGroup()
}
}

@ -35,6 +35,7 @@
<string name="replace_purify_search">替换净化-搜索</string>
<string name="bookshelf">书架</string>
<string name="rss">订阅</string>
<string name="all">全部</string>
<string name="recent_reading">最近阅读</string>
<string name="last_read">最后阅读</string>
<string name="read_summary">让阅读成为一种习惯。</string>

Loading…
Cancel
Save