From e18bb74bfd2101de94d5325940f43b74bd8405ff Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 25 Nov 2022 15:16:37 +0400 Subject: [PATCH] ios: show voice message button based on preference (#1416) --- .../Chat/ComposeMessage/ComposeView.swift | 16 +++++++++------ .../Chat/ComposeMessage/SendMessageView.swift | 9 +++++++-- apps/ios/Shared/Views/TerminalView.swift | 5 +++-- apps/ios/SimpleXChat/ChatTypes.swift | 20 ++++++++++++++----- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift index 0ee3fe8fa8..082888691d 100644 --- a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift +++ b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift @@ -34,7 +34,6 @@ struct ComposeState { var preview: ComposePreview var contextItem: ComposeContextItem var voiceMessageRecordingState: VoiceMessageRecordingState - var voiceMessageAllowed: Bool var inProgress = false var disabled = false var useLinkPreviews: Bool = UserDefaults.standard.bool(forKey: DEFAULT_PRIVACY_LINK_PREVIEWS) @@ -43,14 +42,12 @@ struct ComposeState { message: String = "", preview: ComposePreview = .noPreview, contextItem: ComposeContextItem = .noContextItem, - voiceMessageRecordingState: VoiceMessageRecordingState = .noRecording, - voiceMessageAllowed: Bool = true // TODO based on preference + voiceMessageRecordingState: VoiceMessageRecordingState = .noRecording ) { self.message = message self.preview = preview self.contextItem = contextItem self.voiceMessageRecordingState = voiceMessageRecordingState - self.voiceMessageAllowed = voiceMessageAllowed } init(editingItem: ChatItem) { @@ -63,7 +60,6 @@ struct ComposeState { } else { self.voiceMessageRecordingState = .noRecording } - self.voiceMessageAllowed = false } func copy( @@ -125,6 +121,13 @@ struct ComposeState { default: return false } } + + var voicePreview: Bool { + switch preview { + case .voicePreview: return true + default: return false + } + } } func chatItemPreview(chatItem: ChatItem) -> ComposePreview { @@ -148,7 +151,7 @@ func chatItemPreview(chatItem: ChatItem) -> ComposePreview { struct ComposeView: View { @EnvironmentObject var chatModel: ChatModel - let chat: Chat + @ObservedObject var chat: Chat @Binding var composeState: ComposeState @FocusState.Binding var keyboardVisible: Bool @@ -193,6 +196,7 @@ struct ComposeView: View { sendMessage() resetLinkPreview() }, + voiceMessageAllowed: chat.chatInfo.voiceMessageAllowed, startVoiceMessageRecording: { Task { await startVoiceMessageRecording() diff --git a/apps/ios/Shared/Views/Chat/ComposeMessage/SendMessageView.swift b/apps/ios/Shared/Views/Chat/ComposeMessage/SendMessageView.swift index 479155f8bf..d0ea50d490 100644 --- a/apps/ios/Shared/Views/Chat/ComposeMessage/SendMessageView.swift +++ b/apps/ios/Shared/Views/Chat/ComposeMessage/SendMessageView.swift @@ -12,6 +12,7 @@ import SimpleXChat struct SendMessageView: View { @Binding var composeState: ComposeState var sendMessage: () -> Void + var voiceMessageAllowed: Bool = true var startVoiceMessageRecording: (() -> Void)? = nil var finishVoiceMessageRecording: (() -> Void)? = nil @State private var longPressingVMR = false @@ -63,7 +64,7 @@ struct SendMessageView: View { .padding([.bottom, .trailing], 3) } else { let vmrs = composeState.voiceMessageRecordingState - if composeState.voiceMessageAllowed, + if voiceMessageAllowed, composeState.message.isEmpty, !composeState.editing, (composeState.noPreview && vmrs == .noRecording) @@ -90,7 +91,11 @@ struct SendMessageView: View { .resizable() .foregroundColor(.accentColor) } - .disabled(!composeState.sendEnabled || composeState.disabled) + .disabled( + !composeState.sendEnabled || + composeState.disabled || + (!voiceMessageAllowed && composeState.voicePreview) + ) .frame(width: 29, height: 29) .padding([.bottom, .trailing], 4) } diff --git a/apps/ios/Shared/Views/TerminalView.swift b/apps/ios/Shared/Views/TerminalView.swift index 5017391f90..77f91f761d 100644 --- a/apps/ios/Shared/Views/TerminalView.swift +++ b/apps/ios/Shared/Views/TerminalView.swift @@ -17,7 +17,7 @@ struct TerminalView: View { @EnvironmentObject var chatModel: ChatModel @AppStorage(DEFAULT_PERFORM_LA) private var prefPerformLA = false @AppStorage(DEFAULT_DEVELOPER_TOOLS) private var developerTools = false - @State var composeState: ComposeState = ComposeState(voiceMessageAllowed: false) + @State var composeState: ComposeState = ComposeState() @FocusState private var keyboardVisible: Bool @State var authorized = !UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA) @@ -86,6 +86,7 @@ struct TerminalView: View { SendMessageView( composeState: $composeState, sendMessage: sendMessage, + voiceMessageAllowed: false, keyboardVisible: $keyboardVisible ) .padding(.horizontal, 12) @@ -120,7 +121,7 @@ struct TerminalView: View { } } } - composeState = ComposeState(voiceMessageAllowed: false) + composeState = ComposeState() } } diff --git a/apps/ios/SimpleXChat/ChatTypes.swift b/apps/ios/SimpleXChat/ChatTypes.swift index 1d5bd0e15a..ff3bc08f79 100644 --- a/apps/ios/SimpleXChat/ChatTypes.swift +++ b/apps/ios/SimpleXChat/ChatTypes.swift @@ -452,6 +452,10 @@ public func toGroupPreferences(_ fullPreferences: FullGroupPreferences) -> Group public struct GroupPreference: Codable, Equatable { public var enable: GroupFeatureEnabled + public var on: Bool { + enable == .on + } + public init(enable: GroupFeatureEnabled) { self.enable = enable } @@ -608,11 +612,17 @@ public enum ChatInfo: Identifiable, Decodable, NamedChat { } public var contact: Contact? { - get { - switch self { - case let .direct(contact): return contact - default: return nil - } + switch self { + case let .direct(contact): return contact + default: return nil + } + } + + public var voiceMessageAllowed: Bool { + switch self { + case let .direct(contact): return contact.mergedPreferences.voice.enabled.forUser + case let .group(groupInfo): return groupInfo.fullGroupPreferences.voice.on + default: return true } }