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
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
4 changes: 4 additions & 0 deletions Swiftfin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
091B5A8A2683142E00D78B61 /* ServerDiscovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091B5A872683142E00D78B61 /* ServerDiscovery.swift */; };
091B5A8D268315D400D78B61 /* ServerDiscovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091B5A872683142E00D78B61 /* ServerDiscovery.swift */; };
49C012192C007C5000F7B130 /* HTMLFormattedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49C012182C007C5000F7B130 /* HTMLFormattedText.swift */; };
4E5E48E52AB59806003F1B48 /* CustomizeViewsSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E5E48E42AB59806003F1B48 /* CustomizeViewsSettings.swift */; };
4E8B34EA2AB91B6E0018F305 /* ItemFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E8B34E92AB91B6E0018F305 /* ItemFilter.swift */; };
4E8B34EB2AB91B6E0018F305 /* ItemFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E8B34E92AB91B6E0018F305 /* ItemFilter.swift */; };
Expand Down Expand Up @@ -917,6 +918,7 @@

/* Begin PBXFileReference section */
091B5A872683142E00D78B61 /* ServerDiscovery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerDiscovery.swift; sourceTree = "<group>"; };
49C012182C007C5000F7B130 /* HTMLFormattedText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTMLFormattedText.swift; sourceTree = "<group>"; };
4E5E48E42AB59806003F1B48 /* CustomizeViewsSettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomizeViewsSettings.swift; sourceTree = "<group>"; };
4E8B34E92AB91B6E0018F305 /* ItemFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemFilter.swift; sourceTree = "<group>"; };
531690E6267ABD79005D8AB9 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2058,6 +2060,7 @@
E1581E26291EF59800D6C640 /* SplitContentView.swift */,
E1D27EE62BBC955F00152D16 /* UnmaskSecureField.swift */,
E157562F29355B7900976E1F /* UpdateView.swift */,
49C012182C007C5000F7B130 /* HTMLFormattedText.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -4211,6 +4214,7 @@
E18E0208288749200022598C /* BlurView.swift in Sources */,
E18E01E7288747230022598C /* CollectionItemContentView.swift in Sources */,
E1E1643F28BB075C00323B0A /* SelectorView.swift in Sources */,
49C012192C007C5000F7B130 /* HTMLFormattedText.swift in Sources */,
C46DD8D22A8DC1F60046A504 /* LiveVideoPlayerCoordinator.swift in Sources */,
E18ACA8B2A14301800BB4F35 /* ScalingButtonStyle.swift in Sources */,
E18E01DF288747230022598C /* iPadOSMovieItemView.swift in Sources */,
Expand Down
62 changes: 62 additions & 0 deletions Swiftfin/Components/HTMLFormattedText.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors
//

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.

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

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.


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.

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.

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.

textView.isSelectable = false
textView.isUserInteractionEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isScrollEnabled = false
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.

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.

if let attributeText = self.converHTML(text: text) {
textView.attributedText = attributeText
} else {
textView.text = ""
}
}
}

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.

guard let data = text.data(using: .utf8) else {
return nil
}

if let attributedString = try? NSMutableAttributedString(
data: data,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil
) {
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.

let dynamicFont = UIFont.systemFont(ofSize: fontSize)
let newFont = oldFont.withSize(dynamicFont.pointSize)
attributedString.addAttributes([.font: newFont], range: range)
}
}
return attributedString
} else {
return nil
}
}
}
4 changes: 1 addition & 3 deletions Swiftfin/Views/ItemOverviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ struct ItemOverviewView: View {
}

if let itemOverview = item.overview {
Text(itemOverview)
.font(.body)
.multilineTextAlignment(.leading)
HTMLFormattedText(itemOverview)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down