From 770bb496f55cd076d4c635ef077cd427f6cbaa9c Mon Sep 17 00:00:00 2001 From: James Magahern Date: Thu, 17 Aug 2023 01:07:15 -0700 Subject: [PATCH] Trying to plumb list changes... --- app/build.gradle | 8 ++++++-- .../ui/conversationlist/ConversationListScreen.kt | 9 ++++++--- .../conversationlist/ConversationListViewModel.kt | 13 +++++++++++++ .../kordophone/backend/server/ChatRepository.kt | 4 +++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 27780a9..1a07129 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -83,8 +83,12 @@ dependencies { implementation "androidx.navigation:navigation-compose:$nav_version" implementation 'androidx.activity:activity-compose:1.4.3' - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' - implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1' + + // Lifecycle + def lifecycle_version = "2.6.1" + implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version" implementation "androidx.compose.ui:ui:1.4.3" implementation 'androidx.compose.material:material:1.4.3' diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListScreen.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListScreen.kt index 81e63bd..23f568f 100644 --- a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListScreen.kt +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.hilt.navigation.compose.hiltViewModel import net.buzzert.kordophone.backend.model.Conversation @@ -29,7 +30,7 @@ fun ConversationListScreen( viewModel: ConversationListViewModel = hiltViewModel(), onConversationSelected: (conversationID: String) -> Unit ) { - val conversations by viewModel.conversations.collectAsState(initial = listOf()) + val conversations by viewModel.conversations.collectAsStateWithLifecycle(initialValue = listOf()) ConversationListView(conversations = conversations, onConversationSelected = onConversationSelected) } @@ -56,6 +57,7 @@ fun ConversationListView( name = conversation.formattedDisplayName(), id = conversation.guid, isUnread = conversation.unreadCount > 0, + lastMessagePreview = conversation.lastMessagePreview ?: "", onClick = { onConversationSelected(conversation.guid) } ) } @@ -68,6 +70,7 @@ fun ConversationListItem( name: String, id: String, isUnread: Boolean, + lastMessagePreview: String, onClick: () -> Unit ) { val unreadSize = 12.dp @@ -113,7 +116,7 @@ fun ConversationListItem( Spacer(Modifier.width(horizontalPadding)) } - Text("This is a test.") + Text(lastMessagePreview) Spacer(Modifier.height(verticalPadding)) Divider() @@ -139,7 +142,7 @@ fun UnreadIndicator(size: Dp, modifier: Modifier = Modifier) { @Composable fun ConversationListItemPreview() { Column(modifier = Modifier.background(MaterialTheme.colors.background)) { - ConversationListItem(name = "James Magahern", id = "asdf", isUnread = true) {} + ConversationListItem(name = "James Magahern", id = "asdf", lastMessagePreview = "This is a test", isUnread = true) {} } } diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt index 530e0c7..e0335da 100644 --- a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt @@ -1,9 +1,12 @@ package net.buzzert.kordophonedroid.ui.conversationlist +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import net.buzzert.kordophone.backend.model.Conversation import net.buzzert.kordophone.backend.server.ChatRepository @@ -15,6 +18,10 @@ class ConversationListViewModel @Inject constructor( ) : ViewModel() { val conversations: Flow> get() = repository.conversationChanges + .map { + it.sortedBy { it.date } + .reversed() + } init { viewModelScope.launch { @@ -24,5 +31,11 @@ class ConversationListViewModel @Inject constructor( viewModelScope.launch { repository.beginWatchingForUpdates(this) } + + viewModelScope.launch { + repository.conversationChanges.collect { + Log.v("ViewModel", "Got conversationChanges") + } + } } } \ No newline at end of file diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt index b3a7178..71519f6 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt @@ -122,9 +122,11 @@ class ChatRepository( val conversations = fetchConversations() database.writeConversations(conversations) + // TODO: Delete non-existent conversations. + // Sync top N number of conversations' message content Log.d(REPO_LOG, "Synchronizing messages") - val sortedConversations = conversations.sortedBy { it.date } + val sortedConversations = conversations.sortedBy { it.date }.reversed() for (conversation in sortedConversations.take(CONVERSATION_MESSAGE_SYNC_COUNT)) { val messages = fetchMessages(conversation) database.writeMessages(messages, conversation)