Skip to content

Commit

Permalink
Adds step 6.c - Adds AnimatedSearchBar component
Browse files Browse the repository at this point in the history
  • Loading branch information
aldefy committed Jun 30, 2024
1 parent 37ad2e8 commit ed3b32b
Showing 1 changed file with 98 additions and 0 deletions.
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
)
}
}
}
}

0 comments on commit ed3b32b

Please sign in to comment.