Skip to content

Commit

Permalink
Bring back the original native MessageComposerTextField, use it whe…
Browse files Browse the repository at this point in the history
…n the Rich Text Editor one is disabled (advanced settings)

- this basically reverts parts of #1703
  • Loading branch information
stefanceriu committed Apr 15, 2024
1 parent e7af7fb commit 4d411fd
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 29 deletions.
4 changes: 4 additions & 0 deletions ElementX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
1FEC0A4EC6E6DF693C16B32A /* StringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CEBCB9676FCD1D0F13188DD /* StringTests.swift */; };
206F0DBAB6AF042CA1FF2C0D /* SettingsViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D487C1185D658F8B15B8F55 /* SettingsViewModelTests.swift */; };
208C19811613F9A10F8A7B75 /* MediaLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AFCE895ECFFA53FEE64D62B /* MediaLoader.swift */; };
20BB987875F99190A3E28632 /* MessageComposerTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756F24C1913F809A0039FD0 /* MessageComposerTextField.swift */; };
20C16A3F718802B0E4A19C83 /* URLComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76310030C831D4610A705603 /* URLComponentsTests.swift */; };
21813AF91CFC6F3E3896DB53 /* AppLockSetupBiometricsScreenModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10F130DF775CE6BC51A4E392 /* AppLockSetupBiometricsScreenModels.swift */; };
21AFEFB8CEFE56A3811A1F5B /* VoiceMessageCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 283974987DA7EC61D2AB57D9 /* VoiceMessageCacheTests.swift */; };
Expand Down Expand Up @@ -1225,6 +1226,7 @@
170BF6F7923A5C3792442F27 /* CompletionSuggestionModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompletionSuggestionModels.swift; sourceTree = "<group>"; };
1715E3D7F53C0748AA50C91C /* PostHogAnalyticsClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAnalyticsClient.swift; sourceTree = "<group>"; };
1734A445A58ED855B977A0A8 /* TracingConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TracingConfigurationTests.swift; sourceTree = "<group>"; };
1756F24C1913F809A0039FD0 /* MessageComposerTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageComposerTextField.swift; sourceTree = "<group>"; };
18486B87745B1811E7FBD3D2 /* AnalyticsPromptScreenModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsPromptScreenModels.swift; sourceTree = "<group>"; };
184CF8C196BE143AE226628D /* DecorationTimelineItemProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecorationTimelineItemProtocol.swift; sourceTree = "<group>"; };
1877038D1AD3D5A029F8AE2C /* TimelineReadReceiptsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineReadReceiptsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3114,6 +3116,7 @@
0AE449DFBA7CC863EEB2FD2A /* FormattingToolbar.swift */,
2FD0E68C42CA7DDCD4CAD68D /* MentionSuggestionItemView.swift */,
A0A01AECFF54281CF35909A6 /* MessageComposer.swift */,
1756F24C1913F809A0039FD0 /* MessageComposerTextField.swift */,
3E6A9B9DFEE964962C179DE3 /* RoomAttachmentPicker.swift */,
BFC9F57320EC80C7CE34FE4A /* VoiceMessagePreviewComposer.swift */,
D2E61DDB42C0DE429C0955D8 /* VoiceMessageRecordingButton.swift */,
Expand Down Expand Up @@ -6173,6 +6176,7 @@
5D4643E485C179B2F485C519 /* MentionSuggestionItemView.swift in Sources */,
64AB99285DC4437C0DDE9585 /* MenuSheetLabelStyle.swift in Sources */,
858B0A45257174AAFD448EA0 /* MessageComposer.swift in Sources */,
20BB987875F99190A3E28632 /* MessageComposerTextField.swift in Sources */,
C8E0FA0FF2CD6613264FA6B9 /* MessageForwardingScreen.swift in Sources */,
2BBA132149DEBED6624084A8 /* MessageForwardingScreenCoordinator.swift in Sources */,
695825D20A761C678809345D /* MessageForwardingScreenModels.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,24 @@ struct ComposerToolbarViewState: BindableState {
case .previewVoiceMessage:
return true
default:
return !composerEmpty
if ServiceLocator.shared.settings.richTextEditorEnabled {
return !composerEmpty
} else {
return !bindings.composerPlainText.isEmpty
}
}
}

var sendButtonDisabled: Bool {
if case .previewVoiceMessage = composerMode {
return false
}
return composerEmpty

if ServiceLocator.shared.settings.richTextEditorEnabled {
return composerEmpty
} else {
return bindings.composerPlainText.isEmpty
}
}

var isVoiceMessageModeActivated: Bool {
Expand All @@ -113,6 +122,7 @@ struct ComposerToolbarViewState: BindableState {
}

struct ComposerToolbarViewStateBindings {
var composerPlainText = ""
var composerFocused = false
var composerActionsEnabled = false
var composerExpanded = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,20 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
}
case .sendMessage:
guard !state.sendButtonDisabled else { return }

switch state.composerMode {
case .previewVoiceMessage:
actionsSubject.send(.voiceMessage(.send))
default:
let sendHTML = appSettings.richTextEditorEnabled
actionsSubject.send(.sendMessage(plain: wysiwygViewModel.content.markdown,
html: sendHTML ? wysiwygViewModel.content.html : nil,
mode: state.composerMode,
intentionalMentions: wysiwygViewModel
.getMentionsState()
.toIntentionalMentions()))
if ServiceLocator.shared.settings.richTextEditorEnabled {
let sendHTML = appSettings.richTextEditorEnabled
actionsSubject.send(.sendMessage(plain: wysiwygViewModel.content.markdown,
html: sendHTML ? wysiwygViewModel.content.html : nil,
mode: state.composerMode,
intentionalMentions: wysiwygViewModel.getMentionsState().toIntentionalMentions()))
} else {
actionsSubject.send(.sendMessage(plain: context.composerPlainText, html: nil, mode: state.composerMode, intentionalMentions: .empty))
}
}
case .cancelReply:
set(mode: .default)
Expand Down Expand Up @@ -260,12 +262,12 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
}

private func set(text: String) {
wysiwygViewModel.textView.flushPills()

if appSettings.richTextEditorEnabled {
if ServiceLocator.shared.settings.richTextEditorEnabled {
wysiwygViewModel.textView.flushPills()
wysiwygViewModel.setHtmlContent(text)
} else {
wysiwygViewModel.setMarkdownContent(text)
state.bindings.composerPlainText = text
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ struct ComposerToolbar: View {
}

private var messageComposer: some View {
MessageComposer(composerView: composerView,
MessageComposer(plainText: $context.composerPlainText,
composerView: composerView,
mode: context.viewState.composerMode,
showResizeGrabber: context.viewState.bindings.composerActionsEnabled,
isExpanded: $context.composerExpanded) {
Expand Down
43 changes: 29 additions & 14 deletions ElementX/Sources/Screens/ComposerToolbar/View/MessageComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typealias EnterKeyHandler = () -> Void
typealias PasteHandler = (NSItemProvider) -> Void

struct MessageComposer: View {
@Binding var plainText: String
let composerView: WysiwygComposerView
let mode: RoomScreenComposerMode
let showResizeGrabber: Bool
Expand All @@ -32,6 +33,7 @@ struct MessageComposer: View {
let editCancellationAction: () -> Void
let onAppearAction: () -> Void

@State private var isMultiline = false
@State private var composerTranslation: CGFloat = 0
private let composerShape = RoundedRectangle(cornerRadius: 21, style: .circular)

Expand Down Expand Up @@ -66,19 +68,31 @@ struct MessageComposer: View {
private var mainContent: some View {
VStack(alignment: .leading, spacing: -6) {
header
Color.clear
.overlay(alignment: .top) {
composerView
.clipped()
.readFrame($composerFrame)
}
.frame(minHeight: ComposerConstant.minHeight, maxHeight: max(composerHeight, composerFrame.height),
alignment: .top)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
.onAppear {
onAppearAction()
}

if ServiceLocator.shared.settings.richTextEditorEnabled {
Color.clear
.overlay(alignment: .top) {
composerView
.clipped()
.readFrame($composerFrame)
}
.frame(minHeight: ComposerConstant.minHeight, maxHeight: max(composerHeight, composerFrame.height),
alignment: .top)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
.onAppear {
onAppearAction()
}
} else {
MessageComposerTextField(placeholder: L10n.richTextEditorComposerPlaceholder,
text: $plainText,
isMultiline: $isMultiline,
maxHeight: 300,
enterKeyHandler: sendAction,
pasteHandler: pasteAction)
.tint(.compound.iconAccentTertiary)
.padding(.vertical, 10)
}
}
}

Expand Down Expand Up @@ -212,7 +226,8 @@ struct MessageComposer_Previews: PreviewProvider, TestablePreview {
keyCommands: nil,
pasteHandler: nil)

return MessageComposer(composerView: composerView,
return MessageComposer(plainText: .constant(content),
composerView: composerView,
mode: mode,
showResizeGrabber: false,
isExpanded: .constant(false),
Expand Down
Loading

0 comments on commit 4d411fd

Please sign in to comment.