From 6bed8c39c2a67b2c0a0b27a02b9d2b225047678d Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 28 Sep 2019 22:00:35 +0800 Subject: [PATCH] up --- .../java/io/legado/app/constant/AppConst.kt | 10 ++--- .../ui/main/bookshelf/BookshelfFragment.kt | 38 +++++++++++++++---- .../ui/main/bookshelf/GroupManageDialog.kt | 23 +++++++++-- app/src/main/res/values/strings.xml | 1 + 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index f9fe5b942..99dcb17cb 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -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)) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 008ff6131..797ef096d 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -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(R.layout.fragment_bookshelf), SearchView.OnQueryTextListener, + GroupManageDialog.CallBack, BookshelfAdapter.CallBack { override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) private var bookGroupLiveData: LiveData>? = null - private val bookGroups = mutableListOf().apply { addAll(AppConst.defaultBookGroups) } + private val bookGroups = mutableListOf() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -50,9 +52,8 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b when (item.itemId) { R.id.menu_search -> startActivity() 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(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(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 = "选择书架布局", diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt index 7db85906e..87d43bc1b 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt @@ -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() + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 72667cbab..af7b4e933 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,6 +35,7 @@ 替换净化-搜索 书架 订阅 + 全部 最近阅读 最后阅读 让阅读成为一种习惯。