This commit is contained in:
kunfei
2022-04-21 15:49:11 +08:00
parent b2af3e18bd
commit 46fe025711
6 changed files with 31 additions and 43 deletions
@@ -34,14 +34,10 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
val selection: List<BookSource> val selection: List<BookSource>
get() { get() {
val selection = arrayListOf<BookSource>() return getItems().filter {
getItems().map { selected.contains(it)
if (selected.contains(it)) {
selection.add(it)
} }
} }
return selection.sortedBy { it.customOrder }
}
val diffItemCallback: DiffUtil.ItemCallback<BookSource> val diffItemCallback: DiffUtil.ItemCallback<BookSource>
get() = object : DiffUtil.ItemCallback<BookSource>() { get() = object : DiffUtil.ItemCallback<BookSource>() {
@@ -13,6 +13,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun topSource(vararg sources: BookSource) { fun topSource(vararg sources: BookSource) {
execute { execute {
sources.sortBy { it.customOrder }
val minOrder = appDb.bookSourceDao.minOrder - 1 val minOrder = appDb.bookSourceDao.minOrder - 1
val array = Array(sources.size) { val array = Array(sources.size) {
sources[it].copy(customOrder = minOrder - it) sources[it].copy(customOrder = minOrder - it)
@@ -23,6 +24,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun bottomSource(vararg sources: BookSource) { fun bottomSource(vararg sources: BookSource) {
execute { execute {
sources.sortBy { it.customOrder }
val maxOrder = appDb.bookSourceDao.maxOrder + 1 val maxOrder = appDb.bookSourceDao.maxOrder + 1
val array = Array(sources.size) { val array = Array(sources.size) {
sources[it].copy(customOrder = maxOrder + it) sources[it].copy(customOrder = maxOrder + it)
@@ -25,16 +25,12 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
private val selected = linkedSetOf<ReplaceRule>() private val selected = linkedSetOf<ReplaceRule>()
val selection: LinkedHashSet<ReplaceRule> val selection: List<ReplaceRule>
get() { get() {
val selection = linkedSetOf<ReplaceRule>() return getItems().filter {
getItems().map { selected.contains(it)
if (selected.contains(it)) {
selection.add(it)
} }
} }
return selection
}
val diffItemCallBack = object : DiffUtil.ItemCallback<ReplaceRule>() { val diffItemCallBack = object : DiffUtil.ItemCallback<ReplaceRule>() {
@@ -65,23 +65,21 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
} }
} }
fun enableSelection(rules: LinkedHashSet<ReplaceRule>) { fun enableSelection(rules: List<ReplaceRule>) {
execute { execute {
val list = arrayListOf<ReplaceRule>() val array = Array(rules.size) {
rules.forEach { rules[it].copy(isEnabled = true)
list.add(it.copy(isEnabled = true))
} }
appDb.replaceRuleDao.update(*list.toTypedArray()) appDb.replaceRuleDao.update(*array)
} }
} }
fun disableSelection(rules: LinkedHashSet<ReplaceRule>) { fun disableSelection(rules: List<ReplaceRule>) {
execute { execute {
val list = arrayListOf<ReplaceRule>() val array = Array(rules.size) {
rules.forEach { rules[it].copy(isEnabled = false)
list.add(it.copy(isEnabled = false))
} }
appDb.replaceRuleDao.update(*list.toTypedArray()) appDb.replaceRuleDao.update(*array)
} }
} }
@@ -27,14 +27,10 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
val selection: List<RssSource> val selection: List<RssSource>
get() { get() {
val selection = arrayListOf<RssSource>() return getItems().filter {
getItems().forEach { selected.contains(it)
if (selected.contains(it)) {
selection.add(it)
} }
} }
return selection.sortedBy { it.customOrder }
}
val diffItemCallback = object : DiffUtil.ItemCallback<RssSource>() { val diffItemCallback = object : DiffUtil.ItemCallback<RssSource>() {
@@ -13,21 +13,23 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
fun topSource(vararg sources: RssSource) { fun topSource(vararg sources: RssSource) {
execute { execute {
sources.sortBy { it.customOrder }
val minOrder = appDb.rssSourceDao.minOrder - 1 val minOrder = appDb.rssSourceDao.minOrder - 1
sources.forEachIndexed { index, rssSource -> val array = Array(sources.size) {
rssSource.customOrder = minOrder - index sources[it].copy(customOrder = minOrder - it)
} }
appDb.rssSourceDao.update(*sources) appDb.rssSourceDao.update(*array)
} }
} }
fun bottomSource(vararg sources: RssSource) { fun bottomSource(vararg sources: RssSource) {
execute { execute {
sources.sortBy { it.customOrder }
val maxOrder = appDb.rssSourceDao.maxOrder + 1 val maxOrder = appDb.rssSourceDao.maxOrder + 1
sources.forEachIndexed { index, rssSource -> val array = Array(sources.size) {
rssSource.customOrder = maxOrder + index sources[it].copy(customOrder = maxOrder + it)
} }
appDb.rssSourceDao.update(*sources) appDb.rssSourceDao.update(*array)
} }
} }
@@ -51,21 +53,19 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
fun enableSelection(sources: List<RssSource>) { fun enableSelection(sources: List<RssSource>) {
execute { execute {
val list = arrayListOf<RssSource>() val array = Array(sources.size) {
sources.forEach { sources[it].copy(enabled = true)
list.add(it.copy(enabled = true))
} }
appDb.rssSourceDao.update(*list.toTypedArray()) appDb.rssSourceDao.update(*array)
} }
} }
fun disableSelection(sources: List<RssSource>) { fun disableSelection(sources: List<RssSource>) {
execute { execute {
val list = arrayListOf<RssSource>() val array = Array(sources.size) {
sources.forEach { sources[it].copy(enabled = false)
list.add(it.copy(enabled = false))
} }
appDb.rssSourceDao.update(*list.toTypedArray()) appDb.rssSourceDao.update(*array)
} }
} }