NCChatTitleView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCChatTitleView.h"
  6. #import "NCAPIController.h"
  7. #import "NCAppBranding.h"
  8. #import "NCUserStatus.h"
  9. #import "NextcloudTalk-Swift.h"
  10. @interface NCChatTitleView ()
  11. @property (strong, nonatomic) IBOutlet UIView *contentView;
  12. @property (strong, nonatomic) UIFont *titleFont;
  13. @property (strong, nonatomic) UIFont *subtitleFont;
  14. @end
  15. @implementation NCChatTitleView
  16. - (instancetype)init
  17. {
  18. self = [super init];
  19. if (self) {
  20. [self commonInit];
  21. }
  22. return self;
  23. }
  24. - (instancetype)initWithCoder:(NSCoder *)coder
  25. {
  26. self = [super initWithCoder:coder];
  27. if (self) {
  28. [self commonInit];
  29. }
  30. return self;
  31. }
  32. - (void)commonInit
  33. {
  34. [[NSBundle mainBundle] loadNibNamed:@"NCChatTitleView" owner:self options:nil];
  35. [self addSubview:self.contentView];
  36. self.contentView.frame = self.bounds;
  37. self.avatarimage.layer.cornerRadius = self.avatarimage.bounds.size.width / 2;
  38. self.avatarimage.clipsToBounds = YES;
  39. self.avatarimage.backgroundColor = [UIColor systemGray3Color];
  40. self.titleTextView.textContainer.lineFragmentPadding = 0;
  41. self.titleTextView.textContainerInset = UIEdgeInsetsZero;
  42. self.titleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
  43. self.subtitleFont = [UIFont systemFontOfSize:13];
  44. self.showSubtitle = YES;
  45. self.titleTextColor = [NCAppBranding themeTextColor];
  46. self.userStatusBackgroundColor = [NCAppBranding themeColor];
  47. // Set empty title on init to prevent showing a placeholder on iPhones in landscape
  48. [self setTitle:@"" withSubtitle:nil];
  49. // Use a LongPressGestureRecognizer here to get a "TouchDown" event
  50. self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlGestureRecognizer:)];
  51. self.longPressGestureRecognizer.minimumPressDuration = 0.0;
  52. [self.contentView addGestureRecognizer:self.longPressGestureRecognizer];
  53. }
  54. - (void)layoutSubviews
  55. {
  56. [super layoutSubviews];
  57. self.avatarimage.layer.cornerRadius = self.avatarimage.bounds.size.width / 2;
  58. self.userStatusImage.layer.cornerRadius = self.userStatusImage.bounds.size.width / 2;
  59. }
  60. - (void)updateForRoom:(NCRoom *)room
  61. {
  62. // Set room image
  63. [self.avatarimage setAvatarFor:room];
  64. NSString *subtitle = nil;
  65. if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilitySingleConvStatus]) {
  66. // User status
  67. [self setStatusImageForUserStatus:room.status];
  68. // User status message
  69. if (!room.statusMessage || [room.statusMessage isEqualToString:@""]) {
  70. // We don't have a dedicated statusMessage -> check the room status itself
  71. if ([room.status isEqualToString:kUserStatusDND]) {
  72. subtitle = NSLocalizedString(@"Do not disturb", nil);
  73. } else if ([room.status isEqualToString:kUserStatusAway]) {
  74. subtitle = NSLocalizedString(@"Away", nil);
  75. }
  76. } else if (room.statusMessage && ![room.statusMessage isEqualToString:@""]) {
  77. // A dedicated statusMessage was set -> use it
  78. if (room.statusIcon && ![room.statusIcon isEqualToString:@""]) {
  79. subtitle = [NSString stringWithFormat:@"%@ %@", room.statusIcon, room.statusMessage];
  80. } else {
  81. subtitle = room.statusMessage;
  82. }
  83. }
  84. }
  85. // Show description in group conversations
  86. if (room.type != kNCRoomTypeOneToOne && ![room.roomDescription isEqualToString:@""]) {
  87. subtitle = room.roomDescription;
  88. }
  89. [self setTitle:room.displayName withSubtitle:subtitle];
  90. }
  91. - (void)setStatusImageForUserStatus:(NSString *)userStatus
  92. {
  93. UIImage *statusImage = nil;
  94. if ([userStatus isEqualToString:@"online"]) {
  95. statusImage = [UIImage imageNamed:@"user-status-online-10"];
  96. } else if ([userStatus isEqualToString:@"away"]) {
  97. statusImage = [UIImage imageNamed:@"user-status-away-10"];
  98. } else if ([userStatus isEqualToString:@"dnd"]) {
  99. statusImage = [UIImage imageNamed:@"user-status-dnd-10"];
  100. }
  101. if (statusImage) {
  102. [_userStatusImage setImage:statusImage];
  103. _userStatusImage.contentMode = UIViewContentModeCenter;
  104. _userStatusImage.clipsToBounds = YES;
  105. _userStatusImage.backgroundColor = _userStatusBackgroundColor;
  106. }
  107. }
  108. - (void)setTitle:(NSString *)title withSubtitle:(NSString *)subtitle
  109. {
  110. if (!title) {
  111. return;
  112. }
  113. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  114. paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  115. NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:title];
  116. NSRange rangeTitle = NSMakeRange(0, [title length]);
  117. [attributedTitle addAttribute:NSFontAttributeName value:self.titleFont range:rangeTitle];
  118. [attributedTitle addAttribute:NSForegroundColorAttributeName value:self.titleTextColor range:rangeTitle];
  119. [attributedTitle addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:rangeTitle];
  120. if (self.showSubtitle && subtitle != nil) {
  121. NSMutableAttributedString *attributedSubtitle = [[NSMutableAttributedString alloc] initWithString:subtitle];
  122. NSRange rangeSubtitle = NSMakeRange(0, [subtitle length]);
  123. [attributedSubtitle addAttribute:NSFontAttributeName value:self.subtitleFont range:rangeSubtitle];
  124. [attributedSubtitle addAttribute:NSForegroundColorAttributeName value:self.titleTextColor range:rangeSubtitle];
  125. [attributedSubtitle addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:rangeSubtitle];
  126. [attributedTitle appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
  127. [attributedTitle appendAttributedString:attributedSubtitle];
  128. [self.titleTextView.textContainer setMaximumNumberOfLines:2];
  129. } else {
  130. [self.titleTextView.textContainer setMaximumNumberOfLines:1];
  131. }
  132. [self.titleTextView setAttributedText:attributedTitle];
  133. }
  134. -(void)handlGestureRecognizer:(UILongPressGestureRecognizer *)gestureRecognizer
  135. {
  136. if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
  137. // Simulate a pressed stated. Don't use self.alpha here as it will interfere with NavigationController transitions
  138. self.titleTextView.alpha = 0.7;
  139. self.avatarimage.alpha = 0.7;
  140. self.userStatusImage.alpha = 0.7;
  141. } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
  142. // Call delegate & reset the pressed state -> use dispatch after to give the UI time to show the actual pressed state
  143. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  144. self.titleTextView.alpha = 1.0;
  145. self.avatarimage.alpha = 1.0;
  146. self.userStatusImage.alpha = 1.0;
  147. [self.delegate chatTitleViewTapped:self];
  148. });
  149. }
  150. }
  151. @end