Closed dimsancing2015 closed 7 years ago
Correct me if I am wrong but your issue is when an incoming message is received the current message you are typing in the InputBar
gets lost?
if that is the case I would check to see if you reload the entire view when you get a message.
If you would provide that code we can help more.
import UIKit import JSQMessagesViewController import XMPPFramework
class CustomerMessaging : JSQMessagesViewController ,UINavigationBarDelegate {
let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(red: 10/255, green: 180/255, blue: 230/255, alpha: 1.0))
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: UIColor.lightGray)
var messages = [JSQMessage]()
var mySendMessages: String = ""
var context = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
let stream = XMPPStream()
let prefs: UserDefaults = UserDefaults.standard
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var sms: String = "hi"
var sender_id: String = ""
var someDateTime:String! = ""
var messages_: String = ""
var created = Date()
var from: String!
var to: String!
var uid : String!
var currentDateTime = Date()
var displayName: String = "MN"
var parser = XMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()
var register_OK: Bool = false
var isFirst: Bool = true
var app:UIApplication = UIApplication.shared
var ff: String = ""
var tt: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
debugPrint("CustomerMessaging")
retrieveMessage()
uid = self.prefs.value(forKey: "UID") as! String
NotificationCenter.default.addObserver(self, selector: #selector(CustomerMessaging.myObserverMethod(_:)), name:NSNotification.Name(rawValue: "myObserveMethodID"), object: nil)
// self.tabBarController?.tabBar.layer.zPosition = -1
self.inputToolbar.contentView.leftBarButtonItem.isHidden = true
collectionView.collectionViewLayout.springinessEnabled = false
automaticallyScrollsToMostRecentMessage = true
UIApplication.shared.applicationIconBadgeNumber = 1
UIApplication.shared.applicationIconBadgeNumber = 0
reloadMessagesView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func reloadMessagesView(){
self.collectionView?.reloadData()
self.collectionView?.layoutIfNeeded()
}
func myObserverMethod(_ notification:Notification) {
print("Observer method called")
call()
retrieveMessage()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
debugPrint("SMS")
//reloadMessagesView()
call()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if(self.automaticallyScrollsToMostRecentMessage){
self .scrollToBottom(animated: true)
}
}
func call(){
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd HH:mm"
someDateTime = formatter.string(from: currentDateTime)
currentDateTime = formatter.date(from: someDateTime)!
//senderId_ = "uid_user"
if(prefs.string(forKey: "SMS") == "nil" || prefs.string(forKey: "SMS") == "" || prefs.string(forKey: "SMS") == nil ){
print("NULL")
}
else{
sms = prefs.string(forKey: "SMS")!
from = prefs.string(forKey: "SENDER")
saveMessages(sms,created: currentDateTime as Date , senderId: from as String ,from: from as String, to: uid as String )
self.prefs.set("", forKey: "SMS")
// callLocalNotification()
}
}
func retrieveMessage(){
debugPrint("retrieve messages from CD")
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Messages")
do{
let results = try context.fetch(fetchRequest)
debugPrint(results.count)
messages.removeAll()
debugPrint(messages.count)
/**
* Show the typing indicator to be shown
*/
//self.showTypingIndicator = !self.showTypingIndicator
for managedObject in results{
if let message = (managedObject as AnyObject).value(forKey: "message"){
messages_ = message as! String
debugPrint(messages_)
}
if let created_date = (managedObject as AnyObject).value(forKey: "created"){
created = (created_date as! NSDate) as Date
debugPrint(created)
}
if let sender_Id = (managedObject as AnyObject).value(forKey: "from"){
from = sender_Id as! String
debugPrint(from)
}
if let to_ = (managedObject as AnyObject).value(forKey: "to"){
to = to_ as! String
debugPrint(to)
}
if let sender_ = (managedObject as AnyObject).value(forKey: "sender_Id"){
sender_id = sender_ as! String
debugPrint(sender_id)
}
if let f : String = prefs.value(forKeyPath: "F") as! String{
ff = f as String
}
if let t: String = prefs.value(forKeyPath: "T") as! String{
tt = t as String
}
if( ff as String == from || ff as String == to) {
if ( tt as String == to || tt as String == from){
debugPrint("Pick Data ... \(messages_)")
let message = JSQMessage(senderId: sender_id, senderDisplayName: displayName, date: created, text: messages_)
self.messages.append(message!)
}
}
self.finishSendingMessage(animated: true)
}
self.reloadMessagesView()
}catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
// self.reloadMessagesView()
}
func saveMessages(_ message: String, created: Date, senderId: String , from: String, to : String){
let messagesService = MessagesService(context: context)
_ = messagesService.create(message, created: created, sender_Id: senderId,from: from, to: to)
messagesService.saveChanges()
}
}
extension CustomerMessaging {
func setup(){
self.senderId = UIDevice.current.identifierForVendor?.uuidString
self.senderDisplayName = "Me"
}
}
extension CustomerMessaging {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didDeleteMessageAt indexPath: IndexPath!) {
self.messages.remove(at: indexPath.row)
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
let data = messages[indexPath.row]
// self.collectionView.backgroundColor = UIColor .redColor()
sender_id = data.senderId
debugPrint(sender_id)
switch (sender_id) {
case senderId :
return self.outgoingBubble
default:
return self.incomingBubble
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.item]
if message.senderId == self.senderId {
let senderAvatar = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: "Me", backgroundColor: UIColor(white: 0.85, alpha: 1.0), textColor: UIColor(white: 0.60, alpha: 1.0), font: UIFont(name: "Helvetica Neue", size: 14.0), diameter: 30)
return senderAvatar
} else {
let recipientAvatar = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: "MN", backgroundColor: UIColor(white: 0.85, alpha: 1.0), textColor: UIColor(white: 0.60, alpha: 1.0), font: UIFont(name: "Helvetica Neue", size: 14.0)!, diameter: 30)
return recipientAvatar
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout, heightForCellBottomLabelAt indexPath: IndexPath!) -> CGFloat {
if (indexPath as NSIndexPath).item % 8 == 0 {
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
return 0.0
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForCellTopLabelAt indexPath: IndexPath!) -> NSAttributedString! {
if indexPath.item % 8 == 0 {
let message = self.messages[indexPath.item]
return JSQMessagesTimestampFormatter.shared().attributedTimestamp(for: message.date)
}
return nil;
}
override func collectionView(_ collectionView: JSQMessagesCollectionView, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout, heightForCellTopLabelAt indexPath: IndexPath) -> CGFloat {
if (indexPath as NSIndexPath).item % 8 == 0 {
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
return 0.0
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat {
let data = self.collectionView(self.collectionView, messageDataForItemAt: indexPath)
if (self.senderDisplayName == data?.senderDisplayName()) {
return 0.0
}
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
}
extension CustomerMessaging {
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
debugPrint("DidPressSendButton")
var someDate:String! = ""
var currentDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd HH:mm"
someDate = formatter.string(from: date)
currentDate = formatter.date(from: someDate)!
let message = JSQMessage(senderId: senderId, senderDisplayName: self.senderDisplayName, date: currentDate, text: text)
let senderJID = XMPPJID.init(string: self.prefs.value(forKey: "SENDER") as! String!)
let msg = XMPPMessage(type: "chat", to: senderJID)
DispatchQueue.main.async {
msg?.addBody(message?.text)
self.appDelegate.stream?.send(msg)
self.messages.append(message!)
self.finishSendingMessage(animated: true)
}
self.saveMessages((message?.text)!,created: date , senderId: self.senderId as String ,from: uid as String, to: prefs.value(forKey: "SENDER") as! String! as String)
}
override func didPressAccessoryButton(_ sender: UIButton!) {
debugPrint("Pressed Send Button")
}
}
I would take a look at this method it just screams trouble
reloadMessagesView()
especially within retrieveMessage
I could see that causing the issue you have described.
let me know if that helps 🖖🏽
I delete reloadMessagesView() but still happen error!
Thanks
Did that solve the issue for you?
Didn't solve.
Hello, have you been able to solve this issue?. I am facing the exact same problem.
Quick question , are you running on Version 7.3.3 or version 7.3.4?
My Xcode is Version 8.2.1 (8C1002).
change finishSendingMessage()
to finishReceivingMessage()
Hello everyone!
I'm sorry to inform the community that I'm officially deprecating this project. 😢 Please read my blog post for details:
http://www.jessesquires.com/blog/officially-deprecating-jsqmessagesviewcontroller/
Thus, I'm closing all issues and pull requests and making the necessary updates to formally deprecate the library. I'm sorry if this is unexpected or disappointing. Please know that this was an extremely difficult decision to make. I'd like to thank everyone here for contributing and making this project so great. It was a fun 4 years. 😊
Thanks for understanding, — jsq
New issue checklist
README
, documentation, and FAQ.General information
JSQMessagesViewController
version:What happened?