From f63d0c41018607861fe7d80ca2f5d74681c31041 Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 1 Feb 2022 14:54:28 +0000 Subject: [PATCH] Use a snapshot view for context menu previews. --- ...xtMenuSnapshotPreviewViewController.swift} | 27 +++++++++---------- Riot/Modules/Home/HomeViewController.m | 13 +++++---- Riot/SupportingFiles/Riot-Bridging-Header.h | 1 - 3 files changed, 20 insertions(+), 21 deletions(-) rename Riot/Modules/Home/{RoomContextPreviewViewController.swift => ContextMenuSnapshotPreviewViewController.swift} (58%) diff --git a/Riot/Modules/Home/RoomContextPreviewViewController.swift b/Riot/Modules/Home/ContextMenuSnapshotPreviewViewController.swift similarity index 58% rename from Riot/Modules/Home/RoomContextPreviewViewController.swift rename to Riot/Modules/Home/ContextMenuSnapshotPreviewViewController.swift index 646f6bc2c..fc418913b 100644 --- a/Riot/Modules/Home/RoomContextPreviewViewController.swift +++ b/Riot/Modules/Home/ContextMenuSnapshotPreviewViewController.swift @@ -16,23 +16,21 @@ import UIKit -/// A view controller that provides a preview of a room for use in context menus. +/// A view controller that provides a preview for use in context menus. +/// The preview will display a snapshot of whichever view is passed into the init. @objcMembers -class RoomPreviewViewController: UIViewController { - - // MARK: - Constants - private enum Constants { - static let size = CGSize(width: 80, height: 115) - } +class ContextMenuSnapshotPreviewViewController: UIViewController { // MARK: - Private - private var cellData: MXKCellData + private let snapshotView: UIView? // MARK: - Setup - init(cellData: MXKCellData) { - self.cellData = cellData + /// Creates a new preview by snapshotting the supplied view. + /// - Parameter view: The view to use as a preview. + init(view: UIView) { + self.snapshotView = view.snapshotView(afterScreenUpdates: false) super.init(nibName: nil, bundle: nil) } @@ -40,16 +38,15 @@ class RoomPreviewViewController: UIViewController { fatalError("init(coder:) has not been implemented") } - // MARK: - Lifecyle + // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() - let cell = RoomCollectionViewCell(frame: CGRect(origin: .zero, size: Constants.size)) - cell.render(cellData) - view.vc_addSubViewMatchingParent(cell) + guard let snapshotView = snapshotView else { return } + view.vc_addSubViewMatchingParent(snapshotView) - preferredContentSize = Constants.size + preferredContentSize = snapshotView.bounds.size } } diff --git a/Riot/Modules/Home/HomeViewController.m b/Riot/Modules/Home/HomeViewController.m index 9d92789cd..3a02c310e 100644 --- a/Riot/Modules/Home/HomeViewController.m +++ b/Riot/Modules/Home/HomeViewController.m @@ -673,15 +673,19 @@ - (UIContextMenuConfiguration *)collectionView:(UICollectionView *)collectionView contextMenuConfigurationForItemAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0)) { - id cellData = [recentsDataSource cellDataAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item inSection:collectionView.tag]]; + UIView *cell = [collectionView cellForItemAtIndexPath:indexPath]; MXRoom *room = [self.dataSource getRoomAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:collectionView.tag]]; NSString *roomId = room.roomId; + MXWeakify(self); + MXWeakify(room); + return [UIContextMenuConfiguration configurationWithIdentifier:roomId previewProvider:^UIViewController * _Nullable { // Add a preview using the cell's data to prevent the avatar and displayname from changing with a room list update. - return [[RoomPreviewViewController alloc] initWithCellData:cellData]; + return [[ContextMenuSnapshotPreviewViewController alloc] initWithView:cell]; + } actionProvider:^UIMenu * _Nullable(NSArray * _Nonnull suggestedActions) { - MXWeakify(self); + MXStrongifyAndReturnValueIfNil(room, nil); BOOL isDirect = room.isDirect; UIAction *directChatAction = [UIAction actionWithTitle:isDirect ? VectorL10n.homeContextMenuMakeRoom : VectorL10n.homeContextMenuMakeDm @@ -736,9 +740,8 @@ }]; BOOL isLowPriority = (currentTag && [kMXRoomTagLowPriority isEqualToString:currentTag.name]); - UIImage *lowPriorityImage = [UIImage systemImageNamed:isLowPriority ? @"arrow.up" : @"arrow.down"]; UIAction *lowPriorityAction = [UIAction actionWithTitle:isLowPriority ? VectorL10n.homeContextMenuNormalPriority : VectorL10n.homeContextMenuLowPriority - image:lowPriorityImage + image:[UIImage systemImageNamed:isLowPriority ? @"arrow.up" : @"arrow.down"] identifier:nil handler:^(__kindof UIAction * _Nonnull action) { MXStrongifyAndReturnIfNil(self); diff --git a/Riot/SupportingFiles/Riot-Bridging-Header.h b/Riot/SupportingFiles/Riot-Bridging-Header.h index e8df17d7f..165321909 100644 --- a/Riot/SupportingFiles/Riot-Bridging-Header.h +++ b/Riot/SupportingFiles/Riot-Bridging-Header.h @@ -50,7 +50,6 @@ #import "PlainRoomTimelineCellProvider.h" #import "BubbleRoomTimelineCellProvider.h" #import "RoomSelectedStickerBubbleCell.h" -#import "RoomCollectionViewCell.h" // MatrixKit common imports, shared with all targets #import "MatrixKit-Bridging-Header.h"