Skip to content

Commit

Permalink
fix for data race in displaying an avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Nov 4, 2022
1 parent f04a476 commit 3913192
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions Tinodios/widgets/RoundImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,15 @@ public class RoundImageView: UIImageView {
}

public func set(pub: TheCard?, id: String?, deleted: Bool) {
if let ref = pub?.photo?.ref, let url = URL(string: ref, relativeTo: Cache.tinode.baseURL(useWebsocketProtocol: false)) {
let modifier = AnyModifier { request in
var request = request
LargeFileHelper.addCommonHeaders(to: &request, using: Cache.tinode)
return request
}

KingfisherManager.shared.retrieveImage(with: url.downloadURL, options: [.requestModifier(modifier)], completionHandler: { result in
if case .success(let value) = result {
self.initials = nil
self.backgroundColor = nil
self.image = deleted ? value.image.noir : value.image
}
// Ignoring the error: just keep the placeholder image.
})
}

if let icon = pub?.photo?.image {
// Use thumbnail, if preset.
// Clean up.
self.backgroundColor = nil
self.initials = nil
// Avatar image provided.
self.image = deleted ? icon.noir : icon
} else {
// Missing thumbnail.
if let id = id, !id.isEmpty {
switch Tinode.topicTypeByName(name: id) {
case .p2p: iconType = .p2p
Expand All @@ -143,11 +128,29 @@ public class RoundImageView: UIImageView {
}
self.initials = String(title[title.startIndex]).uppercased()
} else {
// Placeholder image
// Blank name, show placeholder image.
self.image = RoundImageView.defaultIcon(forType: iconType)
self.backgroundColor = Constants.kDeletedBackground
}
}

// Download the full image, if available, and use it instead of thumbnail or letters.
if let ref = pub?.photo?.ref, let url = URL(string: ref, relativeTo: Cache.tinode.baseURL(useWebsocketProtocol: false)) {
let modifier = AnyModifier { request in
var request = request
LargeFileHelper.addCommonHeaders(to: &request, using: Cache.tinode)
return request
}

KingfisherManager.shared.retrieveImage(with: url.downloadURL, options: [.requestModifier(modifier)], completionHandler: { result in
if case .success(let value) = result {
self.initials = nil
self.backgroundColor = nil
self.image = deleted ? value.image.noir : value.image
}
// Ignoring the error: just keep the placeholder image.
})
}
}

public func setIconType(_ type: IconType) {
Expand Down

0 comments on commit 3913192

Please sign in to comment.