|
| 1 | +// |
| 2 | +// SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +// |
| 5 | + |
| 6 | +class AutoCompletionTableViewCell: UITableViewCell { |
| 7 | + |
| 8 | + public static let identifier = "AutoCompletionCellIdentifier" |
| 9 | + public static let cellHeight = 50.0 |
| 10 | + |
| 11 | + private let avatarHeight = 30.0 |
| 12 | + |
| 13 | + public lazy var titleLabel = { |
| 14 | + let label = UILabel() |
| 15 | + label.translatesAutoresizingMaskIntoConstraints = false |
| 16 | + label.backgroundColor = .clear |
| 17 | + label.isUserInteractionEnabled = false |
| 18 | + label.numberOfLines = 1 |
| 19 | + label.font = .preferredFont(forTextStyle: .body) |
| 20 | + label.textColor = .label |
| 21 | + |
| 22 | + return label |
| 23 | + }() |
| 24 | + |
| 25 | + public lazy var avatarButton = { |
| 26 | + let button = AvatarButton(frame: .init(x: 0, y: 0, width: avatarHeight, height: avatarHeight)) |
| 27 | + button.translatesAutoresizingMaskIntoConstraints = false |
| 28 | + button.backgroundColor = NCAppBranding.placeholderColor() |
| 29 | + button.layer.cornerRadius = avatarHeight / 2 |
| 30 | + button.layer.masksToBounds = true |
| 31 | + button.showsMenuAsPrimaryAction = true |
| 32 | + button.imageView?.contentMode = .scaleToFill |
| 33 | + |
| 34 | + return button |
| 35 | + }() |
| 36 | + |
| 37 | + private lazy var userStatusImageView = { |
| 38 | + let imageView = UIImageView(frame: .init(x: 0, y: 0, width: 12, height: 12)) |
| 39 | + imageView.translatesAutoresizingMaskIntoConstraints = false |
| 40 | + imageView.isUserInteractionEnabled = false |
| 41 | + |
| 42 | + return imageView |
| 43 | + }() |
| 44 | + |
| 45 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 46 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 47 | + |
| 48 | + self.backgroundColor = .secondarySystemBackground |
| 49 | + self.configureSubviews() |
| 50 | + } |
| 51 | + |
| 52 | + required init?(coder: NSCoder) { |
| 53 | + fatalError("init(coder:) has not been implemented") |
| 54 | + } |
| 55 | + |
| 56 | + override func prepareForReuse() { |
| 57 | + super.prepareForReuse() |
| 58 | + |
| 59 | + self.titleLabel.font = .preferredFont(forTextStyle: .body) |
| 60 | + self.titleLabel.text = "" |
| 61 | + |
| 62 | + self.avatarButton.cancelCurrentRequest() |
| 63 | + self.avatarButton.setImage(nil, for: .normal) |
| 64 | + |
| 65 | + self.userStatusImageView.image = nil |
| 66 | + self.userStatusImageView.backgroundColor = .clear |
| 67 | + } |
| 68 | + |
| 69 | + func configureSubviews() { |
| 70 | + self.contentView.addSubview(self.avatarButton) |
| 71 | + self.contentView.addSubview(self.userStatusImageView) |
| 72 | + self.contentView.addSubview(self.titleLabel) |
| 73 | + |
| 74 | + let views = [ |
| 75 | + "avatarButton": self.avatarButton, |
| 76 | + "userStatusImageView": self.userStatusImageView, |
| 77 | + "titleLabel": self.titleLabel |
| 78 | + ] |
| 79 | + |
| 80 | + let metrics = [ |
| 81 | + "avatarSize": avatarHeight, |
| 82 | + "right": 10 |
| 83 | + ] |
| 84 | + |
| 85 | + self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-right-[avatarButton(avatarSize)]-right-[titleLabel]-right-|", metrics: metrics, views: views)) |
| 86 | + self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[titleLabel]|", metrics: metrics, views: views)) |
| 87 | + self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-32-[userStatusImageView(12)]-(>=0)-|", metrics: metrics, views: views)) |
| 88 | + self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-32-[userStatusImageView(12)]-(>=0)-|", metrics: metrics, views: views)) |
| 89 | + self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-right-[avatarButton(avatarSize)]-(>=0)-|", metrics: metrics, views: views)) |
| 90 | + } |
| 91 | + |
| 92 | + public func setUserStatus(_ status: String) { |
| 93 | + var statusImage: UIImage? |
| 94 | + |
| 95 | + if status == "online" { |
| 96 | + statusImage = NCUserStatus.getOnlineSFIcon() |
| 97 | + } else if status == "away" { |
| 98 | + statusImage = NCUserStatus.getAwaySFIcon() |
| 99 | + } else if status == "busy" { |
| 100 | + statusImage = NCUserStatus.getBusySFIcon() |
| 101 | + } else if status == "dnd" { |
| 102 | + statusImage = NCUserStatus.getDoNotDisturbSFIcon() |
| 103 | + } |
| 104 | + |
| 105 | + if let statusImage = NCUtils.renderAspectImage(image: statusImage, ofSize: .init(width: 10, height: 10), centerImage: false) { |
| 106 | + userStatusImageView.image = statusImage |
| 107 | + userStatusImageView.contentMode = .center |
| 108 | + userStatusImageView.layer.cornerRadius = 6 |
| 109 | + userStatusImageView.clipsToBounds = true |
| 110 | + |
| 111 | + // When a background color is set directly to the cell it seems that there is no background configuration. |
| 112 | + // In this class, even when no background color is set, the background configuration is nil. |
| 113 | + userStatusImageView.backgroundColor = self.backgroundColor ?? self.backgroundConfiguration?.backgroundColor |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments