diff --git a/apps/ios/Shared/Views/Chat/ChatItem/CIVoiceView.swift b/apps/ios/Shared/Views/Chat/ChatItem/CIVoiceView.swift index c9d5b4bf71..c2fdb9f5bd 100644 --- a/apps/ios/Shared/Views/Chat/ChatItem/CIVoiceView.swift +++ b/apps/ios/Shared/Views/Chat/ChatItem/CIVoiceView.swift @@ -223,17 +223,12 @@ struct VoiceMessagePlayer: View { chatModel.stopPreviousRecPlay = nil } } -// .onChange(of: chatModel.chatId) { _ in -// if sizeMultiplier != 1 { -// stopPlayback() -// } -// } -// .onChange(of: chatModel.currentUser?.userId) { _ in -// if sizeMultiplier != 1 { -// stopPlayback() -// } -// } - .onDisappear { + .onChange(of: chatModel.chatId) { _ in + if sizeMultiplier != 1 { + stopPlayback() + } + } + .onChange(of: chatModel.currentUser?.userId) { _ in if sizeMultiplier != 1 { stopPlayback() } diff --git a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift index 35cb5b3861..929102960f 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift @@ -38,6 +38,10 @@ struct ChatListNavLink: View { @State private var inProgress = false @State private var progressByTimeout = false + @Binding var audioPlayer: AudioPlayer? + @Binding var playbackState: VoiceMessagePlaybackState + @Binding var playbackTime: TimeInterval? + var body: some View { Group { switch chat.chatInfo { @@ -69,7 +73,7 @@ struct ChatListNavLink: View { @ViewBuilder private func contactNavLink(_ contact: Contact) -> some View { Group { if contact.activeConn == nil && contact.profile.contactLink != nil { - ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false)) + ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) .frame(height: rowHeights[dynamicTypeSize]) .swipeActions(edge: .trailing, allowsFullSwipe: true) { Button { @@ -88,7 +92,7 @@ struct ChatListNavLink: View { NavLinkPlain( tag: chat.chatInfo.id, selection: $chatModel.chatId, - label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false)) } + label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) } ) .swipeActions(edge: .leading, allowsFullSwipe: true) { markReadButton() @@ -138,7 +142,7 @@ struct ChatListNavLink: View { @ViewBuilder private func groupNavLink(_ groupInfo: GroupInfo) -> some View { switch (groupInfo.membership.memberStatus) { case .memInvited: - ChatPreviewView(chat: chat, progressByTimeout: $progressByTimeout) + ChatPreviewView(chat: chat, progressByTimeout: $progressByTimeout, audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) .frame(height: rowHeights[dynamicTypeSize]) .swipeActions(edge: .trailing, allowsFullSwipe: true) { joinGroupButton() @@ -158,7 +162,7 @@ struct ChatListNavLink: View { } .disabled(inProgress) case .memAccepted: - ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false)) + ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) .frame(height: rowHeights[dynamicTypeSize]) .onTapGesture { AlertManager.shared.showAlert(groupInvitationAcceptedAlert()) @@ -175,7 +179,7 @@ struct ChatListNavLink: View { NavLinkPlain( tag: chat.chatInfo.id, selection: $chatModel.chatId, - label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false)) }, + label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) }, disabled: !groupInfo.ready ) .frame(height: rowHeights[dynamicTypeSize]) @@ -202,7 +206,7 @@ struct ChatListNavLink: View { NavLinkPlain( tag: chat.chatInfo.id, selection: $chatModel.chatId, - label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false)) }, + label: { ChatPreviewView(chat: chat, progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) }, disabled: !noteFolder.ready ) .frame(height: rowHeights[dynamicTypeSize]) @@ -576,19 +580,22 @@ func getErrorAlert(_ error: Error, _ title: LocalizedStringKey) -> ErrorAlert { struct ChatListNavLink_Previews: PreviewProvider { static var previews: some View { @State var chatId: String? = "@1" + @State var audioPlayer: AudioPlayer? = nil + @State var playbackState: VoiceMessagePlaybackState = .noPlayback + @State var playbackTime: TimeInterval? = nil return Group { ChatListNavLink(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello")] - )) + ), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatListNavLink(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello")] - )) + ), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatListNavLink(chat: Chat( chatInfo: ChatInfo.sampleData.contactRequest, chatItems: [] - )) + ), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) } .previewLayout(.fixed(width: 360, height: 82)) } diff --git a/apps/ios/Shared/Views/ChatList/ChatListView.swift b/apps/ios/Shared/Views/ChatList/ChatListView.swift index 16f87f0c13..00665567ad 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListView.swift @@ -21,6 +21,11 @@ struct ChatListView: View { @State private var newChatMenuOption: NewChatMenuOption? = nil @State private var userPickerVisible = false @State private var showConnectDesktop = false + + @State var audioPlayer: AudioPlayer? = nil + @State var playbackState: VoiceMessagePlaybackState = .noPlayback + @State var playbackTime: TimeInterval? = nil + @AppStorage(DEFAULT_SHOW_UNREAD_AND_FAVORITES) private var showUnreadAndFavorites = false var body: some View { @@ -149,7 +154,7 @@ struct ChatListView: View { .frame(maxWidth: .infinity) } ForEach(cs, id: \.viewId) { chat in - ChatListNavLink(chat: chat) + ChatListNavLink(chat: chat, audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) .padding(.trailing, -16) .disabled(chatModel.chatRunning != true || chatModel.deletedChats.contains(chat.chatInfo.id)) .listRowBackground(Color.clear) diff --git a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift index 36f81696b3..fe1ee252af 100644 --- a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift @@ -18,9 +18,9 @@ struct ChatPreviewView: View { var darkGreen = Color(red: 0, green: 0.5, blue: 0) @State private var activeContentPreview: ActiveContentPreview? = nil - @State var audioPlayer: AudioPlayer? = nil - @State var playbackState: VoiceMessagePlaybackState = .noPlayback - @State var playbackTime: TimeInterval? = nil + @Binding var audioPlayer: AudioPlayer? + @Binding var playbackState: VoiceMessagePlaybackState + @Binding var playbackTime: TimeInterval? @State private var showFullscreenGallery: Bool = false @@ -448,34 +448,38 @@ private struct ActiveContentPreview: Equatable { struct ChatPreviewView_Previews: PreviewProvider { static var previews: some View { - Group { + @State var audioPlayer: AudioPlayer? = nil + @State var playbackState: VoiceMessagePlaybackState = .noPlayback + @State var playbackTime: TimeInterval? = nil + + return Group { ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [] - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello", .sndSent(sndProgress: .complete))] - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello", .sndSent(sndProgress: .complete))], chatStats: ChatStats(unreadCount: 11, minUnreadItemId: 0) - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello", .sndSent(sndProgress: .complete), itemDeleted: .deleted(deletedTs: .now))] - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.direct, chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello", .sndSent(sndProgress: .complete))], chatStats: ChatStats(unreadCount: 3, minUnreadItemId: 0) - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) ChatPreviewView(chat: Chat( chatInfo: ChatInfo.sampleData.group, chatItems: [ChatItem.getSample(1, .directSnd, .now, "Lorem ipsum dolor sit amet, d. consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")], chatStats: ChatStats(unreadCount: 11, minUnreadItemId: 0) - ), progressByTimeout: Binding.constant(false)) + ), progressByTimeout: Binding.constant(false), audioPlayer: $audioPlayer, playbackState: $playbackState, playbackTime: $playbackTime) } .previewLayout(.fixed(width: 360, height: 78)) }