-
Notifications
You must be signed in to change notification settings - Fork 5
Initially tried working with the MessageKit framework but I couldn't … #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,21 +7,194 @@ | |
|
|
||
| import SwiftUI | ||
|
|
||
|
|
||
|
|
||
| struct ChatView: View { | ||
| // @State private var messageInput = "" | ||
| // @State private var chatHistory = "" | ||
|
|
||
| var body: some View { | ||
| NavigationView { | ||
| Form { | ||
| Section { | ||
| Text("Chat message") | ||
| } | ||
| } | ||
| .navigationBarTitle(Text("Chat")) | ||
| } | ||
| // VStack { | ||
| // ScrollView { | ||
| // LazyVStack(alignment: .leading, spacing: 32) { | ||
| // ForEach(0 ..< 12) { _ in | ||
| // TextField("Lorem Ipsum Lorem Ipsum", text: $chatHistory) | ||
| // } | ||
| // | ||
| // } | ||
| // } | ||
| // TextField("Message", text: $messageInput) | ||
| // .frame(width: 300, height: 40) | ||
| // .font(Font.system(size: 15, weight: .semibold)) | ||
| // .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.blue, lineWidth: 1)) | ||
| // .padding() | ||
| // } | ||
|
|
||
|
|
||
| // Borrowed from KavSoft | ||
| chatInstance() | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| struct ChatView_Previews: PreviewProvider { | ||
| static var previews: some View { | ||
| ChatView() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| struct chatInstance : View { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] let's follow Swift standards and name structs with the capital letter |
||
|
|
||
| @State var txt = "" | ||
| @State var height : CGFloat = 0 | ||
| @State var keyboardHeight : CGFloat = 0 | ||
|
|
||
| var body: some View { | ||
| VStack (spacing: 0) { | ||
|
|
||
| HStack { | ||
| Text("Chat") | ||
| .font(.title) | ||
| .fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/) | ||
|
|
||
| Spacer() | ||
| }.padding() | ||
| .background(Color.white) | ||
|
|
||
| Spacer() | ||
|
|
||
| ScrollView(.vertical, showsIndicators: false) { | ||
|
|
||
| // Chat Content | ||
| Text("") | ||
| } | ||
|
|
||
| HStack(spacing: 8) { | ||
|
|
||
| resizeableChat(txt: self.$txt, height: self.$height) | ||
| .frame(height: self.height < 150 ? self.height : 150) | ||
| .padding(.horizontal) | ||
| .background(Color.white) | ||
| .cornerRadius(15) | ||
| Button(action: { | ||
|
|
||
| }) { | ||
|
|
||
| Image(systemName: "arrow.forward.circle") | ||
| .font(.system(size: 18, weight: .regular)) | ||
| .foregroundColor(.black) | ||
| .padding(10) | ||
| } | ||
| .background(Color.white) | ||
| .clipShape(Circle()) | ||
| } | ||
| .padding(.horizontal) | ||
|
|
||
| } | ||
| .padding(.bottom, self.keyboardHeight) | ||
| .background(Color.black.opacity(0.06).edgesIgnoringSafeArea(.bottom) | ||
| .onTapGesture { | ||
|
|
||
| UIApplication.shared.windows.first?.rootViewController?.view | ||
| .endEditing(true) | ||
| }) | ||
| .onAppear { | ||
|
|
||
| NotificationCenter.default.addObserver(forName: | ||
| UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { | ||
| (data) in | ||
|
|
||
| let height1 = data.userInfo![UIResponder.keyboardFrameEndUserInfoKey] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [critical] forced unwrapping is not good, can cause a crash |
||
| as! NSValue | ||
|
|
||
| self.keyboardHeight = height1.cgRectValue.height - 20 | ||
|
|
||
| } | ||
|
|
||
| NotificationCenter.default.addObserver(forName: | ||
| UIResponder.keyboardDidHideNotification, object: nil, queue: .main) { | ||
| (_) in | ||
|
|
||
| self.keyboardHeight = 0 | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| struct resizeableChat : UIViewRepresentable { | ||
|
|
||
| @Binding var txt: String | ||
| @Binding var height : CGFloat | ||
|
|
||
| func makeCoordinator() -> Coordinator { | ||
|
|
||
| return resizeableChat.Coordinator(parent1: self) | ||
| } | ||
|
|
||
|
|
||
| func makeUIView(context: Context) -> UITextView { | ||
|
|
||
| let view = UITextView() | ||
| view.isEditable = true | ||
| view.isScrollEnabled = true | ||
| view.text = "Enter Message" | ||
| view.font = .systemFont(ofSize: 18) | ||
| view.textColor = .gray | ||
| view.backgroundColor = .clear | ||
| view.delegate = context.coordinator | ||
| return view | ||
| } | ||
|
|
||
| func updateUIView(_ uiView: UITextView, context: Context) { | ||
| DispatchQueue.main.async { | ||
|
|
||
| self.height = uiView.contentSize.height | ||
| } | ||
| } | ||
|
|
||
| class Coordinator : NSObject, UITextViewDelegate { | ||
|
|
||
| var parent : resizeableChat | ||
|
|
||
| init(parent1 : resizeableChat) { | ||
|
|
||
| parent = parent1 | ||
| } | ||
|
|
||
| func textViewDidBeginEditing(_ textView: UITextView) { | ||
|
|
||
| if self.parent.txt == "" { | ||
|
|
||
| textView.text = "" | ||
| textView.textColor = .black | ||
| } | ||
| } | ||
|
|
||
| func textViewDidEndEditing(_ textView: UITextView) { | ||
| if self.parent.txt == ""{ | ||
|
|
||
| textView.text = "Enter Message" | ||
| textView.textColor = .gray | ||
| } | ||
| } | ||
|
|
||
| func textViewDidChange(_ textView: UITextView) { | ||
|
|
||
| DispatchQueue.main.async { | ||
| self.parent.height = textView.contentSize.height | ||
| self.parent.txt = textView.text | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If MessageKit is not used can you please remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the proper way to do this?