-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
111 lines (99 loc) · 3.73 KB
/
Copy pathContentView.swift
File metadata and controls
111 lines (99 loc) · 3.73 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// ContentView.swift
// Created by Nikhil Krishnaswamy on 4/14/23.
import SwiftUI
import AudioToolbox
struct CustomButtonStyle:ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.foregroundColor(configuration.isPressed ? Color.clear : Color.blue)
}
}
struct ContentView: View {
@State private var messageText = ""
@State var messages: [String] = ["Welcome! My name is Emi!"]
@ObservedObject var viewModel = ViewModel()
@State private var showSettings = false
var body: some View {
VStack {
ZStack {
HStack{
Image("Title")
}
}
ScrollView {
ForEach(messages, id: \.self) { message in
// If the message contains [USER], that means it's us
if message.contains("[USER]") {
let newMessage = message.replacingOccurrences(of: "[USER]", with: "")
// User message styles
HStack {
Spacer()
Text(newMessage)
.padding()
.background(Color.blue.opacity(0.5))
.cornerRadius(20)
.padding(.horizontal, 16)
.padding(.bottom, 10)
}
} else {
// Bot message styles
HStack {
Text(message)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(20)
.padding(.horizontal, 16)
.padding(.bottom, 10)
Spacer()
}
}
}.rotationEffect(.degrees(180))
}
.rotationEffect(.degrees(180))
// Contains the Message bar
HStack {
TextField("Type here...", text: $messageText)
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(30)
.font(.system(size: 16))
.onSubmit {
sendMessage(message: messageText)
AudioServicesPlaySystemSound(1004)
}
Button {
sendMessage(message: messageText)
AudioServicesPlaySystemSound(1004)
} label: {
Image(systemName: "paperplane.fill").buttonStyle(CustomButtonStyle())
}
.font(.system(size: 20))
.padding(.horizontal, 10)
}
.padding()
}
.onAppear {
viewModel.setup()
}
}
func sendMessage(message: String) {
withAnimation {
messages.append("[USER]" + message)
self.messageText = ""
withAnimation {
viewModel.getBotResponse(text: message) {response in
DispatchQueue.main.async {
messages.append(response)
AudioServicesPlaySystemSound(1003)
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}