From 4712d47903076a5eb0321695f2978fbd649f240f Mon Sep 17 00:00:00 2001 From: tom5079 Date: Thu, 1 Oct 2020 22:20:49 +0900 Subject: [PATCH] Show 10 tags maximum --- app/build.gradle | 2 +- .../pupil/adapters/GalleryBlockAdapter.kt | 46 +++++----- .../xyz/quaver/pupil/ui/view/TagChipGroup.kt | 90 +++++++++++++++++++ app/src/main/res/layout/item_galleryblock.xml | 2 +- app/src/main/res/values/attr.xml | 24 +++++ 5 files changed, 141 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/xyz/quaver/pupil/ui/view/TagChipGroup.kt create mode 100644 app/src/main/res/values/attr.xml diff --git a/app/build.gradle b/app/build.gradle index ef07e100..4d3d546e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,7 +38,7 @@ android { minSdkVersion 16 targetSdkVersion 30 versionCode 61 - versionName "5.1.1-hotfix3" + versionName "5.1.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt index b4e7cd52..93534051 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt @@ -43,7 +43,6 @@ import xyz.quaver.pupil.R import xyz.quaver.pupil.favoriteTags import xyz.quaver.pupil.favorites import xyz.quaver.pupil.types.Tag -import xyz.quaver.pupil.ui.view.TagChip import xyz.quaver.pupil.util.Preferences import xyz.quaver.pupil.util.downloader.Cache import xyz.quaver.pupil.util.downloader.DownloadManager @@ -215,27 +214,32 @@ class GalleryBlockAdapter(private val galleries: List) : RecyclerSwipeAdapt } } - galleryblock_tag_group.removeAllViews() - CoroutineScope(Dispatchers.Default).launch { - galleryBlock.relatedTags.sortedBy { - val tag = Tag.parse(it) - - if (favoriteTags.contains(tag)) - -1 - else - when(Tag.parse(it).area) { - "female" -> 0 - "male" -> 1 - else -> 2 - } - }.map { - TagChip(context, Tag.parse(it)).apply { - setOnClickListener { view -> - for (callback in onChipClickedHandler) - callback.invoke((view as TagChip).tag) - } + with(galleryblock_tag_group) { + onClickListener = { + onChipClickedHandler.forEach { callback -> + callback.invoke(it) } - }.let { launch(Dispatchers.Main) { it.forEach { galleryblock_tag_group.addView(it) } } } + } + + tags.clear() + tags.addAll( + galleryBlock.relatedTags.sortedBy { + val tag = Tag.parse(it) + + if (favoriteTags.contains(tag)) + -1 + else + when(Tag.parse(it).area) { + "female" -> 0 + "male" -> 1 + else -> 2 + } + }.map { + Tag.parse(it) + } + ) + + refresh() } galleryblock_id.text = galleryBlock.id.toString() diff --git a/app/src/main/java/xyz/quaver/pupil/ui/view/TagChipGroup.kt b/app/src/main/java/xyz/quaver/pupil/ui/view/TagChipGroup.kt new file mode 100644 index 00000000..7e76dd26 --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/ui/view/TagChipGroup.kt @@ -0,0 +1,90 @@ +/* + * Pupil, Hitomi.la viewer for Android + * Copyright (C) 2020 tom5079 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package xyz.quaver.pupil.ui.view + +import android.content.Context +import android.content.res.TypedArray +import android.util.AttributeSet +import com.google.android.material.chip.Chip +import com.google.android.material.chip.ChipGroup +import xyz.quaver.pupil.R +import xyz.quaver.pupil.types.Tag +import xyz.quaver.pupil.types.Tags + +class TagChipGroup @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, attrStyle: Int = R.attr.chipGroupStyle, val tags: Tags = Tags()) : ChipGroup(context, attr, attrStyle), MutableSet by tags { + + object Defaults { + val maxChipSize = 10 + } + + var maxChipSize: Int = Defaults.maxChipSize + set(value) { + field = value + + refresh() + } + + private val moreView = Chip(context).apply { + text = "…" + + setEnsureMinTouchTargetSize(false) + + setOnClickListener { + removeView(this) + + for (i in maxChipSize until tags.size) { + val tag = tags.elementAt(i) + + addView(TagChip(context, tag).apply { + setOnClickListener { + onClickListener?.invoke(tag) + } + }) + } + } + } + + var onClickListener: ((Tag) -> Unit)? = null + + private fun applyAttributes(attr: TypedArray) { + maxChipSize = attr.getInt(R.styleable.TagChipGroup_maxTag, Defaults.maxChipSize) + } + + fun refresh() { + this.removeAllViews() + + tags.take(maxChipSize).forEach { + this.addView(TagChip(context, it).apply { + setOnClickListener { + onClickListener?.invoke(this.tag) + } + }) + } + + if (maxChipSize > 0 && this.size > maxChipSize) + addView(moreView) + } + + init { + applyAttributes(context.obtainStyledAttributes(attr, R.styleable.TagChipGroup)) + + refresh() + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/item_galleryblock.xml b/app/src/main/res/layout/item_galleryblock.xml index 67a7244a..d8bbee51 100644 --- a/app/src/main/res/layout/item_galleryblock.xml +++ b/app/src/main/res/layout/item_galleryblock.xml @@ -171,7 +171,7 @@ app:barrierDirection="bottom" app:constraint_referenced_ids="galleryblock_language"/> - + + + + + + + \ No newline at end of file