Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URL previews as a Labs feature #4790

Merged
merged 32 commits into from
Sep 8, 2021
Merged

Conversation

pixlwave
Copy link
Member

@pixlwave pixlwave commented Sep 6, 2021

Adds support for URL previews, fixing #888. Depends on matrix-org/matrix-ios-kit#893 and matrix-org/matrix-ios-sdk#1220.

  • The feature is disabled by default with a Labs setting to enable it.
  • Previews are only generated for unencrypted rooms, with MatrixKit not detecting links for encrypted events.
  • There is a Core Data store to cache preview data for 24 hours and to remember when a preview has been closed.
  • A urlPreviewData property has been added to RoomBubbleCellData which is filled with the preview data once fetched, along with a showURLPreview property to avoid having to hit Core Data too often.
  • Whilst loading events the cell data is split to ensure that links only exist in the last bubble component to simplify timeline display.

Frame 1

Updated:

Frame 2

…ew model.

Changes to RoomDataSource still to come.
Implement close button and store the action in Core Data. Hide the preview image view when no image is received. Remove line breaks in description text.
Make the preview manager a singleton (passing in the MXSession to functions). Fix tests.

PreviewManager → URLPreviewManager
URLPreviewViewData → URLPreviewData
URLPreviewCache → URLPreviewStore
Using a temporary position in the settings screen whilst waiting for feedback.
@pixlwave pixlwave linked an issue Sep 6, 2021 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented Sep 6, 2021

📱 Scan the QR code below to install the build for this PR.
🔒 This build is for internal testing purpose. Only devices listed in the ad-hoc provisioning profile can install Element Alpha.

QR code

If you can't scan the QR code you can install the build via this link: https://i.diawi.com/M4Lpog

Copy link

@niquewoodhouse niquewoodhouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Don't think the loading preview text is needed, there's a spinner already in there

  • The setting could say underneath the switch "will only show in unencrypted rooms" just to set that expectation

@pixlwave pixlwave changed the title Add URL previews Add URL previews as a Labs feature Sep 7, 2021
@langleyd langleyd self-requested a review September 8, 2021 10:51
Copy link
Member

@langleyd langleyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have context on a lot of the code so just picked out some general stuff. Looks great though.

Riot/Managers/URLPreviews/ClosedURLPreview.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewData.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewManager.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewManager.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewStore.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewStore.swift Outdated Show resolved Hide resolved
RiotTests/URLPreviewStoreTests.swift Show resolved Hide resolved
Riot/Managers/URLPreviews/ClosedURLPreview.swift Outdated Show resolved Hide resolved

import CoreData

extension URLPreviewCacheData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we will have more Core Data entities which prefix or suffix we can use to distinguish the managed object from the memory object. A convention like CDURLPreviewData for Core Data and URLPreviewData for the memory representation?

Riot/Managers/URLPreviews/URLPreviewManager.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewManager.swift Outdated Show resolved Hide resolved
Riot/Managers/URLPreviews/URLPreviewManager.swift Outdated Show resolved Hide resolved
Comment on lines +372 to +398
NSURL *link = component.link;
URLPreviewView *urlPreviewView;

// Show a URL preview if the component has a link that should be previewed.
if (link && RiotSettings.shared.roomScreenShowsURLPreviews && cellData.showURLPreview)
{
urlPreviewView = [URLPreviewView instantiate];
urlPreviewView.preview = cellData.urlPreviewData;
urlPreviewView.delegate = self;

[temporaryViews addObject:urlPreviewView];

if (!bubbleCell.tmpSubviews)
{
bubbleCell.tmpSubviews = [NSMutableArray array];
}
[bubbleCell.tmpSubviews addObject:urlPreviewView];

urlPreviewView.translatesAutoresizingMaskIntoConstraints = NO;
[bubbleCell.contentView addSubview:urlPreviewView];

CGFloat leftMargin = RoomBubbleCellLayout.reactionsViewLeftMargin;
if (roomBubbleCellData.containsBubbleComponentWithEncryptionBadge)
{
leftMargin+= RoomBubbleCellLayout.encryptedContentLeftMargin;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath start to become huge, it will be time to make intermediate methods and factorize some code. We can do it in another PR if needed.

Riot/Modules/Room/RoomViewController.m Outdated Show resolved Hide resolved
Comment on lines +37 to +43
if (RiotSettings.shared.roomScreenShowsURLPreviews && bubbleData && bubbleData.showURLPreview)
{
CGFloat height = [super heightForCellData:cellData withMaximumWidth:maxWidth];
return height + RoomBubbleCellLayout.urlPreviewViewTopMargin + [URLPreviewView contentViewHeightFor:bubbleData.urlPreviewData];
}

return [super heightForCellData:cellData withMaximumWidth:maxWidth];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a mechanism in RoomBubbleCellData to compute the final height in - (void)addVerticalWhitespaceToString:(NSMutableAttributedString *)attributedString forEvent:(NSString *)eventId. It can be weird to manipulate sizes in two different places.

This comment applies to all cell classes with a custom + (CGFloat)heightForCellData:(MXKCellData *)cellData withMaximumWidth:(CGFloat)maxWidth

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that's so much neater! I really disliked doing this in the cells individually.

Copy link
Member Author

@pixlwave pixlwave Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted this change for the current build as it doesn't automatically update when a preview changes from loading to loaded, and again from loaded to closed.

I don't want to hold back the release for this, so will look at it next.

URLPreviewManager becomes URLPreviewService.
addVerticalWhitespaceToString used instead of heightForCellData multiple times.
All newline characters removed.
URLPreviewCacheData becomes URLPreviewData in the model with a class name of URLPreviewDataMO
ClosedURLData becomes URLPreviewUserData in the model with a class name of URLPreviewUserDataMO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

URL previews!
4 participants