pull/37/head
kunfei 5 years ago
parent a6b9992c7f
commit d45abb8ffc
  1. 6
      app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt
  2. 140
      app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt
  3. 44
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -51,4 +51,10 @@ interface RssSourceDao {
@Query("delete from rssSources where sourceUrl = :sourceUrl")
fun delete(sourceUrl: String)
@get:Query("select * from rssSources where sourceGroup is null or sourceGroup = ''")
val noGroup: List<RssSource>
@Query("select * from rssSources where sourceGroup like '%' || :group || '%'")
fun getByGroup(group: String): List<RssSource>
}

@ -0,0 +1,140 @@
package io.legado.app.ui.rss.source.manage
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.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
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.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.getViewModelOfActivity
import io.legado.app.utils.requestInputMethod
import io.legado.app.utils.splitNotBlank
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 GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: RssSourceViewModel
private lateinit var adapter: GroupAdapter
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 = getViewModelOfActivity(RssSourceViewModel::class.java)
return inflater.inflate(R.layout.dialog_recycler_view, container)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initData()
}
private fun initData() {
tool_bar.title = getString(R.string.group_manage)
tool_bar.inflateMenu(R.menu.group_manage)
tool_bar.menu.applyTint(requireContext(), false)
tool_bar.setOnMenuItemClickListener(this)
adapter = GroupAdapter(requireContext())
recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(
DividerItemDecoration(requireContext(), RecyclerView.VERTICAL)
)
recycler_view.adapter = adapter
App.db.bookSourceDao().liveGroup().observe(viewLifecycleOwner, Observer {
val groups = linkedSetOf<String>()
it.map { group ->
groups.addAll(group.splitNotBlank(",", ";"))
}
adapter.setItems(groups.toList())
})
}
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(group: String) {
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(group)
}
}
}
yesButton {
viewModel.upGroup(group, editText?.text?.toString())
}
noButton()
}.show().applyTint().requestInputMethod()
}
private inner class GroupAdapter(context: Context) :
SimpleRecyclerAdapter<String>(context, R.layout.item_group_manage) {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) {
tv_group.text = item
tv_edit.onClick { editGroup(item) }
tv_del.onClick { viewModel.delGroup(item) }
}
}
}
}

@ -1,11 +1,13 @@
package io.legado.app.ui.rss.source.manage
import android.app.Application
import android.text.TextUtils
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.splitNotBlank
import java.io.File
class RssSourceViewModel(application: Application) : BaseViewModel(application) {
@ -53,6 +55,48 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
}
}
fun addGroup(group: String) {
execute {
val sources = App.db.rssSourceDao().noGroup
sources.map { source ->
source.sourceGroup = group
}
App.db.rssSourceDao().update(*sources.toTypedArray())
}
}
fun upGroup(oldGroup: String, newGroup: String?) {
execute {
val sources = App.db.rssSourceDao().getByGroup(oldGroup)
sources.map { source ->
source.sourceGroup?.splitNotBlank(",")?.toHashSet()?.let {
it.remove(oldGroup)
if (!newGroup.isNullOrEmpty())
it.add(newGroup)
source.sourceGroup = TextUtils.join(",", it)
}
}
App.db.rssSourceDao().update(*sources.toTypedArray())
}
}
fun delGroup(group: String) {
execute {
execute {
val sources = App.db.rssSourceDao().getByGroup(group)
sources.map { source ->
source.sourceGroup?.splitNotBlank(",")?.toHashSet()?.let {
it.remove(group)
source.sourceGroup = TextUtils.join(",", it)
}
}
App.db.rssSourceDao().update(*sources.toTypedArray())
}
}
}
fun importSourceFromFilePath(path: String) {
execute {
val file = File(path)

Loading…
Cancel
Save