2024-07-12 11:43:14 +00:00
|
|
|
import AVKit
|
2024-07-11 13:59:24 +00:00
|
|
|
import MapKit
|
2024-06-27 11:39:41 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ConversationMessageContainer: View {
|
|
|
|
let message: Message
|
|
|
|
let isOutgoing: Bool
|
|
|
|
|
|
|
|
var body: some View {
|
2024-07-11 15:14:31 +00:00
|
|
|
if let msgText = message.body, msgText.isLocation {
|
|
|
|
EmbededMapView(location: msgText.getLatLon)
|
|
|
|
} else if let attachmentId = message.attachmentId {
|
|
|
|
AttachmentView(attachmentId: attachmentId)
|
2024-07-11 13:59:24 +00:00
|
|
|
} else {
|
2024-07-11 15:14:31 +00:00
|
|
|
Text(message.body ?? "...")
|
2024-07-11 13:59:24 +00:00
|
|
|
.font(.body2)
|
|
|
|
.foregroundColor(.Material.Text.main)
|
|
|
|
.multilineTextAlignment(.leading)
|
|
|
|
.padding(10)
|
|
|
|
}
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MessageAttr: View {
|
|
|
|
let message: Message
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
Text(message.date, style: .time)
|
|
|
|
.font(.sub2)
|
2024-07-04 08:21:12 +00:00
|
|
|
.foregroundColor(.Material.Shape.separator)
|
2024-06-27 11:39:41 +00:00
|
|
|
Spacer()
|
|
|
|
if message.sentError {
|
|
|
|
Image(systemName: "exclamationmark.circle")
|
|
|
|
.font(.body3)
|
2024-07-04 08:21:12 +00:00
|
|
|
.foregroundColor(.Rainbow.red500)
|
2024-06-27 11:39:41 +00:00
|
|
|
} else if message.pending {
|
|
|
|
Image(systemName: "clock")
|
|
|
|
.font(.body3)
|
2024-07-04 08:21:12 +00:00
|
|
|
.foregroundColor(.Material.Shape.separator)
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 13:59:24 +00:00
|
|
|
|
|
|
|
private struct EmbededMapView: View {
|
|
|
|
let location: CLLocationCoordinate2D
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Map(
|
|
|
|
coordinateRegion: .constant(MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))),
|
|
|
|
interactionModes: [],
|
|
|
|
showsUserLocation: false,
|
|
|
|
userTrackingMode: .none,
|
|
|
|
annotationItems: [location],
|
|
|
|
annotationContent: { _ in
|
|
|
|
MapMarker(coordinate: location, tint: .blue)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.frame(width: Const.mapPreviewSize, height: Const.mapPreviewSize)
|
|
|
|
.onTapGesture {
|
|
|
|
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: location))
|
|
|
|
mapItem.name = "Location"
|
|
|
|
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 15:14:31 +00:00
|
|
|
|
|
|
|
private struct AttachmentView: View {
|
2024-07-11 15:46:57 +00:00
|
|
|
@EnvironmentObject var store: AppStore
|
|
|
|
|
2024-07-11 15:14:31 +00:00
|
|
|
let attachmentId: String
|
|
|
|
|
|
|
|
var body: some View {
|
2024-07-12 11:43:14 +00:00
|
|
|
if let attachment {
|
|
|
|
switch attachment.type {
|
|
|
|
case .image:
|
|
|
|
if let thumbnail = thumbnail() {
|
|
|
|
thumbnail
|
|
|
|
.resizable()
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
.frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize)
|
|
|
|
} else {
|
|
|
|
placeholder
|
|
|
|
}
|
|
|
|
|
|
|
|
case .movie:
|
|
|
|
if let file = attachment.localPath {
|
|
|
|
VideoPlayerView(url: file)
|
|
|
|
.frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize)
|
|
|
|
.cornerRadius(Const.attachmentPreviewSize / 10)
|
|
|
|
.overlay(RoundedRectangle(cornerRadius: Const.attachmentPreviewSize / 10).stroke(Color.Material.Shape.separator, lineWidth: 1))
|
|
|
|
} else {
|
|
|
|
placeholder
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
placeholder
|
2024-07-11 15:46:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-12 11:43:14 +00:00
|
|
|
placeholder
|
2024-07-11 15:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 11:43:14 +00:00
|
|
|
@ViewBuilder private var placeholder: some View {
|
|
|
|
Rectangle()
|
|
|
|
.foregroundColor(.Material.Background.dark)
|
|
|
|
.frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize)
|
|
|
|
.overlay {
|
|
|
|
ZStack {
|
|
|
|
ProgressView()
|
|
|
|
.scaleEffect(1.5)
|
|
|
|
.progressViewStyle(CircularProgressViewStyle(tint: .Material.Elements.active))
|
|
|
|
if let attachment {
|
|
|
|
let imageName = progressImageName(attachment.type)
|
|
|
|
Image(systemName: imageName)
|
|
|
|
.font(.body1)
|
|
|
|
.foregroundColor(.Material.Elements.active)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var attachment: Attachment? {
|
|
|
|
store.state.conversationsState.currentAttachments.first(where: { $0.id == attachmentId })
|
|
|
|
}
|
|
|
|
|
2024-07-11 15:46:57 +00:00
|
|
|
private func progressImageName(_ type: AttachmentType) -> String {
|
|
|
|
switch type {
|
|
|
|
case .image:
|
|
|
|
return "photo"
|
|
|
|
case .audio:
|
|
|
|
return "music.note"
|
|
|
|
case .movie:
|
|
|
|
return "film"
|
|
|
|
case .file:
|
|
|
|
return "doc"
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 11:43:14 +00:00
|
|
|
|
|
|
|
private func thumbnail() -> Image? {
|
|
|
|
guard let attachment = attachment else { return nil }
|
|
|
|
guard let thumbnailPath = attachment.localThumbnailPath else { return nil }
|
|
|
|
guard let uiImage = UIImage(contentsOfFile: thumbnailPath.path()) else { return nil }
|
|
|
|
return Image(uiImage: uiImage)
|
|
|
|
}
|
2024-07-11 15:46:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 11:43:14 +00:00
|
|
|
private struct VideoPlayerView: UIViewControllerRepresentable {
|
|
|
|
let url: URL
|
2024-07-11 15:46:57 +00:00
|
|
|
|
2024-07-12 11:43:14 +00:00
|
|
|
func makeUIViewController(context _: Context) -> AVPlayerViewController {
|
|
|
|
let controller = AVPlayerViewController()
|
|
|
|
controller.player = AVPlayer(url: url)
|
|
|
|
return controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIViewController(_: AVPlayerViewController, context _: Context) {
|
|
|
|
// Update the controller if needed.
|
2024-07-11 15:14:31 +00:00
|
|
|
}
|
|
|
|
}
|