diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 9c53671f1..b616d1733 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -29,6 +29,9 @@ interface BookSourceDao { @Query("select * from book_sources where bookSourceGroup like '%' || :group || '%'") fun getByGroup(group: String): List + @get:Query("select * from book_sources where bookSourceGroup = null or bookSourceGroup = ''") + val noGroup: List + @get:Query("select * from book_sources order by customOrder asc") val all: List diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt index a2d28eeb6..ab58abc35 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt @@ -26,6 +26,16 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) execute { App.db.bookSourceDao().update(*bookSource) } } + fun addGroup(group: String) { + execute { + val sources = App.db.bookSourceDao().noGroup + sources.map { source -> + source.bookSourceGroup = group + } + App.db.bookSourceDao().update(*sources.toTypedArray()) + } + } + fun upGroup(oldGroup: String, newGroup: String?) { execute { val sources = App.db.bookSourceDao().getByGroup(oldGroup) diff --git a/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt index 15004660e..8b13dc647 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt @@ -5,9 +5,11 @@ 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.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration @@ -32,7 +34,7 @@ import kotlinx.android.synthetic.main.item_book_group.view.tv_group import kotlinx.android.synthetic.main.item_group_manage.view.* import org.jetbrains.anko.sdk27.listeners.onClick -class GroupManageDialog : DialogFragment() { +class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private lateinit var viewModel: BookSourceViewModel private lateinit var adapter: GroupAdapter @@ -59,6 +61,8 @@ class GroupManageDialog : DialogFragment() { private fun initData() { tool_bar.title = getString(R.string.group_manage) + tool_bar.inflateMenu(R.menu.group_manage) + tool_bar.setOnMenuItemClickListener(this) adapter = GroupAdapter(requireContext(), this) recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.addItemDecoration( @@ -74,6 +78,36 @@ class GroupManageDialog : DialogFragment() { }) } + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_group_manage -> 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_edittext, null).apply { + editText = edit_view.apply { + ATH.applyAccentTint(this) + hint = "分组名称" + } + } + } + yesButton { + editText?.text?.toString()?.let { + if (it.isNotBlank()) { + viewModel.addGroup(it) + } + } + } + noButton() + }.show().applyTint().requestInputMethod() + } + @SuppressLint("InflateParams") private fun editGroup(group: String) { alert(title = getString(R.string.group_edit)) { @@ -87,13 +121,10 @@ class GroupManageDialog : DialogFragment() { } } } - yesButton { viewModel.upGroup(group, editText?.text?.toString()) } - noButton() - }.show().applyTint().requestInputMethod() } diff --git a/app/src/main/res/menu/group_manage.xml b/app/src/main/res/menu/group_manage.xml new file mode 100644 index 000000000..0ddbb4f12 --- /dev/null +++ b/app/src/main/res/menu/group_manage.xml @@ -0,0 +1,9 @@ + + + + + \ 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 86d6d28ce..cb28dabaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -492,5 +492,6 @@ 选择图片 分组管理 编辑分组 + 添加分组