I've taken a look at all these Swift, and asp.net, and javascript questions.
1 2 3 4 5
Goal:
When I select a messages from the list of chat messages in the MessageListController
I want the opened session in the next ChatDetailController
to be the conversation that was selected.
I'm doing the same thing in this iOS image for my WatchKit app. The message with Sophia is selected and the chat with Sophia opens. [![enter image description here][6]][6]
I want to pass the json "message_id" i.e. the chatMessageId
property. I'm already passing the chatMessageId
from the MessageModel
to the ChatDetailController
as you can see in code.
Is it the chatMessageId
of the ChatModel
I need to pass? Or am I already passing the data that I need?
Passed context: Optional(HTWatch_Extension.MessageModel(partner: "9859", nickname: "Marco", message: "Have you seen is dog?", city: "Madrid", countryBadgeImageURL: https://i.imgur.com/PJcyle7.jpg, messageListImageURL: https://i.imgur.com/PJcyle7.jpg, chatMessageId: "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ", status: "offline"))
Attempt:
Do I need to take the do-catch block where I parse the ChatModel
out of the ChatDetailController
's awakeWithContext
method and put it in the didSelectRowAt
method of the MessageListController
?
MessageListController
// ...code...
var messageObject = [MessageModel]()
var chatObject = [ChatModel]()
// ...code...
override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) {
var messageContext = messageObject[rowIndex]
var chatContext = chatObject[rowIndex]
do {
guard let fileUrl = Bundle.main.url(forResource: "Chats", withExtension: "json") else {
print("File could not be located")
return
}
let data = try Data(contentsOf: fileUrl)
let decoder = JSONDecoder()
let msg = try decoder.decode([ChatModel].self, from: data)
self.chatObject = msg
} catch let error {
print(error)
}
messageContext.chatMessageId = (chatObject as AnyObject).filter { (dictionaryTemp:[String:String]) -> Bool in
return dictionaryTemp["message_id"] == chatContext.chatMessageId
}
// WatchKit's model presentation method.
presentController(withName: "ChatDetailController", context: messageContext)
}
If I have understood correctly, your Chat.json
, will have chat's for all message id's. Select one of the message id row and load the respective chat history.
In that case you can parse based on message by using filter
. Let's say you have it in a dictionary like this.
Example:
let responseString = "{\"name\":\"Tom\"}"
if let responseData = responseString.data(using: .utf8){
do {
let object = try JSONSerialization.jsonObject(with:responseData , options: .allowFragments)
print("Response Object=\(object)")
} catch{
print("parsing Error=\(error)")
}
}
You can use a similar code to create your Object. The final object should be something like chatDictionary
let chatDictionary = [
[
"fromId": "zz1234skjksmsjdfwe2zz",
"toId": "qq43922sdkfjsfmmxdfqq",
"messageText": "Have you seen is dog?",
"imageUrl": "https://i.imgur.com/PJcyle7.jpg",
"message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ",
"read": "true"
],
[
"fromId": "zz1234skjksmsjdfwe2zz",
"toId": "qq43922sdkfjsfmmxdfqq",
"messageText": "Yes I have. It's cute.",
"imageUrl": "https://i.imgur.com/PJcyle7.jpg",
"message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ",
"read": "true"
],
[
"fromId": "zz1234skjksmsjdfwe2zz",
"toId": "qq43922sdkfjsfmmxdfqq",
"messageText": "I want to get a pet too.",
"imageUrl": "https://i.imgur.com/PJcyle7.jpg",
"message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ1",
"read": "true"
]
]
Your did Select Row At index
override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) {
let message = messageObjects[rowIndex]
// Create a chat object Dictionary, parse it before you pass it to the detail View Controller , if you have the chat.json.
// I have used 'Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ', but here you can your message id property to make it dynamic
message.chatObjects= chatDictionary.filter { (dictionaryTemp:[String : String]) -> Bool in
return dictionaryTemp["message_id"] == "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ"
}
presentController(withName: "ChatDetailController", context: message)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With