Sort by tag settings is able to sort the tags and save the changes

This commit is contained in:
Jobobby04 2020-09-16 12:25:08 -04:00
parent f5998505a3
commit 905e1b68c6
3 changed files with 22 additions and 15 deletions

View File

@ -352,7 +352,7 @@ class SortTagController :
* @param name The name of the new category. * @param name The name of the new category.
*/ */
override fun createCategory(name: String) { override fun createCategory(name: String) {
presenter.createCategory(name) presenter.createTag(name)
} }
/** /**

View File

@ -23,7 +23,7 @@ class SortTagPresenter : BasePresenter<SortTagController>() {
/** /**
* List containing categories. * List containing categories.
*/ */
private var tags: List<String> = emptyList() private var tags: List<Pair<Int, String>> = emptyList()
val preferences: PreferencesHelper = Injekt.get() val preferences: PreferencesHelper = Injekt.get()
@ -38,10 +38,12 @@ class SortTagPresenter : BasePresenter<SortTagController>() {
super.onCreate(savedState) super.onCreate(savedState)
preferences.sortTagsForLibrary().asFlow().onEach { tags -> preferences.sortTagsForLibrary().asFlow().onEach { tags ->
this.tags = tags.toList() this.tags = tags.map { it.split("|") }
.mapNotNull { (it.getOrNull(0)?.toIntOrNull() ?: return@mapNotNull null) to (it.getOrNull(1) ?: return@mapNotNull null) }
.sortedBy { it.first }
Observable.just(this.tags) Observable.just(this.tags)
.map { it.map(::SortTagItem) } .map { tagPairs -> tagPairs.map { it.second }.map(::SortTagItem) }
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribeLatestCache(SortTagController::setCategories) .subscribeLatestCache(SortTagController::setCategories)
}.launchIn(scope) }.launchIn(scope)
@ -52,40 +54,45 @@ class SortTagPresenter : BasePresenter<SortTagController>() {
* *
* @param name The name of the category to create. * @param name The name of the category to create.
*/ */
fun createCategory(name: String) { fun createTag(name: String) {
// Do not allow duplicate categories. // Do not allow duplicate categories.
if (tagExists(name)) { if (tagExists(name)) {
Observable.just(Unit).subscribeFirst({ view, _ -> view.onTagExistsError() }) Observable.just(Unit).subscribeFirst({ view, _ -> view.onTagExistsError() })
return return
} }
preferences.sortTagsForLibrary() += name val size = preferences.sortTagsForLibrary().get().size
preferences.sortTagsForLibrary() += "$size|$name"
} }
/** /**
* Deletes the given categories from the database. * Deletes the given categories from the database.
* *
* @param categories The list of categories to delete. * @param tags The list of categories to delete.
*/ */
fun deleteTags(categories: List<String>) { fun deleteTags(tags: List<String>) {
categories.forEach { val preferenceTags = preferences.sortTagsForLibrary().get()
preferences.sortTagsForLibrary() -= it tags.forEach { tag ->
preferenceTags.firstOrNull { it.endsWith(tag) }?.let {
preferences.sortTagsForLibrary() -= it
}
} }
} }
/** /**
* Reorders the given categories in the database. * Reorders the given categories in the database.
* *
* @param categories The list of categories to reorder. * @param tags The list of categories to reorder.
*/ */
fun reorderTags(categories: List<String>) { fun reorderTags(tags: List<String>) {
preferences.sortTagsForLibrary().set(categories.toSet()) preferences.sortTagsForLibrary().set(tags.mapIndexed { index, tag -> "$index|$tag" }.toSet())
} }
/** /**
* Returns true if a category with the given name already exists. * Returns true if a category with the given name already exists.
*/ */
private fun tagExists(name: String): Boolean { private fun tagExists(name: String): Boolean {
return tags.any { it.equals(name, true) } return tags.any { it.equals(name) }
} }
} }

View File

@ -313,7 +313,7 @@ class LibraryPresenter(
// SY --> // SY -->
val listOfTags by lazy { val listOfTags by lazy {
preferences.sortTagsForLibrary().get().toList().map { ("(, |^)$it").toRegex(RegexOption.IGNORE_CASE) } preferences.sortTagsForLibrary().get().toList().mapNotNull { it.split("|").getOrNull(1) }.map { ("(, |^)$it").toRegex(RegexOption.IGNORE_CASE) }
} }
// SY <-- // SY <--