Optimize partition search & loading indicator on search
This commit is contained in:
parent
6b83955e05
commit
f3b5ec4a91
1 changed files with 194 additions and 158 deletions
|
|
@ -37,8 +37,10 @@ import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.layout.onGloballyPositioned
|
import androidx.compose.ui.layout.onGloballyPositioned
|
||||||
import androidx.compose.ui.platform.LocalDensity
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
|
import androidx.compose.ui.text.TextRange
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import androidx.compose.ui.text.input.VisualTransformation
|
import androidx.compose.ui.text.input.VisualTransformation
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
|
@ -91,8 +93,6 @@ fun MainScreenWithDrawer(
|
||||||
|
|
||||||
val scrollState = rememberScrollState()
|
val scrollState = rememberScrollState()
|
||||||
val currentActivePath = Solfa.currentFile
|
val currentActivePath = Solfa.currentFile
|
||||||
var textInput = sharedScreenModel.searchTitle.collectAsState().value
|
|
||||||
|
|
||||||
val isSearchActive by sharedScreenModel.showSearchMenu.collectAsState()
|
val isSearchActive by sharedScreenModel.showSearchMenu.collectAsState()
|
||||||
val focusRequester = remember { FocusRequester() }
|
val focusRequester = remember { FocusRequester() }
|
||||||
|
|
||||||
|
|
@ -387,6 +387,14 @@ fun MainScreenWithDrawer(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val solfaKeyboardState = remember { SolfaKeyboardState() }
|
val solfaKeyboardState = remember { SolfaKeyboardState() }
|
||||||
|
val searchTitleFlow by sharedScreenModel.searchTitle.collectAsState()
|
||||||
|
var textInput by remember { mutableStateOf(TextFieldValue(searchTitleFlow, TextRange(searchTitleFlow.length))) }
|
||||||
|
|
||||||
|
LaunchedEffect(searchTitleFlow) {
|
||||||
|
if (searchTitleFlow != textInput.text) {
|
||||||
|
textInput = TextFieldValue(searchTitleFlow, TextRange(searchTitleFlow.length))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CompositionLocalProvider(LocalSolfaKeyboardState provides solfaKeyboardState) {
|
CompositionLocalProvider(LocalSolfaKeyboardState provides solfaKeyboardState) {
|
||||||
|
|
||||||
|
|
@ -437,7 +445,7 @@ fun MainScreenWithDrawer(
|
||||||
value = textInput,
|
value = textInput,
|
||||||
onValueChange = { newValue ->
|
onValueChange = { newValue ->
|
||||||
textInput = newValue
|
textInput = newValue
|
||||||
sharedScreenModel.updateSearchTxt(newValue)
|
sharedScreenModel.updateSearchTxt(newValue.text)
|
||||||
},
|
},
|
||||||
readOnly = isAndroid && isNumericKeyboard,
|
readOnly = isAndroid && isNumericKeyboard,
|
||||||
textStyle = LocalTextStyle.current.copy(
|
textStyle = LocalTextStyle.current.copy(
|
||||||
|
|
@ -459,7 +467,7 @@ fun MainScreenWithDrawer(
|
||||||
),
|
),
|
||||||
decorationBox = { innerTextField ->
|
decorationBox = { innerTextField ->
|
||||||
TextFieldDefaults.DecorationBox(
|
TextFieldDefaults.DecorationBox(
|
||||||
value = textInput,
|
value = textInput.text,
|
||||||
innerTextField = innerTextField,
|
innerTextField = innerTextField,
|
||||||
enabled = true,
|
enabled = true,
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
|
|
@ -503,7 +511,7 @@ fun MainScreenWithDrawer(
|
||||||
|
|
||||||
IconButton(
|
IconButton(
|
||||||
onClick = {
|
onClick = {
|
||||||
textInput = ""
|
textInput = TextFieldValue("", TextRange.Zero)
|
||||||
sharedScreenModel.updateSearchTxt("")
|
sharedScreenModel.updateSearchTxt("")
|
||||||
},
|
},
|
||||||
modifier = Modifier.size(32.dp)
|
modifier = Modifier.size(32.dp)
|
||||||
|
|
@ -1141,7 +1149,7 @@ fun MainScreenWithDrawer(
|
||||||
if (isSearchActive) {
|
if (isSearchActive) {
|
||||||
sharedScreenModel.showSearchMenu(false)
|
sharedScreenModel.showSearchMenu(false)
|
||||||
sharedScreenModel.updateSearchTxt("")
|
sharedScreenModel.updateSearchTxt("")
|
||||||
textInput = ""
|
textInput = TextFieldValue("", TextRange.Zero)
|
||||||
} else {
|
} else {
|
||||||
sharedScreenModel.showSearchMenu(true)
|
sharedScreenModel.showSearchMenu(true)
|
||||||
scope.launch {
|
scope.launch {
|
||||||
|
|
@ -1167,7 +1175,7 @@ fun MainScreenWithDrawer(
|
||||||
}
|
}
|
||||||
|
|
||||||
androidx.compose.animation.AnimatedVisibility(
|
androidx.compose.animation.AnimatedVisibility(
|
||||||
visible = isSearchActive && textInput.isNotEmpty(),
|
visible = isSearchActive && textInput.text.isNotEmpty(),
|
||||||
enter = fadeIn() + expandVertically(),
|
enter = fadeIn() + expandVertically(),
|
||||||
exit = fadeOut() + shrinkVertically(),
|
exit = fadeOut() + shrinkVertically(),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
|
@ -1182,7 +1190,13 @@ fun MainScreenWithDrawer(
|
||||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||||
modifier = Modifier.fillMaxHeight()
|
modifier = Modifier.fillMaxHeight()
|
||||||
) {
|
) {
|
||||||
val sortedSongs = remember(filteredSongs, textInput) {
|
var sortedSongs by remember { mutableStateOf(emptyList<DrawerItem>()) }
|
||||||
|
var isSearching by remember { mutableStateOf(true) }
|
||||||
|
|
||||||
|
LaunchedEffect(filteredSongs, textInput.text) {
|
||||||
|
isSearching = true
|
||||||
|
val query = textInput.text
|
||||||
|
val result = withContext(Dispatchers.Default) {
|
||||||
val naturalComparator = Comparator<DrawerItem> { a, b ->
|
val naturalComparator = Comparator<DrawerItem> { a, b ->
|
||||||
val regex = Regex("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
|
val regex = Regex("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
|
||||||
val chunksA = a.title.lowercase().split(regex)
|
val chunksA = a.title.lowercase().split(regex)
|
||||||
|
|
@ -1204,36 +1218,42 @@ fun MainScreenWithDrawer(
|
||||||
if (result == 0) chunksA.size.compareTo(chunksB.size) else result
|
if (result == 0) chunksA.size.compareTo(chunksB.size) else result
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textInput.isBlank()) {
|
if (query.isBlank()) {
|
||||||
filteredSongs.sortedWith(
|
filteredSongs.sortedWith(
|
||||||
compareBy<DrawerItem> { item -> !item.path.startsWith("assets://") }
|
compareBy<DrawerItem> { item -> !item.path.startsWith("assets://") }
|
||||||
.thenComparing(naturalComparator)
|
.thenComparing(naturalComparator)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
filteredSongs.sortedWith(
|
filteredSongs.sortedWith(
|
||||||
compareByDescending<DrawerItem> { item -> item.getSearchScore(textInput) }
|
compareByDescending<DrawerItem> { item -> item.getSearchScore(query) }
|
||||||
.thenBy { item -> !item.path.startsWith("assets://") }
|
.thenBy { item -> !item.path.startsWith("assets://") }
|
||||||
.thenComparing(naturalComparator)
|
.thenComparing(naturalComparator)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sortedSongs = result
|
||||||
|
isSearching = false
|
||||||
|
}
|
||||||
|
|
||||||
val searchListState = rememberLazyListState()
|
val searchListState = rememberLazyListState()
|
||||||
|
|
||||||
LaunchedEffect(textInput) {
|
LaunchedEffect(textInput.text) {
|
||||||
if (sortedSongs.isNotEmpty()) {
|
if (sortedSongs.isNotEmpty()) {
|
||||||
searchListState.scrollToItem(0)
|
searchListState.scrollToItem(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
state = searchListState,
|
state = searchListState,
|
||||||
modifier = Modifier.fillMaxSize()
|
modifier = Modifier.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
if (sortedSongs.isEmpty() && textInput.isNotEmpty()) {
|
if (!isSearching && sortedSongs.isEmpty() && textInput.text.isNotEmpty()) {
|
||||||
item {
|
item {
|
||||||
ListItem(
|
ListItem(
|
||||||
headlineContent = {
|
headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
text = "Partition non trouvée pour \"$textInput\"",
|
text = "Partition non trouvée pour \"${textInput.text}\"",
|
||||||
color = MaterialTheme.colorScheme.error,
|
color = MaterialTheme.colorScheme.error,
|
||||||
fontWeight = FontWeight.Medium,
|
fontWeight = FontWeight.Medium,
|
||||||
fontSize = 14.sp
|
fontSize = 14.sp
|
||||||
|
|
@ -1338,6 +1358,20 @@ fun MainScreenWithDrawer(
|
||||||
HorizontalDivider()
|
HorizontalDivider()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isSearching) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
contentAlignment = Alignment.Center
|
||||||
|
) {
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier.size(40.dp),
|
||||||
|
color = MaterialTheme.colorScheme.primary,
|
||||||
|
strokeWidth = 3.dp
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1353,21 +1387,23 @@ fun MainScreenWithDrawer(
|
||||||
keyboardHeightDp = with(density) { coordinates.size.height.toDp() }
|
keyboardHeightDp = with(density) { coordinates.size.height.toDp() }
|
||||||
},
|
},
|
||||||
onDigitClick = { digit ->
|
onDigitClick = { digit ->
|
||||||
textInput += digit
|
val newText = textInput.text + digit
|
||||||
sharedScreenModel.updateSearchTxt(textInput)
|
textInput = TextFieldValue(newText, TextRange(newText.length))
|
||||||
|
sharedScreenModel.updateSearchTxt(newText)
|
||||||
},
|
},
|
||||||
onDeleteClick = {
|
onDeleteClick = {
|
||||||
if (textInput.isNotEmpty()) {
|
if (textInput.text.isNotEmpty()) {
|
||||||
textInput = textInput.dropLast(1)
|
val newText = textInput.text.dropLast(1)
|
||||||
sharedScreenModel.updateSearchTxt(textInput)
|
textInput = TextFieldValue(newText, TextRange(newText.length))
|
||||||
|
sharedScreenModel.updateSearchTxt(newText)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onClearClick = {
|
onClearClick = {
|
||||||
textInput = ""
|
textInput = TextFieldValue("", TextRange.Zero)
|
||||||
sharedScreenModel.updateSearchTxt("")
|
sharedScreenModel.updateSearchTxt("")
|
||||||
},
|
},
|
||||||
onPrefixClick = { prefix ->
|
onPrefixClick = { prefix ->
|
||||||
val digits = textInput.trim()
|
val digits = textInput.text.trim()
|
||||||
val cleanPrefix = prefix.trim().lowercase()
|
val cleanPrefix = prefix.trim().lowercase()
|
||||||
|
|
||||||
if (digits.isNotEmpty() && digits.all { it.isDigit() }) {
|
if (digits.isNotEmpty() && digits.all { it.isDigit() }) {
|
||||||
|
|
@ -1380,16 +1416,16 @@ fun MainScreenWithDrawer(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (matchingItem != null) {
|
if (matchingItem != null) {
|
||||||
textInput = ""
|
textInput = TextFieldValue("", TextRange.Zero)
|
||||||
sharedScreenModel.updateSearchTxt("")
|
sharedScreenModel.updateSearchTxt("")
|
||||||
sharedScreenModel.reset()
|
sharedScreenModel.reset()
|
||||||
solfaScreenModel.loadFromFile(matchingItem.path)
|
solfaScreenModel.loadFromFile(matchingItem.path)
|
||||||
} else {
|
} else {
|
||||||
textInput = prefix
|
textInput = TextFieldValue(prefix, TextRange(prefix.length))
|
||||||
sharedScreenModel.updateSearchTxt(prefix)
|
sharedScreenModel.updateSearchTxt(prefix)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
textInput = prefix
|
textInput = TextFieldValue(prefix, TextRange(prefix.length))
|
||||||
sharedScreenModel.updateSearchTxt(prefix)
|
sharedScreenModel.updateSearchTxt(prefix)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue