Skip to content

Commit

Permalink
feat: make search bar scrollable
Browse files Browse the repository at this point in the history
  • Loading branch information
Divinelink committed Aug 5, 2023
1 parent 86a43fc commit 1b190fb
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.andreolas.movierama.components

import androidx.activity.ComponentActivity
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import com.andreolas.movierama.ui.components.MovieRamaSearchBar
import com.andreolas.movierama.ui.components.SEARCH_BAR_LOADING_INDICATOR_TAG
import com.andreolas.movierama.ui.components.SearchBar
import com.andreolas.movierama.ui.components.ToolbarState
import org.junit.Rule
import org.junit.Test
Expand All @@ -15,11 +16,12 @@ class SearchBarTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@OptIn(ExperimentalMaterial3Api::class)
@Test
fun searchBarLoadingTest() {
composeTestRule.setContent {
SearchBar(
searchValue = null,
MovieRamaSearchBar(
query = null,
onClearClicked = {},
onSearchFieldChanged = {},
actions = {},
Expand Down
109 changes: 54 additions & 55 deletions app/src/main/java/com/andreolas/movierama/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import com.andreolas.movierama.destinations.HomeScreenDestination
import com.andreolas.movierama.home.ui.HomeScreen
import com.andreolas.movierama.home.ui.LoadingContent
Expand All @@ -31,65 +30,65 @@ import gr.divinelink.core.util.utils.setNavigationBarColor
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private val viewModel: MainViewModel by viewModels()
private val viewModel: MainViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setNavigationBarColor(ContextCompat.getColor(this, R.color.colorBackground))
// Update for Dark Mode straight away
updateForTheme(viewModel.currentTheme)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// WindowCompat.setDecorFitsSystemWindows(window, false)
setNavigationBarColor(ContextCompat.getColor(this, R.color.colorBackground))
// Update for Dark Mode straight away
updateForTheme(viewModel.currentTheme)

setContent {
AppTheme {
Surface(
color = MaterialTheme.colorScheme.background,
) {
val mainState = viewModel.viewState.collectAsState()
setContent {
AppTheme {
Surface(
color = MaterialTheme.colorScheme.background,
) {
val mainState = viewModel.viewState.collectAsState()

when (mainState.value) {
MainViewState.Completed -> {
AppNavHost(
startRoute = HomeScreenDestination,
)
}
MainViewState.Loading -> {
LoadingContent()
}
is MainViewState.Error -> {
viewModel.retryFetchRemoteConfig()
}
}
}
when (mainState.value) {
MainViewState.Completed -> {
AppNavHost(
startRoute = HomeScreenDestination,
)
}
MainViewState.Loading -> {
LoadingContent()
}
is MainViewState.Error -> {
viewModel.retryFetchRemoteConfig()
}
}
}
}
}
}

@OptIn(ExperimentalMaterialNavigationApi::class)
@Composable
private fun AppNavHost(
startRoute: Route,
) {
DestinationsNavHost(
startRoute = startRoute,
navGraph = NavGraphs.root,
engine = rememberAnimatedNavHostEngine(
rootDefaultAnimations = RootNavGraphDefaultAnimations(
enterTransition = {
slideInHorizontally()
},
exitTransition = {
fadeOut()
},
),
),
manualComposableCallsBuilder = {
composable(HomeScreenDestination) {
HomeScreen(
navigator = destinationsNavigator,
)
}
}
)
}
@OptIn(ExperimentalMaterialNavigationApi::class)
@Composable
private fun AppNavHost(
startRoute: Route,
) {
DestinationsNavHost(
startRoute = startRoute,
navGraph = NavGraphs.root,
engine = rememberAnimatedNavHostEngine(
rootDefaultAnimations = RootNavGraphDefaultAnimations(
enterTransition = {
slideInHorizontally()
},
exitTransition = {
fadeOut()
},
),
),
manualComposableCallsBuilder = {
composable(HomeScreenDestination) {
HomeScreen(
navigator = destinationsNavigator,
)
}
}
)
}
}
19 changes: 11 additions & 8 deletions app/src/main/java/com/andreolas/movierama/home/ui/HomeContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import com.andreolas.movierama.ui.components.BottomSheetMovieContent
import com.andreolas.movierama.ui.components.EmptySectionCard
import com.andreolas.movierama.ui.components.FilterBar
import com.andreolas.movierama.ui.components.Material3CircularProgressIndicator
import com.andreolas.movierama.ui.components.SearchBar
import com.andreolas.movierama.ui.components.MovieRamaSearchBar
import com.andreolas.movierama.ui.components.bottomsheet.BottomSheetUiState
import com.andreolas.movierama.ui.components.bottomsheet.animateBottomSheet
import com.andreolas.movierama.ui.composables.transitionspec.fadeTransitionSpec
Expand Down Expand Up @@ -85,7 +85,7 @@ fun HomeContent(
onClearFiltersClicked: () -> Unit,
onSwipeDown: () -> Unit,
) {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
val keyboardController = LocalSoftwareKeyboardController.current
val scope = rememberCoroutineScope()

Expand Down Expand Up @@ -133,11 +133,14 @@ fun HomeContent(
}
},
modifier = Modifier
.navigationBarsPadding()
.nestedScroll(scrollBehavior.nestedScrollConnection),
.nestedScroll(scrollBehavior.nestedScrollConnection)
.navigationBarsPadding(),
topBar = {
SearchBar(
modifier = Modifier.clip(SearchBarShape),
MovieRamaSearchBar(
scrollBehavior = scrollBehavior,
modifier = Modifier
.navigationBarsPadding()
.clip(SearchBarShape),
actions = {
IconButton(
onClick = {
Expand All @@ -153,7 +156,7 @@ fun HomeContent(
}
},
isLoading = viewState.searchLoading,
searchValue = viewState.query,
query = viewState.query,
onSearchFieldChanged = { query ->
onSearchMovies(query)
},
Expand All @@ -166,7 +169,7 @@ fun HomeContent(
visible = viewState.query.isEmpty(),
) {
FilterBar(
modifier = modifier.padding(start = 8.dp, end = 8.dp, bottom = 8.dp),
modifier = modifier.padding(horizontal = 8.dp, vertical = 4.dp),
filters = viewState.filters,
onFilterClick = { homeFilter ->
onFilterClicked(homeFilter.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.res.Configuration
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -48,9 +49,12 @@ fun PopularMoviesList(

LazyVerticalGrid(
state = scrollState,
modifier = modifier
.fillMaxSize()
.padding(start = 8.dp, end = 8.dp),
modifier = modifier.fillMaxSize(),
contentPadding = PaddingValues(
start = 8.dp,
end = 8.dp,
top = 8.dp
),
columns = GridCells.Adaptive(120.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
Expand Down
Loading

0 comments on commit 1b190fb

Please sign in to comment.