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

Use the website name and description in the card when available #901

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public final class Card: NSManagedObject {
@NSManaged public private(set) var status: Status
}

extension Card {
public var websiteName: String? {
if let authorName, !authorName.isEmpty {
return authorName
} else if let host = url?.host {
return host
}
return nil
}
}

extension Card {

@discardableResult
Expand Down
34 changes: 24 additions & 10 deletions MastodonSDK/Sources/MastodonUI/View/Content/StatusCardControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public final class StatusCardControl: UIControl {
private let dividerView = UIView()
private let imageView = UIImageView()
private let titleLabel = UILabel()
private let linkLabel = UILabel()
private let descriptionLabel = UILabel()
private let siteNameLabel = UILabel()
private lazy var showEmbedButton: UIButton = {
var configuration = UIButton.Configuration.gray()
configuration.background.visualEffect = UIBlurEffect(style: .systemUltraThinMaterial)
Expand Down Expand Up @@ -85,11 +86,20 @@ public final class StatusCardControl: UIControl {

titleLabel.numberOfLines = 2
titleLabel.textColor = Asset.Colors.Label.primary.color
titleLabel.font = .preferredFont(forTextStyle: .body)

linkLabel.numberOfLines = 1
linkLabel.textColor = Asset.Colors.Label.secondary.color
linkLabel.font = .preferredFont(forTextStyle: .subheadline)
let descriptor = UIFont.preferredFont(forTextStyle: .body)
.fontDescriptor
.addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: UIFont.Weight.medium]])
titleLabel.font = UIFontMetrics(forTextStyle: .body)
.scaledFont(for: UIFont(descriptor: descriptor, size: descriptor.pointSize))
titleLabel.adjustsFontForContentSizeCategory = false

descriptionLabel.numberOfLines = 2
descriptionLabel.textColor = Asset.Colors.Label.primary.color
descriptionLabel.font = .preferredFont(forTextStyle: .caption1)

siteNameLabel.numberOfLines = 1
siteNameLabel.textColor = Asset.Colors.Label.secondary.color
siteNameLabel.font = .preferredFont(forTextStyle: .subheadline)

imageView.tintColor = Asset.Colors.Label.secondary.color
imageView.contentMode = .scaleAspectFill
Expand All @@ -100,7 +110,8 @@ public final class StatusCardControl: UIControl {
imageView.setContentCompressionResistancePriority(.zero, for: .vertical)

labelStackView.addArrangedSubview(titleLabel)
labelStackView.addArrangedSubview(linkLabel)
labelStackView.addArrangedSubview(descriptionLabel)
labelStackView.addArrangedSubview(siteNameLabel)
labelStackView.layoutMargins = .init(top: 10, left: 10, bottom: 10, right: 10)
labelStackView.isLayoutMarginsRelativeArrangement = true
labelStackView.axis = .vertical
Expand Down Expand Up @@ -139,14 +150,15 @@ public final class StatusCardControl: UIControl {

public func configure(card: Card) {
let title = card.title.trimmingCharacters(in: .whitespacesAndNewlines)
if let host = card.url?.host {
accessibilityLabel = "\(title) \(host)"
if let websiteName = card.websiteName {
accessibilityLabel = "\(title) \(websiteName)"
} else {
accessibilityLabel = title
}

titleLabel.text = title
linkLabel.text = card.url?.host
descriptionLabel.text = card.desc.trimmingCharacters(in: .whitespacesAndNewlines)
siteNameLabel.text = card.websiteName
imageView.contentMode = .center

imageView.sd_setImage(
Expand Down Expand Up @@ -208,6 +220,7 @@ public final class StatusCardControl: UIControl {
.constraint(lessThanOrEqualToConstant: 400),
]
dividerConstraint = dividerView.heightAnchor.constraint(equalToConstant: pixelSize).activate()
descriptionLabel.isHidden = false
case .compact:
containerStackView.alignment = .center
containerStackView.axis = .horizontal
Expand All @@ -219,6 +232,7 @@ public final class StatusCardControl: UIControl {
dividerView.heightAnchor.constraint(equalTo: containerStackView.heightAnchor),
]
dividerConstraint = dividerView.widthAnchor.constraint(equalToConstant: pixelSize).activate()
descriptionLabel.isHidden = true
}

NSLayoutConstraint.activate(layoutConstraints)
Expand Down