From dcc5e19299de8fcf3b04f6819264fb219b36d3ac Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 13 Feb 2020 20:23:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/arrange/ArrangeBookActivity.kt | 6 +-- .../app/ui/book/group/GroupSelectDialog.kt | 26 +++++++--- .../app/ui/book/info/BookInfoActivity.kt | 25 +++++----- .../app/ui/book/info/BookInfoViewModel.kt | 17 +++++-- .../res/layout/dialog_book_group_picker.xml | 49 +++++++++++++++++++ 5 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 app/src/main/res/layout/dialog_book_group_picker.xml diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index 981e5d991..e3ca00964 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -162,18 +162,18 @@ class ArrangeBookActivity : VMBaseActivity(R.layout.activi GroupSelectDialog.show(supportFragmentManager, groupId, requestCode) } - override fun upGroup(requestCode: Int, group: BookGroup) { + override fun upGroup(requestCode: Int, groupId: Int) { when (requestCode) { groupRequestCode -> { val books = arrayListOf() adapter.selectedBooks.forEach { - books.add(it.copy(group = group.groupId)) + books.add(it.copy(group = groupId)) } viewModel.updateBook(*books.toTypedArray()) } adapter.groupRequestCode -> { adapter.actionItem?.let { - viewModel.updateBook(it.copy(group = group.groupId)) + viewModel.updateBook(it.copy(group = groupId)) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index 8ef086168..3325931f9 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -27,12 +27,15 @@ 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.lib.theme.accentColor import io.legado.app.utils.applyTint import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getViewModel 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_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 org.jetbrains.anko.sdk27.listeners.onClick import java.util.* @@ -72,7 +75,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { savedInstanceState: Bundle? ): View? { 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?) { @@ -82,10 +85,11 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { groupId = it.getInt("groupId") requestCode = it.getInt("requestCode", -1) } + initView() initData() } - private fun initData() { + private fun initView() { tool_bar.title = getString(R.string.group_select) tool_bar.inflateMenu(R.menu.book_group_manage) tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) @@ -96,13 +100,21 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.addItemDecoration(recycler_view.getVerticalDivider()) recycler_view.adapter = adapter - App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { - adapter.setItems(it) - }) val itemTouchCallback = ItemTouchCallback() itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.isCanDrag = true 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 { @@ -195,6 +207,6 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { } interface CallBack { - fun upGroup(requestCode: Int, group: BookGroup) + fun upGroup(requestCode: Int, groupId: Int) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 779d8aa01..cd41c8980 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -17,7 +17,6 @@ import io.legado.app.constant.BookType import io.legado.app.constant.Theme import io.legado.app.data.entities.Book 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.ImageLoader import io.legado.app.help.IntentDataHelp @@ -57,13 +56,6 @@ class BookInfoActivity : tv_intro.movementMethod = ScrollingMovementMethod.getInstance() viewModel.bookData.observe(this, Observer { showBook(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) initOnClick() } @@ -147,6 +139,7 @@ class BookInfoActivity : } } } + upGroup(book.group) } 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() { iv_cover.onClick { viewModel.bookData.value?.let { @@ -314,9 +317,9 @@ class BookInfoActivity : return viewModel.durChapterIndex } - override fun upGroup(requestCode: Int, group: BookGroup) { - viewModel.groupData.postValue(group) - viewModel.bookData.value?.group = group.groupId + override fun upGroup(requestCode: Int, groupId: Int) { + upGroup(groupId) + viewModel.bookData.value?.group = groupId if (viewModel.inBookshelf) { viewModel.saveBook() } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 764dbe626..1a675e21a 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -8,7 +8,6 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter -import io.legado.app.data.entities.BookGroup import io.legado.app.help.BookHelp import io.legado.app.model.WebBook import io.legado.app.model.localBook.AnalyzeTxtFile @@ -19,13 +18,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { val chapterListData = MutableLiveData>() var durChapterIndex = 0 var inBookshelf = false - var groupData = MutableLiveData() fun initData(intent: Intent) { execute { intent.getStringExtra("bookUrl")?.let { App.db.bookDao().getBook(it)?.let { book -> - groupData.postValue(App.db.bookGroupDao().getByID(book.group)) inBookshelf = true setBook(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() + 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) { execute { if (inBookshelf) { diff --git a/app/src/main/res/layout/dialog_book_group_picker.xml b/app/src/main/res/layout/dialog_book_group_picker.xml new file mode 100644 index 000000000..43d0c5d3b --- /dev/null +++ b/app/src/main/res/layout/dialog_book_group_picker.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file