ChatLocationMessageCell.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-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 "ChatLocationMessageCell.h"
  21. #import "LocationMessage.h"
  22. #import "ChatViewController.h"
  23. #import "ChatDefines.h"
  24. #import "UserSettings.h"
  25. #import "UIImage+ColoredImage.h"
  26. #import "ActivityUtil.h"
  27. #ifdef DEBUG
  28. static const DDLogLevel ddLogLevel = DDLogLevelInfo;
  29. #else
  30. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  31. #endif
  32. @implementation ChatLocationMessageCell {
  33. UIImageView *pinView;
  34. UILabel *geocodeLabel;
  35. UIActivityIndicatorView *activityIndicator;
  36. }
  37. + (CGFloat)heightForMessage:(BaseMessage*)message forTableWidth:(CGFloat)tableWidth {
  38. LocationMessage *locationMessage = (LocationMessage*)message;
  39. NSString *text = [ChatLocationMessageCell displayTextForLocationMessage:locationMessage];
  40. CGSize size = [text boundingRectWithSize:CGSizeMake([ChatMessageCell maxContentWidthForTableWidth:tableWidth] - 25, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [ChatMessageCell textFont]} context:nil].size;
  41. size.height = ceilf(size.height);
  42. return MAX(size.height, 34.0f);
  43. }
  44. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier transparent:(BOOL)transparent
  45. {
  46. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier transparent:transparent];
  47. if (self) {
  48. pinView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CurrentLocation"]];
  49. pinView.frame = CGRectMake(0, 0, 15, 15);
  50. [self.contentView addSubview:pinView];
  51. geocodeLabel = [[UILabel alloc] init];
  52. geocodeLabel.clearsContextBeforeDrawing = NO;
  53. geocodeLabel.backgroundColor = [UIColor clearColor];
  54. geocodeLabel.numberOfLines = 0;
  55. geocodeLabel.lineBreakMode = NSLineBreakByWordWrapping;
  56. geocodeLabel.font = [ChatMessageCell textFont];
  57. geocodeLabel.contentMode = UIViewContentModeScaleToFill;
  58. [self.contentView addSubview:geocodeLabel];
  59. UIActivityIndicatorViewStyle style = UIActivityIndicatorViewStyleGray;
  60. switch ([Colors getTheme]) {
  61. case ColorThemeDark:
  62. case ColorThemeDarkWork:
  63. style = UIActivityIndicatorViewStyleWhite;
  64. break;
  65. case ColorThemeLight:
  66. case ColorThemeLightWork:
  67. case ColorThemeUndefined:
  68. break;
  69. }
  70. activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
  71. [self.contentView addSubview:activityIndicator];
  72. UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationMessageTapped:)];
  73. gestureRecognizer.delegate = self;
  74. self.msgBackground.userInteractionEnabled = YES;
  75. [self.msgBackground addGestureRecognizer:gestureRecognizer];
  76. }
  77. return self;
  78. }
  79. - (void)setupColors {
  80. [super setupColors];
  81. geocodeLabel.textColor = [Colors fontNormal];
  82. pinView.image = [UIImage imageNamed:@"CurrentLocation" inColor:[Colors fontLight]];
  83. }
  84. - (void)setMessage:(BaseMessage *)newMessage {
  85. [super setMessage:newMessage];
  86. [self updateView];
  87. }
  88. - (void)layoutSubviews {
  89. CGFloat messageTextWidth;
  90. if (@available(iOS 11.0, *)) {
  91. messageTextWidth = [ChatMessageCell maxContentWidthForTableWidth:self.safeAreaLayoutGuide.layoutFrame.size.width];
  92. } else {
  93. messageTextWidth = [ChatMessageCell maxContentWidthForTableWidth:self.frame.size.width];
  94. }
  95. CGSize textSize = [geocodeLabel.text boundingRectWithSize:CGSizeMake(messageTextWidth - 25, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [ChatMessageCell textFont]} context:nil].size;
  96. textSize.width = ceilf(textSize.width);
  97. textSize.height = ceilf(textSize.height);
  98. [self setBubbleContentSize:CGSizeMake(textSize.width + 25, MAX(34.0f, textSize.height))];
  99. [super layoutSubviews];
  100. CGFloat textY = 7;
  101. if (textSize.height < 34.0)
  102. textY += (34.0 - textSize.height) / 2;
  103. if (self.message.isOwn.boolValue) {
  104. geocodeLabel.frame = CGRectMake(self.contentView.frame.size.width - textSize.width - 20, textY, floor(textSize.width+1), floor(textSize.height+1));
  105. activityIndicator.frame = CGRectMake(self.contentView.frame.size.width - textSize.width - 25 - 22, (geocodeLabel.frame.origin.y + geocodeLabel.frame.size.height/2) - 10, 20, 20);
  106. pinView.frame = CGRectMake(self.contentView.frame.size.width - textSize.width - 46, (geocodeLabel.frame.origin.y + geocodeLabel.frame.size.height/2) - pinView.frame.size.height/2, pinView.frame.size.width, pinView.frame.size.height);
  107. } else {
  108. geocodeLabel.frame = CGRectMake(46 + self.contentLeftOffset, textY, floor(textSize.width+1), floor(textSize.height+1));
  109. activityIndicator.frame = CGRectMake(19 + self.contentLeftOffset, (geocodeLabel.frame.origin.y + geocodeLabel.frame.size.height/2) - 10, 20, 20);
  110. pinView.frame = CGRectMake(23 + self.contentLeftOffset, (geocodeLabel.frame.origin.y + geocodeLabel.frame.size.height/2) - pinView.frame.size.height/2, pinView.frame.size.width, pinView.frame.size.height);
  111. }
  112. }
  113. - (NSString *)accessibilityLabelForContent {
  114. return geocodeLabel.text;
  115. }
  116. - (void)updateView {
  117. LocationMessage *locationMessage = (LocationMessage*)self.message;
  118. NSString *displayText = [ChatLocationMessageCell displayTextForLocationMessage:locationMessage];
  119. if (locationMessage.reverseGeocodingResult == nil && locationMessage.poiName == nil) {
  120. [activityIndicator startAnimating];
  121. pinView.hidden = YES;
  122. } else {
  123. [activityIndicator stopAnimating];
  124. pinView.hidden = NO;
  125. }
  126. geocodeLabel.text = displayText;
  127. if (self.message.isOwn.boolValue) {
  128. geocodeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  129. activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  130. pinView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  131. } else {
  132. geocodeLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  133. activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  134. pinView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  135. }
  136. [self setNeedsLayout];
  137. }
  138. - (void)locationMessageTapped:(id)sender {
  139. DDLogInfo(@"locationMessageTapped");
  140. [self.chatVc locationMessageTapped:(LocationMessage*)self.message];
  141. }
  142. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  143. if (action == @selector(speakMessage:)) {
  144. return YES;
  145. } else {
  146. return [super canPerformAction:action withSender:sender];
  147. }
  148. }
  149. - (void)copyMessage:(UIMenuController *)menuController {
  150. LocationMessage *locationMessage = (LocationMessage*)self.message;
  151. NSString *mapsUrl = [NSString stringWithFormat:@"http://maps.apple.com/maps?ll=%f,%f", locationMessage.latitude.doubleValue, locationMessage.longitude.doubleValue];
  152. [[UIPasteboard generalPasteboard] setURL:[NSURL URLWithString:mapsUrl]];
  153. }
  154. - (void)speakMessage:(UIMenuController *)menuController {
  155. LocationMessage *locationMessage = (LocationMessage*)self.message;
  156. NSString *displayText = [ChatLocationMessageCell displayTextForLocationMessage:locationMessage];
  157. AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:displayText];
  158. AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
  159. [syn speakUtterance:utterance];
  160. }
  161. - (void)shareMessage:(UIMenuController *)menuController {
  162. LocationMessage *locationMessage = (LocationMessage*)self.message;
  163. NSString *mapsUrl = [NSString stringWithFormat:@"http://maps.apple.com/maps?ll=%f,%f", locationMessage.latitude.doubleValue, locationMessage.longitude.doubleValue];
  164. UIActivityViewController* activityView = [ActivityUtil activityViewControllerWithActivityItems:@[mapsUrl] applicationActivities:nil];
  165. [self.chatVc presentActivityViewController:activityView animated:YES fromView:self];
  166. }
  167. + (NSString*)displayTextForLocationMessage:(LocationMessage*)locationMessage {
  168. if (locationMessage.poiName)
  169. return locationMessage.poiName;
  170. else if (locationMessage.reverseGeocodingResult)
  171. return locationMessage.reverseGeocodingResult;
  172. else
  173. return NSLocalizedString(@"locating", nil);
  174. }
  175. - (BOOL)performPlayActionForAccessibility {
  176. [self locationMessageTapped:self];
  177. return YES;
  178. }
  179. - (NSString *)textForQuote {
  180. return geocodeLabel.text;
  181. }
  182. @end