add recente document

This commit is contained in:
m
2026-09-11 23:07:23 +02:00
parent eddf40d806
commit 64690d11f9
6 changed files with 36 additions and 5 deletions
@@ -33,6 +33,15 @@ interface FileDao {
""")
fun searchWithFilters(query: String?, category: String?): Flow<List<FileEntity>>
@Query("""
SELECT * FROM files
WHERE "exists" = 1
AND (:category IS NULL OR category = :category)
ORDER BY added_at DESC, name ASC
LIMIT :limit
""")
fun recentFiles(category: String?, limit: Int): Flow<List<FileEntity>>
@Upsert
suspend fun upsert(file: FileEntity)
@@ -39,6 +39,10 @@ class FileRepository @Inject constructor(
category = category,
)
/** Les `limit` fichiers les plus récemment ajoutés, filtrés par catégorie. */
fun recentFiles(category: String?, limit: Int = RECENT_LIMIT): Flow<List<FileEntity>> =
fileDao.recentFiles(category = category, limit = limit)
suspend fun getFile(resourceId: String): FileEntity? =
fileDao.getByResourceId(resourceId)
@@ -122,6 +126,7 @@ class FileRepository @Inject constructor(
companion object {
private const val PAGE_SIZE = 50
private const val RECENT_LIMIT = 5
}
}
@@ -164,10 +164,10 @@ private fun SearchResults(
onOpenDocument: (String) -> Unit,
modifier: Modifier = Modifier,
) {
val hasQueryOrCategory = uiState.query.isNotBlank() || uiState.selectedCategory != null
val hasQuery = uiState.query.isNotBlank()
when {
!hasQueryOrCategory -> Box(
!hasQuery && uiState.results.isEmpty() -> Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
@@ -180,7 +180,7 @@ private fun SearchResults(
)
}
uiState.results.isEmpty() -> Box(
hasQuery && uiState.results.isEmpty() -> Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
@@ -196,6 +196,16 @@ private fun SearchResults(
contentPadding = PaddingValues(bottom = 16.dp),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
if (!hasQuery) {
item(key = "recent-header") {
Text(
text = stringResource(R.string.search_recent),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
modifier = Modifier.padding(top = 4.dp),
)
}
}
items(uiState.results, key = { it.resourceId }) { file ->
SearchResultCard(file = file, onClick = { onOpenDocument(file.resourceId) })
}
@@ -31,8 +31,13 @@ class SearchViewModel @Inject constructor(
_selectedCategory,
) { query, category -> CategoryQuery(query, category) }
.flatMapLatest { (query, category) ->
fileRepository.searchFiles(query, category)
.map { files -> SearchUiState(query, category, files) }
val trimmed = query.trim()
val flow = if (trimmed.isEmpty()) {
fileRepository.recentFiles(category)
} else {
fileRepository.searchFiles(trimmed, category)
}
flow.map { files -> SearchUiState(query, category, files) }
}
.stateIn(
scope = viewModelScope,
@@ -32,6 +32,7 @@
<string name="search">Search</string>
<string name="search_hint">Search for a document…</string>
<string name="search_empty">Type a search or pick a category</string>
<string name="search_recent">Recent documents</string>
<string name="search_no_results">No results</string>
<string name="category_all">All</string>
<string name="category_pdf">PDF</string>
@@ -33,6 +33,7 @@
<string name="search">Recherche</string>
<string name="search_hint">Rechercher un document…</string>
<string name="search_empty">Saisis une recherche ou choisis une catégorie</string>
<string name="search_recent">Documents récents</string>
<string name="search_no_results">Aucun résultat</string>
<string name="category_all">Tous</string>
<string name="category_pdf">PDF</string>