This commit is contained in:
kunfei
2022-09-18 00:00:32 +08:00
parent 0ec04b5a3c
commit b73f2c68fb
4 changed files with 94 additions and 4 deletions
@@ -1,11 +1,13 @@
package io.legado.app.data.entities package io.legado.app.data.entities
import android.os.Parcelable import android.os.Parcelable
import android.text.TextUtils
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.jayway.jsonpath.DocumentContext import com.jayway.jsonpath.DocumentContext
import io.legado.app.constant.AppPattern
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
@@ -114,6 +116,23 @@ data class RssSource(
} }
} }
fun addGroup(groups: String): RssSource {
sourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.toHashSet()?.let {
it.addAll(groups.splitNotBlank(AppPattern.splitGroupRegex))
sourceGroup = TextUtils.join(",", it)
}
if (sourceGroup.isNullOrBlank()) sourceGroup = groups
return this
}
fun removeGroup(groups: String): RssSource {
sourceGroup?.splitNotBlank(AppPattern.splitGroupRegex)?.toHashSet()?.let {
it.removeAll(groups.splitNotBlank(AppPattern.splitGroupRegex).toSet())
sourceGroup = TextUtils.join(",", it)
}
return this
}
fun sortUrls(): List<Pair<String, String>> = arrayListOf<Pair<String, String>>().apply { fun sortUrls(): List<Pair<String, String>> = arrayListOf<Pair<String, String>>().apply {
kotlin.runCatching { kotlin.runCatching {
var a = sortUrl var a = sortUrl
@@ -146,6 +146,8 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
when (item?.itemId) { when (item?.itemId) {
R.id.menu_enable_selection -> viewModel.enableSelection(adapter.selection) R.id.menu_enable_selection -> viewModel.enableSelection(adapter.selection)
R.id.menu_disable_selection -> viewModel.disableSelection(adapter.selection) R.id.menu_disable_selection -> viewModel.disableSelection(adapter.selection)
R.id.menu_add_group -> selectionAddToGroups()
R.id.menu_remove_group -> selectionRemoveFromGroups()
R.id.menu_top_sel -> viewModel.topSource(*adapter.selection.toTypedArray()) R.id.menu_top_sel -> viewModel.topSource(*adapter.selection.toTypedArray())
R.id.menu_bottom_sel -> viewModel.bottomSource(*adapter.selection.toTypedArray()) R.id.menu_bottom_sel -> viewModel.bottomSource(*adapter.selection.toTypedArray())
R.id.menu_export_selection -> viewModel.saveToFile(adapter.selection) { file -> R.id.menu_export_selection -> viewModel.saveToFile(adapter.selection) { file ->
@@ -154,6 +156,7 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
fileData = Triple("exportRssSource.json", file, "application/json") fileData = Triple("exportRssSource.json", file, "application/json")
} }
} }
R.id.menu_share_source -> viewModel.saveToFile(adapter.selection) { R.id.menu_share_source -> viewModel.saveToFile(adapter.selection) {
share(it) share(it)
} }
@@ -213,6 +216,46 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
} }
} }
@SuppressLint("InflateParams")
private fun selectionAddToGroups() {
alert(titleResource = R.string.add_group) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.setHint(R.string.group_name)
editView.setFilterValues(groups.toList())
editView.dropDownHeight = 180.dpToPx()
}
customView { alertBinding.root }
okButton {
alertBinding.editView.text?.toString()?.let {
if (it.isNotEmpty()) {
viewModel.selectionAddToGroups(adapter.selection, it)
}
}
}
cancelButton()
}
}
@SuppressLint("InflateParams")
private fun selectionRemoveFromGroups() {
alert(titleResource = R.string.remove_group) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.setHint(R.string.group_name)
editView.setFilterValues(groups.toList())
editView.dropDownHeight = 180.dpToPx()
}
customView { alertBinding.root }
okButton {
alertBinding.editView.text?.toString()?.let {
if (it.isNotEmpty()) {
viewModel.selectionRemoveFromGroups(adapter.selection, it)
}
}
}
cancelButton()
}
}
override fun selectAll(selectAll: Boolean) { override fun selectAll(selectAll: Boolean) {
if (selectAll) { if (selectAll) {
adapter.selectAll() adapter.selectAll()
@@ -87,6 +87,24 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun selectionAddToGroups(sources: List<RssSource>, groups: String) {
execute {
val array = Array(sources.size) {
sources[it].copy().addGroup(groups)
}
appDb.rssSourceDao.update(*array)
}
}
fun selectionRemoveFromGroups(sources: List<RssSource>, groups: String) {
execute {
val array = Array(sources.size) {
sources[it].copy().removeGroup(groups)
}
appDb.rssSourceDao.update(*array)
}
}
fun addGroup(group: String) { fun addGroup(group: String) {
execute { execute {
val sources = appDb.rssSourceDao.noGroup val sources = appDb.rssSourceDao.noGroup
+10
View File
@@ -12,6 +12,16 @@
android:title="@string/disable_selection" android:title="@string/disable_selection"
app:showAsAction="never"/> app:showAsAction="never"/>
<item
android:id="@+id/menu_add_group"
android:title="@string/add_group"
app:showAsAction="never"/>
<item
android:id="@+id/menu_remove_group"
android:title="@string/remove_group"
app:showAsAction="never"/>
<item <item
android:id="@+id/menu_top_sel" android:id="@+id/menu_top_sel"
android:title="@string/selection_to_top" android:title="@string/selection_to_top"