SystemMessageTableViewCell.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "SystemMessageTableViewCell.h"
  6. #import "NCAppBranding.h"
  7. #import "NextcloudTalk-Swift.h"
  8. @interface SystemMessageTableViewCell () <UITextFieldDelegate>
  9. @property (nonatomic, assign) BOOL didCreateSubviews;
  10. @end
  11. @implementation SystemMessageTableViewCell
  12. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  13. {
  14. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  15. if (self) {
  16. self.selectionStyle = UITableViewCellSelectionStyleNone;
  17. self.backgroundColor = [NCAppBranding backgroundColor];
  18. }
  19. return self;
  20. }
  21. - (void)configureSubviews
  22. {
  23. if ([self.reuseIdentifier isEqualToString:InvisibleSystemMessageCellIdentifier]) {
  24. return;
  25. }
  26. [self.contentView addSubview:self.dateLabel];
  27. [self.contentView addSubview:self.bodyTextView];
  28. [self.contentView addSubview:self.collapseButton];
  29. NSDictionary *views = @{@"dateLabel": self.dateLabel,
  30. @"bodyTextView": self.bodyTextView,
  31. @"collapseButton" : self.collapseButton
  32. };
  33. NSDictionary *metrics = @{@"dateLabelWidth": @(kChatCellDateLabelWidth),
  34. @"avatarGap": @50,
  35. @"right": @10,
  36. @"left": @5
  37. };
  38. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-avatarGap-[bodyTextView]-[dateLabel(>=dateLabelWidth)]-right-|" options:0 metrics:metrics views:views]];
  39. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[collapseButton(40)]-left-[bodyTextView]-[dateLabel(>=dateLabelWidth)]-right-|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];
  40. [self.bodyTextView.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor].active = YES;
  41. self.didCreateSubviews = YES;
  42. }
  43. - (void)prepareForReuse
  44. {
  45. [super prepareForReuse];
  46. if (!self.didCreateSubviews) {
  47. [self configureSubviews];
  48. }
  49. self.selectionStyle = UITableViewCellSelectionStyleNone;
  50. self.backgroundColor = [NCAppBranding backgroundColor];
  51. self.bodyTextView.text = @"";
  52. self.dateLabel.text = @"";
  53. }
  54. #pragma mark - Getters
  55. - (MessageBodyTextView *)bodyTextView
  56. {
  57. if (!_bodyTextView) {
  58. _bodyTextView = [MessageBodyTextView new];
  59. }
  60. return _bodyTextView;
  61. }
  62. - (UILabel *)dateLabel
  63. {
  64. if (!_dateLabel) {
  65. _dateLabel = [UILabel new];
  66. _dateLabel.textAlignment = NSTextAlignmentRight;
  67. _dateLabel.translatesAutoresizingMaskIntoConstraints = NO;
  68. _dateLabel.backgroundColor = [UIColor clearColor];
  69. _dateLabel.userInteractionEnabled = NO;
  70. _dateLabel.numberOfLines = 1;
  71. _dateLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
  72. _dateLabel.textColor = [UIColor secondaryLabelColor];
  73. }
  74. return _dateLabel;
  75. }
  76. - (UIButton *)collapseButton
  77. {
  78. if (!_collapseButton) {
  79. _collapseButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
  80. _collapseButton.translatesAutoresizingMaskIntoConstraints = NO;
  81. [_collapseButton addTarget:self action:@selector(collapseButtonPressed) forControlEvents:UIControlEventTouchUpInside];
  82. [_collapseButton setImage:[UIImage systemImageNamed:@"rectangle.arrowtriangle.2.inward"] forState:UIControlStateNormal];
  83. _collapseButton.tintColor = [UIColor tertiaryLabelColor];
  84. }
  85. return _collapseButton;
  86. }
  87. - (void)collapseButtonPressed
  88. {
  89. [self.delegate cellWantsToCollapseMessagesWithMessage:self.message];
  90. }
  91. - (void)setupForMessage:(NCChatMessage *)message
  92. {
  93. self.collapseButton.hidden = (message.isCollapsed || message.collapsedMessages.count == 0);
  94. // If the message is not visible, we don't need to setup this cell
  95. if (message.isCollapsed && message.collapsedBy) {
  96. return;
  97. }
  98. if (!self.didCreateSubviews) {
  99. [self configureSubviews];
  100. }
  101. self.bodyTextView.attributedText = message.systemMessageFormat;
  102. self.messageId = message.messageId;
  103. self.message = message;
  104. if (!message.isGroupMessage && !(message.isCollapsed && message.collapsedBy > 0)) {
  105. NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:message.timestamp];
  106. self.dateLabel.text = [NCUtils getTimeFromDate:date];
  107. }
  108. if (!message.isCollapsed && (message.collapsedBy > 0 || message.collapsedMessages.count > 0)) {
  109. self.backgroundColor = [UIColor tertiarySystemFillColor];
  110. } else {
  111. self.backgroundColor = [NCAppBranding backgroundColor];
  112. }
  113. if (message.collapsedMessages.count > 0) {
  114. self.selectionStyle = UITableViewCellSelectionStyleDefault;
  115. } else {
  116. self.selectionStyle = UITableViewCellSelectionStyleNone;
  117. }
  118. }
  119. @end