Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,9 @@ fun AppNavigation(
onItemClick = { contentId ->
navController.navigate(Route.ItemDetail(contentId).route)
},
onPersonClick = { personId ->
navController.navigate(Route.PersonDetail(personId).route)
},
onRequestMediaClick = { item ->
navController.navigate(Route.RequestDetail(item.mediaType, item.tmdbId).route)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.siloserver.silo.android.ui.screens.search

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.siloserver.silo.android.ui.theme.SiloOnSurface
import org.siloserver.silo.android.ui.theme.SiloSecondaryText
import org.siloserver.silo.android.ui.theme.SiloSurfaceElevated
import org.siloserver.silo.common.ui.components.DeferImagePresentationWhileScrolling
import org.siloserver.silo.common.ui.components.ThumbhashImage
import org.siloserver.silo.model.catalog.Person
import org.siloserver.silo.model.catalog.personInitials

/**
* Cast and crew matching the search, as a horizontal rail of circular
* portraits in the style of the detail page's cast rail. Tapping one opens
* that person's page. People without a photo show their initials.
*/
@Composable
fun SearchPeopleRow(
people: List<Person>,
onPersonClick: (Long) -> Unit,
modifier: Modifier = Modifier,
) {
if (people.isEmpty()) return
val rowState = rememberLazyListState()
Column(
modifier = modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
Text(
text = "People",
style = MaterialTheme.typography.titleSmall,
fontWeight = FontWeight.SemiBold,
color = SiloOnSurface,
modifier = Modifier.padding(horizontal = 2.dp),
)
DeferImagePresentationWhileScrolling(rowState) {
LazyRow(
state = rowState,
horizontalArrangement = Arrangement.spacedBy(14.dp),
contentPadding = PaddingValues(end = 16.dp),
modifier = Modifier.fillMaxWidth(),
) {
items(people, key = { it.id }, contentType = { "search-person" }) { person ->
SearchPersonTile(person = person, onClick = { onPersonClick(person.id) })
}
}
}
}
}

@Composable
private fun SearchPersonTile(person: Person, onClick: () -> Unit) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier
.width(96.dp)
.clickable(role = Role.Button, onClick = onClick),
) {
Box(
modifier = Modifier
.size(76.dp)
.clip(CircleShape)
.background(SiloSurfaceElevated)
.border(1.dp, Color.White.copy(alpha = 0.10f), CircleShape),
contentAlignment = Alignment.Center,
) {
if (!person.photoUrl.isNullOrBlank()) {
ThumbhashImage(
url = person.photoUrl,
thumbhash = person.photoThumbhash,
contentDescription = null,
modifier = Modifier
.fillMaxSize()
.clip(CircleShape),
)
} else {
Text(
text = personInitials(person.name),
fontSize = 22.sp,
fontWeight = FontWeight.SemiBold,
color = SiloSecondaryText,
// The name below already says who this is.
modifier = Modifier.clearAndSetSemantics {},
)
}
}
Text(
text = person.name,
fontSize = 12.sp,
lineHeight = 15.sp,
fontWeight = FontWeight.SemiBold,
color = SiloOnSurface,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
textAlign = TextAlign.Center,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,33 @@ import org.siloserver.silo.android.ui.components.MediaGridDefaults
import org.siloserver.silo.android.ui.components.rememberBrowseItemCardActions
import org.siloserver.silo.common.ui.components.DeferImagePresentationWhileScrolling
import org.siloserver.silo.model.catalog.BrowseItem
import org.siloserver.silo.model.catalog.Person

/**
* Displays search results in a vertical grid of media cards.
*
* Supports infinite scroll to load additional results.
*
* @param results The search result items to display.
* @param people Cast and crew matching the query, shown above the titles.
* @param total Total number of matching results.
* @param isSearching Whether a search request is in flight.
* @param hasMore Whether more results are available.
* @param onItemClick Callback with content ID when a result card is tapped.
* @param onPersonClick Callback with person ID when a person is tapped.
* @param onLoadMore Callback to load the next page of results.
* @param modifier Compose modifier.
*/
@Composable
fun SearchResults(
results: List<BrowseItem>,
people: List<Person>,
total: Int,
totalExact: Boolean = true,
isSearching: Boolean,
hasMore: Boolean,
onItemClick: (String) -> Unit,
onPersonClick: (Long) -> Unit,
onLoadMore: () -> Unit,
modifier: Modifier = Modifier,
footer: (@Composable () -> Unit)? = null,
Expand Down Expand Up @@ -92,16 +97,28 @@ fun SearchResults(
verticalArrangement = Arrangement.spacedBy(MediaGridDefaults.PosterGridVerticalSpacing),
modifier = modifier,
) {
// Result count header
item(span = { GridItemSpan(maxLineSpan) }, contentType = "search-result-count") {
Text(
text = "${if (totalExact) "" else "About "}$total result${if (total == 1) "" else "s"}",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
// The grid's own contentPadding supplies the 16.dp gutters, so
// the header only needs to clear the first row of cards.
modifier = Modifier.padding(horizontal = 2.dp, vertical = 4.dp),
)
if (people.isNotEmpty()) {
item(span = { GridItemSpan(maxLineSpan) }, contentType = "search-people") {
SearchPeopleRow(
people = people,
onPersonClick = onPersonClick,
modifier = Modifier.padding(horizontal = 2.dp, vertical = 4.dp),
)
}
}

// Result count header. A people-only answer has no titles to count.
if (results.isNotEmpty() || people.isEmpty()) {
item(span = { GridItemSpan(maxLineSpan) }, contentType = "search-result-count") {
Text(
text = "${if (totalExact) "" else "About "}$total result${if (total == 1) "" else "s"}",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
// The grid's own contentPadding supplies the 16.dp gutters, so
// the header only needs to clear the first row of cards.
modifier = Modifier.padding(horizontal = 2.dp, vertical = 4.dp),
)
}
}

items(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ import org.koin.compose.koinInject
* (debounced 300ms), and appropriate empty/loading/error states.
*
* @param onItemClick Callback with content ID when a result is tapped.
* @param onPersonClick Callback with person ID when a people result is tapped.
* @param viewModel The search ViewModel (provided by Koin).
*/
@Composable
fun SearchScreen(
onItemClick: (String) -> Unit,
onPersonClick: (Long) -> Unit,
onRequestMediaClick: (RequestMediaResult) -> Unit,
onRequestLibraryItemClick: (String) -> Unit,
onBackClick: (() -> Unit)? = null,
Expand Down Expand Up @@ -195,7 +197,11 @@ fun SearchScreen(
}

when {
state.isSearching && state.results.isEmpty() -> {
// Titles can answer before people do. Hold off on "No results"
// until both have, so a person-only match does not flash it.
// A title error is final and shows at once with its Retry.
(state.isSearching || (state.isLoadingPeople && state.error == null)) &&
state.results.isEmpty() && state.people.isEmpty() -> {
// Sits in the top part of the content area, matching the
// empty state's offset, so it stays visible above the IME
// instead of being centred in the space the keyboard covers.
Expand Down Expand Up @@ -254,7 +260,7 @@ fun SearchScreen(
}
}
}
state.hasSearched && state.results.isEmpty() && !state.isSearching -> {
state.hasSearched && state.results.isEmpty() && state.people.isEmpty() && !state.isSearching -> {
Comment thread
Quick104 marked this conversation as resolved.
Resolved
Column(modifier = Modifier.fillMaxSize()) {
Box(modifier = Modifier.weight(1f)) {
SearchEmptyState(
Expand All @@ -268,11 +274,13 @@ fun SearchScreen(
else -> {
SearchResults(
results = state.results,
people = state.people,
total = state.total,
totalExact = state.totalExact,
isSearching = state.isSearching,
hasMore = state.hasMore,
onItemClick = onItemClick,
onPersonClick = onPersonClick,
onLoadMore = { viewModel.loadMore() },
modifier = Modifier.fillMaxSize(),
footer = { RequestSearchFooter() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package org.siloserver.silo.android.ui.screens.search
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import org.siloserver.silo.model.catalog.BrowseItem
import org.siloserver.silo.model.catalog.Person
import org.siloserver.silo.model.navigation.MediaMode
import org.siloserver.silo.model.navigation.mobileMediaModeForLibraryType
import org.siloserver.silo.network.ApiResult
import org.siloserver.silo.network.apiv2.CatalogContinuationV2
import org.siloserver.silo.network.errorMessage
import org.siloserver.silo.repository.CatalogRepository
import org.siloserver.silo.repository.searchPeopleForQuery
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -55,6 +59,19 @@ internal fun MobileSearchMediaType.filterResults(items: List<BrowseItem>): List<
else -> items
}

/**
* People-search scopes for each filter. Audio people are audiobook authors and
* narrators; Reading covers both ebooks and manga, which the server scopes
* separately.
*/
internal val MobileSearchMediaType.peopleMediaScopes: List<String?>
get() = when (this) {
MobileSearchMediaType.All -> listOf(null)
MobileSearchMediaType.Video -> listOf("video")
MobileSearchMediaType.Audio -> listOf("audiobook")
MobileSearchMediaType.Reading -> listOf("ebook", "manga")
}

private val MobileSearchMediaType.isClientFiltered: Boolean
get() = this != MobileSearchMediaType.All

Expand Down Expand Up @@ -88,6 +105,10 @@ data class SearchUiState(
val availableMediaTypes: List<MobileSearchMediaType> = MobileSearchMediaType.entries.toList(),
val isSearching: Boolean = false,
val results: List<BrowseItem> = emptyList(),
/** Cast and crew matching the query, shown above the title results. */
val people: List<Person> = emptyList(),
/** True until the current query's people lookup answers. */
val isLoadingPeople: Boolean = false,
val hasMore: Boolean = false,
val total: Int = 0,
val totalExact: Boolean = false,
Expand All @@ -114,6 +135,7 @@ class SearchViewModel(
private val _queryFlow = MutableStateFlow("")

private var continuation: CatalogContinuationV2? = null
private var peopleJob: Job? = null
private val pageSize = 60

/**
Expand All @@ -135,10 +157,13 @@ class SearchViewModel(
return@collectLatest
}
if (query.isBlank()) {
peopleJob?.cancel()
_uiState.update {
it.copy(
query = query,
results = emptyList(),
people = emptyList(),
isLoadingPeople = false,
hasMore = false,
total = 0,
isSearching = false,
Expand Down Expand Up @@ -193,11 +218,14 @@ class SearchViewModel(
*/
fun clearSearch() {
pendingVoiceQuery = null
peopleJob?.cancel()
_uiState.update {
it.copy(
query = "",
isSearching = false,
results = emptyList(),
people = emptyList(),
isLoadingPeople = false,
hasMore = false,
total = 0,
error = null,
Expand Down Expand Up @@ -256,6 +284,25 @@ class SearchViewModel(
}
}

/**
* Looks up people alongside the title search. It runs on its own so a slow
* or failed people lookup never holds back or fails the title results.
* The previous query's people are cleared first so they never sit beside
* the new query's titles.
*/
private fun searchPeople(query: String, mediaType: MobileSearchMediaType) {
peopleJob?.cancel()
_uiState.update { it.copy(people = emptyList(), isLoadingPeople = true) }
peopleJob = viewModelScope.launch {
val people = catalogRepository.searchPeopleForQuery(query, mediaType.peopleMediaScopes)
// Cancellation surfaces as an empty answer, not an exception.
if (!isActive) return@launch
val latest = _uiState.value
if (latest.query != query || latest.mediaType != mediaType) return@launch
_uiState.update { it.copy(people = people, isLoadingPeople = false) }
}
}

private suspend fun performSearch(query: String, reset: Boolean) {
val currentState = _uiState.value
val requestedMediaType = currentState.mediaType
Expand All @@ -267,6 +314,7 @@ class SearchViewModel(
var total = 0

_uiState.update { it.copy(isSearching = true, error = null) }
if (reset) searchPeople(query, requestedMediaType)

while (true) {
val result = catalogRepository.browse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fun TvCastCrewSection(

@OptIn(ExperimentalTvMaterial3Api::class)
@Composable
private fun TvCastCard(
internal fun TvCastCard(
member: CastMember,
photoSize: Dp,
focusRequester: FocusRequester?,
Expand Down
Loading
Loading