scrolling to top when select directory icon again
This commit is contained in:
parent
352501fb9b
commit
dccf8cc206
|
@ -53,16 +53,22 @@ import kotlin.math.roundToInt
|
|||
@Composable
|
||||
fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
|
||||
val state = directoryViewModel.state
|
||||
directoryViewModel.ObserveEvents()
|
||||
LifecycleEffect(
|
||||
onStart = { directoryViewModel.start() },
|
||||
onStop = { directoryViewModel.stop() }
|
||||
|
||||
val listState: LazyListState = rememberLazyListState(
|
||||
initialFirstVisibleItemIndex = 0,
|
||||
)
|
||||
|
||||
val toolbarHeight = 72.dp
|
||||
val toolbarHeightPx = with(LocalDensity.current) { toolbarHeight.roundToPx().toFloat() }
|
||||
val toolbarOffsetHeightPx = remember { mutableStateOf(0f) }
|
||||
|
||||
directoryViewModel.ObserveEvents(listState, toolbarOffsetHeightPx)
|
||||
|
||||
LifecycleEffect(
|
||||
onStart = { directoryViewModel.start() },
|
||||
onStop = { directoryViewModel.stop() }
|
||||
)
|
||||
|
||||
val nestedScrollConnection = remember {
|
||||
object : NestedScrollConnection {
|
||||
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
||||
|
@ -80,7 +86,7 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
|
|||
) {
|
||||
when (state) {
|
||||
is Content -> {
|
||||
Content(state)
|
||||
Content(listState, state)
|
||||
}
|
||||
EmptyLoading -> CenteredLoading()
|
||||
is Error -> {
|
||||
|
@ -99,7 +105,7 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
|
|||
}
|
||||
|
||||
@Composable
|
||||
private fun DirectoryViewModel.ObserveEvents() {
|
||||
private fun DirectoryViewModel.ObserveEvents(listState: LazyListState, toolbarPosition: MutableState<Float>) {
|
||||
val context = LocalContext.current
|
||||
StartObserving {
|
||||
this@ObserveEvents.events.launch {
|
||||
|
@ -107,21 +113,22 @@ private fun DirectoryViewModel.ObserveEvents() {
|
|||
is OpenDownloadUrl -> {
|
||||
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it.url)))
|
||||
}
|
||||
DirectoryEvent.ScrollToTop -> {
|
||||
toolbarPosition.value = 0f
|
||||
listState.scrollToItem(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Content(state: Content) {
|
||||
private fun Content(listState: LazyListState, state: Content) {
|
||||
val context = LocalContext.current
|
||||
val navigateToRoom = { roomId: RoomId ->
|
||||
context.startActivity(MessengerActivity.newInstance(context, roomId))
|
||||
}
|
||||
val clock = Clock.systemUTC()
|
||||
val listState: LazyListState = rememberLazyListState(
|
||||
initialFirstVisibleItemIndex = 0,
|
||||
)
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
LaunchedEffect(key1 = state.overviewState) {
|
||||
|
|
|
@ -10,5 +10,6 @@ sealed interface DirectoryScreenState {
|
|||
|
||||
sealed interface DirectoryEvent {
|
||||
data class OpenDownloadUrl(val url: String) : DirectoryEvent
|
||||
object ScrollToTop : DirectoryEvent
|
||||
}
|
||||
|
||||
|
|
|
@ -36,5 +36,9 @@ class DirectoryViewModel(
|
|||
fun stop() {
|
||||
syncJob?.cancel()
|
||||
}
|
||||
|
||||
fun scrollToTopOfMessages() {
|
||||
_events.tryEmit(DirectoryEvent.ScrollToTop)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ fun HomeScreen(homeViewModel: HomeViewModel) {
|
|||
homeViewModel.start()
|
||||
}
|
||||
|
||||
|
||||
when (val state = homeViewModel.state) {
|
||||
Loading -> CenteredLoading()
|
||||
is SignedIn -> {
|
||||
|
@ -66,7 +67,12 @@ private fun BottomBar(state: SignedIn, homeViewModel: HomeViewModel) {
|
|||
Directory -> BottomNavigationItem(
|
||||
icon = { Icon(page.icon, contentDescription = null) },
|
||||
selected = state.page == page,
|
||||
onClick = { homeViewModel.changePage(page) },
|
||||
onClick = {
|
||||
when {
|
||||
state.page == page -> homeViewModel.scrollToTopOfMessages()
|
||||
else -> homeViewModel.changePage(page)
|
||||
}
|
||||
},
|
||||
)
|
||||
Profile -> BottomNavigationItem(
|
||||
icon = {
|
||||
|
|
|
@ -55,6 +55,10 @@ class HomeViewModel(
|
|||
}
|
||||
}
|
||||
|
||||
fun scrollToTopOfMessages() {
|
||||
directoryViewModel.scrollToTopOfMessages()
|
||||
}
|
||||
|
||||
fun changePage(page: Page) {
|
||||
state = when (val current = state) {
|
||||
Loading -> current
|
||||
|
|
Loading…
Reference in New Issue