WIP: refactor message views to swift-ui

pull/874/head
ryanzhao 2 years ago
parent 39e316eb51
commit d2c05fc88d

@ -40,6 +40,14 @@ struct MessageInfoView: View {
MessageBubble( MessageBubble(
messageViewModel: messageViewModel messageViewModel: messageViewModel
) )
.background(
RoundedRectangle(cornerRadius: Self.cornerRadius)
.fill(
themeColor: (messageViewModel.variant == .standardIncoming || messageViewModel.variant == .standardIncomingDeleted ?
.messageBubble_incomingBackground :
.messageBubble_outgoingBackground)
)
)
.frame( .frame(
maxWidth: .infinity, maxWidth: .infinity,
maxHeight: .infinity, maxHeight: .infinity,
@ -362,197 +370,190 @@ struct MessageBubble: View {
static private let cornerRadius: CGFloat = 18 static private let cornerRadius: CGFloat = 18
let messageViewModel: MessageViewModel let messageViewModel: MessageViewModel
var bubbleBackgroundColor: ThemeValue {
messageViewModel.variant == .standardIncoming || messageViewModel.variant == .standardIncomingDeleted ? var bodyLabelTextColor: ThemeValue {
.messageBubble_incomingBackground : messageViewModel.variant == .standardOutgoing ?
.messageBubble_outgoingBackground .messageBubble_outgoingText :
.messageBubble_incomingText
} }
var body: some View { var body: some View {
ZStack { ZStack {
// switch messageViewModel.cellType { switch messageViewModel.cellType {
// case .typingIndicator, .dateHeader, .unreadMarker: break case .textOnlyMessage:
// let inset: CGFloat = 12
// case .textOnlyMessage: let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: messageViewModel) - 2 * inset)
// let inset: CGFloat = 12
// let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: messageViewModel) - 2 * inset) if let linkPreview: LinkPreview = messageViewModel.linkPreview {
// switch linkPreview.variant {
// if let linkPreview: LinkPreview = messageViewModel.linkPreview { case .standard:
// switch linkPreview.variant { LinkPreviewView_SwiftUI(
// case .standard: state: LinkPreview.SentState(
// LinkPreviewView_SwiftUI( linkPreview: linkPreview,
// state: LinkPreview.SentState( imageAttachment: messageViewModel.linkPreviewAttachment
// linkPreview: linkPreview, ),
// imageAttachment: messageViewModel.linkPreviewAttachment isOutgoing: (messageViewModel.variant == .standardOutgoing),
// ), maxWidth: maxWidth,
// isOutgoing: (messageViewModel.variant == .standardOutgoing), messageViewModel: messageViewModel,
// maxWidth: maxWidth, bodyLabelTextColor: bodyLabelTextColor,
// messageViewModel: messageViewModel, lastSearchText: nil
// bodyLabelTextColor: nil, )
// lastSearchText: nil
// ) case .openGroupInvitation:
// OpenGroupInvitationView_SwiftUI(
// case .openGroupInvitation: name: (linkPreview.title ?? ""),
// let openGroupInvitationView: OpenGroupInvitationView = OpenGroupInvitationView( url: linkPreview.url,
// name: (linkPreview.title ?? ""), textColor: bodyLabelTextColor,
// url: linkPreview.url, isOutgoing: (messageViewModel.variant == .standardOutgoing))
// textColor: bodyLabelTextColor, }
// isOutgoing: (cellViewModel.variant == .standardOutgoing) }
// ) // else {
// bubbleView.addSubview(openGroupInvitationView) // // Stack view
// bubbleView.pin(to: openGroupInvitationView) // let stackView = UIStackView(arrangedSubviews: [])
// snContentView.addArrangedSubview(bubbleBackgroundView) // stackView.axis = .vertical
// } // stackView.spacing = 2
// } //
// else { // // Quote view
// // Stack view // if let quote: Quote = messageViewModel.quote {
// let stackView = UIStackView(arrangedSubviews: []) // let hInset: CGFloat = 2
// stackView.axis = .vertical // let quoteView: QuoteView = QuoteView(
// stackView.spacing = 2 // for: .regular,
// // authorId: quote.authorId,
// // Quote view // quotedText: quote.body,
// if let quote: Quote = cellViewModel.quote { // threadVariant: cellViewModel.threadVariant,
// let hInset: CGFloat = 2 // currentUserPublicKey: cellViewModel.currentUserPublicKey,
// let quoteView: QuoteView = QuoteView( // currentUserBlinded15PublicKey: cellViewModel.currentUserBlinded15PublicKey,
// for: .regular, // currentUserBlinded25PublicKey: cellViewModel.currentUserBlinded25PublicKey,
// authorId: quote.authorId, // direction: (cellViewModel.variant == .standardOutgoing ?
// quotedText: quote.body, // .outgoing :
// threadVariant: cellViewModel.threadVariant, // .incoming
// currentUserPublicKey: cellViewModel.currentUserPublicKey, // ),
// currentUserBlinded15PublicKey: cellViewModel.currentUserBlinded15PublicKey, // attachment: cellViewModel.quoteAttachment,
// currentUserBlinded25PublicKey: cellViewModel.currentUserBlinded25PublicKey, // hInset: hInset,
// direction: (cellViewModel.variant == .standardOutgoing ? // maxWidth: maxWidth
// .outgoing : // )
// .incoming // let quoteViewContainer = UIView(wrapping: quoteView, withInsets: UIEdgeInsets(top: 0, leading: hInset, bottom: 0, trailing: hInset))
// ), // stackView.addArrangedSubview(quoteViewContainer)
// attachment: cellViewModel.quoteAttachment, // }
// hInset: hInset, //
// maxWidth: maxWidth // // Body text view
// ) // let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel(
// let quoteViewContainer = UIView(wrapping: quoteView, withInsets: UIEdgeInsets(top: 0, leading: hInset, bottom: 0, trailing: hInset)) // for: cellViewModel,
// stackView.addArrangedSubview(quoteViewContainer) // with: maxWidth,
// } // textColor: bodyLabelTextColor,
// // searchText: lastSearchText,
// // Body text view // delegate: self
// let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel( // )
// for: cellViewModel, // self.bodyTappableLabel = bodyTappableLabel
// with: maxWidth, // stackView.addArrangedSubview(bodyTappableLabel)
// textColor: bodyLabelTextColor, //
// searchText: lastSearchText, // // Constraints
// delegate: self // bubbleView.addSubview(stackView)
// ) // stackView.pin(to: bubbleView, withInset: inset)
// self.bodyTappableLabel = bodyTappableLabel // stackView.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth).isActive = true
// stackView.addArrangedSubview(bodyTappableLabel) // snContentView.addArrangedSubview(bubbleBackgroundView)
// // }
// // Constraints //
// bubbleView.addSubview(stackView) // case .mediaMessage:
// stackView.pin(to: bubbleView, withInset: inset) // // Body text view
// stackView.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth).isActive = true // if let body: String = cellViewModel.body, !body.isEmpty {
// snContentView.addArrangedSubview(bubbleBackgroundView) // let inset: CGFloat = 12
// } // let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: cellViewModel) - 2 * inset)
// // let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel(
// case .mediaMessage: // for: cellViewModel,
// // Body text view // with: maxWidth,
// if let body: String = cellViewModel.body, !body.isEmpty { // textColor: bodyLabelTextColor,
// let inset: CGFloat = 12 // searchText: lastSearchText,
// let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: cellViewModel) - 2 * inset) // delegate: self
// let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel( // )
// for: cellViewModel, //
// with: maxWidth, // self.bodyTappableLabel = bodyTappableLabel
// textColor: bodyLabelTextColor, // bubbleView.addSubview(bodyTappableLabel)
// searchText: lastSearchText, // bodyTappableLabel.pin(to: bubbleView, withInset: inset)
// delegate: self // snContentView.addArrangedSubview(bubbleBackgroundView)
// ) // }
// //
// self.bodyTappableLabel = bodyTappableLabel // // Album view
// bubbleView.addSubview(bodyTappableLabel) // let maxMessageWidth: CGFloat = VisibleMessageCell.getMaxWidth(for: cellViewModel)
// bodyTappableLabel.pin(to: bubbleView, withInset: inset) // let albumView = MediaAlbumView(
// snContentView.addArrangedSubview(bubbleBackgroundView) // mediaCache: mediaCache,
// } // items: (cellViewModel.attachments?
// // .filter { $0.isVisualMedia })
// // Album view // .defaulting(to: []),
// let maxMessageWidth: CGFloat = VisibleMessageCell.getMaxWidth(for: cellViewModel) // isOutgoing: (cellViewModel.variant == .standardOutgoing),
// let albumView = MediaAlbumView( // maxMessageWidth: maxMessageWidth
// mediaCache: mediaCache, // )
// items: (cellViewModel.attachments? // self.albumView = albumView
// .filter { $0.isVisualMedia }) // let size = getSize(for: cellViewModel)
// .defaulting(to: []), // albumView.set(.width, to: size.width)
// isOutgoing: (cellViewModel.variant == .standardOutgoing), // albumView.set(.height, to: size.height)
// maxMessageWidth: maxMessageWidth // albumView.loadMedia()
// ) // snContentView.addArrangedSubview(albumView)
// self.albumView = albumView //
// let size = getSize(for: cellViewModel) // unloadContent = { albumView.unloadMedia() }
// albumView.set(.width, to: size.width) //
// albumView.set(.height, to: size.height) // case .audio:
// albumView.loadMedia() // guard let attachment: Attachment = cellViewModel.attachments?.first(where: { $0.isAudio }) else {
// snContentView.addArrangedSubview(albumView) // return
// // }
// unloadContent = { albumView.unloadMedia() } //
// // let voiceMessageView: VoiceMessageView = VoiceMessageView()
// case .audio: // voiceMessageView.update(
// guard let attachment: Attachment = cellViewModel.attachments?.first(where: { $0.isAudio }) else { // with: attachment,
// return // isPlaying: (playbackInfo?.state == .playing),
// } // progress: (playbackInfo?.progress ?? 0),
// // playbackRate: (playbackInfo?.playbackRate ?? 1),
// let voiceMessageView: VoiceMessageView = VoiceMessageView() // oldPlaybackRate: (playbackInfo?.oldPlaybackRate ?? 1)
// voiceMessageView.update( // )
// with: attachment, //
// isPlaying: (playbackInfo?.state == .playing), // bubbleView.addSubview(voiceMessageView)
// progress: (playbackInfo?.progress ?? 0), // voiceMessageView.pin(to: bubbleView)
// playbackRate: (playbackInfo?.playbackRate ?? 1), // snContentView.addArrangedSubview(bubbleBackgroundView)
// oldPlaybackRate: (playbackInfo?.oldPlaybackRate ?? 1) // self.voiceMessageView = voiceMessageView
// ) //
// // case .genericAttachment:
// bubbleView.addSubview(voiceMessageView) // guard let attachment: Attachment = cellViewModel.attachments?.first else { preconditionFailure() }
// voiceMessageView.pin(to: bubbleView) //
// snContentView.addArrangedSubview(bubbleBackgroundView) // let inset: CGFloat = 12
// self.voiceMessageView = voiceMessageView // let maxWidth = (VisibleMessageCell.getMaxWidth(for: cellViewModel) - 2 * inset)
// //
// case .genericAttachment: // // Stack view
// guard let attachment: Attachment = cellViewModel.attachments?.first else { preconditionFailure() } // let stackView = UIStackView(arrangedSubviews: [])
// // stackView.axis = .vertical
// let inset: CGFloat = 12 // stackView.spacing = Values.smallSpacing
// let maxWidth = (VisibleMessageCell.getMaxWidth(for: cellViewModel) - 2 * inset) //
// // // Document view
// // Stack view // let documentView = DocumentView(attachment: attachment, textColor: bodyLabelTextColor)
// let stackView = UIStackView(arrangedSubviews: []) // stackView.addArrangedSubview(documentView)
// stackView.axis = .vertical //
// stackView.spacing = Values.smallSpacing // // Body text view
// // if let body: String = cellViewModel.body, !body.isEmpty { // delegate should always be set at this point
// // Document view // let bodyContainerView: UIView = UIView()
// let documentView = DocumentView(attachment: attachment, textColor: bodyLabelTextColor) // let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel(
// stackView.addArrangedSubview(documentView) // for: cellViewModel,
// // with: maxWidth,
// // Body text view // textColor: bodyLabelTextColor,
// if let body: String = cellViewModel.body, !body.isEmpty { // delegate should always be set at this point // searchText: lastSearchText,
// let bodyContainerView: UIView = UIView() // delegate: self
// let bodyTappableLabel = VisibleMessageCell.getBodyTappableLabel( // )
// for: cellViewModel, //
// with: maxWidth, // self.bodyTappableLabel = bodyTappableLabel
// textColor: bodyLabelTextColor, // bodyContainerView.addSubview(bodyTappableLabel)
// searchText: lastSearchText, // bodyTappableLabel.pin(.top, to: .top, of: bodyContainerView)
// delegate: self // bodyTappableLabel.pin(.leading, to: .leading, of: bodyContainerView, withInset: 12)
// ) // bodyTappableLabel.pin(.trailing, to: .trailing, of: bodyContainerView, withInset: -12)
// // bodyTappableLabel.pin(.bottom, to: .bottom, of: bodyContainerView, withInset: -12)
// self.bodyTappableLabel = bodyTappableLabel // stackView.addArrangedSubview(bodyContainerView)
// bodyContainerView.addSubview(bodyTappableLabel) // }
// bodyTappableLabel.pin(.top, to: .top, of: bodyContainerView) //
// bodyTappableLabel.pin(.leading, to: .leading, of: bodyContainerView, withInset: 12) // bubbleView.addSubview(stackView)
// bodyTappableLabel.pin(.trailing, to: .trailing, of: bodyContainerView, withInset: -12) // stackView.pin(to: bubbleView)
// bodyTappableLabel.pin(.bottom, to: .bottom, of: bodyContainerView, withInset: -12) // snContentView.addArrangedSubview(bubbleBackgroundView)
// stackView.addArrangedSubview(bodyContainerView) // }
// }
// default: EmptyView()
// bubbleView.addSubview(stackView) }
// stackView.pin(to: bubbleView)
// snContentView.addArrangedSubview(bubbleBackgroundView)
// }
} }
.background(
RoundedRectangle(cornerRadius: Self.cornerRadius)
.fill(themeColor: bubbleBackgroundColor)
)
} }
} }

Loading…
Cancel
Save