Chat: Handle tap on icon in case of unsent messages notification.

This commit is contained in:
giomfo
2016-03-07 13:56:52 +01:00
parent 536f63cdaf
commit 41987f1575
5 changed files with 175 additions and 56 deletions
@@ -39,43 +39,63 @@
self.separatorView.backgroundColor = kVectorColorLightGrey;
self.messageLabel.textColor = kVectorTextColorGray;
// Adjust text view
// Remove the container inset: this operation impacts only the vertical margin.
// Reset textContainer.lineFragmentPadding to remove horizontal margin.
self.messageTextView.textContainerInset = UIEdgeInsetsZero;
self.messageTextView.textContainer.lineFragmentPadding = 0;
}
- (void)displayUnsentMessagesNotification:(NSAttributedString*)labelText onLabelTapGesture:(void (^)(void))onLabelTapGesture
- (void)displayUnsentMessagesNotificationWithResendLink:(void (^)(void))onResendLinkPressed andIconTapGesture:(void (^)(void))onIconTapGesture
{
[self reset];
if (labelText.length)
NSString *notification = NSLocalizedStringFromTable(@"room_unsent_messages_notification", @"Vector", nil);
if (onResendLinkPressed)
{
self.iconImageView.image = [UIImage imageNamed:@"error"];
self.messageLabel.attributedText = labelText;
NSString *resendLink = NSLocalizedStringFromTable(@"room_prompt_resend", @"Vector", nil);
NSMutableAttributedString *tappableNotif = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@", notification, resendLink]];
objc_setAssociatedObject(self.messageTextView, "onResendLinkPressed", [onResendLinkPressed copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSRange range = NSMakeRange(notification.length + 1, resendLink.length);
[tappableNotif addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
[tappableNotif addAttribute:NSLinkAttributeName value:@"onResendLink" range:range];
NSRange wholeString = NSMakeRange(0, tappableNotif.length);
[tappableNotif addAttribute:NSForegroundColorAttributeName value:kVectorTextColorRed range:wholeString];
[tappableNotif addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:wholeString];
self.messageTextView.attributedText = tappableNotif;
self.messageTextView.tintColor = kVectorTextColorRed;
self.messageTextView.hidden = NO;
}
else
{
self.messageLabel.text = notification;
self.messageLabel.textColor = kVectorTextColorRed;
self.iconImageView.hidden = NO;
self.messageLabel.hidden = NO;
if (onLabelTapGesture)
{
objc_setAssociatedObject(self.messageLabel, "onLabelTapGesture", [onLabelTapGesture copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Listen to label tap
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLabelTap:)];
[tapGesture setNumberOfTouchesRequired:1];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture setDelegate:self];
[self.messageLabel addGestureRecognizer:tapGesture];
self.messageLabel.userInteractionEnabled = YES;
}
}
}
- (void)onLabelTap:(UITapGestureRecognizer*)sender
{
void (^onLabelTapGesture)(void) = objc_getAssociatedObject(self.messageLabel, "onLabelTapGesture");
if (onLabelTapGesture)
self.iconImageView.image = [UIImage imageNamed:@"error"];
self.iconImageView.hidden = NO;
if (onIconTapGesture)
{
onLabelTapGesture ();
objc_setAssociatedObject(self.iconImageView, "onIconTapGesture", [onIconTapGesture copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Listen to icon tap
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onIconTap:)];
[tapGesture setNumberOfTouchesRequired:1];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture setDelegate:self];
[self.iconImageView addGestureRecognizer:tapGesture];
self.iconImageView.userInteractionEnabled = YES;
}
}
- (void)displayNetworkErrorNotification:(NSString*)labelText
@@ -112,16 +132,48 @@
self.iconImageView.hidden = YES;
self.messageLabel.hidden = YES;
[self.messageTextView resignFirstResponder];
self.messageTextView.hidden = YES;
self.messageLabel.textColor = kVectorTextColorGray;
// Remove all gesture recognizer
while (self.messageLabel.gestureRecognizers.count)
// Remove all gesture recognizers
while (self.iconImageView.gestureRecognizers.count)
{
[self.messageLabel removeGestureRecognizer:self.messageLabel.gestureRecognizers[0]];
[self.iconImageView removeGestureRecognizer:self.iconImageView.gestureRecognizers[0]];
}
self.messageLabel.userInteractionEnabled = NO;
self.iconImageView.userInteractionEnabled = NO;
objc_removeAssociatedObjects(self.messageLabel);
objc_removeAssociatedObjects(self.iconImageView);
objc_removeAssociatedObjects(self.messageTextView);
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([[URL absoluteString] isEqualToString:@"onResendLink"])
{
void (^onResendLinkPressed)(void) = objc_getAssociatedObject(self.messageTextView, "onResendLinkPressed");
if (onResendLinkPressed)
{
onResendLinkPressed ();
}
return NO;
}
return YES;
}
#pragma mark - UIGestureRecognizerDelegate
- (void)onIconTap:(UITapGestureRecognizer*)sender
{
void (^onIconTapGesture)(void) = objc_getAssociatedObject(self.iconImageView, "onIconTapGesture");
if (onIconTapGesture)
{
onIconTapGesture ();
}
}
@end