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

fix: Resolve RichTextView scrolling issue & Prevent app freezing on tvOS #309

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// RichTextView-tvOS.swift
// Pulse
//
// Created by seunghwan Lee on 12/1/24.
//

#if os(tvOS)
import UIKit
import SwiftUI
import Pulse

struct ScrollableTextView: UIViewRepresentable {
var text: String?
var attributedText: NSAttributedString?

init(text: String? = nil, attributedText: AttributedString? = nil) {
self.text = text
if let attributedText {
self.attributedText = NSAttributedString(attributedText)
}
}

func makeUIView(context: UIViewRepresentableContext<ScrollableTextView>) -> UITextView {
let textView = UITextView()
textView.isScrollEnabled = true
textView.isUserInteractionEnabled = true
textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
textView.bounces = true
textView.contentInsetAdjustmentBehavior = .never
textView.insetsLayoutMarginsFromSafeArea = false
textView.showsVerticalScrollIndicator = true
textView.showsHorizontalScrollIndicator = false
textView.indicatorStyle = .white
return textView
}

func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<ScrollableTextView>) {
if let attributedText {
uiView.attributedText = attributedText
} else if let text {
uiView.text = text
}
}
}

struct RichTextView: View {
let viewModel: RichTextViewModel

var body: some View {
if let attributedText = viewModel.attributedString {
ScrollableTextView(attributedText: attributedText)
} else {
ScrollableTextView(text: viewModel.text)
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import SwiftUI
import Pulse

#if os(watchOS) || os(tvOS) || os(macOS)

#if os(watchOS) || os(macOS)
struct RichTextView: View {
let viewModel: RichTextViewModel

Expand All @@ -27,6 +26,7 @@ struct RichTextView: View {
#endif
}
}
#endif

final class RichTextViewModel: ObservableObject {
let text: String
Expand All @@ -49,5 +49,3 @@ final class RichTextViewModel: ObservableObject {
self.text = string.string
}
}

#endif