From 60b35f3e609dde9b5116472cd4e7c849bb0bdb2a Mon Sep 17 00:00:00 2001 From: Nora Trapp Date: Wed, 30 Mar 2022 12:44:06 -0700 Subject: [PATCH] Support preview only text stories --- .../Stories/StoriesViewController.swift | 6 +- .../HomeView/Stories/TextAttachmentView.swift | 60 +++++++++++-------- .../src/Messages/Stories/StoryMessage.swift | 7 +-- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/Stories/StoriesViewController.swift b/Signal/src/ViewControllers/HomeView/Stories/StoriesViewController.swift index af9d9b48b6..8dd084a029 100644 --- a/Signal/src/ViewControllers/HomeView/Stories/StoriesViewController.swift +++ b/Signal/src/ViewControllers/HomeView/Stories/StoriesViewController.swift @@ -385,7 +385,11 @@ extension StoriesViewController: ContextMenuInteractionDelegate { } AttachmentSharing.showShareUI(forAttachment: attachment, sender: cell) case .text(let attachment): - AttachmentSharing.showShareUI(forText: attachment.text, sender: cell) + if let url = attachment.preview?.urlString { + AttachmentSharing.showShareUI(for: URL(string: url)!, sender: cell) + } else if let text = attachment.text { + AttachmentSharing.showShareUI(forText: text, sender: cell) + } case .missing: owsFailDebug("Unexpectedly missing attachment for story.") } diff --git a/Signal/src/ViewControllers/HomeView/Stories/TextAttachmentView.swift b/Signal/src/ViewControllers/HomeView/Stories/TextAttachmentView.swift index 46feb2756a..e4095f3cab 100644 --- a/Signal/src/ViewControllers/HomeView/Stories/TextAttachmentView.swift +++ b/Signal/src/ViewControllers/HomeView/Stories/TextAttachmentView.swift @@ -29,35 +29,37 @@ class TextAttachmentView: UIView { addSubview(contentStackView) contentStackView.autoPinEdgesToSuperviewEdges() - let label = UILabel() - label.numberOfLines = 0 - label.textColor = attachment.textForegroundColor ?? Theme.darkThemePrimaryColor - label.text = transformedText(attachment.text, for: attachment.textStyle) - label.textAlignment = .center - label.font = font(for: attachment.textStyle) - label.adjustsFontSizeToFitWidth = true - label.minimumScaleFactor = 0.2 + if let text = attachment.text { + let label = UILabel() + label.numberOfLines = 0 + label.textColor = attachment.textForegroundColor ?? Theme.darkThemePrimaryColor + label.text = transformedText(text, for: attachment.textStyle) + label.textAlignment = .center + label.font = font(for: attachment.textStyle) + label.adjustsFontSizeToFitWidth = true + label.minimumScaleFactor = 0.2 - if let textBackgroundColor = attachment.textBackgroundColor { - let labelBackgroundView = UIView() - labelBackgroundView.layoutMargins = UIEdgeInsets(hMargin: 16, vMargin: 16) - labelBackgroundView.backgroundColor = textBackgroundColor - labelBackgroundView.layer.cornerRadius = 18 + if let textBackgroundColor = attachment.textBackgroundColor { + let labelBackgroundView = UIView() + labelBackgroundView.layoutMargins = UIEdgeInsets(hMargin: 16, vMargin: 16) + labelBackgroundView.backgroundColor = textBackgroundColor + labelBackgroundView.layer.cornerRadius = 18 - labelBackgroundView.addSubview(label) - label.autoPinEdgesToSuperviewMargins() + labelBackgroundView.addSubview(label) + label.autoPinEdgesToSuperviewMargins() - let labelWrapper = UIView() - labelWrapper.addSubview(labelBackgroundView) - labelBackgroundView.autoPinWidthToSuperview(withMargin: 24) - labelBackgroundView.autoPinHeightToSuperview() - contentStackView.addArrangedSubview(labelWrapper) - } else { - let labelWrapper = UIView() - labelWrapper.addSubview(label) - label.autoPinWidthToSuperview(withMargin: 40) - label.autoPinHeightToSuperview() - contentStackView.addArrangedSubview(labelWrapper) + let labelWrapper = UIView() + labelWrapper.addSubview(labelBackgroundView) + labelBackgroundView.autoPinWidthToSuperview(withMargin: 24) + labelBackgroundView.autoPinHeightToSuperview() + contentStackView.addArrangedSubview(labelWrapper) + } else { + let labelWrapper = UIView() + labelWrapper.addSubview(label) + label.autoPinWidthToSuperview(withMargin: 40) + label.autoPinHeightToSuperview() + contentStackView.addArrangedSubview(labelWrapper) + } } if let linkPreviewView = buildLinkPreviewView(attachment.preview) { @@ -217,6 +219,8 @@ class TextAttachmentView: UIView { titleLabel.font = .boldSystemFont(ofSize: 16) titleLabel.textColor = Theme.darkThemePrimaryColor titleLabel.numberOfLines = 2 + titleLabel.setCompressionResistanceVerticalHigh() + titleLabel.setContentHuggingVerticalHigh() previewVStack.addArrangedSubview(titleLabel) } @@ -226,6 +230,8 @@ class TextAttachmentView: UIView { descriptionLabel.font = .systemFont(ofSize: 12) descriptionLabel.textColor = Theme.darkThemePrimaryColor descriptionLabel.numberOfLines = 3 + descriptionLabel.setCompressionResistanceVerticalHigh() + descriptionLabel.setContentHuggingVerticalHigh() previewVStack.addArrangedSubview(descriptionLabel) } @@ -233,6 +239,8 @@ class TextAttachmentView: UIView { footerLabel.font = .systemFont(ofSize: 12) footerLabel.numberOfLines = 2 footerLabel.textColor = Theme.darkThemeSecondaryTextAndIconColor + footerLabel.setCompressionResistanceVerticalHigh() + footerLabel.setContentHuggingVerticalHigh() previewVStack.addArrangedSubview(footerLabel) var footerText: String diff --git a/SignalServiceKit/src/Messages/Stories/StoryMessage.swift b/SignalServiceKit/src/Messages/Stories/StoryMessage.swift index 63a1f3c64f..daec5d8229 100644 --- a/SignalServiceKit/src/Messages/Stories/StoryMessage.swift +++ b/SignalServiceKit/src/Messages/Stories/StoryMessage.swift @@ -257,7 +257,7 @@ public enum StoryMessageAttachment: Codable { } public struct TextAttachment: Codable { - public let text: String + public let text: String? public enum TextStyle: Int, Codable { case regular = 0 @@ -310,10 +310,7 @@ public struct TextAttachment: Codable { public private(set) var preview: OWSLinkPreview? init(from proto: SSKProtoTextAttachment, transaction: SDSAnyWriteTransaction) throws { - guard let text = proto.text?.nilIfEmpty else { - throw OWSAssertionError("Missing text for attachment.") - } - self.text = text + self.text = proto.text?.nilIfEmpty guard let style = proto.textStyle else { throw OWSAssertionError("Missing style for attachment.")