File size: 2,702 Bytes
0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 17afd55 0a56873 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import SwiftUI
public enum TypeChat: String {
case SEND = "SEND"
case RECEIVE = "RECEIVE"
case WIDGET = "WIDGET"
}
public enum TypeWidget: String {
case EMPTY = "EMPTY"
case CONTACT = "CONTACT"
case IMAGE = "IMAGE"
}
struct ChatMessage: Hashable {
let type: TypeChat
let message: String
let widgetType: TypeWidget
let widgetDesc: String
}
struct ChatBubble: Shape {
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect,
byRoundingCorners: [.allCorners],
cornerRadii: CGSize(width: 9, height: 9))
return Path(path.cgPath)
}
}
struct ChatMessageRow: View {
let chatItem: ChatMessage
var body: some View {
HStack {
switch chatItem.type {
case TypeChat.SEND:
Spacer()
Text(chatItem.message)
.font(.system(size: 15))
.foregroundColor(.white)
.padding(5)
.background(Color.green)
.clipShape(ChatBubble())
case TypeChat.RECEIVE:
Text(chatItem.message)
.font(.system(size: 15))
.foregroundColor(.black)
.padding(5)
.background(Color.primary)
.clipShape(ChatBubble())
Spacer()
case TypeChat.WIDGET:
switch chatItem.widgetType {
case TypeWidget.CONTACT:
let contact = convertStringToContact(jsonData: chatItem.widgetDesc)
NavigationLink(destination: ContactView(contact: contact)){
ContactRow(contact: contact)
}
case TypeWidget.IMAGE:
ImageRow()
default:
EmptyView()
}
}
}.padding(5)
}
}
func convertStringToContact(jsonData: String) -> Contact {
do {
let jsonData = jsonData.data(using: .utf8)!
let contact = try JSONDecoder().decode(Contact.self, from: jsonData)
return contact
} catch {
print("Error converting JSON string to object: \(error)")
}
return Contact(contactId: "", displayName: "Empty", phoneNumbers: [])
}
struct ChatMessageRow_Previews: PreviewProvider {
static var previews: some View {
ChatMessageRow(chatItem:ChatMessage(
type: TypeChat.WIDGET,
message: "Hello!",
widgetType: TypeWidget.IMAGE,
widgetDesc: "Nothing"
)
)
}
}
|