Group available extensions by language (#2210)

This commit is contained in:
arkon 2019-10-11 13:07:55 -04:00 committed by inorichi
parent d3cb10a74e
commit 86e53e08de
3 changed files with 25 additions and 16 deletions

View File

@ -3,18 +3,14 @@ package eu.kanade.tachiyomi.ui.extension
import android.annotation.SuppressLint
import android.view.View
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import kotlinx.android.synthetic.main.extension_card_header.*
import kotlinx.android.synthetic.main.extension_card_header.title
class ExtensionGroupHolder(view: View, adapter: FlexibleAdapter<*>) :
BaseFlexibleViewHolder(view, adapter) {
@SuppressLint("SetTextI18n")
fun bind(item: ExtensionGroupItem) {
title.text = when {
item.installed -> itemView.context.getString(R.string.ext_installed)
else -> itemView.context.getString(R.string.ext_available)
} + " (" + item.size + ")"
title.text = item.name + " (" + item.size + ")"
}
}

View File

@ -6,11 +6,12 @@ import eu.davidea.flexibleadapter.items.AbstractHeaderItem
import eu.kanade.tachiyomi.R
/**
* Item that contains the language header.
* Item that contains the group header.
*
* @param code The lang code.
* @param name The header name.
* @param size The number of items in the group.
*/
data class ExtensionGroupItem(val installed: Boolean, val size: Int) : AbstractHeaderItem<ExtensionGroupHolder>() {
data class ExtensionGroupItem(val name: String, val size: Int) : AbstractHeaderItem<ExtensionGroupHolder>() {
/**
* Returns the layout resource of this item.
@ -38,13 +39,13 @@ data class ExtensionGroupItem(val installed: Boolean, val size: Int) : AbstractH
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other is ExtensionGroupItem) {
return installed == other.installed
return name == other.name
}
return false
}
override fun hashCode(): Int {
return installed.hashCode()
return name.hashCode()
}
}

View File

@ -1,10 +1,13 @@
package eu.kanade.tachiyomi.ui.extension
import android.app.Application
import android.os.Bundle
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.extension.ExtensionManager
import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.LocaleHelper
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
@ -49,6 +52,8 @@ open class ExtensionPresenter(
@Synchronized
private fun toItems(tuple: ExtensionTuple): List<ExtensionItem> {
val context = Injekt.get<Application>()
val (installed, untrusted, available) = tuple
val items = mutableListOf<ExtensionItem>()
@ -62,7 +67,7 @@ open class ExtensionPresenter(
.sortedBy { it.pkgName }
if (installedSorted.isNotEmpty() || untrustedSorted.isNotEmpty()) {
val header = ExtensionGroupItem(true, installedSorted.size + untrustedSorted.size)
val header = ExtensionGroupItem(context.getString(R.string.ext_installed), installedSorted.size + untrustedSorted.size)
items += installedSorted.map { extension ->
ExtensionItem(extension, header, currentDownloads[extension.pkgName])
}
@ -71,10 +76,17 @@ open class ExtensionPresenter(
}
}
if (availableSorted.isNotEmpty()) {
val header = ExtensionGroupItem(false, availableSorted.size)
items += availableSorted.map { extension ->
ExtensionItem(extension, header, currentDownloads[extension.pkgName])
}
val availableGroupedByLang = availableSorted
.groupBy { LocaleHelper.getDisplayName(it.lang, context) }
.toSortedMap()
availableGroupedByLang
.forEach {
val header = ExtensionGroupItem(it.key, it.value.size)
items += it.value.map { extension ->
ExtensionItem(extension, header, currentDownloads[extension.pkgName])
}
}
}
this.extensions = items