Skip to content

Commit

Permalink
Fix code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPushkin committed Oct 27, 2023
1 parent 0739080 commit df7d01e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 57 deletions.
149 changes: 92 additions & 57 deletions app/src/main/java/ru/spbu/depnav/ui/component/MapSearchBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ private val ACTIVATION_EXIT_SPEC = tween<Float>(
easing = CubicBezierEasing(0.0f, 1.0f, 0.0f, 1.0f)
)

/**
* Search bar for querying map markers on [ru.spbu.depnav.ui.screen.MapScreen].
*/
@Composable
@Suppress(
"LongMethod", // No point in further shrinking
"LongParameterList" // Considered OK for composables
)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
fun MapSearchBar(
query: String,
Expand Down Expand Up @@ -116,66 +123,19 @@ fun MapSearchBar(
.then(modifier),
placeholder = { Text(stringResource(R.string.search_markers), maxLines = 1) },
leadingIcon = {
AnimatedContent(
active,
modifier = Modifier.padding(start = innerStartPadding),
label = "Map search bar leading icon change"
) { active ->
if (active) {
IconButton(onClick = { onActiveChange(false) }) {
Icon(
Icons.Rounded.ArrowBack,
contentDescription = stringResource(R.string.label_navigate_back)
)
}
} else {
Icon(
Icons.Rounded.Search,
contentDescription = null,
// To have the same size as the back button above
modifier = Modifier.minimumInteractiveComponentSize()
)
}
AnimatedLeadingIcon(active, modifier = Modifier.padding(start = innerStartPadding)) {
onActiveChange(false)
}
},
trailingIcon = {
AnimatedContent(
active to query.isEmpty(),
modifier = Modifier.padding(end = innerEndPadding),
transitionSpec = { fadeIn() togetherWith fadeOut() },
label = "Map search bar trailing icon change"
) { (active, emptyQuery) ->
if (active) {
if (emptyQuery) {
Spacer(modifier = Modifier.minimumInteractiveComponentSize())
} else {
IconButton(
onClick = { onQueryChange("") }
) {
Icon(
Icons.Rounded.Clear,
contentDescription = stringResource(R.string.label_clear_text_field)
)
}
}
} else {
Row {
IconButton(onClick = onInfoClick) {
Icon(
Icons.Rounded.Info,
contentDescription = stringResource(R.string.label_open_map_info)
)
}

IconButton(onClick = onSettingsClick) {
Icon(
Icons.Rounded.Settings,
contentDescription = stringResource(R.string.label_open_settings)
)
}
}
}
}
AnimatedTrailingIcons(
active,
query.isEmpty(),
onClearClick = { onQueryChange("") },
onInfoClick = onInfoClick,
onSettingsClick = onSettingsClick,
modifier = Modifier.padding(end = innerEndPadding)
)
}
) {
val keyboard = LocalSoftwareKeyboardController.current
Expand Down Expand Up @@ -203,3 +163,78 @@ fun MapSearchBar(
)
}
}

@Composable
private fun AnimatedLeadingIcon(
searchBarActive: Boolean,
modifier: Modifier = Modifier,
onNavigateBackClick: () -> Unit
) {
AnimatedContent(
searchBarActive,
modifier = modifier,
label = "Map search bar leading icon change"
) { showBackButton ->
if (showBackButton) {
IconButton(onClick = onNavigateBackClick) {
Icon(
Icons.Rounded.ArrowBack,
contentDescription = stringResource(R.string.label_navigate_back)
)
}
} else {
Icon(
Icons.Rounded.Search,
contentDescription = null,
// To have the same size as the back button above
modifier = Modifier.minimumInteractiveComponentSize()
)
}
}
}

@Composable
private fun AnimatedTrailingIcons(
searchBarActive: Boolean,
queryEmpty: Boolean,
onClearClick: () -> Unit,
onInfoClick: () -> Unit,
onSettingsClick: () -> Unit,
modifier: Modifier = Modifier
) {
AnimatedContent(
searchBarActive to queryEmpty,
modifier = modifier,
transitionSpec = { fadeIn() togetherWith fadeOut() },
label = "Map search bar trailing icon change"
) { (active, emptyQuery) ->
if (active) {
if (emptyQuery) {
Spacer(modifier = Modifier.minimumInteractiveComponentSize())
} else {
IconButton(onClick = onClearClick) {
Icon(
Icons.Rounded.Clear,
contentDescription = stringResource(R.string.label_clear_text_field)
)
}
}
} else {
Row {
IconButton(onClick = onInfoClick) {
Icon(
Icons.Rounded.Info,
contentDescription = stringResource(R.string.label_open_map_info)
)
}

IconButton(onClick = onSettingsClick) {
Icon(
Icons.Rounded.Settings,
contentDescription = stringResource(R.string.label_open_settings)
)
}
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/java/ru/spbu/depnav/ui/screen/MapScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fun MapScreen(vm: MapViewModel = viewModel()) {
}

@Composable
@Suppress("LongParameterList") // Considered OK for composables
private fun BoxScope.AnimatedSearchBar(
visible: Boolean,
query: String,
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/ru/spbu/depnav/ui/viewmodel/MapViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,12 @@ class MapViewModel @Inject constructor(
}
}

/** Describes states of map UI. */
sealed interface MapUiState {
/** Map has not yet been loaded. */
data object Loading : MapUiState

/** Map has been loaded. */
data class Ready(
/** State of the map. */
val mapState: MapState,
Expand All @@ -405,7 +408,16 @@ sealed interface MapUiState {
) : MapUiState
}

/** Map markers search UI state. */
data class SearchUiState(
/** Marker search query entered by the user. */
val query: String = "",
/**
* Either the actual results of the query or a history of previous searches if the query was
* empty.
*
* Note that these result mey correspond not to the current query, but to some query in the
* past.
* */
val results: Map<Marker, MarkerText> = emptyMap()
)

0 comments on commit df7d01e

Please sign in to comment.