scrolling to top when select directory icon again

This commit is contained in:
Adam Brown 2022-04-13 21:33:16 +01:00
parent 352501fb9b
commit dccf8cc206
5 changed files with 33 additions and 11 deletions

View File

@ -53,16 +53,22 @@ import kotlin.math.roundToInt
@Composable @Composable
fun DirectoryScreen(directoryViewModel: DirectoryViewModel) { fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
val state = directoryViewModel.state val state = directoryViewModel.state
directoryViewModel.ObserveEvents()
LifecycleEffect( val listState: LazyListState = rememberLazyListState(
onStart = { directoryViewModel.start() }, initialFirstVisibleItemIndex = 0,
onStop = { directoryViewModel.stop() }
) )
val toolbarHeight = 72.dp val toolbarHeight = 72.dp
val toolbarHeightPx = with(LocalDensity.current) { toolbarHeight.roundToPx().toFloat() } val toolbarHeightPx = with(LocalDensity.current) { toolbarHeight.roundToPx().toFloat() }
val toolbarOffsetHeightPx = remember { mutableStateOf(0f) } val toolbarOffsetHeightPx = remember { mutableStateOf(0f) }
directoryViewModel.ObserveEvents(listState, toolbarOffsetHeightPx)
LifecycleEffect(
onStart = { directoryViewModel.start() },
onStop = { directoryViewModel.stop() }
)
val nestedScrollConnection = remember { val nestedScrollConnection = remember {
object : NestedScrollConnection { object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
@ -80,7 +86,7 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
) { ) {
when (state) { when (state) {
is Content -> { is Content -> {
Content(state) Content(listState, state)
} }
EmptyLoading -> CenteredLoading() EmptyLoading -> CenteredLoading()
is Error -> { is Error -> {
@ -99,7 +105,7 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
} }
@Composable @Composable
private fun DirectoryViewModel.ObserveEvents() { private fun DirectoryViewModel.ObserveEvents(listState: LazyListState, toolbarPosition: MutableState<Float>) {
val context = LocalContext.current val context = LocalContext.current
StartObserving { StartObserving {
this@ObserveEvents.events.launch { this@ObserveEvents.events.launch {
@ -107,21 +113,22 @@ private fun DirectoryViewModel.ObserveEvents() {
is OpenDownloadUrl -> { is OpenDownloadUrl -> {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it.url))) context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it.url)))
} }
DirectoryEvent.ScrollToTop -> {
toolbarPosition.value = 0f
listState.scrollToItem(0)
}
} }
} }
} }
} }
@Composable @Composable
private fun Content(state: Content) { private fun Content(listState: LazyListState, state: Content) {
val context = LocalContext.current val context = LocalContext.current
val navigateToRoom = { roomId: RoomId -> val navigateToRoom = { roomId: RoomId ->
context.startActivity(MessengerActivity.newInstance(context, roomId)) context.startActivity(MessengerActivity.newInstance(context, roomId))
} }
val clock = Clock.systemUTC() val clock = Clock.systemUTC()
val listState: LazyListState = rememberLazyListState(
initialFirstVisibleItemIndex = 0,
)
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
LaunchedEffect(key1 = state.overviewState) { LaunchedEffect(key1 = state.overviewState) {

View File

@ -10,5 +10,6 @@ sealed interface DirectoryScreenState {
sealed interface DirectoryEvent { sealed interface DirectoryEvent {
data class OpenDownloadUrl(val url: String) : DirectoryEvent data class OpenDownloadUrl(val url: String) : DirectoryEvent
object ScrollToTop : DirectoryEvent
} }

View File

@ -36,5 +36,9 @@ class DirectoryViewModel(
fun stop() { fun stop() {
syncJob?.cancel() syncJob?.cancel()
} }
fun scrollToTopOfMessages() {
_events.tryEmit(DirectoryEvent.ScrollToTop)
}
} }

View File

@ -25,6 +25,7 @@ fun HomeScreen(homeViewModel: HomeViewModel) {
homeViewModel.start() homeViewModel.start()
} }
when (val state = homeViewModel.state) { when (val state = homeViewModel.state) {
Loading -> CenteredLoading() Loading -> CenteredLoading()
is SignedIn -> { is SignedIn -> {
@ -66,7 +67,12 @@ private fun BottomBar(state: SignedIn, homeViewModel: HomeViewModel) {
Directory -> BottomNavigationItem( Directory -> BottomNavigationItem(
icon = { Icon(page.icon, contentDescription = null) }, icon = { Icon(page.icon, contentDescription = null) },
selected = state.page == page, selected = state.page == page,
onClick = { homeViewModel.changePage(page) }, onClick = {
when {
state.page == page -> homeViewModel.scrollToTopOfMessages()
else -> homeViewModel.changePage(page)
}
},
) )
Profile -> BottomNavigationItem( Profile -> BottomNavigationItem(
icon = { icon = {

View File

@ -55,6 +55,10 @@ class HomeViewModel(
} }
} }
fun scrollToTopOfMessages() {
directoryViewModel.scrollToTopOfMessages()
}
fun changePage(page: Page) { fun changePage(page: Page) {
state = when (val current = state) { state = when (val current = state) {
Loading -> current Loading -> current