AutoCompletionTableViewCell.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "AutoCompletionTableViewCell.h"
  6. #import "ChatTableViewCell.h"
  7. #import "SLKUIConstants.h"
  8. #import "NextcloudTalk-Swift.h"
  9. #import "NCAPIController.h"
  10. #import "NCAppBranding.h"
  11. #import "NCDatabaseManager.h"
  12. @implementation AutoCompletionTableViewCell
  13. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  14. {
  15. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  16. if (self) {
  17. self.backgroundColor = [NCAppBranding backgroundColor];
  18. [self configureSubviews];
  19. }
  20. return self;
  21. }
  22. - (void)configureSubviews
  23. {
  24. _avatarButton = [[AvatarButton alloc] initWithFrame:CGRectMake(0, 0, kChatCellAvatarHeight, kChatCellAvatarHeight)];
  25. _avatarButton.translatesAutoresizingMaskIntoConstraints = NO;
  26. _avatarButton.backgroundColor = [NCAppBranding placeholderColor];
  27. _avatarButton.layer.cornerRadius = kChatCellAvatarHeight/2.0;
  28. _avatarButton.layer.masksToBounds = YES;
  29. _avatarButton.showsMenuAsPrimaryAction = YES;
  30. _avatarButton.imageView.contentMode = UIViewContentModeScaleToFill;
  31. [self.contentView addSubview:_avatarButton];
  32. _userStatusImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
  33. _userStatusImageView.translatesAutoresizingMaskIntoConstraints = NO;
  34. _userStatusImageView.userInteractionEnabled = NO;
  35. [self.contentView addSubview:_userStatusImageView];
  36. [self.contentView addSubview:self.titleLabel];
  37. NSDictionary *views = @{
  38. @"avatarButton": self.avatarButton,
  39. @"userStatusImageView": self.userStatusImageView,
  40. @"titleLabel": self.titleLabel
  41. };
  42. NSDictionary *metrics = @{
  43. @"avatarSize": @(kChatCellAvatarHeight),
  44. @"right": @10
  45. };
  46. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-right-[avatarButton(avatarSize)]-right-[titleLabel]-right-|" options:0 metrics:metrics views:views]];
  47. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleLabel]|" options:0 metrics:metrics views:views]];
  48. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-32-[userStatusImageView(12)]-(>=0)-|" options:0 metrics:metrics views:views]];
  49. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-32-[userStatusImageView(12)]-(>=0)-|" options:0 metrics:metrics views:views]];
  50. self.backgroundColor = [UIColor secondarySystemBackgroundColor];
  51. self.titleLabel.textColor = [UIColor labelColor];
  52. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-right-[avatarButton(avatarSize)]-(>=0)-|" options:0 metrics:metrics views:views]];
  53. }
  54. - (void)prepareForReuse
  55. {
  56. [super prepareForReuse];
  57. self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  58. self.titleLabel.text = @"";
  59. [self.avatarButton cancelCurrentRequest];
  60. [self.avatarButton setImage:nil forState:UIControlStateNormal];
  61. self.userStatusImageView.image = nil;
  62. self.userStatusImageView.backgroundColor = [UIColor clearColor];
  63. }
  64. #pragma mark - Getters
  65. - (UILabel *)titleLabel
  66. {
  67. if (!_titleLabel) {
  68. _titleLabel = [UILabel new];
  69. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  70. _titleLabel.backgroundColor = [UIColor clearColor];
  71. _titleLabel.userInteractionEnabled = NO;
  72. _titleLabel.numberOfLines = 1;
  73. _titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  74. _titleLabel.textColor = [UIColor secondaryLabelColor];
  75. }
  76. return _titleLabel;
  77. }
  78. - (void)setUserStatus:(NSString *)userStatus
  79. {
  80. UIImage *statusImage = nil;
  81. if ([userStatus isEqualToString:@"online"]) {
  82. statusImage = [UIImage imageNamed:@"user-status-online-10"];
  83. } else if ([userStatus isEqualToString:@"away"]) {
  84. statusImage = [UIImage imageNamed:@"user-status-away-10"];
  85. } else if ([userStatus isEqualToString:@"dnd"]) {
  86. statusImage = [UIImage imageNamed:@"user-status-dnd-10"];
  87. }
  88. if (statusImage) {
  89. [_userStatusImageView setImage:statusImage];
  90. _userStatusImageView.contentMode = UIViewContentModeCenter;
  91. _userStatusImageView.layer.cornerRadius = 6;
  92. _userStatusImageView.clipsToBounds = YES;
  93. // When a background color is set directly to the cell it seems that there is no background configuration.
  94. // In this class, even when no background color is set, the background configuration is nil.
  95. _userStatusImageView.backgroundColor = (self.backgroundColor) ? self.backgroundColor : [[self backgroundConfiguration] backgroundColor];
  96. }
  97. }
  98. @end