From 1957fa7b15a28c5e6ac4fe70229538cfdc0f3542 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 11 Oct 2024 11:34:17 +0200 Subject: [PATCH] Made "enteredForeground" variable accessible to all conversation views and added a check for its value before performing chatroom.compose --- Linphone/UI/Call/CallView.swift | 8 ++++++-- Linphone/UI/Main/ContentView.swift | 14 +++++++++++--- .../UI/Main/Conversations/ConversationsView.swift | 7 +++++-- .../Fragments/ConversationFragment.swift | 6 ++++-- .../Fragments/ConversationsFragment.swift | 8 +++++--- .../Fragments/ConversationsListFragment.swift | 10 ++++------ 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/Linphone/UI/Call/CallView.swift b/Linphone/UI/Call/CallView.swift index d0e46d4b6..494c46680 100644 --- a/Linphone/UI/Call/CallView.swift +++ b/Linphone/UI/Call/CallView.swift @@ -68,6 +68,8 @@ struct CallView: View { @State var buttonSize = 60.0 + @Binding var enteredForeground: Bool + var body: some View { GeometryReader { geo in ZStack { @@ -197,7 +199,8 @@ struct CallView: View { conversationViewModel: conversationViewModel, conversationsListViewModel: conversationsListViewModel, conversationForwardMessageViewModel: conversationForwardMessageViewModel, - isShowConversationFragment: $isShowConversationFragment + isShowConversationFragment: $isShowConversationFragment, + enteredForeground: $enteredForeground ) .frame(maxWidth: .infinity) .background(Color.gray100) @@ -2785,7 +2788,8 @@ struct PressedButtonStyle: ButtonStyle { conversationForwardMessageViewModel: ConversationForwardMessageViewModel(), fullscreenVideo: .constant(false), isShowStartCallFragment: .constant(false), - isShowConversationFragment: .constant(false) + isShowConversationFragment: .constant(false), + enteredForeground: .constant(false) ) } // swiftlint:enable type_body_length diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index 4e147f5fc..497b8ea6f 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -75,6 +75,8 @@ struct ContentView: View { @State var isShowScheduleMeetingFragment = false @State private var isShowLoginFragment: Bool = false + @State private var enteredForeground: Bool = false + var body: some View { let pub = NotificationCenter.default .publisher(for: NSNotification.Name("ContactLoaded")) @@ -597,7 +599,8 @@ struct ContentView: View { conversationViewModel: conversationViewModel, conversationsListViewModel: conversationsListViewModel, text: $text, - isShowStartConversationFragment: $isShowStartConversationFragment + isShowStartConversationFragment: $isShowStartConversationFragment, + enteredForeground: $enteredForeground ) .roundedCorner(25, corners: [.topRight, .topLeft]) .shadow( @@ -861,7 +864,8 @@ struct ContentView: View { conversationViewModel: conversationViewModel, conversationsListViewModel: conversationsListViewModel, conversationForwardMessageViewModel: conversationForwardMessageViewModel, - isShowConversationFragment: $isShowConversationFragment + isShowConversationFragment: $isShowConversationFragment, + enteredForeground: $enteredForeground ) .frame(maxWidth: .infinity) .background(Color.gray100) @@ -1176,7 +1180,8 @@ struct ContentView: View { conversationForwardMessageViewModel: conversationForwardMessageViewModel, fullscreenVideo: $fullscreenVideo, isShowStartCallFragment: $isShowStartCallFragment, - isShowConversationFragment: $isShowConversationFragment + isShowConversationFragment: $isShowConversationFragment, + enteredForeground: $enteredForeground ) .zIndex(5) .transition(.scale.combined(with: .move(edge: .top))) @@ -1229,6 +1234,9 @@ struct ContentView: View { } orientation = newOrientation } + .onChange(of: scenePhase) { newPhase in + enteredForeground = newPhase == .active + } } func openMenu() { diff --git a/Linphone/UI/Main/Conversations/ConversationsView.swift b/Linphone/UI/Main/Conversations/ConversationsView.swift index 3918ed6c8..06a120dd5 100644 --- a/Linphone/UI/Main/Conversations/ConversationsView.swift +++ b/Linphone/UI/Main/Conversations/ConversationsView.swift @@ -27,10 +27,12 @@ struct ConversationsView: View { @Binding var isShowStartConversationFragment: Bool + @Binding var enteredForeground: Bool + var body: some View { NavigationView { ZStack(alignment: .bottomTrailing) { - ConversationsFragment(conversationViewModel: conversationViewModel, conversationsListViewModel: conversationsListViewModel, text: $text) + ConversationsFragment(conversationViewModel: conversationViewModel, conversationsListViewModel: conversationsListViewModel, text: $text, enteredForeground: $enteredForeground) Button { withAnimation { @@ -62,6 +64,7 @@ struct ConversationsView: View { conversationViewModel: ConversationViewModel(), conversationsListViewModel: ConversationsListViewModel(), showingSheet: .constant(false), - text: .constant("") + text: .constant(""), + enteredForeground: .constant(false) ) } diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift index 0f970c8f5..c0b1707b5 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift @@ -58,6 +58,8 @@ struct ConversationFragment: View { @State private var selectedCategoryIndex = 0 + @Binding var enteredForeground: Bool + var body: some View { NavigationView { GeometryReader { geometry in @@ -573,7 +575,7 @@ struct ConversationFragment: View { .focused($isMessageTextFocused) .padding(.vertical, 5) .onChange(of: conversationViewModel.messageText) { text in - if !text.isEmpty { + if !text.isEmpty && !enteredForeground { conversationViewModel.compose() } } @@ -586,7 +588,7 @@ struct ConversationFragment: View { .default_text_style(styleSize: 15) .focused($isMessageTextFocused) .onChange(of: conversationViewModel.messageText) { text in - if !text.isEmpty { + if !text.isEmpty && !enteredForeground { conversationViewModel.compose() } } diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsFragment.swift index b91bf86e0..22e62e055 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsFragment.swift @@ -29,11 +29,13 @@ struct ConversationsFragment: View { @State var showingSheet: Bool = false @Binding var text: String + @Binding var enteredForeground: Bool + var body: some View { ZStack { if #available(iOS 16.0, *), idiom != .pad { ConversationsListFragment(conversationViewModel: conversationViewModel, - conversationsListViewModel: conversationsListViewModel, showingSheet: $showingSheet, text: $text) + conversationsListViewModel: conversationsListViewModel, showingSheet: $showingSheet, text: $text, enteredForeground: $enteredForeground) .sheet(isPresented: $showingSheet) { ConversationsListBottomSheet( conversationsListViewModel: conversationsListViewModel, @@ -43,7 +45,7 @@ struct ConversationsFragment: View { } } else { ConversationsListFragment(conversationViewModel: conversationViewModel, - conversationsListViewModel: conversationsListViewModel, showingSheet: $showingSheet, text: $text) + conversationsListViewModel: conversationsListViewModel, showingSheet: $showingSheet, text: $text, enteredForeground: $enteredForeground) .halfSheet(showSheet: $showingSheet) { ConversationsListBottomSheet( conversationsListViewModel: conversationsListViewModel, @@ -56,5 +58,5 @@ struct ConversationsFragment: View { } #Preview { - ConversationsFragment(conversationViewModel: ConversationViewModel(), conversationsListViewModel: ConversationsListViewModel(), text: .constant("")) + ConversationsFragment(conversationViewModel: ConversationViewModel(), conversationsListViewModel: ConversationsListViewModel(), text: .constant(""), enteredForeground: .constant(false)) } diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift index 73132c185..2e4e8fc75 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift @@ -23,8 +23,6 @@ import linphonesw struct ConversationsListFragment: View { @EnvironmentObject var navigationManager: NavigationManager - @Environment(\.scenePhase) var scenePhase - @State private var enteredForeground: Bool = false @ObservedObject var conversationViewModel: ConversationViewModel @ObservedObject var conversationsListViewModel: ConversationsListViewModel @@ -32,6 +30,8 @@ struct ConversationsListFragment: View { @Binding var showingSheet: Bool @Binding var text: String + @Binding var enteredForeground: Bool + var body: some View { let pub = NotificationCenter.default .publisher(for: NSNotification.Name("ChatRoomsComputed")) @@ -198,9 +198,6 @@ struct ConversationsListFragment: View { } .navigationTitle("") .navigationBarHidden(true) - .onChange(of: scenePhase) { newPhase in - enteredForeground = newPhase == .active - } } } @@ -209,6 +206,7 @@ struct ConversationsListFragment: View { conversationViewModel: ConversationViewModel(), conversationsListViewModel: ConversationsListViewModel(), showingSheet: .constant(false), - text: .constant("") + text: .constant(""), + enteredForeground: .constant(false) ) }