Skip to content

Instantly share code, notes, and snippets.

@nQaze
Last active July 6, 2020 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nQaze/80338f90abe692fcc53d2aeaa2a5286f to your computer and use it in GitHub Desktop.
Save nQaze/80338f90abe692fcc53d2aeaa2a5286f to your computer and use it in GitHub Desktop.
CometChat- Chat Application
import CometChatPro
class ConversationsVC: CometChatConversationList {
@IBOutlet weak var newChatButton: UIButton!
static func storyboardInstance() -> ConversationsVC? {
let storyboard = UIStoryboard(name: "Main" ,bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "ConversationsVC") as? ConversationsVC
}
override func viewDidLoad() {
super.viewDidLoad()
}
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
newChatButton.superview?.bringSubviewToFront(newChatButton)
}
@IBAction func newChatTapped(_ sender: Any) {
openContactsList()
}
private func openContactsList(){
DispatchQueue.main.async {
let userList = CometChatUserList()
let navigationController = UINavigationController(rootViewController:userList)
userList.set(title:"Contacts", mode: .automatic)
self.present(navigationController, animated:true, completion:nil)
}
}
}
import CometChatPro
class RegistrationLoginVC: UIViewController {
@IBOutlet weak var signIn: UIButton!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mobileTextField: UITextField!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func signInPressed(_ sender: Any) {
let uid = "\(nameTextField.text!.first!)\(mobileTextField.text!)"
registerUser(uid: uid , name: nameTextField.text!)
}
private func registerUser(uid: String, name: String){
showActivityIndicator()
let newUser : User = User(uid: uid, name: name)
CometChat.createUser(user: newUser, apiKey: Constants.apiKey, onSuccess: { (User) in
print("User created successfully. \(User.stringValue())")
self.hideActivityIndicator()
if CometChat.getLoggedInUser() == nil {
self.loginUser(uid: uid)
}
}) { (error) in
print("The error is \(String(describing: error?.errorDescription))")
self.hideActivityIndicator()
}
}
private func loginUser(uid: String){
showActivityIndicator()
CometChat.login(UID: uid, apiKey: Constants.apiKey, onSuccess: { (user) in
print("Login successful : " + user.stringValue())
self.hideActivityIndicator()
self.openChats()
}) { (error) in
print("Login failed with error: " + error.errorDescription);
self.hideActivityIndicator()
}
}
private func openChats(){
DispatchQueue.main.async {
let conversationList = ConversationsVC.storyboardInstance()
let navigationController = UINavigationController(rootViewController:conversationList!)
conversationList!.set(title:"Chats", mode: .automatic)
self.present(navigationController, animated:true, completion:nil)
}
}
private func showActivityIndicator(){
DispatchQueue.main.async {
self.activityIndicator.startAnimating()
}
}
private func hideActivityIndicator(){
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment