From 9c2d4028895e7e70ef39b0066094adbc50f341ae Mon Sep 17 00:00:00 2001 From: TimPushkin Date: Thu, 7 Dec 2023 17:44:25 +0300 Subject: [PATCH] Fix code style issues --- .../depnav/data/db/MarketWithTextDaoTest.kt | 2 +- .../depnav/data/composite/MarkerWithText.kt | 2 +- .../spbu/depnav/ui/component/MapSearchBar.kt | 2 +- .../spbu/depnav/ui/component/SearchResults.kt | 2 +- .../ru/spbu/depnav/ui/screen/MapScreen.kt | 95 ++++++++++++------- .../spbu/depnav/ui/viewmodel/MapViewModel.kt | 30 ++---- 6 files changed, 72 insertions(+), 61 deletions(-) diff --git a/app/src/androidTest/java/ru/spbu/depnav/data/db/MarketWithTextDaoTest.kt b/app/src/androidTest/java/ru/spbu/depnav/data/db/MarketWithTextDaoTest.kt index 7e4e3cd7..6cc4d9ca 100644 --- a/app/src/androidTest/java/ru/spbu/depnav/data/db/MarketWithTextDaoTest.kt +++ b/app/src/androidTest/java/ru/spbu/depnav/data/db/MarketWithTextDaoTest.kt @@ -31,7 +31,7 @@ import ru.spbu.depnav.data.model.MarkerText private val INSERTED_MAP = MapInfo(123, "test-map", 100, 100, 128, 5, 3) /** Instrumentation tests for [MarkerWithTextDao]. */ -@Suppress("TooManyFunctions") // OK for a test class +@Suppress("TooManyFunctions", "StringLiteralDuplication") // OK for a test class class MarketWithTextDaoTest : AppDatabaseDaoTest() { private lateinit var markerWithTextDao: MarkerWithTextDao diff --git a/app/src/main/java/ru/spbu/depnav/data/composite/MarkerWithText.kt b/app/src/main/java/ru/spbu/depnav/data/composite/MarkerWithText.kt index 87202bf7..3b75d17c 100644 --- a/app/src/main/java/ru/spbu/depnav/data/composite/MarkerWithText.kt +++ b/app/src/main/java/ru/spbu/depnav/data/composite/MarkerWithText.kt @@ -39,6 +39,6 @@ data class MarkerWithText(val marker: Marker, val text: MarkerText) { } companion object { - val ID_DIVIDER = ':' + const val ID_DIVIDER = ':' } } diff --git a/app/src/main/java/ru/spbu/depnav/ui/component/MapSearchBar.kt b/app/src/main/java/ru/spbu/depnav/ui/component/MapSearchBar.kt index 40cae555..5f1d1845 100644 --- a/app/src/main/java/ru/spbu/depnav/ui/component/MapSearchBar.kt +++ b/app/src/main/java/ru/spbu/depnav/ui/component/MapSearchBar.kt @@ -76,7 +76,7 @@ private val ACTIVATION_EXIT_SPEC = tween( @Composable @Suppress( "LongMethod", // No point in further shrinking - "LongParameterList" // Considered OK for composables + "LongParameterList" // Considered OK for a composable ) @OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class) fun MapSearchBar( diff --git a/app/src/main/java/ru/spbu/depnav/ui/component/SearchResults.kt b/app/src/main/java/ru/spbu/depnav/ui/component/SearchResults.kt index ff2ac558..3d4a0529 100644 --- a/app/src/main/java/ru/spbu/depnav/ui/component/SearchResults.kt +++ b/app/src/main/java/ru/spbu/depnav/ui/component/SearchResults.kt @@ -213,7 +213,7 @@ private fun SearchResultLongContentsPreview() { Language.EN, "Some very very very very very long title", "Some very very very very very very long location", - "Some description" + "Some very very very very very very long description" ), onClick = {} ) {} diff --git a/app/src/main/java/ru/spbu/depnav/ui/screen/MapScreen.kt b/app/src/main/java/ru/spbu/depnav/ui/screen/MapScreen.kt index 34cab300..d086849a 100644 --- a/app/src/main/java/ru/spbu/depnav/ui/screen/MapScreen.kt +++ b/app/src/main/java/ru/spbu/depnav/ui/screen/MapScreen.kt @@ -86,6 +86,7 @@ import ru.spbu.depnav.ui.theme.DEFAULT_PADDING import ru.spbu.depnav.ui.viewmodel.MapUiState import ru.spbu.depnav.ui.viewmodel.MapViewModel import ru.spbu.depnav.ui.viewmodel.SearchResults +import ru.spbu.depnav.ui.viewmodel.SearchUiState import ru.spbu.depnav.ui.viewmodel.SearchViewModel /** Screen containing a navigable map. */ @@ -134,54 +135,76 @@ fun MapScreen( val readyMapUiState = mapUiState as? MapUiState.Ready ?: return@ModalNavigationDrawer val searchUiState by searchVm.uiState.collectAsStateWithLifecycle() + val markerAlpha by mapVm.markerAlpha.collectAsStateWithLifecycle() + val markersVisible by remember { derivedStateOf { markerAlpha > 0f } } + val mapColor = MaterialTheme.colorScheme.outline LaunchedEffect(mapColor) { mapVm.setMapColor(mapColor) } MapUI(state = readyMapUiState.mapState) - Box(modifier = Modifier.fillMaxSize()) { - CompositionLocalProvider(LocalAbsoluteTonalElevation provides 4.dp) { - AnimatedSearchBar( - visible = readyMapUiState.showOnMapUi, - mapTitle = readyMapUiState.mapTitle, - query = searchUiState.query, - onQueryChange = searchVm::search, - searchResults = searchUiState.results, - onResultClick = { markerId -> - mapVm.focusOnMarker(markerId) - searchVm.addToSearchHistory(markerId) - }, - onMenuCLick = { scope.launch { drawerState.open() } } - ) - - AnimatedFloorSwitch( - visible = readyMapUiState.showOnMapUi, - currentFloor = readyMapUiState.currentFloor, - maxFloor = readyMapUiState.floorsNum, - onFloorSwitch = mapVm::setFloor - ) - - val markerAlpha by mapVm.markerAlpha.collectAsStateWithLifecycle() - val markersVisible by remember { derivedStateOf { markerAlpha > 0f } } - - AnimatedBottom( - pinnedMarker = readyMapUiState.pinnedMarker, - showZoomInHint = !markersVisible - ) - } - } + OnMapUi( + mapUiState = readyMapUiState, + searchUiState = searchUiState, + onSearchQueryChange = searchVm::search, + onSearchResultClick = { markerId -> + mapVm.focusOnMarker(markerId) + searchVm.addToSearchHistory(markerId) + }, + onMainMenuClick = { scope.launch { drawerState.open() } }, + onFloorSwitch = mapVm::setFloor, + markersVisible = markersVisible + ) } } - val locale = Locale.current // This one is not a State - LaunchedEffect(key1 = ConfigurationCompat.getLocales(LocalConfiguration.current)[0]) { + LaunchedEffect(ConfigurationCompat.getLocales(LocalConfiguration.current)[0]) { + val locale = Locale.current // This is not a State searchVm.onLocaleChange(locale) mapVm.onLocaleChange(locale) } } @Composable -@Suppress("LongParameterList") // Considered OK for composables +@Suppress("LongParameterList") // Considered OK for a composable +private fun OnMapUi( + mapUiState: MapUiState.Ready, + searchUiState: SearchUiState, + onSearchQueryChange: (String) -> Unit, + onSearchResultClick: (Int) -> Unit, + onMainMenuClick: () -> Unit, + onFloorSwitch: (Int) -> Unit, + markersVisible: Boolean +) { + CompositionLocalProvider(LocalAbsoluteTonalElevation provides 4.dp) { + Box(modifier = Modifier.fillMaxSize()) { + AnimatedSearchBar( + visible = mapUiState.showOnMapUi, + mapTitle = mapUiState.mapTitle, + query = searchUiState.query, + onQueryChange = onSearchQueryChange, + searchResults = searchUiState.results, + onResultClick = onSearchResultClick, + onMenuClick = onMainMenuClick + ) + + AnimatedFloorSwitch( + visible = mapUiState.showOnMapUi, + currentFloor = mapUiState.currentFloor, + maxFloor = mapUiState.floorsNum, + onFloorSwitch = onFloorSwitch + ) + + AnimatedBottom( + pinnedMarker = mapUiState.pinnedMarker, + showZoomInHint = !markersVisible + ) + } + } +} + +@Composable +@Suppress("LongParameterList") // Considered OK for a composable private fun BoxScope.AnimatedSearchBar( visible: Boolean, mapTitle: String, @@ -189,7 +212,7 @@ private fun BoxScope.AnimatedSearchBar( onQueryChange: (String) -> Unit, searchResults: SearchResults, onResultClick: (Int) -> Unit, - onMenuCLick: () -> Unit + onMenuClick: () -> Unit ) { var searchBarActive by rememberSaveable { mutableStateOf(false) } if (!visible) { @@ -221,7 +244,7 @@ private fun BoxScope.AnimatedSearchBar( onActiveChange = { searchBarActive = it }, results = searchResults, onResultClick = onResultClick, - onMenuClick = onMenuCLick, + onMenuClick = onMenuClick, modifier = Modifier.padding(horizontal = horizontalPadding) ) } diff --git a/app/src/main/java/ru/spbu/depnav/ui/viewmodel/MapViewModel.kt b/app/src/main/java/ru/spbu/depnav/ui/viewmodel/MapViewModel.kt index 9a54ca04..1e24b4c6 100644 --- a/app/src/main/java/ru/spbu/depnav/ui/viewmodel/MapViewModel.kt +++ b/app/src/main/java/ru/spbu/depnav/ui/viewmodel/MapViewModel.kt @@ -211,15 +211,12 @@ class MapViewModel @Inject constructor( onTap { _, _ -> state.update { - it.copy( - showOnMapUi = if (it.pinnedMarker != null) { - removeMarker(PIN_ID) - it.showOnMapUi - } else { - !it.showOnMapUi - }, - pinnedMarker = null - ) + if (it.pinnedMarker != null) { + removeMarker(PIN_ID) + it.copy(pinnedMarker = null) + } else { + it.copy(showOnMapUi = !it.showOnMapUi) + } } } @@ -238,12 +235,7 @@ class MapViewModel @Inject constructor( addLayer(layer) } for (markerWithText in firstFloor.markers.values) { - addMarker( - markerWithText.extendedId, - markerWithText.marker, - markerWithText.text, - markerAlpha - ) + with(markerWithText) { addMarker(extendedId, marker, text, markerAlpha) } } markerAlphaUpdater.cancel("New map state arrived") @@ -323,12 +315,7 @@ class MapViewModel @Inject constructor( } } for (markerWithText in floor.markers.values) { - addMarker( - markerWithText.extendedId, - markerWithText.marker, - markerWithText.text, - markerAlpha - ) + with(markerWithText) { addMarker(extendedId, marker, text, markerAlpha) } } } state.update { @@ -444,6 +431,7 @@ class MapViewModel @Inject constructor( /** Describes states of map UI. */ sealed interface MapUiState { + /** State that has a list of available maps. */ sealed interface WithAvailableMaps : MapUiState { /** Maps available in the app. */ val availableMaps: List