Skip to content

Commit

Permalink
Implemented Material3 SearchBar feature in FoodExpirationDates App
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragkanojiya1 committed Jan 3, 2025
1 parent 409b22f commit 0693488
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class MainActivity : ComponentActivity() {
darkScrim = android.graphics.Color.TRANSPARENT,
detectDarkMode = { _ -> isInDarkTheme }
)
val searchQuery = remember { mutableStateOf("") }

enableEdgeToEdge(
statusBarStyle = systemBarStyle,
navigationBarStyle = systemBarStyle
Expand All @@ -88,12 +90,14 @@ class MainActivity : ComponentActivity() {
MyScaffold(
activity = this,
navController = navController,
showSnackbar = showSnackbar
showSnackbar = showSnackbar,
searchQuery = searchQuery
) {
Navigation(
activity = this,
navController = navController,
showSnackbar = showSnackbar
showSnackbar = showSnackbar,
searchQuery = searchQuery
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fun Navigation(
activity: MainActivity? = null,
navController: NavHostController,
startDestination: String = Screen.MainScreen.route,
showSnackbar: MutableState<Boolean>
showSnackbar: MutableState<Boolean>,
searchQuery: MutableState<String>
) {
NavHost(
modifier = Modifier.fillMaxSize(),
Expand All @@ -34,7 +35,8 @@ fun Navigation(
MainScreen(
activity = activity,
navController = navController,
showSnackbar = showSnackbar
showSnackbar = showSnackbar,
searchQuery
)
}
composable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@ package com.lorenzovainigli.foodexpirationdates.view.composable
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.DrawableRes
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.util.FirebaseUtils
import com.lorenzovainigli.foodexpirationdates.util.OperationResult
Expand All @@ -30,9 +44,11 @@ data class MenuItem(
val onClick: () -> Unit = {}
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreenMenu(
activity: MainActivity? = null,
searchQuery: MutableState<String>
) {
val viewModel = activity?.viewModel
val exportTaskSuccess = viewModel?.exportTaskSuccess?.value
Expand All @@ -53,92 +69,127 @@ fun MainScreenMenu(
?: OperationResult(state = OperationResult.State.NOT_PERFORMED)
}
}
IconButton(
onClick = { isExpanded = true }
) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = stringResource(id = R.string.back),
tint = MaterialTheme.colorScheme.primary
)
}
DropdownMenu(
expanded = isExpanded,
onDismissRequest = {
isExpanded = false
}
) {
arrayOf(
MenuItem(
iconId = R.drawable.ic_export,
label = stringResource(R.string.export_data),
onClick = {
if (viewModel != null) {
viewModel.exportData(context)
} else {
FirebaseUtils.logToCrashlytics("Cannot export data, viewModel is null")
}
isExpanded = false
}
),
MenuItem(
iconId = R.drawable.ic_import,
label = stringResource(R.string.import_data),
onClick = {
if (filePickerLauncher != null){
filePickerLauncher.launch(arrayOf("*/*"))
} else {
FirebaseUtils.logToCrashlytics("Cannot import data, filePickerLauncher is null")
}


// TopAppBar(
// title = {
// BasicTextField(
// value = searchQuery,
// onValueChange = { searchQuery = it },
// textStyle = TextStyle(color = Color.White, fontSize = 18.sp),
// modifier = Modifier.fillMaxWidth()
// )
// },
// backgroundColor = MaterialTheme.colorScheme.primary
// )


var isSearchBarExpanded by remember { mutableStateOf(false) }

SearchBar(
query = searchQuery.value,
onQueryChange = { searchQuery.value = it },
onSearch = { /* Trigger search logic if needed */ },
active = isSearchBarExpanded,
onActiveChange = { isSearchBarExpanded = it },
placeholder = { Text("Search...") },
leadingIcon = {
Icon(Icons.Default.Search, contentDescription = null)
},
trailingIcon = {
IconButton(
onClick = { isExpanded = true }
) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = stringResource(id = R.string.back),
tint = MaterialTheme.colorScheme.primary
)
}
DropdownMenu(
expanded = isExpanded,
onDismissRequest = {
isExpanded = false
}
)
).forEach {
DropdownMenuItem(
leadingIcon = {
Icon(
painter = painterResource(id = it.iconId),
contentDescription = stringResource(id = R.string.back),
tint = MaterialTheme.colorScheme.primary
) {
arrayOf(
MenuItem(
iconId = R.drawable.ic_export,
label = stringResource(R.string.export_data),
onClick = {
if (viewModel != null) {
viewModel.exportData(context)
} else {
FirebaseUtils.logToCrashlytics("Cannot export data, viewModel is null")
}
isExpanded = false
}
),
MenuItem(
iconId = R.drawable.ic_import,
label = stringResource(R.string.import_data),
onClick = {
if (filePickerLauncher != null){
filePickerLauncher.launch(arrayOf("*/*"))
} else {
FirebaseUtils.logToCrashlytics("Cannot import data, filePickerLauncher is null")
}
isExpanded = false
}
)
},
text = {
Text(it.label)
},
onClick = it.onClick
)
}
}
if (notifyExportTaskDone == true) {
if (exportTaskSuccess == true) {
).forEach {
DropdownMenuItem(
leadingIcon = {
Icon(
painter = painterResource(id = it.iconId),
contentDescription = stringResource(id = R.string.back),
tint = MaterialTheme.colorScheme.primary
)
},
text = {
Text(it.label)
},
onClick = it.onClick
)
}
}
if (notifyExportTaskDone == true) {
if (exportTaskSuccess == true) {
// SuccessDialog(
// onDismiss = {
// viewModel.resetNotifyExportTaskDone()
// },
// message = stringResource(id = R.string.data_export_success)
// )
} else {
ErrorDialog(
onDismiss = {
viewModel.resetNotifyExportTaskDone()
},
message = stringResource(id = R.string.data_export_error)
)
}
}
when (operationResult.value.state){
OperationResult.State.FAILURE -> ErrorDialog(
onDismiss = {
operationResult.value = OperationResult()
},
message = operationResult.value.message
)
OperationResult.State.SUCCESS -> SuccessDialog(
onDismiss = {
operationResult.value = OperationResult()
},
message = operationResult.value.message
)
OperationResult.State.NOT_PERFORMED -> {}
} else {
ErrorDialog(
onDismiss = {
viewModel.resetNotifyExportTaskDone()
},
message = stringResource(id = R.string.data_export_error)
)
}
}
when (operationResult.value.state){
OperationResult.State.FAILURE -> ErrorDialog(
onDismiss = {
operationResult.value = OperationResult()
},
message = operationResult.value.message
)
OperationResult.State.SUCCESS -> SuccessDialog(
onDismiss = {
operationResult.value = OperationResult()
},
message = operationResult.value.message
)
OperationResult.State.NOT_PERFORMED -> {}
}
},
colors = SearchBarDefaults.colors(),
modifier = Modifier.fillMaxWidth()
) {

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lorenzovainigli.foodexpirationdates.view.composable

import android.annotation.SuppressLint
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
Expand All @@ -8,16 +9,20 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.Chat
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
Expand Down Expand Up @@ -57,13 +62,15 @@ fun MyScaffold(
navController: NavHostController,
navDestination: String? = null,
showSnackbar: MutableState<Boolean>,
content: @Composable () -> Unit
searchQuery : MutableState<String>,
content: @Composable () -> Unit,
) {
val snackbarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
val currentBackStackEntry by navController.currentBackStackEntryAsState()
val context = LocalContext.current

if (showSnackbar.value){
coroutineScope.launch {
try {
Expand Down Expand Up @@ -118,7 +125,7 @@ fun MyScaffold(
},
actions = {
if (destination?.contains(Screen.MainScreen.route) == true) {
MainScreenMenu(activity)
MainScreenMenu(activity, searchQuery)
}
},
navigationIcon = {
Expand Down Expand Up @@ -154,6 +161,7 @@ fun MyScaffold(
}
}

@SuppressLint("UnrememberedMutableState")
@RequiresApi(Build.VERSION_CODES.O)
@PreviewLightDark
@PreviewScreenSizes
Expand All @@ -167,10 +175,13 @@ fun MyScaffoldPreview() {
}
MyScaffold(
navController = navController,
showSnackbar = showSnackbar
showSnackbar = showSnackbar,
searchQuery = mutableStateOf("")
) {

MainScreen(
navController = rememberNavController()
navController = rememberNavController(),
searchQuery = mutableStateOf("")
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.lorenzovainigli.foodexpirationdates.BuildConfig
import com.google.android.datatransport.BuildConfig
import com.lorenzovainigli.foodexpirationdates.DEVELOPER_EMAIL
import com.lorenzovainigli.foodexpirationdates.GITHUB_URL
import com.lorenzovainigli.foodexpirationdates.PLAY_STORE_URL
Expand Down
Loading

0 comments on commit 0693488

Please sign in to comment.