From c42a694ef6c7cf6d96119bc979d359edef1b2159 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 29 Mar 2021 16:42:06 +1100 Subject: [PATCH] Show group images --- .../Open Groups/OpenGroupSuggestionGrid.swift | 24 +++++++++++++++---- .../Open Groups/V2/OpenGroupAPIV2.swift | 10 ++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Session/Open Groups/OpenGroupSuggestionGrid.swift b/Session/Open Groups/OpenGroupSuggestionGrid.swift index 32734cc90..eb2c5c142 100644 --- a/Session/Open Groups/OpenGroupSuggestionGrid.swift +++ b/Session/Open Groups/OpenGroupSuggestionGrid.swift @@ -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 diff --git a/SessionMessagingKit/Open Groups/V2/OpenGroupAPIV2.swift b/SessionMessagingKit/Open Groups/V2/OpenGroupAPIV2.swift index 692ca7875..55040c344 100644 --- a/SessionMessagingKit/Open Groups/V2/OpenGroupAPIV2.swift +++ b/SessionMessagingKit/Open Groups/V2/OpenGroupAPIV2.swift @@ -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]] = [:] // 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 = 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