Skip to content

Commit

Permalink
Search through all sources in catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 22, 2023
1 parent b9fd2e1 commit 95fbe49
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
sealed interface SourceCatalogItem : ListModel {

data class Source(
val source: MangaSource
val source: MangaSource,
val showSummary: Boolean,
) : SourceCatalogItem {

override fun areItemsTheSame(other: ListModel): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.LifecycleOwner
import coil.ImageLoader
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.getSummary
import org.koitharu.kotatsu.core.model.isNsfw
import org.koitharu.kotatsu.core.parser.favicon.faviconUri
import org.koitharu.kotatsu.core.ui.image.FaviconDrawable
Expand Down Expand Up @@ -43,6 +44,12 @@ fun sourceCatalogItemSourceAD(
} else {
item.source.title
}
if (item.showSummary) {
binding.textViewDescription.text = item.source.getSummary(context)
binding.textViewDescription.isVisible = true
} else {
binding.textViewDescription.isVisible = false
}
val fallbackIcon = FaviconDrawable(context, R.style.FaviconDrawable_Small, item.source.name)
binding.imageViewIcon.newImageRequest(lifecycleOwner, item.source.faviconUri())?.run {
crossfade(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.koitharu.kotatsu.settings.sources.catalog

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.core.graphics.Insets
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import coil.ImageLoader
import com.google.android.material.appbar.AppBarLayout
Expand All @@ -28,7 +31,7 @@ import javax.inject.Inject
class SourcesCatalogActivity : BaseActivity<ActivitySourcesCatalogBinding>(),
TabLayout.OnTabSelectedListener,
OnListItemClickListener<SourceCatalogItem.Source>,
AppBarOwner {
AppBarOwner, MenuItem.OnActionExpandListener {

@Inject
lateinit var coil: ImageLoader
Expand Down Expand Up @@ -56,7 +59,7 @@ class SourcesCatalogActivity : BaseActivity<ActivitySourcesCatalogBinding>(),
viewModel.locale.observe(this) {
supportActionBar?.subtitle = it.getLocaleDisplayName()
}
addMenuProvider(SourcesCatalogMenuProvider(this, viewModel))
addMenuProvider(SourcesCatalogMenuProvider(this, viewModel, this))
}

override fun onWindowInsetsChanged(insets: Insets) {
Expand All @@ -83,6 +86,19 @@ class SourcesCatalogActivity : BaseActivity<ActivitySourcesCatalogBinding>(),
viewBinding.recyclerView.firstVisibleItemPosition = 0
}

override fun onMenuItemActionExpand(item: MenuItem): Boolean {
viewBinding.tabs.isVisible = false
val sq = (item.actionView as? SearchView)?.query?.trim()?.toString().orEmpty()
viewModel.performSearch(sq)
return true
}

override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
viewBinding.tabs.isVisible = true
viewModel.performSearch(null)
return true
}

private fun initTabs() {
val tabs = viewBinding.tabs
for (type in ContentType.entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SourcesCatalogListProducer @AssistedInject constructor(
) : InvalidationTracker.Observer(TABLE_SOURCES), RetainedLifecycle.OnClearedListener {

private val scope = lifecycle.lifecycleScope
private var query: String = ""
private var query: String? = null
val list = MutableStateFlow(emptyList<SourceCatalogItem>())

private var job = scope.launch(Dispatchers.Default) {
Expand All @@ -54,20 +54,21 @@ class SourcesCatalogListProducer @AssistedInject constructor(
}
}

fun setQuery(value: String) {
fun setQuery(value: String?) {
this.query = value
onInvalidated(emptySet())
}

private suspend fun buildList(): List<SourceCatalogItem> {
val sources = repository.getDisabledSources().toMutableList()
sources.retainAll { it.contentType == contentType && it.locale == locale }
if (query.isNotEmpty()) {
sources.retainAll { it.title.contains(query, ignoreCase = true) }
when (val q = query) {
null -> sources.retainAll { it.contentType == contentType && it.locale == locale }
"" -> return emptyList()
else -> sources.retainAll { it.title.contains(q, ignoreCase = true) }
}
return if (sources.isEmpty()) {
listOf(
if (query.isEmpty()) {
if (query == null) {
SourceCatalogItem.Hint(
icon = R.drawable.ic_empty_feed,
title = R.string.no_manga_sources,
Expand All @@ -86,6 +87,7 @@ class SourcesCatalogListProducer @AssistedInject constructor(
sources.map {
SourceCatalogItem.Source(
source = it,
showSummary = query != null,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.koitharu.kotatsu.parsers.util.toTitleCase
class SourcesCatalogMenuProvider(
private val activity: Activity,
private val viewModel: SourcesCatalogViewModel,
private val expandListener: MenuItem.OnActionExpandListener,
) : MenuProvider,
MenuItem.OnActionExpandListener,
SearchView.OnQueryTextListener {
Expand All @@ -40,18 +41,18 @@ class SourcesCatalogMenuProvider(

override fun onMenuItemActionExpand(item: MenuItem): Boolean {
(activity as? AppBarOwner)?.appBar?.setExpanded(false, true)
return true
return expandListener.onMenuItemActionExpand(item)
}

override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
(item.actionView as SearchView).setQuery("", false)
return true
return expandListener.onMenuItemActionCollapse(item)
}

override fun onQueryTextSubmit(query: String?): Boolean = false

override fun onQueryTextChange(newText: String?): Boolean {
viewModel.performSearch(newText.orEmpty())
viewModel.performSearch(newText?.trim().orEmpty())
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import javax.inject.Inject
class SourcesCatalogViewModel @Inject constructor(
private val repository: MangaSourcesRepository,
private val listProducerFactory: SourcesCatalogListProducer.Factory,
private val settings: AppSettings,
settings: AppSettings,
) : BaseViewModel() {

private val lifecycle = RetainedLifecycleImpl()
private var searchQuery: String = ""
private var searchQuery: String? = null
val onActionDone = MutableEventFlow<ReversibleAction>()
val contentType = MutableStateFlow(ContentType.entries.first())
val locales = getLocalesImpl()
Expand All @@ -59,7 +59,7 @@ class SourcesCatalogViewModel @Inject constructor(
lifecycle.dispatchOnCleared()
}

fun performSearch(query: String) {
fun performSearch(query: String?) {
searchQuery = query
listProducer.value?.setQuery(query)
}
Expand Down
29 changes: 23 additions & 6 deletions app/src/main/res/layout/item_source_catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,34 @@
app:shapeAppearance="?shapeAppearanceCornerSmall"
tools:src="@tools:sample/avatars" />

<TextView
android:id="@+id/textView_title"
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="?android:listPreferredItemPaddingStart"
android:layout_marginEnd="?android:listPreferredItemPaddingEnd"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceTitleSmall"
tools:text="@tools:sample/lorem[15]" />
android:orientation="vertical">

<TextView
android:id="@+id/textView_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceTitleSmall"
tools:text="@tools:sample/lorem[15]" />

<TextView
android:id="@+id/textView_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceBodySmall"
tools:text="English" />

</LinearLayout>

<ImageView
android:id="@+id/imageView_add"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@
<string name="source_summary_pattern">%1$s, %2$s</string>
<string name="sources_catalog">Sources catalog</string>
<string name="source_enabled">Source enabled</string>
<string name="no_manga_sources_catalog_text">No available sources in this section yet. Stay tuned</string>
<string name="no_manga_sources_catalog_text">There are no sources available in this section, or all of it might have been already added.\nStay tuned</string>
<string name="no_manga_sources_found">No available manga sources found by your query</string>
<string name="catalog">Catalog</string>
<string name="manage_sources">Manage sources</string>
Expand Down

0 comments on commit 95fbe49

Please sign in to comment.