Skip to content
Snippets Groups Projects
Commit 7586324c authored by Kateryna Kostiuk's avatar Kateryna Kostiuk
Browse files

conversation: verify avatar update

This patch fixes a race condition that may cause the default
avatar to be shown instead of the profile image.

Gitlab: #426
Change-Id: I737576d83e3547c2680f63c32365d6301612ac05
parent 44fcbac7
Branches
Tags
No related merge requests found
...@@ -58,8 +58,11 @@ class ParticipantInfo: Equatable, Hashable { ...@@ -58,8 +58,11 @@ class ParticipantInfo: Equatable, Hashable {
var profileName = BehaviorRelay(value: "") var profileName = BehaviorRelay(value: "")
var finalName = BehaviorRelay(value: "") var finalName = BehaviorRelay(value: "")
let disposeBag = DisposeBag() let disposeBag = DisposeBag()
var hasProfileAvatar = false var hasProfileAvatar = false
let profileLock = NSLock()
init(jamiId: String, role: ParticipantRole) { init(jamiId: String, role: ParticipantRole) {
self.jamiId = jamiId self.jamiId = jamiId
self.role = role self.role = role
...@@ -68,11 +71,15 @@ class ParticipantInfo: Equatable, Hashable { ...@@ -68,11 +71,15 @@ class ParticipantInfo: Equatable, Hashable {
.observe(on: ConcurrentDispatchQueueScheduler(qos: .background)) .observe(on: ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe { [weak self] name in .subscribe { [weak self] name in
guard let self = self else { return } guard let self = self else { return }
// when profile does not have an avatar, contact image
// should be updated each time when name changed.
if !self.hasProfileAvatar, !name.isEmpty {
self.avatar.accept(UIImage.createContactAvatar(username: name, size: CGSize(width: 55, height: 55))) // Only create backup avatar if profile avatar set and name is available
if !name.isEmpty {
self.profileLock.lock()
if !self.hasProfileAvatar {
let backupAvatar = UIImage.createContactAvatar(username: name, size: CGSize(width: 55, height: 55))
self.avatar.accept(backupAvatar)
}
self.profileLock.unlock()
} }
} onError: { _ in } onError: { _ in
} }
...@@ -394,8 +401,10 @@ class SwarmInfo: SwarmInfoProtocol { ...@@ -394,8 +401,10 @@ class SwarmInfo: SwarmInfoProtocol {
guard let participantInfo = participantInfo else { return } guard let participantInfo = participantInfo else { return }
// The view has a size of avatarHeight. Create a larger image for better resolution. // The view has a size of avatarHeight. Create a larger image for better resolution.
if let imageString = profile.photo, let image = imageString.createImage(size: self.avatarHeight * 2) { if let imageString = profile.photo, let image = imageString.createImage(size: self.avatarHeight * 2) {
participantInfo.avatar.accept(image) participantInfo.profileLock.lock()
participantInfo.hasProfileAvatar = true participantInfo.hasProfileAvatar = true
participantInfo.avatar.accept(image)
participantInfo.profileLock.unlock()
} }
if let profileName = profile.alias, !profileName.isEmpty { if let profileName = profile.alias, !profileName.isEmpty {
participantInfo.profileName.accept(profileName) participantInfo.profileName.accept(profileName)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment