From a52fbb012a58531edc15401e86bedc0e15fd1602 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Jul 2020 18:25:30 -0400 Subject: [PATCH] Add pin icon to sources list (closes #2862) --- .../ui/browse/source/SourceAdapter.kt | 17 +++----------- .../ui/browse/source/SourceController.kt | 23 ++++++++++++------- .../ui/browse/source/SourceHolder.kt | 18 +++++++++++++-- .../tachiyomi/ui/browse/source/SourceItem.kt | 6 ++++- .../ui/browse/source/SourcePresenter.kt | 7 +++--- .../main/res/drawable/ic_push_pin_24dp.xml | 9 ++++++++ .../res/drawable/ic_push_pin_filled_24dp.xml | 10 ++++++++ .../source_main_controller_card_item.xml | 23 ++++++++++++++++++- 8 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 app/src/main/res/drawable/ic_push_pin_24dp.xml create mode 100644 app/src/main/res/drawable/ic_push_pin_filled_24dp.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceAdapter.kt index 8f9557d70d..8c8ab7a6a3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceAdapter.kt @@ -22,26 +22,15 @@ class SourceAdapter(val controller: SourceController) : /** * Listener for browse item clicks. */ - val browseClickListener: OnBrowseClickListener = controller - - /** - * Listener for latest item clicks. - */ - val latestClickListener: OnLatestClickListener = controller + val clickListener: OnSourceClickListener = controller /** * Listener which should be called when user clicks browse. * Note: Should only be handled by [SourceController] */ - interface OnBrowseClickListener { + interface OnSourceClickListener { fun onBrowseClick(position: Int) - } - - /** - * Listener which should be called when user clicks latest. - * Note: Should only be handled by [SourceController] - */ - interface OnLatestClickListener { fun onLatestClick(position: Int) + fun onPinClick(position: Int) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt index 52636430c0..b196a98a09 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt @@ -41,15 +41,14 @@ import uy.kohesive.injekt.api.get /** * This controller shows and manages the different catalogues enabled by the user. * This controller should only handle UI actions, IO actions should be done by [SourcePresenter] - * [SourceAdapter.OnBrowseClickListener] call function data on browse item click. + * [SourceAdapter.OnSourceClickListener] call function data on browse item click. * [SourceAdapter.OnLatestClickListener] call function data on latest item click */ class SourceController : NucleusController(), FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener, - SourceAdapter.OnBrowseClickListener, - SourceAdapter.OnLatestClickListener { + SourceAdapter.OnSourceClickListener { private val preferences: PreferencesHelper = Injekt.get() @@ -134,7 +133,7 @@ class SourceController : val items = mutableListOf( Pair( activity.getString(if (isPinned) R.string.action_unpin else R.string.action_pin), - { pinSource(item.source, isPinned) } + { toggleSourcePin(item.source) } ) ) if (item.source !is LocalSource) { @@ -159,12 +158,12 @@ class SourceController : presenter.updateSources() } - private fun pinSource(source: Source, isPinned: Boolean) { - val current = preferences.pinnedSources().get() + private fun toggleSourcePin(source: Source) { + val isPinned = source.id.toString() in preferences.pinnedSources().get() if (isPinned) { - preferences.pinnedSources().set(current - source.id.toString()) + preferences.pinnedSources() -= source.id.toString() } else { - preferences.pinnedSources().set(current + source.id.toString()) + preferences.pinnedSources() += source.id.toString() } presenter.updateSources() @@ -185,6 +184,14 @@ class SourceController : openSource(item.source, LatestUpdatesController(item.source)) } + /** + * Called when pin icon is clicked in [SourceAdapter] + */ + override fun onPinClick(position: Int) { + val item = adapter?.getItem(position) as? SourceItem ?: return + toggleSourcePin(item.source) + } + /** * Opens a catalogue with the given controller. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceHolder.kt index a985df5139..f829057e6b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceHolder.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.ui.base.holder.SlicedHolder import io.github.mthli.slice.Slice import kotlinx.android.synthetic.main.source_main_controller_card_item.card import kotlinx.android.synthetic.main.source_main_controller_card_item.image +import kotlinx.android.synthetic.main.source_main_controller_card_item.pin import kotlinx.android.synthetic.main.source_main_controller_card_item.source_browse import kotlinx.android.synthetic.main.source_main_controller_card_item.source_latest import kotlinx.android.synthetic.main.source_main_controller_card_item.title @@ -27,11 +28,15 @@ class SourceHolder(view: View, override val adapter: SourceAdapter) : init { source_browse.setOnClickListener { - adapter.browseClickListener.onBrowseClick(bindingAdapterPosition) + adapter.clickListener.onBrowseClick(bindingAdapterPosition) } source_latest.setOnClickListener { - adapter.latestClickListener.onLatestClick(bindingAdapterPosition) + adapter.clickListener.onLatestClick(bindingAdapterPosition) + } + + pin.setOnClickListener { + adapter.clickListener.onPinClick(bindingAdapterPosition) } } @@ -53,5 +58,14 @@ class SourceHolder(view: View, override val adapter: SourceAdapter) : source_browse.setText(R.string.browse) source_latest.isVisible = source.supportsLatest + + pin.isVisible = true + pin.setImageResource( + if (item.isPinned) { + R.drawable.ic_push_pin_filled_24dp + } else { + R.drawable.ic_push_pin_24dp + } + ) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt index 56008a5a92..b721aecbee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt @@ -14,7 +14,11 @@ import eu.kanade.tachiyomi.source.CatalogueSource * @param source Instance of [CatalogueSource] containing source information. * @param header The header for this item. */ -data class SourceItem(val source: CatalogueSource, val header: LangItem? = null) : +data class SourceItem( + val source: CatalogueSource, + val header: LangItem? = null, + val isPinned: Boolean = false +) : AbstractSectionableItem(header) { /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt index 66d71a5bc7..cbfc1b5b9c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt @@ -72,11 +72,12 @@ class SourcePresenter( var sourceItems = byLang.flatMap { val langItem = LangItem(it.key) it.value.map { source -> - if (source.id.toString() in pinnedSourceIds) { - pinnedSources.add(SourceItem(source, LangItem(PINNED_KEY))) + val isPinned = source.id.toString() in pinnedSourceIds + if (isPinned) { + pinnedSources.add(SourceItem(source, LangItem(PINNED_KEY), isPinned)) } - SourceItem(source, langItem) + SourceItem(source, langItem, isPinned) } } diff --git a/app/src/main/res/drawable/ic_push_pin_24dp.xml b/app/src/main/res/drawable/ic_push_pin_24dp.xml new file mode 100644 index 0000000000..3f513dbae6 --- /dev/null +++ b/app/src/main/res/drawable/ic_push_pin_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_push_pin_filled_24dp.xml b/app/src/main/res/drawable/ic_push_pin_filled_24dp.xml new file mode 100644 index 0000000000..b8994923ed --- /dev/null +++ b/app/src/main/res/drawable/ic_push_pin_filled_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/source_main_controller_card_item.xml b/app/src/main/res/layout/source_main_controller_card_item.xml index bb0d8c9618..777a751b85 100644 --- a/app/src/main/res/layout/source_main_controller_card_item.xml +++ b/app/src/main/res/layout/source_main_controller_card_item.xml @@ -20,6 +20,7 @@ app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" + tools:ignore="ContentDescription" tools:src="@mipmap/ic_launcher_round" /> + +