From c155296a77372992b1431edc63951927315336ca Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 23 Nov 2019 18:11:33 +0800 Subject: [PATCH] up --- .../app/ui/book/info/BookInfoActivity.kt | 9 +- .../app/ui/book/info/GroupSelectDialog.kt | 181 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt 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 850917570..cf2814252 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 @@ -11,6 +11,7 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.BookType 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.ImageLoader import io.legado.app.help.IntentDataHelp import io.legado.app.ui.audio.AudioPlayActivity @@ -30,8 +31,10 @@ import org.jetbrains.anko.toast class BookInfoActivity : VMBaseActivity(R.layout.activity_book_info), + GroupSelectDialog.CallBack, ChapterListAdapter.CallBack, ChangeSourceDialog.CallBack { + override val viewModel: BookInfoViewModel get() = getViewModel(BookInfoViewModel::class.java) @@ -191,7 +194,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } } tv_group.onClick { - + GroupSelectDialog.show(supportFragmentManager) } } @@ -248,6 +251,10 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo return viewModel.durChapterIndex } + override fun upGroup(group: BookGroup) { + viewModel.groupData.postValue(group) + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { diff --git a/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt new file mode 100644 index 000000000..f13e08b1c --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt @@ -0,0 +1,181 @@ +package io.legado.app.ui.book.info + +import android.annotation.SuppressLint +import android.content.Context +import android.os.Bundle +import android.util.DisplayMetrics +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.widget.EditText +import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.FragmentManager +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import io.legado.app.App +import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.BookGroup +import io.legado.app.help.ItemTouchCallback +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.ui.main.bookshelf.BookshelfViewModel +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import io.legado.app.utils.requestInputMethod +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.* +import org.jetbrains.anko.sdk27.listeners.onClick + +class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { + + companion object { + const val tag = "groupSelectDialog" + + fun show(manager: FragmentManager) { + val fragment = GroupSelectDialog() + fragment.show(manager, tag) + } + } + + private lateinit var viewModel: BookshelfViewModel + private lateinit var adapter: GroupAdapter + private var callBack: CallBack? = null + + override fun onStart() { + super.onStart() + val dm = DisplayMetrics() + activity?.windowManager?.defaultDisplay?.getMetrics(dm) + dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), (dm.heightPixels * 0.9).toInt()) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + viewModel = getViewModel(BookshelfViewModel::class.java) + return inflater.inflate(R.layout.dialog_recycler_view, container) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + callBack = activity as? CallBack + initData() + } + + private fun initData() { + tool_bar.title = getString(R.string.group_select) + 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).isVisible = false + tool_bar.menu.findItem(R.id.menu_group_audio).isVisible = false + adapter = GroupAdapter(requireContext()) + recycler_view.layoutManager = LinearLayoutManager(requireContext()) + recycler_view.addItemDecoration( + DividerItemDecoration(requireContext(), RecyclerView.VERTICAL) + ) + 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) + } + + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_add -> addGroup() + } + return true + } + + @SuppressLint("InflateParams") + private fun addGroup() { + alert(title = getString(R.string.add_group)) { + var editText: EditText? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view.apply { + hint = "分组名称" + } + } + } + yesButton { + editText?.text?.toString()?.let { + if (it.isNotBlank()) { + viewModel.addGroup(it) + } + } + } + noButton() + }.show().applyTint().requestInputMethod() + } + + @SuppressLint("InflateParams") + private fun editGroup(bookGroup: BookGroup) { + alert(title = getString(R.string.group_edit)) { + var editText: EditText? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view.apply { + hint = "分组名称" + setText(bookGroup.groupName) + } + } + } + yesButton { + viewModel.upGroup(bookGroup.copy(groupName = editText?.text?.toString() ?: "")) + } + noButton() + }.show().applyTint().requestInputMethod() + } + + private inner class GroupAdapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_group_manage), + ItemTouchCallback.OnItemTouchCallbackListener { + + override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { + with(holder.itemView) { + tv_group.text = item.groupName + tv_edit.onClick { editGroup(item) } + tv_del.onClick { viewModel.delGroup(item) } + this.onClick { + callBack?.upGroup(item) + } + } + } + + override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + val srcItem = getItem(srcPosition) + val targetItem = getItem(targetPosition) + if (srcItem != null && targetItem != null) { + val order = srcItem.order + srcItem.order = targetItem.order + targetItem.order = order + viewModel.upGroup(srcItem, targetItem) + } + return true + } + + override fun onSwiped(adapterPosition: Int) { + + } + } + + interface CallBack { + fun upGroup(group: BookGroup) + } +} \ 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 0366f5099..187f91207 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -528,6 +528,7 @@ 文字颜色 选择图片 分组管理 + 分组选择 编辑分组 添加分组 新建替换