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

move search screen to bottom navigation bar #95

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
4 changes: 3 additions & 1 deletion app/src/main/java/com/kanyideveloper/muviz/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.ramcosta.composedestinations.generated.NavGraphs
import com.ramcosta.composedestinations.generated.destinations.AccountScreenDestination
import com.ramcosta.composedestinations.generated.destinations.FavoritesScreenDestination
import com.ramcosta.composedestinations.generated.destinations.HomeScreenDestination
import com.ramcosta.composedestinations.generated.destinations.SearchScreenDestination
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.rememberNavHostEngine
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -65,7 +66,8 @@ class MainActivity : ComponentActivity() {
showBottomBar = route in listOf(
HomeScreenDestination.route,
FavoritesScreenDestination.route,
AccountScreenDestination.route
AccountScreenDestination.route,
SearchScreenDestination.route,
)
) { innerPadding ->
SharedTransitionLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.kanyideveloper.muviz.R
import com.ramcosta.composedestinations.generated.destinations.AccountScreenDestination
import com.ramcosta.composedestinations.generated.destinations.FavoritesScreenDestination
import com.ramcosta.composedestinations.generated.destinations.HomeScreenDestination
import com.ramcosta.composedestinations.generated.destinations.SearchScreenDestination

sealed class BottomNavItem(
val title: String,
Expand All @@ -30,6 +31,11 @@ sealed class BottomNavItem(
icon = R.drawable.ic_home,
route = HomeScreenDestination.route
)
data object Search: BottomNavItem(
title = "Search",
icon = R.drawable.ic_search,
route = SearchScreenDestination.route,
)
data object Favorites: BottomNavItem(
title = "Favorites",
icon = R.drawable.ic_star,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fun StandardScaffold(
showBottomBar: Boolean = true,
items: List<BottomNavItem> = listOf(
BottomNavItem.Home,
BottomNavItem.Search,
BottomNavItem.Favorites,
BottomNavItem.Account
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.kanyideveloper.muviz.common.data.network.TMDBApi
import com.kanyideveloper.muviz.common.util.Constants.PAGING_SIZE
import com.kanyideveloper.muviz.home.domain.model.Movie
import com.kanyideveloper.muviz.home.data.paging.NowPlayingMoviesSource
import com.kanyideveloper.muviz.home.data.paging.PopularMoviesSource
Expand All @@ -32,7 +33,7 @@ class MoviesRepository @Inject constructor(private val api: TMDBApi) {

fun getTrendingMoviesThisWeek(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
TrendingMoviesSource(api)
}
Expand All @@ -41,7 +42,7 @@ class MoviesRepository @Inject constructor(private val api: TMDBApi) {

fun getUpcomingMovies(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
UpcomingMoviesSource(api)
}
Expand All @@ -50,7 +51,7 @@ class MoviesRepository @Inject constructor(private val api: TMDBApi) {

fun getTopRatedMovies(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
TopRatedMoviesSource(api)
}
Expand All @@ -59,7 +60,7 @@ class MoviesRepository @Inject constructor(private val api: TMDBApi) {

fun getNowPlayingMovies(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
NowPlayingMoviesSource(api)
}
Expand All @@ -68,7 +69,7 @@ class MoviesRepository @Inject constructor(private val api: TMDBApi) {

fun getPopularMovies(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
PopularMoviesSource(api)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ fun SharedTransitionScope.HomeScreen(
navigator.navigateUp()
}

HomeUiEvents.OnSearchClick -> {
navigator.navigate(SearchScreenDestination)
}

is HomeUiEvents.NavigateToFilmDetails -> {
navigator.navigate(
FilmDetailsScreenDestination(
Expand Down Expand Up @@ -187,17 +183,6 @@ fun SharedTransitionScope.HomeScreenContent(
},
modifier = Modifier.fillMaxWidth(),
showBackArrow = false,
navActions = {
IconButton(onClick = {
onEvent(HomeUiEvents.OnSearchClick)
}) {
Icon(
painter = painterResource(id = R.drawable.ic_search),
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground.copy(.5f),
)
}
}
)
}
) { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.kanyideveloper.muviz.common.domain.model.Film
import com.kanyideveloper.muviz.genre.domain.model.Genre

sealed interface HomeUiEvents {
data object OnSearchClick : HomeUiEvents
data object NavigateBack : HomeUiEvents
data object OnPullToRefresh : HomeUiEvents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.kanyideveloper.muviz.search.data.repository

import android.nfc.tech.MifareUltralight.PAGE_SIZE
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.kanyideveloper.muviz.search.data.paging.SearchPagingSource
import com.kanyideveloper.muviz.common.data.network.TMDBApi
import com.kanyideveloper.muviz.common.util.Constants.PAGING_SIZE
import com.kanyideveloper.muviz.search.domain.model.Search
import com.kanyideveloper.muviz.search.domain.repository.SearchRepository
import kotlinx.coroutines.flow.Flow
Expand All @@ -29,7 +29,7 @@ import javax.inject.Inject
class SearchRepositoryImpl @Inject constructor(private val api: TMDBApi): SearchRepository {
override fun multiSearch(queryParam: String): Flow<PagingData<Search>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = PAGE_SIZE),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
SearchPagingSource(api, queryParam)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
Expand Down Expand Up @@ -106,10 +106,6 @@ fun SearchScreen(
state = searchUiState,
onEvent = { event ->
when (event) {
is SearchUiEvents.NavigateBack -> {
navigator.popBackStack()
}

is SearchUiEvents.SearchFilm -> {
viewModel.searchAll(event.searchTerm)
keyboardController?.hide()
Expand All @@ -131,6 +127,10 @@ fun SearchScreen(
)
}
}

SearchUiEvents.ClearSearchTerm -> {
viewModel.clearSearch()
}
}
}
)
Expand All @@ -146,17 +146,13 @@ fun SearchScreenContent(
Scaffold(
topBar = {
StandardToolbar(
onBackArrowClicked = {
onEvent(SearchUiEvents.NavigateBack)
},
title = {
Text(
text = stringResource(R.string.search_title),
style = MaterialTheme.typography.titleLarge,
)
},
modifier = Modifier.fillMaxWidth(),
showBackArrow = true
)
}
) { innerPadding ->
Expand Down Expand Up @@ -197,7 +193,7 @@ fun SearchScreenContent(

searchResult.loadState.let { loadState ->
when {
loadState.refresh is LoadState.Loading -> {
loadState.refresh is LoadState.Loading && state.searchTerm.isNotEmpty() -> {
item {
Column(
modifier = Modifier
Expand Down Expand Up @@ -330,14 +326,16 @@ fun SearchBar(
maxLines = 1,
singleLine = true,
trailingIcon = {
IconButton(onClick = {
onEvent(SearchUiEvents.SearchFilm(searchTerm = state.searchTerm))
}) {
Icon(
imageVector = Icons.Default.Search,
tint = MaterialTheme.colorScheme.onBackground.copy(.5f),
contentDescription = null
)
if (state.searchTerm.isNotEmpty()) {
IconButton(onClick = {
onEvent(SearchUiEvents.ClearSearchTerm)
}) {
Icon(
imageVector = Icons.Default.Close,
tint = MaterialTheme.colorScheme.onBackground.copy(.5f),
contentDescription = null
)
}
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ sealed interface SearchUiEvents {

data class OpenFilmDetails(val search: Search?) :
SearchUiEvents
data object NavigateBack : SearchUiEvents
data object ClearSearchTerm: SearchUiEvents
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -122,4 +123,13 @@ class SearchViewModel @Inject constructor(
}
}
}

fun clearSearch() {
_searchUiState.update {
it.copy(
searchResult = emptyFlow(),
searchTerm = ""
)
}
}
}
Loading