Skip to content

Commit

Permalink
Fixes #4976 - Replaced HPGrowingTextView with GrowingTextView. Reduce…
Browse files Browse the repository at this point in the history
…d inheritance chain.
  • Loading branch information
stefanceriu committed Oct 11, 2021
1 parent 60bc34d commit 5d81c42
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ abstract_target 'RiotPods' do
# Remove warnings from "bad" pods
pod 'OLMKit', :inhibit_warnings => true
pod 'zxcvbn-ios', :inhibit_warnings => true
pod 'HPGrowingTextView', :inhibit_warnings => true

# Tools
pod 'SwiftGen', '~> 6.3'
Expand All @@ -73,6 +72,7 @@ abstract_target 'RiotPods' do
pod 'SideMenu', '~> 6.5'
pod 'DSWaveformImage', '~> 6.1.1'
pod 'ffmpeg-kit-ios-audio', '~> 4.5'
pod 'GrowingTextView', '~> 0.7.2'

pod 'FLEX', '~> 4.5.0', :configurations => ['Debug']

Expand Down
41 changes: 0 additions & 41 deletions Riot/Modules/Room/Views/InputToolbar/KeyboardGrowingTextView.m

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright 2021 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 GrowingTextView

class RoomInputToolbarTextView: GrowingTextView {

override var keyCommands: [UIKeyCommand]? {
return [UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(keyCommandSelector(_:)))]
}

