forked from omni/OmniCards
feat: added templates search
This commit is contained in:
@@ -9,19 +9,15 @@ import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.background
|
||||
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.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -66,6 +62,8 @@ fun SelectTemplatePage(innerPadding: PaddingValues) {
|
||||
|
||||
val language = getLanguage(context)
|
||||
|
||||
var searchQuery by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
var templates by remember { mutableStateOf<List<Template>?>(null) }
|
||||
var loadingError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
@@ -100,14 +98,25 @@ fun SelectTemplatePage(innerPadding: PaddingValues) {
|
||||
stringResource(R.string.choosing_template),
|
||||
innerPadding
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = searchQuery,
|
||||
onValueChange = { newValue ->
|
||||
searchQuery = newValue
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text(stringResource(R.string.search_templates)) }
|
||||
)
|
||||
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
val currentTemplates = templates
|
||||
|
||||
if (loadingError != null) {
|
||||
item {
|
||||
Text(loadingError!!)
|
||||
}
|
||||
} else if (templates == null) {
|
||||
} else if (currentTemplates == null) {
|
||||
item {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
@@ -119,24 +128,49 @@ fun SelectTemplatePage(innerPadding: PaddingValues) {
|
||||
)
|
||||
}
|
||||
}
|
||||
} else if (templates!!.isEmpty()) {
|
||||
item {
|
||||
Text(stringResource(R.string.templates_not_found))
|
||||
}
|
||||
} else {
|
||||
for (template in templates) {
|
||||
val searchResults = currentTemplates.filter { template ->
|
||||
var queryCleaned = searchQuery.lowercase()
|
||||
|
||||
if (queryCleaned.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
|
||||
for (name in template.names.values) {
|
||||
var nameCleaned = name.lowercase()
|
||||
|
||||
for (spaceSymbol in SPACE_SYMBOLS) {
|
||||
nameCleaned = nameCleaned.replace(spaceSymbol, "")
|
||||
queryCleaned = queryCleaned.replace(spaceSymbol, "")
|
||||
}
|
||||
|
||||
if (nameCleaned.contains(queryCleaned)) {
|
||||
return@filter true
|
||||
}
|
||||
}
|
||||
|
||||
return@filter false
|
||||
}
|
||||
|
||||
if (searchResults.isEmpty()) {
|
||||
item {
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
val intent = Intent(context, FillTemplateActivity::class.java)
|
||||
Text(stringResource(R.string.templates_not_found))
|
||||
}
|
||||
} else {
|
||||
for (template in searchResults) {
|
||||
item {
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
val intent = Intent(context, FillTemplateActivity::class.java)
|
||||
|
||||
intent.putExtra("templateJson", Json.encodeToString(template))
|
||||
intent.putExtra("templateJson", Json.encodeToString(template))
|
||||
|
||||
fillTemplateLauncher.launch(intent)
|
||||
fillTemplateLauncher.launch(intent)
|
||||
}
|
||||
) {
|
||||
Text(selectTemplateNameViaLanguage(template.names, language))
|
||||
}
|
||||
) {
|
||||
Text(selectTemplateNameViaLanguage(template.names, language))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user