79 lines
2.8 KiB
Swift
79 lines
2.8 KiB
Swift
//
|
|
// FollowNotificationGroupTableViewCell.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/5/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
class FollowNotificationGroupTableViewCell: UITableViewCell {
|
|
|
|
var delegate: TuskerNavigationDelegate?
|
|
|
|
@IBOutlet weak var avatarStackView: UIStackView!
|
|
@IBOutlet weak var actionLabel: UILabel!
|
|
|
|
var group: NotificationGroup!
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
|
}
|
|
|
|
@objc func updateUIForPreferences() {
|
|
let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account }
|
|
updateActionLabel(people: people)
|
|
|
|
for case let imageView as UIImageView in avatarStackView.arrangedSubviews {
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView)
|
|
}
|
|
}
|
|
|
|
func updateUI(group: NotificationGroup) {
|
|
self.group = group
|
|
|
|
let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account }
|
|
|
|
updateActionLabel(people: people)
|
|
|
|
avatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
for account in people {
|
|
let imageView = UIImageView()
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.layer.masksToBounds = true
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
|
ImageCache.avatars.get(account.avatar) { (data) in
|
|
guard let data = data, self.group.id == group.id else { return }
|
|
DispatchQueue.main.async {
|
|
imageView.image = UIImage(data: data)
|
|
}
|
|
}
|
|
avatarStackView.addArrangedSubview(imageView)
|
|
NSLayoutConstraint.activate([
|
|
imageView.widthAnchor.constraint(equalToConstant: 30),
|
|
imageView.heightAnchor.constraint(equalToConstant: 30),
|
|
])
|
|
}
|
|
}
|
|
|
|
func updateActionLabel(people: [Account]) {
|
|
// todo: figure out how to localize this
|
|
let peopleStr: String
|
|
switch (people.count) {
|
|
case 1:
|
|
peopleStr = people.first!.realDisplayName
|
|
case 2:
|
|
peopleStr = people.first!.realDisplayName + " and " + people.last!.realDisplayName
|
|
default:
|
|
peopleStr = people.dropLast().map { $0.realDisplayName }.joined(separator: ", ") + ", and " + people.last!.realDisplayName
|
|
|
|
}
|
|
actionLabel.text = "Followed by \(peopleStr)"
|
|
}
|
|
|
|
}
|