@objc private func keyCommandSelector(_ keyCommand: UIKeyCommand) {
guard keyCommand.input == "\r", let delegate = (self.delegate as? RoomInputToolbarView) else {
return
}

delegate.onTouchUp(inside: delegate.rightInputToolbarButton)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef enum : NSUInteger
`RoomInputToolbarView` instance is a view used to handle all kinds of available inputs
for a room (message composer, attachments selection...).
*/
@interface RoomInputToolbarView : MXKRoomInputToolbarViewWithHPGrowingText
@interface RoomInputToolbarView : MXKRoomInputToolbarView

/**
The delegate notified when inputs are ready.
Expand Down
96 changes: 60 additions & 36 deletions Riot/Modules/Room/Views/InputToolbar/RoomInputToolbarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#import "WidgetManager.h"
#import "IntegrationManagerViewController.h"

@import GrowingTextView;

const double kContextBarHeight = 24;
const NSTimeInterval kSendModeAnimationDuration = .15;
const NSTimeInterval kActionMenuAttachButtonAnimationDuration = .4;
Expand All @@ -36,12 +38,15 @@
const NSTimeInterval kActionMenuComposerHeightAnimationDuration = .3;
const CGFloat kComposerContainerTrailingPadding = 12;

@interface RoomInputToolbarView()
@interface RoomInputToolbarView() <GrowingTextViewDelegate>
{
// The intermediate action sheet
UIAlertController *actionSheet;
}

@property (nonatomic, weak) IBOutlet RoomInputToolbarTextView *textView;
@property (nonatomic, assign) CGFloat expandedMainToolbarHeight;

@end

@implementation RoomInputToolbarView
Expand Down Expand Up @@ -102,21 +107,21 @@ -(void)customizeViewRendering
self.backgroundColor = [UIColor clearColor];

// Custom the growingTextView display
growingTextView.layer.cornerRadius = 0;
growingTextView.layer.borderWidth = 0;
growingTextView.backgroundColor = [UIColor clearColor];
growingTextView.font = [UIFont systemFontOfSize:15];
growingTextView.textColor = ThemeService.shared.theme.textPrimaryColor;
growingTextView.tintColor = ThemeService.shared.theme.tintColor;
growingTextView.placeholderColor = ThemeService.shared.theme.textTertiaryColor;
growingTextView.internalTextView.showsVerticalScrollIndicator = NO;
self.textView.layer.cornerRadius = 0;
self.textView.layer.borderWidth = 0;
self.textView.backgroundColor = [UIColor clearColor];

self.textView.font = [UIFont systemFontOfSize:15];
self.textView.textColor = ThemeService.shared.theme.textPrimaryColor;
self.textView.tintColor = ThemeService.shared.theme.tintColor;
self.textView.placeholderColor = ThemeService.shared.theme.textTertiaryColor;
self.textView.showsVerticalScrollIndicator = NO;

growingTextView.internalTextView.keyboardAppearance = ThemeService.shared.theme.keyboardAppearance;
if (growingTextView.isFirstResponder)
self.textView.keyboardAppearance = ThemeService.shared.theme.keyboardAppearance;
if (self.textView.isFirstResponder)
{
[growingTextView resignFirstResponder];
[growingTextView becomeFirstResponder];
[self.textView resignFirstResponder];
[self.textView becomeFirstResponder];
}

self.attachMediaButton.accessibilityLabel = [VectorL10n roomAccessibilityUpload];
Expand Down Expand Up @@ -148,8 +153,15 @@ -(void)customizeViewRendering

- (void)setTextMessage:(NSString *)textMessage
{
[self updateUIWithTextMessage:textMessage animated:YES];
[super setTextMessage:textMessage];

self.textView.text = textMessage;
[self updateUIWithTextMessage:textMessage animated:YES];
}

- (NSString *)textMessage
{
return self.textView.text;
}

- (void)setIsEncryptionEnabled:(BOOL)isEncryptionEnabled
Expand Down Expand Up @@ -184,7 +196,7 @@ - (void)updateToolbarButtonLabelWithPreviousMode:(RoomInputToolbarViewSendMode)p

self.inputContextViewHeightConstraint.constant = kContextBarHeight;
updatedHeight += kContextBarHeight;
self->growingTextView.maxHeight -= kContextBarHeight;
self.textView.maxHeight -= kContextBarHeight;
break;
case RoomInputToolbarViewSendModeEdit:
buttonImage = [UIImage imageNamed:@"save_icon"];
Expand All @@ -193,15 +205,15 @@ - (void)updateToolbarButtonLabelWithPreviousMode:(RoomInputToolbarViewSendMode)p

self.inputContextViewHeightConstraint.constant = kContextBarHeight;
updatedHeight += kContextBarHeight;
self->growingTextView.maxHeight -= kContextBarHeight;
self.textView.maxHeight -= kContextBarHeight;
break;
default:
buttonImage = [UIImage imageNamed:@"send_icon"];

if (previousMode != _sendMode)
{
updatedHeight -= kContextBarHeight;
self->growingTextView.maxHeight += kContextBarHeight;
self.textView.maxHeight += kContextBarHeight;
}
self.inputContextViewHeightConstraint.constant = 0;
break;
Expand All @@ -211,7 +223,7 @@ - (void)updateToolbarButtonLabelWithPreviousMode:(RoomInputToolbarViewSendMode)p

if (self.maxHeight && updatedHeight > self.maxHeight)
{
growingTextView.maxHeight -= updatedHeight - self.maxHeight;
self.textView.maxHeight -= updatedHeight - self.maxHeight;
updatedHeight = self.maxHeight;
}

Expand Down Expand Up @@ -290,6 +302,12 @@ - (void)updatePlaceholder
self.placeholder = placeholder;
}

- (void)setPlaceholder:(NSString *)inPlaceholder
{
[super setPlaceholder:inPlaceholder];
self.textView.placeholder = inPlaceholder;
}

#pragma mark - Actions

- (IBAction)cancelAction:(id)sender
Expand All @@ -300,45 +318,48 @@ - (IBAction)cancelAction:(id)sender
}
}

#pragma mark - HPGrowingTextView delegate
#pragma mark - GrowingTextViewDelegate

- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *newText = [growingTextView.text stringByReplacingCharactersInRange:range withString:text];
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
[self updateUIWithTextMessage:newText animated:YES];

return YES;
}

- (void)growingTextViewDidChange:(HPGrowingTextView *)hpGrowingTextView
- (void)textViewDidChange:(UITextView *)textView
{
// Clean the carriage return added on return press
if ([self.textMessage isEqualToString:@"\n"])
{
self.textMessage = nil;
}

[super growingTextViewDidChange:hpGrowingTextView];
if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:isTyping:)])
{
[self.delegate roomInputToolbarView:self isTyping:(self.textMessage.length > 0 ? YES : NO)];
}
}

- (void)growingTextView:(HPGrowingTextView *)hpGrowingTextView willChangeHeight:(float)height
- (void)textViewDidChangeHeight:(GrowingTextView *)textView height:(CGFloat)height
{
// Update height of the main toolbar (message composer)
CGFloat updatedHeight = height + (self.messageComposerContainerTopConstraint.constant + self.messageComposerContainerBottomConstraint.constant) + self.inputContextViewHeightConstraint.constant;

if (self.maxHeight && updatedHeight > self.maxHeight)
{
hpGrowingTextView.maxHeight -= updatedHeight - self.maxHeight;
textView.maxHeight -= updatedHeight - self.maxHeight;
updatedHeight = self.maxHeight;
}

if (updatedHeight < self.mainToolbarMinHeightConstraint.constant)
{
updatedHeight = self.mainToolbarMinHeightConstraint.constant;
}

self.mainToolbarHeightConstraint.constant = updatedHeight;

// Update toolbar superview
if ([self.delegate respondsToSelector:@selector(roomInputToolbarView:heightDidChanged:completion:)])
{
Expand Down Expand Up @@ -377,12 +398,12 @@ - (void)setActionMenuOpened:(BOOL)actionMenuOpened
{
_actionMenuOpened = actionMenuOpened;

if (self->growingTextView.internalTextView.selectedRange.length > 0)
if (self.textView.selectedRange.length > 0)
{
NSRange range = self->growingTextView.internalTextView.selectedRange;
NSRange range = self.textView.selectedRange;
range.location = range.location + range.length;
range.length = 0;
self->growingTextView.internalTextView.selectedRange = range;
self.textView.selectedRange = range;
}

if (_actionMenuOpened) {
Expand All @@ -402,18 +423,19 @@ - (void)setActionMenuOpened:(BOOL)actionMenuOpened

[UIView animateWithDuration:kActionMenuContentAlphaAnimationDuration delay:_actionMenuOpened ? 0 : .1 options:UIViewAnimationOptionCurveEaseIn animations:^{
self->messageComposerContainer.alpha = actionMenuOpened ? 0 : 1;
self.rightInputToolbarButton.alpha = self->growingTextView.text.length == 0 || actionMenuOpened ? 0 : 1;
self.voiceMessageToolbarView.alpha = self->growingTextView.text.length > 0 || actionMenuOpened ? 0 : 1;
self.rightInputToolbarButton.alpha = self.textView.text.length == 0 || actionMenuOpened ? 0 : 1;
self.voiceMessageToolbarView.alpha = self.textView.text.length > 0 || actionMenuOpened ? 0 : 1;
} completion:nil];

[UIView animateWithDuration:kActionMenuComposerHeightAnimationDuration animations:^{
if (actionMenuOpened)
{
self.expandedMainToolbarHeight = self.mainToolbarHeightConstraint.constant;
self.mainToolbarHeightConstraint.constant = self.mainToolbarMinHeightConstraint.constant;
}
else
{
[self->growingTextView refreshHeight];
self.mainToolbarHeightConstraint.constant = self.expandedMainToolbarHeight;
}
[self layoutIfNeeded];
[self.delegate roomInputToolbarView:self heightDidChanged:self.mainToolbarHeightConstraint.constant completion:nil];
Expand All @@ -438,6 +460,8 @@ - (void)updateUIWithTextMessage:(NSString *)textMessage animated:(BOOL)animated

[UIView animateWithDuration:(animated ? 0.15f : 0.0f) animations:^{
self.rightInputToolbarButton.alpha = textMessage.length ? 1.0f : 0.0f;
self.rightInputToolbarButton.enabled = textMessage.length;

self.voiceMessageToolbarView.alpha = textMessage.length ? 0.0f : 1.0;
}];
}
Expand Down
11 changes: 7 additions & 4 deletions Riot/Modules/Room/Views/InputToolbar/RoomInputToolbarView.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -77,10 +77,13 @@
<constraint firstItem="48y-kn-7b5" firstAttribute="centerY" secondItem="dVr-ZM-kkX" secondAttribute="centerY" id="z5v-Vy-6tc"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="wgb-ON-N29" customClass="KeyboardGrowingTextView">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="wgb-ON-N29" customClass="RoomInputToolbarTextView" customModule="Riot" customModuleProvider="target">
<rect key="frame" x="5" y="33" width="474" height="4"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<accessibility key="accessibilityConfiguration" identifier="GrowingTextView"/>
<connections>
<outlet property="delegate" destination="iN0-l3-epB" id="CV3-Fk-GFg"/>
</connections>
</view>
</subviews>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down Expand Up @@ -138,7 +141,6 @@
<connections>
<outlet property="actionsBar" destination="ESv-9w-KJF" id="h7H-vz-yzO"/>
<outlet property="attachMediaButton" destination="Hga-l8-Wua" id="Osr-ek-c91"/>
<outlet property="growingTextView" destination="wgb-ON-N29" id="nwF-uV-Ng9"/>
<outlet property="inputContextButton" destination="48y-kn-7b5" id="yRn-1S-96w"/>
<outlet property="inputContextImageView" destination="PZ4-0Y-TmL" id="PMS-K7-aMr"/>
<outlet property="inputContextLabel" destination="dVr-ZM-kkX" id="ve6-gY-cV9"/>
Expand All @@ -152,6 +154,7 @@
<outlet property="messageComposerContainerTopConstraint" destination="WyZ-3i-OHi" id="OcO-1f-bNA"/>
<outlet property="messageComposerContainerTrailingConstraint" destination="hXO-cY-Jgz" id="0m7-AB-90i"/>
<outlet property="rightInputToolbarButton" destination="G8Z-CM-tGs" id="NCk-5m-aNF"/>
<outlet property="textView" destination="wgb-ON-N29" id="ySl-IB-xvn"/>
</connections>
<point key="canvasLocation" x="137.59999999999999" y="151.12443778110946"/>
</view>
Expand Down
1 change: 1 addition & 0 deletions Riot/SupportingFiles/Riot-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
#import "BuildInfo.h"
#import "RoomMemberDetailsViewController.h"
#import "Tools.h"
#import "RoomInputToolbarView.h"
1 change: 1 addition & 0 deletions changelog.d/4976.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replaced deprecated HPGrowingTextView with GrowingTextView.

0 comments on commit 5d81c42

Please sign in to comment.