Add pin icon to sources list (closes #2862)

(cherry picked from commit a52fbb012a)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceHolder.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt
This commit is contained in:
arkon
2020-07-25 18:25:30 -04:00
committed by Jobobby04
parent 94d14af2a4
commit 87e3a610e1
8 changed files with 88 additions and 29 deletions
@@ -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)
}
}
@@ -47,15 +47,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(bundle: Bundle? = null) :
NucleusController<SourceMainControllerBinding, SourcePresenter>(bundle),
FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener,
SourceAdapter.OnBrowseClickListener,
SourceAdapter.OnLatestClickListener,
SourceAdapter.OnSourceClickListener,
/*SY -->*/ ChangeSourceCategoriesDialog.Listener /*SY <--*/ {
private val preferences: PreferencesHelper = Injekt.get()
@@ -173,7 +172,7 @@ class SourceController(bundle: Bundle? = null) :
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) {
@@ -218,12 +217,12 @@ class SourceController(bundle: Bundle? = null) :
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()
@@ -305,6 +304,14 @@ class SourceController(bundle: Bundle? = null) :
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.
*/
@@ -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 /* SY --> */,
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)
}
// SY -->
@@ -60,5 +65,14 @@ class SourceHolder(view: View, override val adapter: SourceAdapter /* SY --> */,
source_browse.setText(R.string.browse)
source_latest.isVisible = source.supportsLatest/* SY --> */ && showButtons /* SY <-- */
pin.isVisible = true
pin.setImageResource(
if (item.isPinned) {
R.drawable.ic_push_pin_filled_24dp
} else {
R.drawable.ic_push_pin_24dp
}
)
}
}
@@ -14,7 +14,14 @@ 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 /* SY --> */, val showButtons: Boolean /* SY <-- */) :
data class SourceItem(
val source: CatalogueSource,
val header: LangItem? = null,
val isPinned: Boolean = false,
// SY -->
val showButtons: Boolean
// SY <--
) :
AbstractSectionableItem<SourceHolder, LangItem>(header) {
/**
@@ -91,8 +91,9 @@ 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), controllerMode == SourceController.Mode.CATALOGUE))
val isPinned = source.id.toString() in pinnedSourceIds
if (isPinned) {
pinnedSources.add(SourceItem(source, LangItem(PINNED_KEY), isPinned, controllerMode == SourceController.Mode.CATALOGUE))
}
// SY -->
@@ -106,6 +107,7 @@ class SourcePresenter(
SourceItem(
source,
LangItem("custom|" + SourceAndCategory.second),
isPinned,
controllerMode == SourceController.Mode.CATALOGUE
)
)
@@ -115,7 +117,7 @@ class SourcePresenter(
}
// SY <--
SourceItem(source, langItem, controllerMode == SourceController.Mode.CATALOGUE)
SourceItem(source, langItem, isPinned, controllerMode == SourceController.Mode.CATALOGUE)
}
}