-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds step 6.c - Adds AnimatedSearchBar component
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
composeApp/src/commonMain/kotlin/app/academy/components/AnimatedSearchBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package app.academy.components | ||
|
||
import androidx.compose.animation.AnimatedContent | ||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.sizeIn | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material.icons.filled.Close | ||
import androidx.compose.material.icons.filled.Search | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.SearchBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import app.academy.model.Note | ||
import app.academy.utils.getWindowSize | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@ExperimentalFoundationApi | ||
@Composable | ||
private fun AnimatedSearchBar( | ||
query: String, | ||
isSearchBarActive: Boolean, | ||
onQueryChange: (String) -> Unit, | ||
onClearSearchQueryButtonClick: () -> Unit, | ||
onActiveChange: (Boolean) -> Unit, | ||
onBackButtonClick: () -> Unit, | ||
onNoteDismissed: (Note) -> Unit, | ||
onNoteItemClick: (Note) -> Unit, | ||
modifier: Modifier = Modifier, | ||
suggestionsForQuery: List<Note> | ||
) { | ||
val leadingIcon = @Composable { | ||
AnimatedContent(targetState = isSearchBarActive, label = "") { isActive -> | ||
if (isActive) { | ||
IconButton(onClick = onBackButtonClick) { | ||
Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = null) | ||
} | ||
} else { | ||
Icon(imageVector = Icons.Filled.Search, contentDescription = null) | ||
} | ||
} | ||
} | ||
|
||
val trailingIcon = @Composable { | ||
AnimatedContent(targetState = isSearchBarActive, label = "") { isActive -> | ||
if (isActive) { | ||
IconButton( | ||
onClick = onClearSearchQueryButtonClick, | ||
content = { Icon(imageVector = Icons.Filled.Close, contentDescription = null) } | ||
) | ||
} | ||
} | ||
} | ||
val windowSizeWithoutInsets = getWindowSize() | ||
Column(modifier = modifier) { | ||
SearchBar( | ||
modifier = Modifier | ||
.align(Alignment.CenterHorizontally) | ||
.sizeIn( | ||
// need sizeIn modifier to prevent exception being thrown about size being set to infinity when search bar is expanded | ||
maxWidth = windowSizeWithoutInsets.width, | ||
maxHeight = windowSizeWithoutInsets.height | ||
), | ||
query = query, | ||
onQueryChange = onQueryChange, | ||
onSearch = { | ||
// no need for this callback because this app uses instant search | ||
}, | ||
active = isSearchBarActive, | ||
onActiveChange = onActiveChange, | ||
leadingIcon = leadingIcon, | ||
trailingIcon = trailingIcon, | ||
placeholder = { Text(text = "Search notes") }, | ||
) { | ||
LazyColumn( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(top = 8.dp) | ||
) { | ||
NoteItems( | ||
notes = suggestionsForQuery, | ||
onClick = onNoteItemClick, | ||
onDismissed = onNoteDismissed | ||
) | ||
} | ||
} | ||
} | ||
} |