Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Chats not loading on tablet layout #280

Merged
merged 15 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ internal fun ConversationChatListScreen(
val posts: LazyPagingItems<ChatListItem> =
viewModel.chatListUseCase.postPagingData.collectAsLazyPagingItems()

LaunchedEffect(courseId, conversationId) {
chatListState.scrollToItem(0)
}

ConversationChatListScreen(
modifier = modifier,
courseId = courseId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package de.tum.informatics.www1.artemis.native_app.feature.metis.conversation.ui

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.SizeTransform
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.VerticalDivider
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import de.tum.informatics.www1.artemis.native_app.core.ui.getWindowSizeClass
import de.tum.informatics.www1.artemis.native_app.core.ui.navigation.DefaultTransition
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.StandalonePostId
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf

private const val ConversationOverviewMaxWeight = 0.3f
private val ConversationOverviewMaxWidth = 600.dp

/**
* Displays a conversation. Uses [ConversationChatListScreen] and [ConversationThreadScreen] to show the conversations.
* @param conversationId the conversation that should be displayed
Expand All @@ -49,138 +40,97 @@ fun ConversationScreen(
onNavigateToSettings: () -> Unit,
conversationsOverview: @Composable (Modifier) -> Unit
) {
val store = remember { ViewModelStore() }

val owner = remember(store) {
object : ViewModelStoreOwner {
override val viewModelStore: ViewModelStore = store
}
}

DisposableEffect(courseId, conversationId) {
onDispose {
store.clear()
}
}

val viewModel: ConversationViewModel =
koinViewModel(
key = "$courseId|$conversationId",
viewModelStoreOwner = owner
) { parametersOf(courseId, conversationId, threadPostId) }
) { parametersOf(courseId, conversationId, threadPostId) }

LaunchedEffect(threadPostId, viewModel) {
viewModel.updateOpenedThread(threadPostId)
}

val widthSizeClass = getWindowSizeClass().widthSizeClass
var showThread by remember { mutableStateOf(threadPostId != null) }

val onNavigateUp = {
onCloseThread()
showThread = false
val showThread by remember(threadPostId) {
mutableStateOf(threadPostId != null)
}
val onClickViewPost = { clientPostId: StandalonePostId ->
onOpenThread(clientPostId)
showThread = true

WindowSizeAwareTwoColumnLayout(
modifier = modifier,
optionalColumn = conversationsOverview,
) { innerModifier ->
AnimatedContent(
modifier = innerModifier,
targetState = showThread,
transitionSpec = {
if (targetState) {
DefaultTransition.navigateForward
} else {
DefaultTransition.navigateBack
}.using(
SizeTransform(clip = false)
)
},
label = "ConversationScreen chatList thread navigation animation"
) { _showThread ->
if (_showThread) {
ConversationThreadScreen(
modifier = Modifier.fillMaxSize(),
viewModel = viewModel,
onNavigateUp = onCloseThread
)
} else {
ConversationChatListScreen(
modifier = Modifier.fillMaxSize(),
viewModel = viewModel,
courseId = courseId,
conversationId = conversationId,
onNavigateBack = onCloseConversation,
onNavigateToSettings = onNavigateToSettings,
onClickViewPost = onOpenThread
)
}
}
}
}


@Composable
fun WindowSizeAwareTwoColumnLayout(
modifier: Modifier = Modifier,
optionalColumnWeight: Float = 1f,
priorityColumnWeight: Float = 2f,
optionalColumn: @Composable (Modifier) -> Unit,
priorityColumn: @Composable (Modifier) -> Unit
) {
val widthSizeClass = getWindowSizeClass().widthSizeClass

when {
widthSizeClass <= WindowWidthSizeClass.Compact -> {
Box(modifier = modifier) {
AnimatedContent(
targetState = showThread,
transitionSpec = {
if (targetState) {
DefaultTransition.navigateForward
} else {
DefaultTransition.navigateBack
}.using(
SizeTransform(clip = false)
)
},
label = "ConversationScreen chatList thread navigation animation"
) { _showThread ->
if (_showThread) {
ConversationThreadScreen(
modifier = Modifier.fillMaxSize(),
viewModel = viewModel,
onNavigateUp = onNavigateUp
)
} else {
ConversationChatListScreen(
modifier = Modifier.fillMaxSize(),
viewModel = viewModel,
courseId = courseId,
conversationId = conversationId,
onNavigateBack = onCloseConversation,
onNavigateToSettings = onNavigateToSettings,
onClickViewPost = onClickViewPost
)
}
}
priorityColumn(Modifier.fillMaxSize())
}
}

else -> {
val arrangement = Arrangement.spacedBy(8.dp)

Row(
modifier = modifier,
horizontalArrangement = arrangement
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
val isOverviewVisible = !showThread || widthSizeClass >= WindowWidthSizeClass.Expanded
AnimatedVisibility(
Box(
modifier = Modifier
.weight(ConversationOverviewMaxWeight)
.widthIn(max = ConversationOverviewMaxWidth)
.fillMaxHeight(),
visible = isOverviewVisible
.weight(optionalColumnWeight)
.fillMaxHeight()
) {
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = arrangement
) {
conversationsOverview(
Modifier
.weight(1f)
.fillMaxHeight()
)

VerticalDivider()
}
}

val otherWeight = when {
isOverviewVisible && showThread -> 0.35f
isOverviewVisible && !showThread -> 0.7f
else -> 0.5f
optionalColumn(Modifier.fillMaxSize())
}

val otherModifier = Modifier
.weight(otherWeight)
.fillMaxHeight()
VerticalDivider()

ConversationChatListScreen(
Box(
modifier = Modifier
.weight(1f)
.fillMaxHeight(),
viewModel = viewModel,
courseId = courseId,
conversationId = conversationId,
onNavigateBack = onCloseConversation,
onNavigateToSettings = onNavigateToSettings,
onClickViewPost = onClickViewPost
)

if (showThread) {
VerticalDivider()

ConversationThreadScreen(
modifier = otherModifier,
viewModel = viewModel,
onNavigateUp = onNavigateUp
)
.weight(priorityColumnWeight)
.fillMaxHeight()
) {
priorityColumn(Modifier.fillMaxSize())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.tum.informatics.www1.artemis.native_app.feature.metis.conversation.ui

import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.itemKey

/**
* Abstract layer over the loaded posts state, so we can use both a Pager and a simple list to display posts.
Expand Down Expand Up @@ -72,7 +71,13 @@ sealed interface PostsDataState {
}

fun LazyPagingItems<ChatListItem>.asPostsDataState(): PostsDataState = when {
itemCount == 0 -> PostsDataState.Empty
itemCount == 0 -> {
if (loadState.isIdle) {
PostsDataState.Empty
} else {
PostsDataState.Loading
}
}
loadState.refresh is LoadState.Loading -> PostsDataState.Loading
loadState.refresh is LoadState.Error -> PostsDataState.Error(::retry)
else -> PostsDataState.Loaded.WithLazyPagingItems(
Expand Down
Loading