get rid of additional UIHostingController. Cleanup and comments.

This commit is contained in:
David Langley
2022-10-12 14:14:25 +01:00
parent e525bd84f9
commit 89c643734c
9 changed files with 93 additions and 56 deletions

View File

@@ -22,26 +22,32 @@ import Combine
import UIKit
import CoreGraphics
class SelfSizingHostingController<Content>: UIHostingController<Content> where Content: View {
var heightSubject = CurrentValueSubject<CGFloat, Never>(0)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = sizeThatFits(in: CGSize(width: self.view.frame.width, height: 800)).height
heightSubject.send(height)
}
}
@objc protocol HtmlRoomInputToolbarViewProtocol: RoomInputToolbarViewProtocol {
@objc func setHtml(content: String)
}
// The toolbar for editing with rich text
class WysiwygInputToolbarView: MXKRoomInputToolbarView, NibLoadable, HtmlRoomInputToolbarViewProtocol {
// MARK: - Properties
// MARK: Private
private var cancellables = Set<AnyCancellable>()
private var heightConstraint: NSLayoutConstraint!
private var hostingViewController: VectorHostingController!
private var viewModel: WysiwygComposerViewModel!
// MARK: Public
/// The display name to show when in edit/reply
var eventSenderDisplayName: String!
/// Whether the composer is in send, reply or edit mode.
var sendMode: RoomInputToolbarViewSendMode = .send
// MARK: - Setup
override class func instantiate() -> MXKRoomInputToolbarView! {
return loadFromNib()
}
@@ -50,15 +56,10 @@ class WysiwygInputToolbarView: MXKRoomInputToolbarView, NibLoadable, HtmlRoomInp
return (delegate as? RoomInputToolbarViewDelegate) ?? nil
}
private var cancellables = Set<AnyCancellable>()
private var heightConstraint: NSLayoutConstraint!
private var hostingViewController: SelfSizingHostingController<Composer>!
private static let minToolbarHeight: CGFloat = 100
override func awakeFromNib() {
super.awakeFromNib()
let viewModel = WysiwygComposerViewModel()
viewModel = WysiwygComposerViewModel()
let composer = Composer(viewModel: viewModel, sendMessageAction: { [weak self] content in
guard let self = self else { return }
self.sendWysiwygMessage(content: content)
@@ -67,8 +68,9 @@ class WysiwygInputToolbarView: MXKRoomInputToolbarView, NibLoadable, HtmlRoomInp
self.showSendMediaActions()
})
hostingViewController = SelfSizingHostingController(rootView: composer)
let height = hostingViewController.sizeThatFits(in: CGSize(width: self.frame.width, height: 800)).height
hostingViewController = VectorHostingController(rootView: composer)
hostingViewController.publishHeightChanges = true
let height = hostingViewController.sizeThatFits(in: CGSize(width: self.frame.width, height: UIView.layoutFittingExpandedSize.height)).height
let subView: UIView = hostingViewController.view
self.addSubview(subView)
@@ -82,46 +84,43 @@ class WysiwygInputToolbarView: MXKRoomInputToolbarView, NibLoadable, HtmlRoomInp
subView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
cancellables = [
hostingViewController.heightSubject
hostingViewController.heightPublisher
.removeDuplicates()
.sink(receiveValue: { [weak self] idealHeight in
guard let self = self else { return }
let h = self.hostingViewController.sizeThatFits(in: CGSize(width: self.frame.width, height: 800)).height
self.updateToolbarHeight(wysiwygHeight: h)
self.updateToolbarHeight(wysiwygHeight: idealHeight)
})
]
update(theme: ThemeService.shared().theme)
registerThemeServiceDidChangeThemeNotification()
}
override func customizeRendering() {
super.customizeRendering()
self.backgroundColor = .clear
}
// MARK: - Public
/// Set the html content on the composer
/// - Parameter content: The html string
func setHtml(content: String) {
hostingViewController.rootView.viewModel.setHtmlContent(content)
viewModel.setHtmlContent(content)
}
func setVoiceMessageToolbarView(_ voiceMessageToolbarView: UIView!) {
//TODO embed the voice messages UI
}
// MARK: - Private
func toolbarHeight() -> CGFloat {
return heightConstraint.constant
}
private func updateToolbarHeight(wysiwygHeight: CGFloat) {
self.heightConstraint.constant = wysiwygHeight
toolbarViewDelegate?.roomInputToolbarView?(self, heightDidChanged: wysiwygHeight, completion: nil)
private func updateToolbarHeight(wysiwygHeight: CGFloat) {
self.heightConstraint.constant = wysiwygHeight
toolbarViewDelegate?.roomInputToolbarView?(self, heightDidChanged: wysiwygHeight, completion: nil)
}
private func sendWysiwygMessage(content: WysiwygComposerContent) {
delegate?.roomInputToolbarView?(self, sendFormattedTextMessage: content.html, withRawText: content.plainText)
}
private func showSendMediaActions() {
delegate?.roomInputToolbarViewShowSendMediaActions?(self)
}
@@ -137,4 +136,16 @@ class WysiwygInputToolbarView: MXKRoomInputToolbarView, NibLoadable, HtmlRoomInp
private func update(theme: Theme) {
hostingViewController.view.backgroundColor = theme.colors.background
}
// MARK: - RoomInputToolbarViewProtocol
/// Add the voice message toolbar to the composer
/// - Parameter voiceMessageToolbarView: the voice message toolbar UIView
func setVoiceMessageToolbarView(_ voiceMessageToolbarView: UIView!) {
// TODO embed the voice messages UI
}
func toolbarHeight() -> CGFloat {
return heightConstraint.constant
}
}