Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Material3 SearchBar feature in FoodExpirationDates App #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class MainActivity : ComponentActivity() {
darkScrim = android.graphics.Color.TRANSPARENT,
detectDarkMode = { _ -> isInDarkTheme }
)
val searchQuery = remember { mutableStateOf("") }

enableEdgeToEdge(
statusBarStyle = systemBarStyle,
navigationBarStyle = systemBarStyle
Expand All @@ -90,12 +92,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
// )

Comment on lines +72 to +85
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to introduce commented code.


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>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever possible, if you introduce a new parameter, add a default. This prevent you from having to change all calls of the function. Think about the situation in which a function is called 100 times.

Suggested change
searchQuery : MutableState<String>,
searchQuery : MutableState<String> = mutableStateOf(""),

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("")
Comment on lines +178 to +179
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change no longer necessary after introducing a default.

Suggested change
showSnackbar = showSnackbar,
searchQuery = mutableStateOf("")
showSnackbar = showSnackbar

) {

MainScreen(
navController = rememberNavController()
navController = rememberNavController(),
searchQuery = mutableStateOf("")
Comment on lines +183 to +184
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change no longer necessary after introducing a default.

Suggested change
navController = rememberNavController(),
searchQuery = mutableStateOf("")
navController = rememberNavController()

)
}
}
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong, please revert this.

import com.lorenzovainigli.foodexpirationdates.DEVELOPER_EMAIL
import com.lorenzovainigli.foodexpirationdates.GITHUB_URL
import com.lorenzovainigli.foodexpirationdates.PLAY_STORE_URL
Expand Down
Loading
Loading