Napa Ram
09/26/2022, 8:09 AMjossiwolf
09/26/2022, 8:50 AMChris Fillmore
09/26/2022, 2:43 PMNapa Ram
09/26/2022, 3:56 PMChris Fillmore
09/26/2022, 3:59 PMChris Fillmore
09/26/2022, 4:00 PMOleksandr Balan
09/26/2022, 4:34 PMStylianos Gakis
09/26/2022, 6:05 PMNapa Ram
09/27/2022, 5:05 AM@Composable
@ExperimentalMaterialApi
fun ModelBottomSheet() {
val modalBottomSheetState = rememberModalBottomSheetState(
initialValue =ModalBottomSheetValue.Hidden
)
val scope = rememberCoroutineScope()
val viewModel: MyViewModel = viewModel()
val isRefreshing by viewModel.isRefreshing.collectAsState()
ModalBottomSheetLayout(
sheetState = modalBottomSheetState,
sheetContent = {
Text(
text = "Modal Bottom Sheet", fontSize = 20.sp, modifier = Modifier.padding(10.dp),
color = Color.Black, fontWeight = FontWeight.ExtraBold
)
Divider(modifier = Modifier.padding(5.dp), color = Color.Gray)
SwipeRefresh(state = rememberSwipeRefreshState(isRefreshing = isRefreshing),
onRefresh = { viewModel.refresh() }) {
LazyColumn() {
items(50) {
ListItem(
text = { Text("Item $it") },
icon = {
Icon(
Icons.Default.ThumbUp,
contentDescription = "Localized description"
)
}
)
}
}
}
}
) {
ModalBottomSheetMainscreenView(scope, modalBottomSheetState)
}
}
@ExperimentalMaterialApi
@Composable
fun ModalBottomSheetMainscreenView(
scope: CoroutineScope,
modalBottomSheetState: ModalBottomSheetState
) {
Box(
Modifier
.fillMaxWidth()
){
Button(
modifier = Modifier
.padding(20.dp)
.align(alignment = Alignment.TopCenter),
onClick = {
scope.launch{
modalBottomSheetState.show()
}
}
) {
Text(
text = "Click to show Model Bottom Sheet"
)
}
}
}
class MyViewModel : ViewModel() {
private val _isRefreshing = MutableStateFlow(false)
val isRefreshing: StateFlow<Boolean>
get() = _isRefreshing.asStateFlow()
fun refresh() {
// This doesn't handle multiple 'refreshing' tasks, don't use this
viewModelScope.launch {
// A fake 2 second 'refresh'
_isRefreshing.emit(true)
delay(2000)
_isRefreshing.emit(false)
}
}
}