fix link preview with swift ui

pull/874/head
Ryan Zhao 1 year ago
parent 33bcefb710
commit 74b62e9857

@ -35,10 +35,17 @@ public struct LinkPreviewView_SwiftUI: View {
} }
public var body: some View { public var body: some View {
VStack( ZStack(
alignment: .leading, alignment: .leading
spacing: Values.mediumSpacing
) { ) {
if state is LinkPreview.SentState {
if #available(iOS 14.0, *) {
ThemeManager.currentTheme.colorSwiftUI(for: .messageBubble_overlay).ignoresSafeArea()
} else {
ThemeManager.currentTheme.colorSwiftUI(for: .messageBubble_overlay)
}
}
HStack( HStack(
alignment: .center, alignment: .center,
spacing: Values.mediumSpacing spacing: Values.mediumSpacing
@ -63,20 +70,27 @@ public struct LinkPreviewView_SwiftUI: View {
state is LinkPreview.DraftState || state is LinkPreview.SentState, state is LinkPreview.DraftState || state is LinkPreview.SentState,
let defaultImage: UIImage = UIImage(named: "Link")?.withRenderingMode(.alwaysTemplate) let defaultImage: UIImage = UIImage(named: "Link")?.withRenderingMode(.alwaysTemplate)
{ {
Image(uiImage: defaultImage) ZStack {
.foregroundColor( if #available(iOS 14.0, *) {
themeColor: isOutgoing ? ThemeManager.currentTheme.colorSwiftUI(for: .messageBubble_overlay).ignoresSafeArea()
.messageBubble_outgoingText : } else {
.messageBubble_incomingText ThemeManager.currentTheme.colorSwiftUI(for: .messageBubble_overlay)
) }
.frame(
width: imageSize, Image(uiImage: defaultImage)
height: imageSize .foregroundColor(
) themeColor: isOutgoing ?
.cornerRadius(state is LinkPreview.SentState ? 0 : 8) .messageBubble_outgoingText :
.messageBubble_incomingText
)
.cornerRadius(state is LinkPreview.SentState ? 0 : 8)
}
.frame(
width: imageSize,
height: imageSize
)
} else { } else {
ActivityIndicator() ActivityIndicator(themeColor: .borderSeparator, width: 2)
.foregroundColor(.black)
.frame( .frame(
width: Self.loaderSize, width: Self.loaderSize,
height: Self.loaderSize height: Self.loaderSize
@ -94,6 +108,8 @@ public struct LinkPreviewView_SwiftUI: View {
.messageBubble_outgoingText : .messageBubble_outgoingText :
.messageBubble_incomingText .messageBubble_incomingText
) )
.fixedSize(horizontal: false, vertical: true)
.padding(.trailing, Values.mediumSpacing)
} }
// Cancel button // Cancel button

@ -8,11 +8,17 @@ struct VoiceMessageView_SwiftUI: View {
@State var isPlaying: Bool = false @State var isPlaying: Bool = false
@State var time: String = "0:00" @State var time: String = "0:00"
@State var speed: String = "1.5×" @State var speed: String = "1.5×"
@State var progress: Double = 1.0 @State var progress: Double = 0.0
private static let width: CGFloat = 160 private static let width: CGFloat = 160
private static let toggleContainerSize: CGFloat = 20 private static let toggleContainerSize: CGFloat = 20
private var attachment: Attachment
public init(attachment: Attachment) {
self.attachment = attachment
}
var body: some View { var body: some View {
ZStack(alignment: .leading) { ZStack(alignment: .leading) {
Rectangle() Rectangle()
@ -40,6 +46,12 @@ struct VoiceMessageView_SwiftUI: View {
height: 8 height: 8
) )
} }
// ActivityIndicator(themeColor: .textPrimary, width: 2)
// .frame(
// width: Self.toggleContainerSize,
// height: Self.toggleContainerSize
// )
} }
Rectangle() Rectangle()
@ -59,7 +71,7 @@ struct VoiceMessageView_SwiftUI: View {
.font(.system(size: Values.smallFontSize)) .font(.system(size: Values.smallFontSize))
} }
} }
.padding(.horizontal, Values.smallSpacing) .padding(.all, Values.smallSpacing)
} }
.frame( .frame(
width: Self.width width: Self.width
@ -69,7 +81,7 @@ struct VoiceMessageView_SwiftUI: View {
struct VoiceMessageView_SwiftUI_Previews: PreviewProvider { struct VoiceMessageView_SwiftUI_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
VoiceMessageView_SwiftUI() VoiceMessageView_SwiftUI(attachment: Attachment(variant: .voiceMessage, contentType: "mp4", byteCount: 100))
.frame(height: 58) .frame(height: 58)
} }
} }

@ -384,68 +384,73 @@ struct MessageBubble: View {
ZStack { ZStack {
switch messageViewModel.cellType { switch messageViewModel.cellType {
case .textOnlyMessage: case .textOnlyMessage:
let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: messageViewModel) - 2 * Self.inset) let maxWidth: CGFloat = (VisibleMessageCell.getMaxWidth(for: messageViewModel) - 2 * Self.inset)
if let linkPreview: LinkPreview = messageViewModel.linkPreview { VStack(
switch linkPreview.variant { alignment: .leading,
case .standard: spacing: 0
LinkPreviewView_SwiftUI(
state: LinkPreview.SentState(
linkPreview: linkPreview,
imageAttachment: messageViewModel.linkPreviewAttachment
),
isOutgoing: (messageViewModel.variant == .standardOutgoing),
maxWidth: maxWidth,
messageViewModel: messageViewModel,
bodyLabelTextColor: bodyLabelTextColor,
lastSearchText: nil
)
case .openGroupInvitation:
OpenGroupInvitationView_SwiftUI(
name: (linkPreview.title ?? ""),
url: linkPreview.url,
textColor: bodyLabelTextColor,
isOutgoing: (messageViewModel.variant == .standardOutgoing))
}
}
// else {
// // Stack view
// let stackView = UIStackView(arrangedSubviews: [])
// stackView.axis = .vertical
// stackView.spacing = 2
//
// // Quote view
// if let quote: Quote = messageViewModel.quote {
// let hInset: CGFloat = 2
// let quoteView: QuoteView = QuoteView(
// for: .regular,
// authorId: quote.authorId,
// quotedText: quote.body,
// threadVariant: cellViewModel.threadVariant,
// currentUserPublicKey: cellViewModel.currentUserPublicKey,
// currentUserBlinded15PublicKey: cellViewModel.currentUserBlinded15PublicKey,
// currentUserBlinded25PublicKey: cellViewModel.currentUserBlinded25PublicKey,
// direction: (cellViewModel.variant == .standardOutgoing ?
// .outgoing :
// .incoming
// ),
// attachment: cellViewModel.quoteAttachment,
// hInset: hInset,
// maxWidth: maxWidth
// )
// let quoteViewContainer = UIView(wrapping: quoteView, withInsets: UIEdgeInsets(top: 0, leading: hInset, bottom: 0, trailing: hInset))
// stackView.addArrangedSubview(quoteViewContainer)
// }
if let bodyText: NSAttributedString = VisibleMessageCell.getBodyAttributedText(
for: messageViewModel,
theme: ThemeManager.currentTheme,
primaryColor: ThemeManager.primaryColor,
textColor: bodyLabelTextColor,
searchText: nil
) { ) {
AttributedText(bodyText) if let linkPreview: LinkPreview = messageViewModel.linkPreview {
.padding(.all, Self.inset) switch linkPreview.variant {
case .standard:
LinkPreviewView_SwiftUI(
state: LinkPreview.SentState(
linkPreview: linkPreview,
imageAttachment: messageViewModel.linkPreviewAttachment
),
isOutgoing: (messageViewModel.variant == .standardOutgoing),
maxWidth: maxWidth,
messageViewModel: messageViewModel,
bodyLabelTextColor: bodyLabelTextColor,
lastSearchText: nil
)
case .openGroupInvitation:
OpenGroupInvitationView_SwiftUI(
name: (linkPreview.title ?? ""),
url: linkPreview.url,
textColor: bodyLabelTextColor,
isOutgoing: (messageViewModel.variant == .standardOutgoing))
}
}
// else {
// // Stack view
// let stackView = UIStackView(arrangedSubviews: [])
// stackView.axis = .vertical
// stackView.spacing = 2
//
// // Quote view
// if let quote: Quote = messageViewModel.quote {
// let hInset: CGFloat = 2
// let quoteView: QuoteView = QuoteView(
// for: .regular,
// authorId: quote.authorId,
// quotedText: quote.body,
// threadVariant: cellViewModel.threadVariant,
// currentUserPublicKey: cellViewModel.currentUserPublicKey,
// currentUserBlinded15PublicKey: cellViewModel.currentUserBlinded15PublicKey,
// currentUserBlinded25PublicKey: cellViewModel.currentUserBlinded25PublicKey,
// direction: (cellViewModel.variant == .standardOutgoing ?
// .outgoing :
// .incoming
// ),
// attachment: cellViewModel.quoteAttachment,
// hInset: hInset,
// maxWidth: maxWidth
// )
// let quoteViewContainer = UIView(wrapping: quoteView, withInsets: UIEdgeInsets(top: 0, leading: hInset, bottom: 0, trailing: hInset))
// stackView.addArrangedSubview(quoteViewContainer)
// }
if let bodyText: NSAttributedString = VisibleMessageCell.getBodyAttributedText(
for: messageViewModel,
theme: ThemeManager.currentTheme,
primaryColor: ThemeManager.primaryColor,
textColor: bodyLabelTextColor,
searchText: nil
) {
AttributedText(bodyText)
.padding(.all, Self.inset)
}
} }
case .mediaMessage: case .mediaMessage:
if let bodyText: NSAttributedString = VisibleMessageCell.getBodyAttributedText( if let bodyText: NSAttributedString = VisibleMessageCell.getBodyAttributedText(
@ -458,13 +463,12 @@ struct MessageBubble: View {
AttributedText(bodyText) AttributedText(bodyText)
.padding(.all, Self.inset) .padding(.all, Self.inset)
} }
case .audio: case .voiceMessage:
if let attachment: Attachment = messageViewModel.attachments?.first(where: { $0.isAudio }){ if let attachment: Attachment = messageViewModel.attachments?.first(where: { $0.isAudio }){
// TODO: Playback Info and check if playing function is needed // TODO: Playback Info and check if playing function is needed
VoiceMessageView_SwiftUI() VoiceMessageView_SwiftUI(attachment: attachment)
.padding(.all, Self.inset)
} }
case .genericAttachment: case .audio, .genericAttachment:
if let attachment: Attachment = messageViewModel.attachments?.first { if let attachment: Attachment = messageViewModel.attachments?.first {
VStack(spacing: Values.smallSpacing) { VStack(spacing: Values.smallSpacing) {
DocumentView_SwiftUI(attachment: attachment, textColor: bodyLabelTextColor) DocumentView_SwiftUI(attachment: attachment, textColor: bodyLabelTextColor)

@ -1,21 +1,30 @@
// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. // Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import SwiftUI import SwiftUI
import SessionUIKit
public struct ActivityIndicator: View { public struct ActivityIndicator: View {
@State private var strokeStart: Double = 0.95 @State private var strokeStart: Double = 0.95
@State private var strokeEnd: Double = 1.0 @State private var strokeEnd: Double = 1.0
@State private var shorten: Bool = false @State private var shorten: Bool = false
@State private var isRotating: Bool = false @State private var isRotating: Bool = false
private var themeColor: ThemeValue
private var width: CGFloat
public init(themeColor: ThemeValue, width: CGFloat) {
self.themeColor = themeColor
self.width = width
}
public var body: some View { public var body: some View {
GeometryReader { (geometry: GeometryProxy) in GeometryReader { (geometry: GeometryProxy) in
Circle() Circle()
.trim(from: strokeStart, to: strokeEnd) .trim(from: strokeStart, to: strokeEnd)
.stroke( .stroke(
themeColor: .borderSeparator, themeColor: themeColor,
style: StrokeStyle( style: StrokeStyle(
lineWidth: 2, lineWidth: width,
lineCap: .round lineCap: .round
) )
) )
@ -36,7 +45,7 @@ public struct ActivityIndicator: View {
} }
self.trimStroke() self.trimStroke()
Timer.scheduledTimerOnMainThread(withTimeInterval: 1.5, repeats: true) { _ in Timer.scheduledTimer(withTimeInterval: 1.5, repeats: true) { _ in
self.trimStroke() self.trimStroke()
} }
} }
@ -65,8 +74,7 @@ public struct ActivityIndicator: View {
struct ActivityIndicator_Previews: PreviewProvider { struct ActivityIndicator_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ActivityIndicator() ActivityIndicator(themeColor: .textPrimary, width: 2)
.foregroundColor(.black)
.frame( .frame(
width: 40, width: 40,
height: 40 height: 40

Loading…
Cancel
Save