avoiding recreating navigation callback on recomposition

This commit is contained in:
Adam Brown 2022-10-02 17:17:37 +01:00
parent 86d41fd95f
commit 47be48579d
1 changed files with 9 additions and 2 deletions

View File

@ -91,6 +91,7 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) {
is Error -> GenericError { is Error -> GenericError {
// TODO // TODO
} }
is Content -> Content(listState, state) is Content -> Content(listState, state)
} }
Toolbar(title = "Messages", offset = { IntOffset(x = 0, y = toolbarOffsetHeightPx.value.roundToInt()) }) Toolbar(title = "Messages", offset = { IntOffset(x = 0, y = toolbarOffsetHeightPx.value.roundToInt()) })
@ -106,6 +107,7 @@ private fun DirectoryViewModel.ObserveEvents(listState: LazyListState, toolbarPo
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 -> { DirectoryEvent.ScrollToTop -> {
toolbarPosition.value = 0f toolbarPosition.value = 0f
listState.scrollToItem(0) listState.scrollToItem(0)
@ -121,8 +123,10 @@ val clock = Clock.systemUTC()
@Composable @Composable
private fun Content(listState: LazyListState, state: Content) { private fun Content(listState: LazyListState, state: Content) {
val context = LocalContext.current val context = LocalContext.current
val navigateToRoom = { roomId: RoomId -> val navigateToRoom = remember {
context.startActivity(MessengerActivity.newInstance(context, roomId)) { roomId: RoomId ->
context.startActivity(MessengerActivity.newInstance(context, roomId))
}
} }
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@ -247,6 +251,7 @@ private fun body(overview: RoomOverview, secondaryText: Color, typing: SyncServi
color = MaterialTheme.colorScheme.primary color = MaterialTheme.colorScheme.primary
) )
} }
else -> when (val lastMessage = overview.lastMessage) { else -> when (val lastMessage = overview.lastMessage) {
null -> { null -> {
Text( Text(
@ -256,6 +261,7 @@ private fun body(overview: RoomOverview, secondaryText: Color, typing: SyncServi
color = secondaryText color = secondaryText
) )
} }
else -> { else -> {
when (overview.isGroup) { when (overview.isGroup) {
true -> { true -> {
@ -268,6 +274,7 @@ private fun body(overview: RoomOverview, secondaryText: Color, typing: SyncServi
color = secondaryText color = secondaryText
) )
} }
false -> { false -> {
Text( Text(
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,