session-ios/SignalMessaging/Views/AvatarImageView.swift

159 lines
4.7 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import UIKit
2018-04-26 16:12:21 +02:00
@objc
public class AvatarImageView: UIImageView {
public init() {
super.init(frame: .zero)
self.configureView()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.configureView()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.configureView()
}
override init(image: UIImage?) {
super.init(image: image)
self.configureView()
}
func configureView() {
self.autoPinToSquareAspectRatio()
self.layer.minificationFilter = kCAFilterTrilinear
self.layer.magnificationFilter = kCAFilterTrilinear
self.layer.borderWidth = 0.5
self.layer.masksToBounds = true
self.contentMode = .scaleToFill
}
override public func layoutSubviews() {
2018-08-08 17:31:45 +02:00
self.layer.borderColor = UIColor(white: 0, alpha: 0.15).cgColor
2018-04-26 16:12:21 +02:00
self.layer.cornerRadius = self.frame.size.width / 2
}
}
/// Avatar View which updates itself as necessary when the profile, contact, or group picture changes.
@objc
public class ConversationAvatarImageView: AvatarImageView {
let thread: TSThread
let diameter: UInt
let contactsManager: OWSContactsManager
// nil if group avatar
let recipientId: String?
// nil if contact avatar
let groupThreadId: String?
required public init(thread: TSThread, diameter: UInt, contactsManager: OWSContactsManager) {
self.thread = thread
self.diameter = diameter
self.contactsManager = contactsManager
switch thread {
case let contactThread as TSContactThread:
self.recipientId = contactThread.contactIdentifier()
self.groupThreadId = nil
case let groupThread as TSGroupThread:
self.recipientId = nil
self.groupThreadId = groupThread.uniqueId
default:
2018-08-27 16:27:48 +02:00
owsFailDebug("unexpected thread type: \(thread)")
self.recipientId = nil
self.groupThreadId = nil
}
super.init(frame: .zero)
if recipientId != nil {
NotificationCenter.default.addObserver(self, selector: #selector(handleOtherUsersProfileChanged(notification:)), name: NSNotification.Name(rawValue: kNSNotificationName_OtherUsersProfileDidChange), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleSignalAccountsChanged(notification:)), name: NSNotification.Name.OWSContactsManagerSignalAccountsDidChange, object: nil)
}
if groupThreadId != nil {
NotificationCenter.default.addObserver(self, selector: #selector(handleGroupAvatarChanged(notification:)), name: .TSGroupThreadAvatarChanged, object: nil)
}
// TODO group avatar changed
self.updateImage()
}
required public init?(coder aDecoder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
2018-05-25 18:54:25 +02:00
@objc func handleSignalAccountsChanged(notification: Notification) {
2018-08-23 16:37:34 +02:00
Logger.debug("")
2018-04-26 16:12:21 +02:00
// PERF: It would be nice if we could do this only if *this* user's SignalAccount changed,
// but currently this is only a course grained notification.
self.updateImage()
}
2018-05-25 18:54:25 +02:00
@objc func handleOtherUsersProfileChanged(notification: Notification) {
2018-08-23 16:37:34 +02:00
Logger.debug("")
guard let changedRecipientId = notification.userInfo?[kNSNotificationKey_ProfileRecipientId] as? String else {
2018-08-27 16:27:48 +02:00
owsFailDebug("recipientId was unexpectedly nil")
return
}
guard let recipientId = self.recipientId else {
// shouldn't call this for group threads
2018-08-27 16:27:48 +02:00
owsFailDebug("contactId was unexpectedly nil")
return
}
2018-04-26 16:12:21 +02:00
guard recipientId == changedRecipientId else {
// not this avatar
return
}
self.updateImage()
}
2018-05-25 18:54:25 +02:00
@objc func handleGroupAvatarChanged(notification: Notification) {
2018-08-23 16:37:34 +02:00
Logger.debug("")
guard let changedGroupThreadId = notification.userInfo?[TSGroupThread_NotificationKey_UniqueId] as? String else {
2018-08-27 16:27:48 +02:00
owsFailDebug("groupThreadId was unexpectedly nil")
return
}
guard let groupThreadId = self.groupThreadId else {
// shouldn't call this for contact threads
2018-08-27 16:27:48 +02:00
owsFailDebug("groupThreadId was unexpectedly nil")
return
}
guard groupThreadId == changedGroupThreadId else {
// not this avatar
return
}
thread.reload()
self.updateImage()
}
2018-06-28 19:28:14 +02:00
public func updateImage() {
2018-08-23 16:37:34 +02:00
Logger.debug("updateImage")
self.image = OWSAvatarBuilder.buildImage(thread: thread, diameter: diameter)
}
}