Fix multi-criterion search detail highligh
This commit is contained in:
		
							parent
							
								
									2e4c87c052
								
							
						
					
					
						commit
						9e5655c7d7
					
				
					 4 changed files with 28 additions and 21 deletions
				
			
		| 
						 | 
				
			
			@ -66,6 +66,7 @@ import com.pixelized.rplexicon.utilitary.extentions.annotatedString
 | 
			
		|||
import com.pixelized.rplexicon.utilitary.extentions.highlightRegex
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.lexicon
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.scrollOffset
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.searchCriterion
 | 
			
		||||
import com.skydoves.landscapist.ImageOptions
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
| 
						 | 
				
			
			@ -104,7 +105,8 @@ data class AnnotatedLexiconDetailUio(
 | 
			
		|||
fun LexiconDetailUio.annotate(): AnnotatedLexiconDetailUio {
 | 
			
		||||
    val colorScheme = MaterialTheme.colorScheme
 | 
			
		||||
    val highlight = remember { SpanStyle(color = colorScheme.primary) }
 | 
			
		||||
    val highlightRegex = remember(search) { search.highlightRegex }
 | 
			
		||||
    val trimmedSearch = remember(search) { search.searchCriterion() }
 | 
			
		||||
    val highlightRegex = remember(search) { trimmedSearch.highlightRegex }
 | 
			
		||||
    val gender = stringResource(id = gender, short = true)
 | 
			
		||||
    val race = stringResource(id = race)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,7 @@ import com.pixelized.rplexicon.utilitary.extentions.finderRegex
 | 
			
		|||
import com.pixelized.rplexicon.utilitary.extentions.foldAll
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.highlightRegex
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.lexicon
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.searchCriterion
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
class SearchItemUio(
 | 
			
		||||
| 
						 | 
				
			
			@ -109,9 +110,7 @@ class AnnotatedSearchItemUio(
 | 
			
		|||
private fun SearchItemUio.annotate(): AnnotatedSearchItemUio {
 | 
			
		||||
    val colorScheme = MaterialTheme.colorScheme
 | 
			
		||||
    val highlight = remember { SpanStyle(color = colorScheme.primary) }
 | 
			
		||||
    val trimmedSearch = remember(search) {
 | 
			
		||||
        search.split(" ").mapNotNull { it.trim().takeIf { trim -> trim.isNotEmpty() } }
 | 
			
		||||
    }
 | 
			
		||||
    val trimmedSearch = remember(search) { search.searchCriterion() }
 | 
			
		||||
    val highlightRegex = remember(search) { trimmedSearch.highlightRegex }
 | 
			
		||||
    val finderRegex = remember(search) { trimmedSearch.finderRegex }
 | 
			
		||||
    val gender = stringResource(id = gender, short = true)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,6 +11,7 @@ import com.pixelized.rplexicon.repository.data.LexiconRepository
 | 
			
		|||
import com.pixelized.rplexicon.ui.composable.form.DropDownFieldUio
 | 
			
		||||
import com.pixelized.rplexicon.ui.composable.form.TextFieldUio
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.composable.stringResource
 | 
			
		||||
import com.pixelized.rplexicon.utilitary.extentions.searchCriterion
 | 
			
		||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
			
		||||
import javax.inject.Inject
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -52,17 +53,15 @@ class SearchViewModel @Inject constructor(
 | 
			
		|||
        data.filter { item ->
 | 
			
		||||
            val gender = _gender.value?.let { it == item.gender }
 | 
			
		||||
            val race = _race.value?.let { it == item.race }
 | 
			
		||||
            val search = _search.value.split(" ").mapNotNull {
 | 
			
		||||
                it.takeIf { it.isNotEmpty() }?.trim()?.let { search ->
 | 
			
		||||
                    val name = item.name.contains(search, true)
 | 
			
		||||
                    val diminutive = item.diminutive?.contains(search, true) == true
 | 
			
		||||
                    val status = item.status?.contains(search, true) == true
 | 
			
		||||
                    val location = item.location?.contains(search, true) == true
 | 
			
		||||
                    val description = item.description?.contains(search, true) == true
 | 
			
		||||
                    val history = item.history?.contains(search, true) == true
 | 
			
		||||
                    val tag = item.tags?.contains(search, true) == true
 | 
			
		||||
                    name || diminutive || status || location || description || history || tag
 | 
			
		||||
                }
 | 
			
		||||
            val search = _search.value.searchCriterion().map { criteria ->
 | 
			
		||||
                val name = item.name.contains(criteria, true)
 | 
			
		||||
                val diminutive = item.diminutive?.contains(criteria, true) == true
 | 
			
		||||
                val status = item.status?.contains(criteria, true) == true
 | 
			
		||||
                val location = item.location?.contains(criteria, true) == true
 | 
			
		||||
                val description = item.description?.contains(criteria, true) == true
 | 
			
		||||
                val history = item.history?.contains(criteria, true) == true
 | 
			
		||||
                val tag = item.tags?.contains(criteria, true) == true
 | 
			
		||||
                name || diminutive || status || location || description || history || tag
 | 
			
		||||
            }
 | 
			
		||||
            (gender == null || gender) && (race == null || race) && (search.all { it })
 | 
			
		||||
        }.map {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -41,12 +41,6 @@ val List<String>.finderRegex: Regex?
 | 
			
		|||
        )
 | 
			
		||||
    } else null
 | 
			
		||||
 | 
			
		||||
fun String?.toUriOrNull(): Uri? = try {
 | 
			
		||||
    this?.takeIf { it.isNotBlank() }?.toUri()
 | 
			
		||||
} catch (_: Exception) {
 | 
			
		||||
    null
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
fun String.annotateWithDropCap(
 | 
			
		||||
    style: SpanStyle,
 | 
			
		||||
| 
						 | 
				
			
			@ -57,3 +51,16 @@ fun String.annotateWithDropCap(
 | 
			
		|||
        spanStyle = style,
 | 
			
		||||
    )
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
fun String?.toUriOrNull(): Uri? = try {
 | 
			
		||||
    this?.takeIf { it.isNotBlank() }?.toUri()
 | 
			
		||||
} catch (_: Exception) {
 | 
			
		||||
    null
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
fun String?.searchCriterion(): List<String> = this
 | 
			
		||||
    ?.split(" ")
 | 
			
		||||
    ?.mapNotNull { it.trim().takeIf { trim -> trim.isNotEmpty() } }
 | 
			
		||||
    ?: emptyList()
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue