2022-01-04 22:28:18 +08:00
|
|
|
//
|
|
|
|
// MessageItemView.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2021/12/29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-01-10 00:26:29 +08:00
|
|
|
import MarkdownUI
|
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import Photos
|
2022-01-04 22:28:18 +08:00
|
|
|
|
|
|
|
struct MessageItemView: View {
|
2022-01-15 22:13:23 +08:00
|
|
|
let messageItem: MessageModel
|
2022-01-04 22:28:18 +08:00
|
|
|
/// 删除按钮点击的回调
|
|
|
|
let deleteAction : () -> ()
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
HStack {
|
|
|
|
HLine().stroke(Color(UIColor.lightGray))
|
|
|
|
.frame(width: 20, height: 1)
|
|
|
|
Image("avatar2")
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
|
|
|
.frame(width: 38, height: 38)
|
2022-01-15 22:13:23 +08:00
|
|
|
Text(messageItem.pushkey_name ?? "")
|
2022-01-04 22:28:18 +08:00
|
|
|
.font(.system(size: 14))
|
2022-01-16 00:10:47 +08:00
|
|
|
.foregroundColor(Color("text2Color"))
|
2022-01-10 00:26:29 +08:00
|
|
|
Text(messageItem.createdDateStr)
|
2022-01-04 22:28:18 +08:00
|
|
|
.font(.system(size: 12))
|
2022-01-16 00:10:47 +08:00
|
|
|
.foregroundColor(Color("text2Color"))
|
2022-01-04 22:28:18 +08:00
|
|
|
HLine().stroke(Color(UIColor.lightGray))
|
|
|
|
.frame(height: 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
DeletableView(contentView: {
|
2022-01-10 00:26:29 +08:00
|
|
|
MessageContentView(messageItem: messageItem)
|
|
|
|
}, deleteAction: deleteAction)
|
|
|
|
.padding(messageItem.type == "image" ? EdgeInsets.init() : EdgeInsets(top: 10, leading: 26, bottom: 0, trailing: 24))
|
|
|
|
|
|
|
|
}
|
|
|
|
.padding(.top, 25)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MessageContentView: View {
|
2022-01-15 22:13:23 +08:00
|
|
|
let messageItem: MessageModel
|
2022-01-10 00:26:29 +08:00
|
|
|
@State private var image: PlatformImage? = nil
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
switch messageItem.type {
|
|
|
|
case "markdown":
|
|
|
|
CardView {
|
|
|
|
VStack(alignment: .leading, spacing: 5) {
|
2022-01-15 22:13:23 +08:00
|
|
|
Markdown(Document(messageItem.text ?? ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
.markdownStyle(
|
|
|
|
DefaultMarkdownStyle(
|
|
|
|
font: .system(size: 14),
|
2022-01-16 00:10:47 +08:00
|
|
|
foregroundColor: UIColor(named: "textColor") ?? UIColor.darkGray
|
2022-01-10 00:26:29 +08:00
|
|
|
)
|
|
|
|
)
|
2022-01-15 22:13:23 +08:00
|
|
|
if !(messageItem.desp?.isEmpty ?? true) {
|
|
|
|
Markdown(Document(messageItem.desp!))
|
2022-01-10 00:26:29 +08:00
|
|
|
.markdownStyle(
|
|
|
|
DefaultMarkdownStyle(
|
|
|
|
font: .system(size: 14),
|
2022-01-16 00:10:47 +08:00
|
|
|
foregroundColor: UIColor(named: "textColor") ?? UIColor.darkGray
|
2022-01-10 00:26:29 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
|
|
|
|
case "image":
|
2022-01-15 22:13:23 +08:00
|
|
|
WebImage(url: URL(string: messageItem.text ?? ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
.onSuccess { image, data, cacheType in
|
2022-01-15 22:13:23 +08:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.image = image
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
.resizable()
|
2022-01-15 22:13:23 +08:00
|
|
|
.placeholder(content: {
|
|
|
|
ZStack {
|
2022-01-16 00:10:47 +08:00
|
|
|
Color("background2Color")
|
2022-01-15 22:13:23 +08:00
|
|
|
Image(systemName: "photo")
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
.font(.system(size: 100))
|
|
|
|
}
|
|
|
|
.frame(width: nil, height: 200, alignment: .center)
|
|
|
|
})
|
|
|
|
.indicator(.activity)
|
|
|
|
.transition(.fade(duration: 0.5))
|
2022-01-10 00:26:29 +08:00
|
|
|
.scaledToFill()
|
2022-01-16 00:10:47 +08:00
|
|
|
.background(Color("backgroundColor"))
|
2022-01-10 00:26:29 +08:00
|
|
|
.contextMenu {
|
|
|
|
Button {
|
2022-01-15 22:13:23 +08:00
|
|
|
guard let image = image else {
|
|
|
|
HToast.showWarning(NSLocalizedString("图片未加载成功", comment: ""))
|
|
|
|
return
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
UIPasteboard.general.image = image
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showSuccess(NSLocalizedString("已拷贝", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
} label: {
|
|
|
|
Label("拷贝图片",systemImage: "doc.on.doc")
|
|
|
|
}
|
|
|
|
Button {
|
2022-01-15 22:13:23 +08:00
|
|
|
guard let image = image else {
|
|
|
|
HToast.showWarning(NSLocalizedString("图片未加载成功", comment: ""))
|
|
|
|
return
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
PHPhotoLibrary.shared().performChanges {
|
|
|
|
PHAssetChangeRequest.creationRequestForAsset(from: image)
|
|
|
|
} completionHandler: { (isSuccess, error) in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
if isSuccess {// 成功
|
|
|
|
print("Success")
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showSuccess(NSLocalizedString("保存成功", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
} else {
|
|
|
|
print(error as Any)
|
2022-01-15 22:13:23 +08:00
|
|
|
HToast.showError(NSLocalizedString("保存失败", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("保存图片",systemImage: "square.and.arrow.down")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
CardView {
|
|
|
|
VStack(alignment: .leading, spacing: 5) {
|
2022-01-04 22:28:18 +08:00
|
|
|
HStack{
|
2022-01-15 22:13:23 +08:00
|
|
|
Text(messageItem.text ?? "")
|
2022-01-04 22:28:18 +08:00
|
|
|
.font(.system(size: 14))
|
2022-01-16 00:10:47 +08:00
|
|
|
.foregroundColor(Color("textColor"))
|
2022-01-04 22:28:18 +08:00
|
|
|
Spacer(minLength: 0)
|
|
|
|
}
|
2022-01-15 22:13:23 +08:00
|
|
|
if !(messageItem.desp?.isEmpty ?? true) {
|
|
|
|
Text(messageItem.desp ?? "")
|
2022-01-10 00:26:29 +08:00
|
|
|
.font(.system(size: 14))
|
2022-01-16 00:10:47 +08:00
|
|
|
.foregroundColor(Color("textColor"))
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
.padding()
|
|
|
|
.contextMenu {
|
|
|
|
Button {
|
2022-01-15 22:13:23 +08:00
|
|
|
UIPasteboard.general.string = (messageItem.text ?? "") + (messageItem.desp ?? "")
|
|
|
|
HToast.showSuccess(NSLocalizedString("已复制", comment: ""))
|
2022-01-10 00:26:29 +08:00
|
|
|
} label: {
|
|
|
|
Label("复制",systemImage: "doc.on.doc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MessageItemView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
VStack {
|
2022-01-15 22:13:23 +08:00
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "纯文本的效果", desp: "你好呀", type: "text", pushkey_name: "Key", created_at: "2022-01-08T18:00:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "纯文本的效果纯文本的效果纯文本的效果纯文本的效果纯文本的效果纯文本的效果纯文本的效果", desp: "", type: "text", pushkey_name: "Key", created_at: "2022-01-08T18:00:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "https://blog.wskfz.com/usr/uploads/2018/06/2498727457.png", desp: "", type: "image", pushkey_name: "Key1", created_at: "2022-01-08T18:00:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "https://blog.wskfz.com/usr/uploads/2018/06/2151130181.png", desp: "", type: "image", pushkey_name: "Key2", created_at: "2022-01-08T18:00:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "https://blog.wskfz.com/usr/uploads/2018/06/1718629805.png", desp: "", type: "image", pushkey_name: "Key2", created_at: "2022-01-08T18:00:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: "*MarkDown*的**效果**", desp: "*MarkDown*的**效果**", type: "markdown", pushkey_name: "Key", created_at: "2021-12-28T13:44:48.000000Z")){}
|
|
|
|
MessageItemView(messageItem: MessageModel(id: 1, uid: "1", text: """
|
2022-01-10 00:26:29 +08:00
|
|
|
It's very easy to make some words **bold** and other words *italic* with Markdown.
|
|
|
|
|
|
|
|
**Want to experiment with Markdown?** Play with the [reference CommonMark
|
|
|
|
implementation](https://spec.commonmark.org/dingus/).
|
2022-01-15 22:13:23 +08:00
|
|
|
""", desp: "", type: "markdown", pushkey_name: "3", created_at: "2021-12-28T13:44:48.000000Z")){}
|
2022-01-04 22:28:18 +08:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|