Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Moved logic out of the Searchbar composable, code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DotsTeaLab committed Nov 14, 2022
1 parent 0e403a0 commit fd3f8c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,21 @@ import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.IntOffset
import bruhcollective.itaysonlab.jetispot.ui.screens.search.SearchViewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.PagerState
import kotlinx.coroutines.launch


@OptIn(ExperimentalMaterial3Api::class, ExperimentalPagerApi::class)
@Composable
fun Searchbar(modifier: Modifier = Modifier, viewModel: SearchViewModel, virtualPagerState: PagerState) {
val focusManager = LocalFocusManager.current

val clearFunc: () -> Unit = {
viewModel.launch {
focusManager.clearFocus()
virtualPagerState.scrollToPage(0)
viewModel.clear()
}
}

fun Searchbar(
modifier: Modifier = Modifier,
viewModel: SearchViewModel, virtualPagerState: PagerState,
clearFunc: () -> Unit,
enterFunc: () -> Unit
) {
TextField(
value = viewModel.searchQuery,
onValueChange = { viewModel.searchQuery = it },
Expand All @@ -45,9 +38,7 @@ fun Searchbar(modifier: Modifier = Modifier, viewModel: SearchViewModel, virtual
Text(text = "What would you like to listen?")
}
},
leadingIcon = {
Icon(imageVector = Icons.Rounded.Search, contentDescription = null)
},
leadingIcon = { Icon(imageVector = Icons.Rounded.Search, contentDescription = null) },
trailingIcon = {
AnimatedVisibility(
visible = viewModel.searchQuery.text.isNotEmpty(),
Expand All @@ -69,14 +60,6 @@ fun Searchbar(modifier: Modifier = Modifier, viewModel: SearchViewModel, virtual
),
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
keyboardActions = KeyboardActions(
onSearch = {
viewModel.launch {
focusManager.clearFocus()
virtualPagerState.scrollToPage(1)
viewModel.initiateSearch()
}
}
)
keyboardActions = KeyboardActions(onSearch = { enterFunc() })
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import bruhcollective.itaysonlab.jetispot.core.api.SpInternalApi
Expand All @@ -23,53 +24,66 @@ import bruhcollective.itaysonlab.jetispot.ui.blocks.TwoColumnAndImageBlock
import bruhcollective.itaysonlab.jetispot.ui.hub.components.Searchbar
import bruhcollective.itaysonlab.jetispot.ui.navigation.LocalNavigationController
import bruhcollective.itaysonlab.jetispot.ui.screens.hub.HubScreen
import bruhcollective.itaysonlab.jetispot.ui.shared.EmptyWindowInsets
import bruhcollective.itaysonlab.jetispot.ui.shared.PagingInfoPage
import bruhcollective.itaysonlab.jetispot.ui.shared.PagingLoadingPage
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.VerticalPager
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch

@OptIn(
ExperimentalMaterial3Api::class, ExperimentalPagerApi::class, ExperimentalFoundationApi::class
)
@Composable
fun SearchScreen(
viewModel: SearchViewModel = hiltViewModel()
) {
fun SearchScreen(viewModel: SearchViewModel = hiltViewModel()) {
val navController = LocalNavigationController.current
val virtualPagerState = rememberPagerState()
val virtualPagerState = rememberPagerState(0)
val focusManager = LocalFocusManager.current

fun clearFunc() {
viewModel.launch {
focusManager.clearFocus()
virtualPagerState.animateScrollToPage(0)
viewModel.clear()
}
}

fun enterFunc() {
viewModel.launch {
focusManager.clearFocus()
virtualPagerState.animateScrollToPage(1)
viewModel.initiateSearch()
}
}

Scaffold(
topBar = {
Searchbar(
Modifier
.statusBarsPadding()
.padding(16.dp)
.fillMaxWidth()
.focusTarget(),
Modifier.statusBarsPadding().padding(16.dp).fillMaxWidth().focusTarget(),
viewModel,
virtualPagerState
virtualPagerState,
{ clearFunc() },
{ enterFunc() }
)
},
contentWindowInsets = EmptyWindowInsets
) { padding ->
HorizontalPager(
VerticalPager(
count = 2,
state = virtualPagerState,
userScrollEnabled = false,
modifier = Modifier
.fillMaxSize()
.padding(padding)
modifier = Modifier.fillMaxSize().padding(padding)
) { idx ->
when (idx) {
0 -> HubScreen(loader = SpInternalApi::getBrowseView)
1 -> SearchBinder(viewModel.searchResponse, onClick = { type, uri ->
when (type) {
SearchEntity.EntityCase.TRACK -> viewModel.dispatchPlay(uri)
else -> navController.navigate(uri)
1 -> SearchBinder(
viewModel.searchResponse,
onClick = { type, uri ->
when (type) {
SearchEntity.EntityCase.TRACK -> viewModel.dispatchPlay(uri)
else -> navController.navigate(uri)
}
}
})
)
}
}
}
Expand Down

0 comments on commit fd3f8c9

Please sign in to comment.