@@ -27,7 +27,6 @@ import androidx.compose.foundation.lazy.LazyColumn
27
27
import androidx.compose.foundation.lazy.items
28
28
import androidx.compose.material.icons.Icons
29
29
import androidx.compose.material.icons.automirrored.filled.ArrowBack
30
- import androidx.compose.material.icons.filled.ArrowBack
31
30
import androidx.compose.material.icons.filled.MoreVert
32
31
import androidx.compose.material.icons.filled.Search
33
32
import androidx.compose.material3.DockedSearchBar
@@ -38,6 +37,7 @@ import androidx.compose.material3.IconButton
38
37
import androidx.compose.material3.IconButtonDefaults
39
38
import androidx.compose.material3.ListItem
40
39
import androidx.compose.material3.MaterialTheme
40
+ import androidx.compose.material3.SearchBarDefaults
41
41
import androidx.compose.material3.Text
42
42
import androidx.compose.material3.TopAppBar
43
43
import androidx.compose.material3.TopAppBarDefaults
@@ -63,8 +63,11 @@ fun ReplyDockedSearchBar(
63
63
modifier : Modifier = Modifier
64
64
) {
65
65
var query by remember { mutableStateOf(" " ) }
66
- var active by remember { mutableStateOf(false ) }
66
+ var expanded by remember { mutableStateOf(false ) }
67
67
val searchResults = remember { mutableStateListOf<Email >() }
68
+ val onExpandedChange: (Boolean ) -> Unit = {
69
+ expanded = it
70
+ }
68
71
69
72
LaunchedEffect (query) {
70
73
searchResults.clear()
@@ -85,84 +88,90 @@ fun ReplyDockedSearchBar(
85
88
}
86
89
87
90
DockedSearchBar (
88
- modifier = modifier,
89
- query = query,
90
- onQueryChange = {
91
- query = it
92
- },
93
- onSearch = { active = false },
94
- active = active,
95
- onActiveChange = {
96
- active = it
97
- },
98
- placeholder = { Text (text = stringResource(id = R .string.search_emails)) },
99
- leadingIcon = {
100
- if (active) {
101
- Icon (
102
- imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
103
- contentDescription = stringResource(id = R .string.back_button),
104
- modifier = Modifier
105
- .padding(start = 16 .dp)
106
- .clickable {
107
- active = false
108
- query = " "
109
- },
110
- )
111
- } else {
112
- Icon (
113
- imageVector = Icons .Default .Search ,
114
- contentDescription = stringResource(id = R .string.search),
115
- modifier = Modifier .padding(start = 16 .dp),
116
- )
117
- }
118
- },
119
- trailingIcon = {
120
- ReplyProfileImage (
121
- drawableResource = R .drawable.avatar_6,
122
- description = stringResource(id = R .string.profile),
123
- modifier = Modifier
124
- .padding(12 .dp)
125
- .size(32 .dp)
126
- )
127
- },
128
- ) {
129
- if (searchResults.isNotEmpty()) {
130
- LazyColumn (
91
+ inputField = {
92
+ SearchBarDefaults .InputField (
93
+ query = query,
94
+ onQueryChange = {
95
+ query = it
96
+ },
97
+ onSearch = { expanded = false },
98
+ expanded = expanded,
99
+ onExpandedChange = onExpandedChange,
131
100
modifier = Modifier .fillMaxWidth(),
132
- contentPadding = PaddingValues (16 .dp),
133
- verticalArrangement = Arrangement .spacedBy(4 .dp)
134
- ) {
135
- items(items = searchResults, key = { it.id }) { email ->
136
- ListItem (
137
- headlineContent = { Text (email.subject) },
138
- supportingContent = { Text (email.sender.fullName) },
139
- leadingContent = {
140
- ReplyProfileImage (
141
- drawableResource = email.sender.avatar,
142
- description = stringResource(id = R .string.profile),
143
- modifier = Modifier
144
- .size(32 .dp)
145
- )
146
- },
147
- modifier = Modifier .clickable {
148
- onSearchItemSelected.invoke(email)
149
- query = " "
150
- active = false
151
- }
101
+ placeholder = { Text (text = stringResource(id = R .string.search_emails)) },
102
+ leadingIcon = {
103
+ if (expanded) {
104
+ Icon (
105
+ imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
106
+ contentDescription = stringResource(id = R .string.back_button),
107
+ modifier = Modifier
108
+ .padding(start = 16 .dp)
109
+ .clickable {
110
+ expanded = false
111
+ query = " "
112
+ },
113
+ )
114
+ } else {
115
+ Icon (
116
+ imageVector = Icons .Default .Search ,
117
+ contentDescription = stringResource(id = R .string.search),
118
+ modifier = Modifier .padding(start = 16 .dp),
119
+ )
120
+ }
121
+ },
122
+ trailingIcon = {
123
+ ReplyProfileImage (
124
+ drawableResource = R .drawable.avatar_6,
125
+ description = stringResource(id = R .string.profile),
126
+ modifier = Modifier
127
+ .padding(12 .dp)
128
+ .size(32 .dp)
152
129
)
153
- }
154
- }
155
- } else if (query.isNotEmpty()) {
156
- Text (
157
- text = stringResource(id = R .string.no_item_found),
158
- modifier = Modifier .padding(16 .dp)
159
- )
160
- } else
161
- Text (
162
- text = stringResource(id = R .string.no_search_history),
163
- modifier = Modifier .padding(16 .dp)
130
+ },
164
131
)
165
- }
132
+ },
133
+ expanded = expanded,
134
+ onExpandedChange = onExpandedChange,
135
+ modifier = modifier,
136
+ content = {
137
+ if (searchResults.isNotEmpty()) {
138
+ LazyColumn (
139
+ modifier = Modifier .fillMaxWidth(),
140
+ contentPadding = PaddingValues (16 .dp),
141
+ verticalArrangement = Arrangement .spacedBy(4 .dp)
142
+ ) {
143
+ items(items = searchResults, key = { it.id }) { email ->
144
+ ListItem (
145
+ headlineContent = { Text (email.subject) },
146
+ supportingContent = { Text (email.sender.fullName) },
147
+ leadingContent = {
148
+ ReplyProfileImage (
149
+ drawableResource = email.sender.avatar,
150
+ description = stringResource(id = R .string.profile),
151
+ modifier = Modifier
152
+ .size(32 .dp)
153
+ )
154
+ },
155
+ modifier = Modifier .clickable {
156
+ onSearchItemSelected.invoke(email)
157
+ query = " "
158
+ expanded = false
159
+ }
160
+ )
161
+ }
162
+ }
163
+ } else if (query.isNotEmpty()) {
164
+ Text (
165
+ text = stringResource(id = R .string.no_item_found),
166
+ modifier = Modifier .padding(16 .dp)
167
+ )
168
+ } else
169
+ Text (
170
+ text = stringResource(id = R .string.no_search_history),
171
+ modifier = Modifier .padding(16 .dp)
172
+ )
173
+ }
174
+ )
166
175
}
167
176
168
177
@OptIn(ExperimentalMaterial3Api ::class )
0 commit comments