pull/32/head
kunfei 5 years ago
parent 5b8fc1f273
commit 61d2f90d8e
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 10
      app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt
  3. 39
      app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt
  4. 9
      app/src/main/res/menu/group_manage.xml
  5. 1
      app/src/main/res/values/strings.xml

@ -29,6 +29,9 @@ interface BookSourceDao {
@Query("select * from book_sources where bookSourceGroup like '%' || :group || '%'")
fun getByGroup(group: String): List<BookSource>
@get:Query("select * from book_sources where bookSourceGroup = null or bookSourceGroup = ''")
val noGroup: List<BookSource>
@get:Query("select * from book_sources order by customOrder asc")
val all: List<BookSource>

@ -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)

@ -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()
}

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_add_empty"
android:title="@string/add_group"
app:showAsAction="always" />
</menu>

@ -492,5 +492,6 @@
<string name="select_image">选择图片</string>
<string name="group_manage">分组管理</string>
<string name="group_edit">编辑分组</string>
<string name="add_group">添加分组</string>
</resources>

Loading…
Cancel
Save