I have simple chat app, and I want to use simple emoji in this chat app, my emoji is located in
EmojiView
an when I click the emoji button, I want to add in live chat, but I do not know how I will forward for it, I was look at many resources but I did not find any example on internet, is it possible to do it?
import SwiftUI
struct MessageDetailsView: View {
var body: some View {
HomeMessageDetails()}}
struct MessageDetailsView_Previews: PreviewProvider {
static var previews: some View {
MessageDetailsView()
}}
struct HomeMessageDetails : View {
@State var message = ""
@StateObject var allMessages = Messages()
@State private var emojiData = false
var body: some View{
ZStack {
VStack{
ScrollView(.vertical, showsIndicators: false, content: {
ScrollViewReader{reader in
VStack{
ForEach(allMessages.messages){message in
ChatBubble(message: message)
}
.onChange(of: allMessages.messages) { (value) in
if value.last!.chatMessages{
reader.scrollTo(value.last?.id)}}}
.padding([.horizontal,.bottom])}})
HStack {
HStack{
TextField("Message", text: self.$message)
Button(action: {
emojiData = true
}, label: {
Image("emoji")
}) .sheet(isPresented: $emojiData) {
EmojiView()
} }
.padding(.vertical, 10)
.padding(.horizontal)
Button(action: {
allMessages.messages.append(Message(id: Date(), message: message, chatMessages: true))
message = ""
}, label: {
Image("reply")
}) }
.padding(.horizontal)
}} }}
struct ChatBubble : View {
var message : Message
var body: some View{
HStack(alignment: .top,spacing: 10){
if message.chatMessages{
Text(message.message)
.foregroundColor(Color("black))
}
else{
Text(message.message)
.foregroundColor(.white) }}
.id(message.id)}}
struct Message : Identifiable,Equatable{
var id : Date
var message : String
var chatMessages : Bool
}
class Messages : ObservableObject{
@Published var messages : [Message] = []
init() {
let strings = ["Hii","Hello !!!!"]
for i in 0..<strings.count{
messages.append(Message(id: Date(), message: strings[i], chatMessages: i % 2 == 0 ? true : false))}}
func writeMessage(id: Date,message: String,chatMessages: Bool){
messages.append(Message(id: id, message: message, chatMessages: chatMessages))}}
struct EmojiView: View {
var body: some View {
Button(action: {
}, label: {
Image("smile_emoji")
}) }}