From ac8253e47e157b62054c000fe3141c05fdc13a74 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 25 Oct 2024 14:44:56 +0200 Subject: [PATCH] Fix refresh conversation item in list --- .../Fragments/ConversationsListFragment.swift | 286 +++++++++--------- 1 file changed, 150 insertions(+), 136 deletions(-) diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift index e4cdd1fcd..38212c570 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift @@ -31,144 +31,17 @@ struct ConversationsListFragment: View { @Binding var text: String var body: some View { - let pub = NotificationCenter.default - .publisher(for: NSNotification.Name("ChatRoomsComputed")) - VStack { List { - ForEach(0.. 0) { view in - view.default_text_style_700(styleSize: 14) - } - .default_text_style(styleSize: 14) - .frame(maxWidth: .infinity, alignment: .leading) - .lineLimit(1) - - Text(conversationsListViewModel.conversationsList[index].lastMessageText) - .foregroundStyle(Color.grayMain2c400) - .if(conversationsListViewModel.conversationsList[index].unreadMessagesCount > 0) { view in - view.default_text_style_700(styleSize: 14) - } - .default_text_style(styleSize: 14) - .frame(maxWidth: .infinity, alignment: .leading) - .lineLimit(1) - - Spacer() - } - - Spacer() - - VStack(alignment: .trailing, spacing: 0) { - Spacer() - - HStack { - if !conversationsListViewModel.conversationsList[index].encryptionEnabled { - Image("warning-circle") - .renderingMode(.template) - .resizable() - .foregroundStyle(Color.redDanger500) - .frame(width: 18, height: 18, alignment: .trailing) - } - - Text(conversationsListViewModel.getCallTime(startDate: conversationsListViewModel.conversationsList[index].lastUpdateTime)) - .foregroundStyle(Color.grayMain2c400) - .default_text_style(styleSize: 14) - .lineLimit(1) - } - - Spacer() - - HStack { - if conversationsListViewModel.conversationsList[index].isMuted == false - && !(!conversationsListViewModel.conversationsList[index].lastMessageText.isEmpty - && conversationsListViewModel.conversationsList[index].lastMessageIsOutgoing == true) - && conversationsListViewModel.conversationsList[index].unreadMessagesCount == 0 { - Text("") - .frame(width: 18, height: 18, alignment: .trailing) - } - - if conversationsListViewModel.conversationsList[index].isMuted { - Image("bell-slash") - .renderingMode(.template) - .resizable() - .foregroundStyle(Color.orangeMain500) - .frame(width: 18, height: 18, alignment: .trailing) - } - - if !conversationsListViewModel.conversationsList[index].lastMessageText.isEmpty - && conversationsListViewModel.conversationsList[index].lastMessageIsOutgoing == true { - let imageName = LinphoneUtils.getChatIconState(chatState: conversationsListViewModel.conversationsList[index].lastMessageState) - Image(imageName) - .renderingMode(.template) - .resizable() - .foregroundStyle(Color.orangeMain500) - .frame(width: 18, height: 18, alignment: .trailing) - } - - if conversationsListViewModel.conversationsList[index].unreadMessagesCount > 0 { - HStack { - Text( - conversationsListViewModel.conversationsList[index].unreadMessagesCount < 99 - ? String(conversationsListViewModel.conversationsList[index].unreadMessagesCount) - : "99+" - ) - .foregroundStyle(.white) - .default_text_style(styleSize: 10) - .lineLimit(1) - } - .frame(width: 18, height: 18) - .background(Color.redDanger500) - .cornerRadius(50) - } - } - - Spacer() - } - .padding(.trailing, 10) - } - .frame(height: 50) - .buttonStyle(.borderless) - .listRowInsets(EdgeInsets(top: 6, leading: 20, bottom: 6, trailing: 20)) - .listRowSeparator(.hidden) - .background(.white) - .onReceive(pub) { _ in - if CoreContext.shared.enteredForeground && conversationViewModel.displayedConversation != nil - && (navigationManager.peerAddr == nil || navigationManager.peerAddr == conversationViewModel.displayedConversation!.remoteSipUri) { - if conversationViewModel.displayedConversation != nil { - conversationViewModel.resetDisplayedChatRoom(conversationsList: conversationsListViewModel.conversationsList) - } - } - - CoreContext.shared.enteredForeground = false - - if navigationManager.peerAddr != nil - && index < conversationsListViewModel.conversationsList.count - && conversationsListViewModel.conversationsList[index].remoteSipUri.contains(navigationManager.peerAddr!) { - conversationViewModel.getChatRoomWithStringAddress(conversationsList: conversationsListViewModel.conversationsList, stringAddr: navigationManager.peerAddr!) - navigationManager.peerAddr = nil - } - } - .onTapGesture { - if index < conversationsListViewModel.conversationsList.count { - conversationViewModel.changeDisplayedChatRoom(conversationModel: conversationsListViewModel.conversationsList[index]) - } - } - .onLongPressGesture(minimumDuration: 0.2) { - if index < conversationsListViewModel.conversationsList.count { - conversationsListViewModel.selectedConversation = conversationsListViewModel.conversationsList[index] - showingSheet.toggle() - } - } - } + ForEach(conversationsListViewModel.conversationsList) { conversation in + ConversationRow( + navigationManager: _navigationManager, + conversation: conversation, + conversationViewModel: conversationViewModel, + conversationsListViewModel: conversationsListViewModel, + showingSheet: $showingSheet, + text: $text + ) } } .safeAreaInset(edge: .top, content: { @@ -199,6 +72,147 @@ struct ConversationsListFragment: View { } } +struct ConversationRow: View { + @EnvironmentObject var navigationManager: NavigationManager + + @ObservedObject var conversation: ConversationModel + @ObservedObject var conversationViewModel: ConversationViewModel + @ObservedObject var conversationsListViewModel: ConversationsListViewModel + + @Binding var showingSheet: Bool + @Binding var text: String + + var body: some View { + let pub = NotificationCenter.default + .publisher(for: NSNotification.Name("ChatRoomsComputed")) + HStack { + Avatar(contactAvatarModel: conversation.avatarModel, avatarSize: 50) + + VStack(spacing: 0) { + Spacer() + + Text(conversation.subject) + .foregroundStyle(Color.grayMain2c800) + .if(conversation.unreadMessagesCount > 0) { view in + view.default_text_style_700(styleSize: 14) + } + .default_text_style(styleSize: 14) + .frame(maxWidth: .infinity, alignment: .leading) + .lineLimit(1) + + Text(conversation.lastMessageText) + .foregroundStyle(Color.grayMain2c400) + .if(conversation.unreadMessagesCount > 0) { view in + view.default_text_style_700(styleSize: 14) + } + .default_text_style(styleSize: 14) + .frame(maxWidth: .infinity, alignment: .leading) + .lineLimit(1) + + Spacer() + } + + Spacer() + + VStack(alignment: .trailing, spacing: 0) { + Spacer() + + HStack { + if !conversation.encryptionEnabled { + Image("warning-circle") + .renderingMode(.template) + .resizable() + .foregroundStyle(Color.redDanger500) + .frame(width: 18, height: 18, alignment: .trailing) + } + + Text(conversationsListViewModel.getCallTime(startDate: conversation.lastUpdateTime)) + .foregroundStyle(Color.grayMain2c400) + .default_text_style(styleSize: 14) + .lineLimit(1) + } + + Spacer() + + HStack { + if conversation.isMuted == false + && !(!conversation.lastMessageText.isEmpty + && conversation.lastMessageIsOutgoing == true) + && conversation.unreadMessagesCount == 0 { + Text("") + .frame(width: 18, height: 18, alignment: .trailing) + } + + if conversation.isMuted { + Image("bell-slash") + .renderingMode(.template) + .resizable() + .foregroundStyle(Color.orangeMain500) + .frame(width: 18, height: 18, alignment: .trailing) + } + + if !conversation.lastMessageText.isEmpty + && conversation.lastMessageIsOutgoing == true { + let imageName = LinphoneUtils.getChatIconState(chatState: conversation.lastMessageState) + Image(imageName) + .renderingMode(.template) + .resizable() + .foregroundStyle(Color.orangeMain500) + .frame(width: 18, height: 18, alignment: .trailing) + } + + if conversation.unreadMessagesCount > 0 { + HStack { + Text( + conversation.unreadMessagesCount < 99 + ? String(conversation.unreadMessagesCount) + : "99+" + ) + .foregroundStyle(.white) + .default_text_style(styleSize: 10) + .lineLimit(1) + } + .frame(width: 18, height: 18) + .background(Color.redDanger500) + .cornerRadius(50) + } + } + + Spacer() + } + .padding(.trailing, 10) + } + .frame(height: 50) + .buttonStyle(.borderless) + .listRowInsets(EdgeInsets(top: 6, leading: 20, bottom: 6, trailing: 20)) + .listRowSeparator(.hidden) + .background(.white) + .onReceive(pub) { _ in + if CoreContext.shared.enteredForeground && conversationViewModel.displayedConversation != nil + && (navigationManager.peerAddr == nil || navigationManager.peerAddr == conversationViewModel.displayedConversation!.remoteSipUri) { + if conversationViewModel.displayedConversation != nil { + conversationViewModel.resetDisplayedChatRoom(conversationsList: conversationsListViewModel.conversationsList) + } + } + + CoreContext.shared.enteredForeground = false + + if navigationManager.peerAddr != nil + && conversation.remoteSipUri.contains(navigationManager.peerAddr!) { + conversationViewModel.getChatRoomWithStringAddress(conversationsList: conversationsListViewModel.conversationsList, stringAddr: navigationManager.peerAddr!) + navigationManager.peerAddr = nil + } + } + .onTapGesture { + conversationViewModel.changeDisplayedChatRoom(conversationModel: conversation) + } + .onLongPressGesture(minimumDuration: 0.2) { + conversationsListViewModel.selectedConversation = conversation + showingSheet.toggle() + } + } +} + #Preview { ConversationsListFragment( conversationViewModel: ConversationViewModel(),