QuoteView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "QuoteView.h"
  21. #import "Colors.h"
  22. #import "Contact.h"
  23. #import "UserSettings.h"
  24. #import "MyIdentityStore.h"
  25. #import "UILabel+Markup.h"
  26. #import "UIImage+ColoredImage.h"
  27. #import "TextStyleUtils.h"
  28. #import "TTTAttributedLabel.h"
  29. #import "BundleUtil.h"
  30. #import "NSString+Hex.h"
  31. #import "BallotMessage.h"
  32. #import "LocationMessage.h"
  33. #import "UserSettings.h"
  34. #define QUOTE_FONT_SIZE_FACTOR 0.8
  35. static CGFloat quoteTextSpacing = 8.0f;
  36. static CGFloat quoteBarWidth = 2.0f;
  37. static CGFloat quoteBarSpacing = 8.0f;
  38. static CGFloat cancelButtonSpacing = 5.0f;
  39. static CGFloat cancelButtonSize = 30.0f;
  40. static CGFloat quoteImageSize = 60.0;
  41. static CGFloat quoteImageSpacing = 8.0;
  42. static CGFloat quoteIconSpacing = 8.0;
  43. @implementation QuoteView {
  44. UIView *quoteBar;
  45. TTTAttributedLabel *quoteLabel;
  46. UIView *borderView;
  47. UIButton *cancelButton;
  48. UIImageView *quoteImage;
  49. UIImageView *quoteIcon;
  50. NSString *quotedText;
  51. Contact *quotedContact;
  52. }
  53. - (instancetype)init {
  54. self = [super init];
  55. if (self) {
  56. quoteBar = [[UIView alloc] init];
  57. [self addSubview:quoteBar];
  58. quoteLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
  59. quoteLabel.numberOfLines = 4;
  60. quoteLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  61. [self addSubview:quoteLabel];
  62. cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  63. [cancelButton addTarget:self action:@selector(cancelQuote:) forControlEvents:UIControlEventTouchUpInside];
  64. cancelButton.accessibilityLabel = [BundleUtil localizedStringForKey:@"cancel"];
  65. [self addSubview:cancelButton];
  66. quoteImage = [[UIImageView alloc] initWithFrame:self.bounds];
  67. quoteImage.contentMode = UIViewContentModeScaleAspectFill;
  68. quoteImage.clipsToBounds = true;
  69. [self addSubview:quoteImage];
  70. quoteIcon = [[UIImageView alloc] initWithFrame:self.bounds];
  71. quoteIcon.contentMode = UIViewContentModeScaleAspectFill;
  72. quoteIcon.clipsToBounds = true;
  73. [self addSubview:quoteIcon];
  74. _buttonWidthHint = cancelButtonSize + cancelButtonSpacing;
  75. [self setupColors];
  76. [self setNeedsLayout];
  77. }
  78. return self;
  79. }
  80. - (void)layoutSubviews {
  81. [super layoutSubviews];
  82. quoteBar.frame = CGRectMake(0, 0, quoteBarWidth, self.frame.size.height);
  83. if (!quoteImage.hidden) {
  84. quoteLabel.frame = CGRectMake(quoteBarWidth + quoteTextSpacing, 0, self.frame.size.width - quoteBarWidth - quoteTextSpacing - _buttonWidthHint - quoteImageSize - quoteImageSpacing, self.frame.size.height);
  85. quoteImage.frame = CGRectMake(quoteLabel.frame.origin.x + quoteLabel.frame.size.width + quoteImageSpacing, quoteBarSpacing, quoteImageSize, quoteImageSize);
  86. cancelButton.frame = CGRectMake(1 + quoteImage.frame.origin.x + quoteImage.frame.size.width + (_buttonWidthHint - cancelButtonSize) / 2, (self.frame.size.height - cancelButtonSize) / 2, cancelButtonSize, cancelButtonSize);
  87. } else {
  88. if (!quoteIcon.hidden) {
  89. quoteLabel.frame = CGRectMake(quoteBarWidth + quoteTextSpacing + [quoteLabel.font pointSize] + quoteIconSpacing, 0, self.frame.size.width - quoteBarWidth - quoteTextSpacing - _buttonWidthHint - [quoteLabel.font pointSize] - quoteIconSpacing, self.frame.size.height);
  90. quoteIcon.frame = CGRectMake(quoteBarWidth + quoteTextSpacing, (self.frame.size.height / 2) - ([quoteLabel.font pointSize] / 2), [quoteLabel.font pointSize], [quoteLabel.font pointSize]);
  91. cancelButton.frame = CGRectMake(1 + quoteLabel.frame.origin.x + quoteLabel.frame.size.width + (_buttonWidthHint - cancelButtonSize) / 2, (self.frame.size.height - cancelButtonSize) / 2, cancelButtonSize, cancelButtonSize);
  92. } else {
  93. quoteLabel.frame = CGRectMake(quoteBarWidth + quoteTextSpacing, 0, self.frame.size.width - quoteBarWidth - quoteTextSpacing - _buttonWidthHint, self.frame.size.height);
  94. cancelButton.frame = CGRectMake(1 + quoteLabel.frame.origin.x + quoteLabel.frame.size.width + (_buttonWidthHint - cancelButtonSize) / 2, (self.frame.size.height - cancelButtonSize) / 2, cancelButtonSize, cancelButtonSize);
  95. }
  96. }
  97. }
  98. - (void)setupColors {
  99. quoteBar.backgroundColor = [Colors quoteBar];
  100. [cancelButton setImage:[UIImage imageNamed:@"Close" inColor:[Colors backgroundInverted]] forState:UIControlStateNormal];
  101. quoteLabel.attributedText = [self makeQuoteAttributedString];
  102. }
  103. - (CGSize)sizeThatFits:(CGSize)size {
  104. // Calculate the size that we actually need, which may be less than what we have available
  105. UILabel *dummyLabel = [[UILabel alloc] init];
  106. dummyLabel.numberOfLines = 4;
  107. dummyLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  108. dummyLabel.attributedText = [self makeQuoteAttributedString];
  109. CGFloat imageOrIconWidth = 0.0;
  110. if (!quoteImage.hidden) {
  111. imageOrIconWidth = quoteImageSpacing + quoteImageSize;
  112. }
  113. if (!quoteIcon.hidden) {
  114. imageOrIconWidth = quoteIconSpacing;
  115. }
  116. CGFloat reservedWidth = quoteBarWidth + quoteTextSpacing + _buttonWidthHint + imageOrIconWidth;
  117. CGSize availableSizeForLabel = CGSizeMake(size.width - reservedWidth, size.height);
  118. CGSize labelSize = [dummyLabel sizeThatFits:availableSizeForLabel];
  119. CGSize quoteSize = CGSizeMake(labelSize.width + reservedWidth, labelSize.height);
  120. if (!quoteImage.hidden && quoteSize.height < quoteImageSize + quoteBarSpacing) {
  121. quoteSize.height = quoteImageSize + quoteBarSpacing;
  122. }
  123. return quoteSize;
  124. }
  125. - (void)setQuotedText:(NSString *)newQuotedText quotedContact:(Contact *)newQuotedContact {
  126. quotedText = newQuotedText;
  127. quotedContact = newQuotedContact;
  128. quoteLabel.attributedText = [self makeQuoteAttributedString];
  129. quoteImage.hidden = true;
  130. quoteIcon.hidden = true;
  131. [self setNeedsLayout];
  132. }
  133. - (void)setQuotedMessage:(BaseMessage *)quotedMessage {
  134. _quotedMessage = quotedMessage;
  135. quotedText = _quotedMessage.quotePreviewText;
  136. Contact *sender;
  137. if (_quotedMessage.isOwn.boolValue) {
  138. sender = nil;
  139. } else if (_quotedMessage.sender != nil) {
  140. sender = _quotedMessage.sender;
  141. } else {
  142. sender = _quotedMessage.conversation.contact;
  143. }
  144. quotedContact = sender;
  145. quoteLabel.attributedText = [self makeQuoteAttributedString];
  146. quoteImage.hidden = true;
  147. quoteImage.image = nil;
  148. quoteIcon.hidden = true;
  149. quoteIcon.image = nil;
  150. if ([quotedMessage isKindOfClass:[ImageMessage class]]) {
  151. if (((ImageMessage *) quotedMessage).thumbnail != nil) {
  152. quoteImage.hidden = false;
  153. quoteImage.image = ((ImageMessage *)quotedMessage).thumbnail.uiImage;
  154. }
  155. }
  156. else if ([quotedMessage isKindOfClass:[VideoMessage class]]) {
  157. if (((VideoMessage *) quotedMessage).thumbnail != nil) {
  158. quoteImage.hidden = false;
  159. quoteImage.image = ((VideoMessage *)quotedMessage).thumbnail.uiImage;
  160. }
  161. }
  162. else if ([quotedMessage isKindOfClass:[FileMessage class]]) {
  163. if (((FileMessage *) quotedMessage).thumbnail != nil) {
  164. quoteImage.hidden = false;
  165. quoteImage.image = ((FileMessage *)quotedMessage).thumbnail.uiImage;
  166. }
  167. }
  168. else if ([quotedMessage isKindOfClass:[AudioMessage class]]) {
  169. quoteIcon.hidden = false;
  170. quoteIcon.image = [[BundleUtil imageNamed:@"ActionMicrophone"] imageWithTint:[Colors fontQuoteText]];
  171. }
  172. else if ([quotedMessage isKindOfClass:[BallotMessage class]]) {
  173. quoteIcon.hidden = false;
  174. quoteIcon.image = [[BundleUtil imageNamed:@"ActionBallot"] imageWithTint:[Colors fontQuoteText]];
  175. }
  176. else if ([quotedMessage isKindOfClass:[LocationMessage class]]) {
  177. quoteIcon.hidden = false;
  178. quoteIcon.image = [[BundleUtil imageNamed:@"CurrentLocation"] imageWithTint:[Colors fontQuoteText]];
  179. }
  180. [self setNeedsLayout];
  181. }
  182. - (void)cancelQuote:(id)sender {
  183. [self.delegate quoteCancelled];
  184. }
  185. - (NSAttributedString*)makeQuoteAttributedString {
  186. if (quotedText == nil)
  187. return nil;
  188. NSMutableAttributedString *quoteString = [[NSMutableAttributedString alloc] init];
  189. // Resolve identity to name
  190. NSString *identityNewline;
  191. if (quotedContact == nil) {
  192. identityNewline = [[BundleUtil localizedStringForKey:@"me"] stringByAppendingString:@"\n"];
  193. } else {
  194. identityNewline = [quotedContact.displayName stringByAppendingString:@"\n"];
  195. }
  196. [quoteString appendAttributedString:[[NSAttributedString alloc] initWithString:identityNewline attributes:@{
  197. NSForegroundColorAttributeName: [Colors fontQuoteId],
  198. NSFontAttributeName: [QuoteView quoteIdentityFont],
  199. @"ZSWTappableLabelTappableRegionAttributeName": @YES,
  200. @"NSTextCheckingResult": @"searchQuote"
  201. }]];
  202. NSAttributedString *quotedTextAttr = [[NSMutableAttributedString alloc] initWithString:quotedText attributes:@{
  203. NSForegroundColorAttributeName: [Colors fontQuoteText],
  204. NSFontAttributeName: [QuoteView quoteFont],
  205. @"ZSWTappableLabelTappableRegionAttributeName": @YES,
  206. @"NSTextCheckingResult": @"searchQuote"
  207. }];
  208. NSAttributedString *quotedTextAttrMarkup = [quoteLabel applyMarkupFor:quotedTextAttr];
  209. [quoteString appendAttributedString:quotedTextAttrMarkup];
  210. NSAttributedString *styledString = [TextStyleUtils makeMentionsAttributedStringForAttributedString:quoteString textFont:[QuoteView quoteFont] atColor:[[Colors fontNormal] colorWithAlphaComponent:0.4] messageInfo:TextStyleUtilsMessageInfoReceivedMessage application:[UIApplication sharedApplication]];
  211. return styledString;
  212. }
  213. - (NSString *)makeQuoteWithReply:(NSString *)reply {
  214. if ([[UserSettings sharedUserSettings] quoteV2Active]) {
  215. NSMutableString *quoteString = [[NSMutableString alloc] initWithString:@"> quote #"];
  216. [quoteString appendString:[NSString stringWithHexData:_quotedMessage.id]];
  217. [quoteString appendString:@"\n\n"];
  218. [quoteString appendString:reply];
  219. return quoteString;
  220. } else {
  221. NSMutableString *quoteString = [[NSMutableString alloc] initWithString:@"> "];
  222. if (quotedContact == nil) {
  223. [quoteString appendString:[MyIdentityStore sharedMyIdentityStore].identity];
  224. } else {
  225. [quoteString appendString:quotedContact.identity];
  226. }
  227. [quoteString appendString:@": "];
  228. NSArray *lines = [quotedText componentsSeparatedByString:@"\n"];
  229. int i = 0;
  230. for (NSString *line in lines) {
  231. if (i > 0) {
  232. [quoteString appendString:@"\n> "];
  233. }
  234. [quoteString appendString:line];
  235. i++;
  236. }
  237. [quoteString appendString:@"\n"];
  238. [quoteString appendString:reply];
  239. return quoteString;
  240. }
  241. }
  242. + (UIFont *)quoteFont {
  243. return [UIFont systemFontOfSize:[UserSettings sharedUserSettings].chatFontSize * QUOTE_FONT_SIZE_FACTOR];
  244. }
  245. + (UIFont *)quoteIdentityFont {
  246. return [UIFont boldSystemFontOfSize:[UserSettings sharedUserSettings].chatFontSize * QUOTE_FONT_SIZE_FACTOR];
  247. }
  248. @end