AvatarImageView.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. import SDWebImage
  7. @objcMembers class AvatarImageView: UIImageView {
  8. private var currentRequest: SDWebImageCombinedOperation?
  9. public func cancelCurrentRequest() {
  10. self.currentRequest?.cancel()
  11. }
  12. // MARK: - Init
  13. override init(frame: CGRect) {
  14. super.init(frame: frame)
  15. self.commonInit()
  16. }
  17. required init?(coder: NSCoder) {
  18. super.init(coder: coder)
  19. self.commonInit()
  20. }
  21. private func commonInit() {
  22. self.contentMode = .scaleToFill
  23. }
  24. // MARK: - Conversation avatars
  25. public func setAvatar(for room: NCRoom) {
  26. self.cancelCurrentRequest()
  27. self.currentRequest = AvatarManager.shared.getAvatar(for: room, with: self.traitCollection.userInterfaceStyle) { image in
  28. guard let image = image else {
  29. return
  30. }
  31. self.image = image
  32. self.contentMode = .scaleToFill
  33. self.backgroundColor = .clear
  34. }
  35. }
  36. public func setGroupAvatar() {
  37. if let image = AvatarManager.shared.getGroupAvatar(with: self.traitCollection.userInterfaceStyle) {
  38. self.image = image
  39. }
  40. }
  41. public func setMailAvatar() {
  42. if let image = AvatarManager.shared.getMailAvatar(with: self.traitCollection.userInterfaceStyle) {
  43. self.image = image
  44. }
  45. }
  46. // MARK: - User avatars
  47. public func setActorAvatar(forMessage message: NCChatMessage) {
  48. self.setActorAvatar(forId: message.actorId, withType: message.actorType, withDisplayName: message.actorDisplayName, withRoomToken: message.token)
  49. }
  50. public func setActorAvatar(forId actorId: String?, withType actorType: String?, withDisplayName actorDisplayName: String?, withRoomToken roomToken: String?) {
  51. self.setActorAvatar(forId: actorId, withType: actorType, withDisplayName: actorDisplayName, withRoomToken: roomToken, using: nil)
  52. }
  53. public func setActorAvatar(forId actorId: String?, withType actorType: String?, withDisplayName actorDisplayName: String?, withRoomToken roomToken: String?, using account: TalkAccount?) {
  54. self.cancelCurrentRequest()
  55. self.currentRequest = AvatarManager.shared.getActorAvatar(forId: actorId, withType: actorType, withDisplayName: actorDisplayName, withRoomToken: roomToken, withStyle: self.traitCollection.userInterfaceStyle, usingAccount: account) { image in
  56. guard let image = image else {
  57. return
  58. }
  59. self.image = image
  60. self.contentMode = .scaleToFill
  61. }
  62. }
  63. }