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

Fixed incorrectly formatted media descriptions. #1067

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Alexk2309
Copy link

@Alexk2309 Alexk2309 commented May 24, 2024

Video descriptions often had HTML tags that weren't parsed correctly for example.

image

This change corrects the formatting so it now looks like
image

@Alexk2309 Alexk2309 closed this May 24, 2024
@Alexk2309 Alexk2309 reopened this May 24, 2024
Copy link
Member

@LePips LePips left a comment

Choose a reason for hiding this comment

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

I apologize but this work may be a lot more than you probably had anticipated. We "officially" support HTML and Markdown for item descriptions (and the server disclaimer).

However if you find a package that does what we need, we can use that instead.


struct HTMLFormattedText: UIViewRepresentable {
let text: String
private let textView = UITextView()
Copy link
Member

@LePips LePips May 24, 2024

Choose a reason for hiding this comment

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

Created views go within makeUIView, not at the top level.


import SwiftUI

struct HTMLFormattedText: UIViewRepresentable {
Copy link
Member

Choose a reason for hiding this comment

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

I asked our team and we "officially" support HTML and Markdown, so this view would have to accommodate both.

return textView
}

func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) {
Copy link
Member

Choose a reason for hiding this comment

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

Change UIViewRepresentableContext<Self>) to just Context.

self.text = content
}

func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
Copy link
Member

Choose a reason for hiding this comment

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

Change UIViewRepresentableContext<Self>) to just Context.

let range = NSRange(location: 0, length: attributedString.length)
attributedString.enumerateAttribute(.font, in: range, options: []) { value, range, _ in
if let oldFont = value as? UIFont {
let fontSize = UIScreen.main.bounds.width * 0.05 // Adjust the multiplier as needed
Copy link
Member

Choose a reason for hiding this comment

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

We can't determine a font size based on the screen width. We should be using a provided system font size and the San Francisco font, not whatever serif font is being rendered.

}

func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) {
DispatchQueue.main.async {
Copy link
Member

Choose a reason for hiding this comment

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

This isn't necessary. Nothing is being updated externally that requires this view to update itself.

import SwiftUI

struct HTMLFormattedText: UIViewRepresentable {
let text: String
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let text: String
private let text: String

let text: String
private let textView = UITextView()

init(_ content: String) {
Copy link
Member

Choose a reason for hiding this comment

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

Change content to text.

}

func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
textView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
Copy link
Member

Choose a reason for hiding this comment

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

UITextViews in SwiftUI are very tricky to get right. I actually just helped someone make a UITextView wrapper so this came at the right time. For sizing we shouldn't set constraints and instead have to go to setContentCompressionResistancePriority.

Here is the code that I helped the other person with, so you'll have to decipher it a bit, but it is an example of how to wrap a UITextView in SwiftUI. I am unsure how it works with HTML or Markdown, so you will have to test that. This work may be bigger than you expected.

}
}

private func converHTML(text: String) -> NSAttributedString? {
Copy link
Member

Choose a reason for hiding this comment

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

We will have to determine whether the text has HTML and then "render" it as HTML.

@Alexk2309
Copy link
Author

I apologize but this work may be a lot more than you probably had anticipated. We "officially" support HTML and Markdown for item descriptions (and the server disclaimer).

However if you find a package that does what we need, we can use that instead.

Hi if this is the case meaning html is supported how come the tags aren't being rendered correctly. Also does that mean i would have to find a package that converts html to markdown ? Because i know swift already supports markdown .

@LePips
Copy link
Member

LePips commented May 25, 2024

My comment was that HTML and Markdown support was implemented, but that since we "support both", we need to implement support for both.

SwiftUI sadly only supports basic inline Markdown attributes (bold, italic) but not stuff like headers or bullet points. So, technically, even Markdown needs proper implementation.

We don't need something to convert HTML to Markdown, but something that supports both of them.

@LePips
Copy link
Member

LePips commented Jun 8, 2024

I'm actually going to have to think about this a bit more. While we/other clients "officially" support HTML, I think that this would be too large of a hassle for all clients to support and would advocate for stripping HTML on the server.

@chickdan
Copy link
Contributor

chickdan commented Jun 13, 2024

I tested the solution in this SO post (NSAttributedString) and it worked. Text can also handle Markdown natively (albeit limited) so maybe if there was a function to detect html (regex aught to do it) and return the overview as NSAttributedString otherwise return as a String. That would save having to use UIVIewRepresentable to make a UITextView and resolve the issue of supporting both formats.

Edit: also I noticed that .multilineTextAlignment(.leading) for some reason messes up the markdown (gotta love the random quirks of SwiftUI). Putting this on the VStack fixes it, plus it cleans up some redundancy since it's applied to both Text elements in the stack.

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.

3 participants