SwiftTerm icon indicating copy to clipboard operation
SwiftTerm copied to clipboard

Content Overlap When Scrolling & Updating

Open hiDandelion opened this issue 1 year ago • 1 comments

Describe the bug TerminalView doesn't update correctly when scrolling or update its content. Updates to the view lead to content overlapping.

To Reproduce My code for reproducing this issue.

class TerminalViewController: UIViewController {
    var terminalView: TerminalView?
    var terminalViewModel: TerminalViewModel?
    private var cancellables = Set<AnyCancellable>()
    
    init(terminalView: TerminalView, terminalViewModel: TerminalViewModel) {
        self.terminalView = terminalView
        self.terminalViewModel = terminalViewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let terminalView = terminalView else {
            return
        }
        
        self.terminalView = terminalView
        
        terminalView.frame = view.frame
        terminalView.translatesAutoresizingMaskIntoConstraints = true
        terminalView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        view.addSubview(terminalView)
        
        setupBindings()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    func setupBindings() {
        terminalViewModel?.$messages
            .receive(on: DispatchQueue.main)
            .sink { [weak self] messages in
                self?.updateTerminalContent(messages.compactMap { $0.content }.joined())
            }
            .store(in: &cancellables)
    }
    
    func updateTerminalContent(_ content: String) {
        terminalView?.feed(text: content)
    }
}

struct AppTerminalView: UIViewControllerRepresentable {
    typealias UIViewControllerType = TerminalViewController
    
    @ObservedObject var terminalViewModel: TerminalViewModel
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<AppTerminalView>) -> TerminalViewController {
        let terminalView = TerminalView()
        let terminalViewController = TerminalViewController(terminalView: terminalView, terminalViewModel: terminalViewModel)
        
        return terminalViewController
    }
    
    func updateUIViewController(_ uiViewController: TerminalViewController, context: UIViewControllerRepresentableContext<AppTerminalView>) {
        
    }
}

Expected behavior TerminalView handles scrolling correctly.

Screenshots Initial Feed: Simulator Screenshot - iPhone 15 Pro - 2024-08-23 at 19 48 14 Scrolling: Simulator Screenshot - iPhone 15 Pro - 2024-08-23 at 19 48 28 Updating: Simulator Screenshot - iPhone 15 Pro - 2024-08-23 at 19 48 50

Smartphone (please complete the following information):

  • Device: iPhone 15 Pro Simulator
  • OS: iOS 17.5

hiDandelion avatar Aug 24 '24 02:08 hiDandelion

Any solution?

Agnakaraara avatar Oct 18 '24 09:10 Agnakaraara

Must be something in your code, does not happen for me with the sample app.

migueldeicaza avatar Oct 23 '24 13:10 migueldeicaza