forked from MessageKit/MessageKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into development
# Conflicts: # Example/ChatExample.xcodeproj/project.pbxproj # Example/Sources/View Controllers/LaunchViewController.swift # README.md
- Loading branch information
Showing
7 changed files
with
214 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// MessagesView.swift | ||
// ChatExample | ||
// | ||
// Created by Kino Roy on 2020-07-18. | ||
// Copyright © 2020 MessageKit. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import MessageKit | ||
import InputBarAccessoryView | ||
|
||
final class MessageSwiftUIVC: MessagesViewController { | ||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
// Because SwiftUI wont automatically make our controller the first responder, we need to do it on viewDidAppear | ||
becomeFirstResponder() | ||
messagesCollectionView.scrollToBottom(animated: true) | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
struct MessagesView: UIViewControllerRepresentable { | ||
|
||
@State var initialized = false | ||
@Binding var messages: [MessageType] | ||
|
||
func makeUIViewController(context: Context) -> MessagesViewController { | ||
let messagesVC = MessageSwiftUIVC() | ||
|
||
messagesVC.messagesCollectionView.messagesDisplayDelegate = context.coordinator | ||
messagesVC.messagesCollectionView.messagesLayoutDelegate = context.coordinator | ||
messagesVC.messagesCollectionView.messagesDataSource = context.coordinator | ||
messagesVC.messageInputBar.delegate = context.coordinator | ||
messagesVC.scrollsToBottomOnKeyboardBeginsEditing = true // default false | ||
messagesVC.maintainPositionOnKeyboardFrameChanged = true // default false | ||
|
||
return messagesVC | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: MessagesViewController, context: Context) { | ||
uiViewController.messagesCollectionView.reloadData() | ||
scrollToBottom(uiViewController) | ||
} | ||
|
||
private func scrollToBottom(_ uiViewController: MessagesViewController) { | ||
DispatchQueue.main.async { | ||
// The initialized state variable allows us to start at the bottom with the initial messages without seeing the inital scroll flash by | ||
uiViewController.messagesCollectionView.scrollToBottom(animated: self.initialized) | ||
self.initialized = true | ||
} | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
return Coordinator(messages: $messages) | ||
} | ||
|
||
final class Coordinator { | ||
|
||
let formatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateStyle = .medium | ||
return formatter | ||
}() | ||
|
||
var messages: Binding<[MessageType]> | ||
init(messages: Binding<[MessageType]>) { | ||
self.messages = messages | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MessagesView.Coordinator: MessagesDataSource { | ||
func currentSender() -> SenderType { | ||
return SampleData.shared.currentSender | ||
} | ||
|
||
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { | ||
return messages.wrappedValue[indexPath.section] | ||
} | ||
|
||
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { | ||
return messages.wrappedValue.count | ||
} | ||
|
||
func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { | ||
let name = message.sender.displayName | ||
return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)]) | ||
} | ||
|
||
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { | ||
let dateString = formatter.string(from: message.sentDate) | ||
return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]) | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MessagesView.Coordinator: InputBarAccessoryViewDelegate { | ||
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) { | ||
let message = MockMessage(text: text, user: SampleData.shared.currentSender, messageId: UUID().uuidString, date: Date()) | ||
messages.wrappedValue.append(message) | ||
inputBar.inputTextView.text = "" | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MessagesView.Coordinator: MessagesLayoutDelegate, MessagesDisplayDelegate { | ||
func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) { | ||
let avatar = SampleData.shared.getAvatarFor(sender: message.sender) | ||
avatarView.set(avatar: avatar) | ||
} | ||
func messageTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { | ||
return 20 | ||
} | ||
|
||
func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { | ||
return 16 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// ChatView.swift | ||
// ChatExample | ||
// | ||
// Created by Kino Roy on 2020-07-18. | ||
// Copyright © 2020 MessageKit. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import MessageKit | ||
|
||
@available(iOS 13.0, *) | ||
struct SwiftUIExampleView: View { | ||
|
||
@State var messages: [MessageType] = SampleData.shared.getMessages(count: 20) | ||
|
||
var body: some View { | ||
MessagesView(messages: $messages).onAppear { | ||
self.connectToMessageSocket() | ||
}.onDisappear { | ||
self.cleanupSocket() | ||
} | ||
.navigationBarTitle("SwiftUI Example", displayMode: .inline) | ||
} | ||
|
||
private func connectToMessageSocket() { | ||
MockSocket.shared.connect(with: [SampleData.shared.nathan, SampleData.shared.wu]).onNewMessage { message in | ||
self.messages.append(message) | ||
} | ||
} | ||
|
||
private func cleanupSocket() { | ||
MockSocket.shared.disconnect() | ||
} | ||
|
||
} | ||
|
||
@available(iOS 13.0, *) | ||
struct SwiftUIExampleView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
SwiftUIExampleView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters