Enable sending messages with pills

This commit is contained in:
aringenbach
2022-05-02 13:35:35 +02:00
parent 968c2e34c5
commit 8d65cd8a40
17 changed files with 555 additions and 175 deletions

View File

@@ -51,6 +51,13 @@ class RoomInputToolbarTextView: UITextView {
}
override var text: String! {
didSet {
assert(false)
updateUI()
}
}
override var attributedText: NSAttributedString! {
didSet {
updateUI()
}
@@ -89,7 +96,7 @@ class RoomInputToolbarTextView: UITextView {
override func draw(_ rect: CGRect) {
super.draw(rect)
guard text.isEmpty, let placeholder = placeholder else {
guard attributedText.length == 0, let placeholder = placeholder else {
return
}

View File

@@ -19,6 +19,7 @@
#import "MediaPickerViewController.h"
@class RoomActionsBar;
@class RoomInputToolbarView;
/**
Destination of the message in the composer
@@ -54,6 +55,8 @@ typedef enum : NSUInteger
*/
- (void)roomInputToolbarViewDidOpenActionMenu:(MXKRoomInputToolbarView*)toolbarView;
- (void)roomInputToolbarView:(RoomInputToolbarView *)toolbarView sendAttributedTextMessage:(NSAttributedString *)attributedTextMessage;
@end
/**
@@ -102,6 +105,13 @@ typedef enum : NSUInteger
*/
@property (nonatomic, weak, readonly) UIButton *attachMediaButton;
/**
The current attributed text message in message composer.
*/
@property (nonatomic) NSAttributedString *attributedTextMessage;
@property (nonatomic, readonly) UIFont *textDefaultFont;
/**
Adds a voice message toolbar view to be displayed inside this input toolbar
*/

View File

@@ -77,7 +77,7 @@ static const NSTimeInterval kActionMenuComposerHeightAnimationDuration = .3;
self.isEncryptionEnabled = _isEncryptionEnabled;
[self updateUIWithTextMessage:nil animated:NO];
[self updateUIWithAttributedTextMessage:nil animated:NO];
self.textView.toolbarDelegate = self;
@@ -154,18 +154,41 @@ static const NSTimeInterval kActionMenuComposerHeightAnimationDuration = .3;
- (void)setTextMessage:(NSString *)textMessage
{
[super setTextMessage:textMessage];
self.textView.text = textMessage;
[self updateUIWithTextMessage:textMessage animated:YES];
if (!textMessage)
{
[self setAttributedTextMessage:nil];
}
}
- (void)setAttributedTextMessage:(NSAttributedString *)attributedTextMessage
{
self.textView.attributedText = attributedTextMessage;
[self updateUIWithAttributedTextMessage:attributedTextMessage animated:YES];
[self textViewDidChange:self.textView];
}
- (NSAttributedString *)attributedTextMessage
{
return self.textView.attributedText;
}
- (NSString *)textMessage
{
return self.textView.text;
}
- (UIFont *)textDefaultFont
{
if (self.textView.font)
{
return self.textView.font;
}
else
{
return ThemeService.shared.theme.fonts.body;
}
}
- (void)setIsEncryptionEnabled:(BOOL)isEncryptionEnabled
{
_isEncryptionEnabled = isEncryptionEnabled;
@@ -329,9 +352,10 @@ static const NSTimeInterval kActionMenuComposerHeightAnimationDuration = .3;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
[self updateUIWithTextMessage:newText animated:YES];
NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[newText replaceCharactersInRange:range withString:text];
[self updateUIWithAttributedTextMessage:newText animated:YES];
return YES;
}
@@ -466,15 +490,15 @@ static const NSTimeInterval kActionMenuComposerHeightAnimationDuration = .3;
#pragma mark - Private
- (void)updateUIWithTextMessage:(NSString *)textMessage animated:(BOOL)animated
- (void)updateUIWithAttributedTextMessage:(NSAttributedString *)attributedTextMessage animated:(BOOL)animated
{
self.actionMenuOpened = NO;
[UIView animateWithDuration:(animated ? 0.15f : 0.0f) animations:^{
self.rightInputToolbarButton.alpha = textMessage.length ? 1.0f : 0.0f;
self.rightInputToolbarButton.enabled = textMessage.length;
self.rightInputToolbarButton.alpha = attributedTextMessage.length ? 1.0f : 0.0f;
self.rightInputToolbarButton.enabled = attributedTextMessage.length;
self.voiceMessageToolbarView.alpha = textMessage.length ? 0.0f : 1.0;
self.voiceMessageToolbarView.alpha = attributedTextMessage.length ? 0.0f : 1.0;
}];
}

View File

@@ -0,0 +1,33 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
extension RoomInputToolbarView {
open override func sendCurrentMessage() {
// TODO: trigger auto correct ?
// Send message if any.
if let messageToSend = self.attributedTextMessage, messageToSend.length > 0 {
self.delegate.roomInputToolbarView(self, sendAttributedTextMessage: messageToSend)
}
// Reset message, disable view animation during the update to prevent placeholder distorsion.
UIView.setAnimationsEnabled(false)
self.attributedTextMessage = nil
UIView.setAnimationsEnabled(true)
}
}