2021-09-18 15:33:35 -07:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct MessageBubble: View {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-17 10:18:32 -07:00
|
|
|
@State var showAlert = false
|
2021-09-18 15:33:35 -07:00
|
|
|
var contentMessage: String
|
|
|
|
|
var isCurrentUser: Bool
|
|
|
|
|
var time: Int32
|
|
|
|
|
var shortName: String
|
2021-10-17 10:18:32 -07:00
|
|
|
var id: UInt32
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-09-18 15:33:35 -07:00
|
|
|
var body: some View {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
|
|
2021-09-27 19:37:38 -07:00
|
|
|
CircleText(text: shortName, color: isCurrentUser ? Color.blue : Color(.darkGray)).padding(.all, 5)
|
2021-10-17 10:18:32 -07:00
|
|
|
.gesture(LongPressGesture(minimumDuration: 2)
|
|
|
|
|
.onEnded {_ in
|
|
|
|
|
print("I want to delete message: \(id)")
|
|
|
|
|
self.showAlert = true
|
|
|
|
|
})
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
VStack(alignment: .leading) {
|
2021-09-27 19:37:38 -07:00
|
|
|
Text(contentMessage)
|
|
|
|
|
.textSelection(.enabled)
|
|
|
|
|
.padding(10)
|
|
|
|
|
.foregroundColor(.white)
|
|
|
|
|
.background(isCurrentUser ? Color.blue : Color(.darkGray))
|
|
|
|
|
.cornerRadius(10)
|
2021-11-29 15:59:06 -08:00
|
|
|
HStack(spacing: 4) {
|
|
|
|
|
|
2021-10-01 08:33:11 -07:00
|
|
|
let messageDate = Date(timeIntervalSince1970: TimeInterval(time))
|
2021-09-26 20:12:38 -07:00
|
|
|
|
2021-10-01 08:33:11 -07:00
|
|
|
if time != 0 {
|
|
|
|
|
Text(messageDate, style: .date).font(.caption2).foregroundColor(.gray)
|
|
|
|
|
Text(messageDate, style: .time).font(.caption2).foregroundColor(.gray)
|
2021-11-29 15:59:06 -08:00
|
|
|
} else {
|
2021-10-01 08:33:11 -07:00
|
|
|
Text("Unknown").font(.caption2).foregroundColor(.gray)
|
|
|
|
|
}
|
2021-09-26 20:12:38 -07:00
|
|
|
}
|
2021-09-27 19:37:38 -07:00
|
|
|
.padding(.bottom, 10)
|
2021-09-26 20:12:38 -07:00
|
|
|
}
|
2021-09-27 19:37:38 -07:00
|
|
|
Spacer()
|
|
|
|
|
}
|
2021-10-17 10:18:32 -07:00
|
|
|
.alert(isPresented: $showAlert) {
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-10-17 10:18:32 -07:00
|
|
|
Alert(title: Text("Are you sure you want to delete this message?"), message: Text("This action is permanent."),
|
2021-11-29 15:59:06 -08:00
|
|
|
primaryButton: .destructive(Text("OK")) {
|
2021-10-17 10:18:32 -07:00
|
|
|
print("OK button tapped")
|
|
|
|
|
},
|
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MessageBubble_Previews: PreviewProvider {
|
|
|
|
|
static var previews: some View {
|
|
|
|
|
Group {
|
2021-10-17 10:18:32 -07:00
|
|
|
MessageBubble(contentMessage: "this is the best text ever", isCurrentUser: true, time: 0, shortName: "EB", id: 12)
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
|
|
|
|
.previewLayout(.fixed(width: 300, height: 100))
|
|
|
|
|
}
|
|
|
|
|
}
|