Show group images

This commit is contained in:
Niels Andriesse 2021-03-29 16:42:06 +11:00
parent 50ab253576
commit c42a694ef6
2 changed files with 28 additions and 6 deletions

View file

@ -110,6 +110,16 @@ extension OpenGroupSuggestionGrid {
static let identifier = "OpenGroupSuggestionGridCell"
private lazy var imageView: UIImageView = {
let result = UIImageView()
let size: CGFloat = 24
result.set(.width, to: size)
result.set(.height, to: size)
result.layer.cornerRadius = size / 2
result.clipsToBounds = true
return result
}()
private lazy var label: UILabel = {
let result = UILabel()
result.textColor = Colors.text
@ -129,10 +139,13 @@ extension OpenGroupSuggestionGrid {
}
private func setUpViewHierarchy() {
addSubview(label)
label.center(in: self)
label.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: Values.smallSpacing).isActive = true
trailingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor, constant: Values.smallSpacing).isActive = true
let stackView = UIStackView(arrangedSubviews: [ imageView, label ])
stackView.axis = .horizontal
stackView.spacing = Values.smallSpacing
addSubview(stackView)
stackView.center(in: self)
stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: Values.smallSpacing).isActive = true
trailingAnchor.constraint(greaterThanOrEqualTo: stackView.trailingAnchor, constant: Values.smallSpacing).isActive = true
setUpSeparators()
}
@ -165,6 +178,9 @@ extension OpenGroupSuggestionGrid {
private func update() {
guard let room = room else { return }
let promise = OpenGroupAPIV2.getGroupImage(for: room.id, on: OpenGroupAPIV2.defaultServer)
imageView.image = given(promise.value) { UIImage(data: $0)! }
imageView.isHidden = (imageView.image == nil)
label.text = room.name
rightSeparator.alpha = showRightSeparator ? 1 :0
bottomSeparator.alpha = showBottomSeparator ? 1 :0

View file

@ -1,6 +1,8 @@
import PromiseKit
import SessionSnodeKit
// TODO: Cache group images
@objc(SNOpenGroupAPIV2)
public final class OpenGroupAPIV2 : NSObject {
private static var moderators: [String:[String:Set<String>]] = [:] // Server URL to room ID to set of moderator IDs
@ -322,9 +324,13 @@ public final class OpenGroupAPIV2 : NSObject {
Storage.shared.write(with: { transaction in
Storage.shared.setOpenGroupPublicKey(for: defaultServer, to: defaultServerPublicKey, using: transaction)
}, completion: {
defaultRoomsPromise = attempt(maxRetryCount: 8, recoveringOn: DispatchQueue.main) {
let promise = attempt(maxRetryCount: 8, recoveringOn: DispatchQueue.main) {
OpenGroupAPIV2.getAllRooms(from: defaultServer)
}
let _ = promise.done(on: DispatchQueue.global(qos: .userInitiated)) { items in
items.forEach { getGroupImage(for: $0.id, on: defaultServer).retainUntilComplete() }
}
defaultRoomsPromise = promise
})
}
@ -370,7 +376,7 @@ public final class OpenGroupAPIV2 : NSObject {
if let promise = groupImagePromises["\(server).\(room)"] {
return promise
} else {
let request = Request(verb: .get, room: room, server: server, endpoint: "image")
let request = Request(verb: .get, room: room, server: server, endpoint: "group_image")
let promise: Promise<Data> = send(request).map(on: DispatchQueue.global(qos: .userInitiated)) { json in
guard let base64EncodedFile = json["result"] as? String, let file = Data(base64Encoded: base64EncodedFile) else { throw Error.parsingFailed }
return file