diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 68b739f20..0b9533d8f 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -14,20 +14,20 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun topSource(vararg sources: BookSource) { execute { val minOrder = appDb.bookSourceDao.minOrder - 1 - sources.forEachIndexed { index, bookSource -> - bookSource.customOrder = minOrder - index + val array = Array(sources.size) { + sources[it].copy(customOrder = minOrder - it) } - appDb.bookSourceDao.update(*sources) + appDb.bookSourceDao.update(*array) } } fun bottomSource(vararg sources: BookSource) { execute { val maxOrder = appDb.bookSourceDao.maxOrder + 1 - sources.forEachIndexed { index, bookSource -> - bookSource.customOrder = maxOrder + index + val array = Array(sources.size) { + sources[it].copy(customOrder = maxOrder + it) } - appDb.bookSourceDao.update(*sources) + appDb.bookSourceDao.update(*array) } } @@ -51,59 +51,55 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun enableSelection(sources: List) { execute { - val list = arrayListOf() - sources.forEach { - list.add(it.copy(enabled = true)) + val array = Array(sources.size) { + sources[it].copy(enabled = true) } - appDb.bookSourceDao.update(*list.toTypedArray()) + appDb.bookSourceDao.update(*array) } } fun disableSelection(sources: List) { execute { - val list = arrayListOf() - sources.forEach { - list.add(it.copy(enabled = false)) + val array = Array(sources.size) { + sources[it].copy(enabled = false) } - appDb.bookSourceDao.update(*list.toTypedArray()) + appDb.bookSourceDao.update(*array) } } fun enableSelectExplore(sources: List) { execute { - val list = arrayListOf() - sources.forEach { - list.add(it.copy(enabledExplore = true)) + val array = Array(sources.size) { + sources[it].copy(enabledExplore = true) } - appDb.bookSourceDao.update(*list.toTypedArray()) + appDb.bookSourceDao.update(*array) } } fun disableSelectExplore(sources: List) { execute { - val list = arrayListOf() - sources.forEach { - list.add(it.copy(enabledExplore = false)) + val array = Array(sources.size) { + sources[it].copy(enabledExplore = false) } - appDb.bookSourceDao.update(*list.toTypedArray()) + appDb.bookSourceDao.update(*array) } } fun selectionAddToGroups(sources: List, groups: String) { execute { - sources.forEach { source -> - source.addGroup(groups) + val array = Array(sources.size) { + sources[it].copy().addGroup(groups) } - appDb.bookSourceDao.update(*sources.toTypedArray()) + appDb.bookSourceDao.update(*array) } } fun selectionRemoveFromGroups(sources: List, groups: String) { execute { - sources.forEach { source -> - source.removeGroup(groups) + val array = Array(sources.size) { + sources[it].copy().removeGroup(groups) } - appDb.bookSourceDao.update(*sources.toTypedArray()) + appDb.bookSourceDao.update(*array) } }