ChatContactCell.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-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 "ChatContactCell.h"
  21. #import "UserSettings.h"
  22. #import "Contact.h"
  23. #import "ChatDefines.h"
  24. #import "Threema-Swift.h"
  25. #define CONTACT_LABEL_BG_COLOR [[Colors backgroundDark] colorWithAlphaComponent:0.9]
  26. @implementation ChatContactCell {
  27. UILabel *nameLabel;
  28. }
  29. @synthesize contact;
  30. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  31. {
  32. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  33. if (self) {
  34. self.backgroundColor = [UIColor clearColor];
  35. if ([UserSettings sharedUserSettings].wallpaper) {
  36. nameLabel = [[RoundedRectLabel alloc] init];
  37. nameLabel.backgroundColor = CONTACT_LABEL_BG_COLOR;
  38. ((RoundedRectLabel*)nameLabel).cornerRadius = 6;
  39. } else {
  40. nameLabel = [[UILabel alloc] init];
  41. nameLabel.backgroundColor = [UIColor clearColor];
  42. }
  43. float fontSize = roundf([UserSettings sharedUserSettings].chatFontSize * 12.0 / 16.0);
  44. if (fontSize < kChatContactMinFontSize)
  45. fontSize = kChatContactMinFontSize;
  46. else if (fontSize > kChatContactMaxFontSize)
  47. fontSize = kChatContactMaxFontSize;
  48. nameLabel.font = [UIFont systemFontOfSize:fontSize];
  49. nameLabel.numberOfLines = 1;
  50. nameLabel.frame = CGRectMake(56, 4, self.contentView.frame.size.width - 32, 16);
  51. nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  52. nameLabel.textAlignment = NSTextAlignmentLeft;
  53. [self.contentView addSubview:nameLabel];
  54. [self setupColors];
  55. }
  56. return self;
  57. }
  58. - (void)dealloc {
  59. [contact removeObserver:self forKeyPath:@"displayName"];
  60. }
  61. - (void)updateName {
  62. /* append public nickname (if no real name is available) */
  63. if ([contact.displayName isEqualToString:contact.identity] && contact.publicNickname.length > 0) {
  64. nameLabel.text = [NSString stringWithFormat:@"%@ (~%@)", contact.identity, contact.publicNickname];
  65. } else {
  66. nameLabel.text = contact.displayName;
  67. }
  68. }
  69. - (void)setupColors {
  70. nameLabel.textColor = [Colors fontLight];
  71. }
  72. - (void)setContact:(Contact *)newContact {
  73. [contact removeObserver:self forKeyPath:@"displayName"];
  74. contact = newContact;
  75. [contact addObserver:self forKeyPath:@"displayName" options:0 context:nil];
  76. [self updateName];
  77. /* set background again as it seems to be lost sometimes with RoundedRectLabel */
  78. if ([UserSettings sharedUserSettings].wallpaper)
  79. nameLabel.backgroundColor = CONTACT_LABEL_BG_COLOR;
  80. else
  81. nameLabel.backgroundColor = [UIColor clearColor];
  82. }
  83. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  84. dispatch_async(dispatch_get_main_queue(), ^{
  85. if (object == contact) {
  86. [self updateName];
  87. }
  88. });
  89. }
  90. - (UIContextMenuConfiguration *)getContextMenu:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0)) {
  91. return nil;
  92. }
  93. @end