Skip to content

Commit

Permalink
Merging UserConversation into Conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
rickmak committed Jun 14, 2017
2 parents 5ddae35 + 1fdb42c commit 02c1cf0
Show file tree
Hide file tree
Showing 21 changed files with 569 additions and 452 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: objective-c
os: osx
osx_image: xcode8.1
osx_image: xcode8.3
cache:
- cocoapods
- pip
Expand Down
26 changes: 13 additions & 13 deletions Example/Swift Example/ConversationDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
let participantIdsSection = 1
let adminIdsSection = 2

var userCon: SKYUserConversation!
var conversation: SKYConversation!

// MARK: - Lifecycle

Expand All @@ -44,7 +44,7 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
if let id = participantTextField.text, !id.isEmpty {
SKYContainer.default().chatExtension?.addParticipants(
userIDs: [id],
to: userCon.conversation
to: self.conversation
) { (conversation, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to add user to participant.", message: err.localizedDescription, preferredStyle: .alert)
Expand All @@ -62,7 +62,7 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
if let id = participantTextField.text, !id.isEmpty {
SKYContainer.default().chatExtension?.removeParticipants(
userIDs: [id],
from: userCon.conversation
from: self.conversation
) { (conversation, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to remove user from participant.", message: err.localizedDescription, preferredStyle: .alert)
Expand All @@ -77,10 +77,10 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
}

func refreshConversation() {
SKYContainer.default().chatExtension?.fetchUserConversation(
conversationID: self.userCon.conversation.recordID.recordName,
SKYContainer.default().chatExtension?.fetchConversation(
conversationID: self.conversation.recordName(),
fetchLastMessage:false) { (conversation, error) in
self.userCon = conversation
self.conversation = conversation
self.tableView.reloadData()
}
}
Expand All @@ -96,9 +96,9 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
case unreadMessageCount:
return 1
case participantIdsSection:
return userCon.conversation.participantIds.count
return conversation.participantIds.count
case adminIdsSection:
return userCon.conversation.adminIds.count
return conversation.adminIds.count
default:
return 0
}
Expand All @@ -121,15 +121,15 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
switch indexPath.section {
case unreadMessageCount:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(userCon.unreadCount)"
cell.textLabel?.text = "\(conversation.unreadCount)"
return cell
case participantIdsSection:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = userCon.conversation.participantIds[indexPath.row]
cell.textLabel?.text = conversation.participantIds[indexPath.row]
return cell
case adminIdsSection:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = userCon.conversation.adminIds[indexPath.row]
cell.textLabel?.text = conversation.adminIds[indexPath.row]
return cell
default:
return UITableViewCell()
Expand All @@ -139,9 +139,9 @@ class ConversationDetailViewController: UITableViewController, UITextFieldDelega
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case participantIdsSection:
self.performSegue(withIdentifier: "showDetail", sender: userCon.conversation.participantIds[indexPath.row])
self.performSegue(withIdentifier: "showDetail", sender: conversation.participantIds[indexPath.row])
case adminIdsSection:
self.performSegue(withIdentifier: "showDetail", sender: userCon.conversation.adminIds[indexPath.row])
self.performSegue(withIdentifier: "showDetail", sender: conversation.adminIds[indexPath.row])
default: break
}
}
Expand Down
11 changes: 4 additions & 7 deletions Example/Swift Example/ConversationListDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ConversationListDemoViewController: SKYChatConversationListViewController
let showConversationSegueIdentifier: String = "ShowConversationSegue"

var selectedConversation: SKYConversation?
var selectedUserConversation: SKYUserConversation?

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -41,7 +40,6 @@ class ConversationListDemoViewController: SKYChatConversationListViewController
case showConversationSegueIdentifier:
if let dest = segue.destination as? ConversationDemoViewController {
dest.conversation = self.selectedConversation
dest.userConversation = self.selectedUserConversation
}
default:
break
Expand All @@ -53,10 +51,10 @@ extension ConversationListDemoViewController: SKYChatConversationListViewControl
func listViewController(_ controller: SKYChatConversationListViewController,
didSelectConversation conversation: SKYConversation)
{
print("Conversation \(conversation.recordID.recordName!) is selected")
print("Conversation \(conversation.recordID().recordName!) is selected")

SVProgressHUD.show()
self.skygear.chatExtension?.fetchUserConversation(
self.skygear.chatExtension?.fetchConversation(
conversation: conversation, fetchLastMessage: false, completion: { (result, error) in
SVProgressHUD.dismiss()
guard error == nil else {
Expand All @@ -65,14 +63,13 @@ extension ConversationListDemoViewController: SKYChatConversationListViewControl
return
}

guard let userConv = result else {
print("Error: Get nil user conversation")
guard let conversation = result else {
print("Error: Get nil conversation")
SVProgressHUD.showError(withStatus: "Cannot get conversation information")
return
}

self.selectedConversation = conversation
self.selectedUserConversation = userConv
self.performSegue(withIdentifier: self.showConversationSegueIdentifier,
sender: self)
}
Expand Down
37 changes: 17 additions & 20 deletions Example/Swift Example/ConversationRoomViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConversationRoomViewController: UIViewController,
@IBOutlet var messageMetadataTextField: UITextField!
@IBOutlet var chosenAsseTexttLabel: UILabel!

var userCon: SKYUserConversation!
var conversation: SKYConversation!
var messages = [SKYMessage]()
var lastReadMessage: SKYMessage?
var chosenAsset: SKYAsset?
Expand All @@ -44,8 +44,8 @@ class ConversationRoomViewController: UIViewController,
override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.title = userCon.conversation.title
self.lastReadMessage = userCon.lastReadMessage
self.navigationItem.title = conversation.title
self.lastReadMessage = conversation.lastReadMessage

// listening keyboard event
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { (note) in
Expand Down Expand Up @@ -75,15 +75,15 @@ class ConversationRoomViewController: UIViewController,
}

// subscribe chat messages
chat.subscribeToMessages(in: userCon.conversation) { (event, message) in
chat.subscribeToMessages(in: conversation) { (event, message) in
if event == .create {
self.messages.insert(message, at: 0)
self.tableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
}
}

// get conversation messages
chat.fetchMessages(conversation: userCon.conversation, limit: 100, beforeTime: Date()) { (messages, error) in
chat.fetchMessages(conversation: conversation, limit: 100, beforeTime: Date()) { (messages, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to fetch conversations", message: err.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
Expand All @@ -107,7 +107,7 @@ class ConversationRoomViewController: UIViewController,
// MARK: - Action

@IBAction func showDetail(_ sender: AnyObject) {
self.performSegue(withIdentifier: "conversation_detail", sender: userCon)
self.performSegue(withIdentifier: "conversation_detail", sender: conversation)
}

@IBAction func sendMessage(_ sender: AnyObject) {
Expand All @@ -122,14 +122,11 @@ class ConversationRoomViewController: UIViewController,
}
}

guard let message = SKYMessage() else {
print("cannot create message")
return
}
let message = SKYMessage()
message.body = messaegBodyTextField.text
message.metadata = metadateDic
message.attachment = chosenAsset
SKYContainer.default().chatExtension?.addMessage(message, to: userCon.conversation) { (message, error) in
SKYContainer.default().chatExtension?.addMessage(message, to: conversation) { (message, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to send message", message: err.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
Expand Down Expand Up @@ -167,26 +164,26 @@ class ConversationRoomViewController: UIViewController,
let message = messages[indexPath.row]

var lastRead = ""
if lastReadMessage != nil && lastReadMessage?.recordID.recordName == message.recordID.recordName {
if lastReadMessage != nil && lastReadMessage?.recordID().recordName == message.recordID().recordName {
lastRead = " === last read message ==="
}
let messageBody = message.body != nil ? message.body! : ""
cell.textLabel?.text = messageBody + lastRead
cell.detailTextLabel?.text = message.recordID.canonicalString
cell.detailTextLabel?.text = message.recordID().canonicalString

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "message_detail", sender: messages[indexPath.row].dictionary)
self.performSegue(withIdentifier: "message_detail", sender: messages[indexPath.row].dictionary())
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let unreadAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Mark as unread") { (action, indexPath) in

let message = self.messages[indexPath.row]
SKYContainer.default().chatExtension?.markLastReadMessage(message,
in: self.userCon) { (userCon, error) in
in: self.conversation) { (conversation, error) in

if let err = error {
let alert = UIAlertController(title: "Unable to mark last read message", message: err.localizedDescription, preferredStyle: .alert)
Expand All @@ -203,7 +200,7 @@ class ConversationRoomViewController: UIViewController,

let message = self.messages[indexPath.row]
SKYContainer.default().chatExtension?.deleteMessage(message,
in: self.userCon) { (userCon, error) in
in: self.conversation) { (conv, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to delete message", message: err.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
Expand Down Expand Up @@ -261,16 +258,16 @@ class ConversationRoomViewController: UIViewController,
controller.dictionary = sender as! NSDictionary
} else if segue.identifier == "conversation_detail" {
let controller = segue.destination as! ConversationDetailViewController
controller.userCon = sender as! SKYUserConversation
controller.conversation = sender as! SKYConversation
}
}

func refreshConversation() {
SKYContainer.default().chatExtension?.fetchUserConversation(
conversationID: self.userCon.conversation.recordID.recordName,
SKYContainer.default().chatExtension?.fetchConversation(
conversationID: self.conversation.recordName(),
fetchLastMessage: false) { (conversation, error) in
if let conv = conversation {
self.userCon = conv
self.conversation = conv
self.lastReadMessage = conv.lastReadMessage
self.tableView.reloadData()
}
Expand Down
18 changes: 9 additions & 9 deletions Example/Swift Example/ConversationsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import SKYKitChat

class ConversationsViewController: UITableViewController {

var userCons = [SKYUserConversation]()
var conversations = [SKYConversation]()

// MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

SKYContainer.default().chatExtension?.fetchUserConversations(fetchLastMessage:false) { (userCons, error) in
SKYContainer.default().chatExtension?.fetchConversations(fetchLastMessage:true) { (conversations, error) in
if let err = error {
let alert = UIAlertController(title: "Unable to fetch conversations", message: err.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}

if let cons = userCons {
self.userCons = cons.reversed()
if let cons = conversations {
self.conversations = cons.reversed()
self.tableView.reloadData()
}
}
Expand All @@ -50,7 +50,7 @@ class ConversationsViewController: UITableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "conversation_room" {
let controller = segue.destination as! ConversationRoomViewController
controller.userCon = sender as! SKYUserConversation
controller.conversation = sender as! SKYConversation
}
}

Expand All @@ -61,21 +61,21 @@ class ConversationsViewController: UITableViewController {
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userCons.count
return conversations.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

let conversation = userCons[indexPath.row].conversation
let conversation = conversations[indexPath.row]
cell.textLabel?.text = conversation.title
cell.detailTextLabel?.text = conversation.recordID.canonicalString
cell.detailTextLabel?.text = conversation.recordID().canonicalString

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "conversation_room", sender: userCons[indexPath.row])
self.performSegue(withIdentifier: "conversation_room", sender: conversations[indexPath.row])
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CreateDirectConversationViewController: UIViewController {
return
}

self.createdConversationTextView.text = conversation?.recordID.canonicalString
self.createdConversationTextView.text = conversation?.recordID().canonicalString
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CreateGroupConversationViewController: UIViewController,
return
}

self.createdConversationTextView.text = conversation?.recordID.canonicalString
self.createdConversationTextView.text = conversation?.recordID().canonicalString
}
}

Expand Down
4 changes: 2 additions & 2 deletions Example/Swift Example/UserQueryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Kingfisher
class UserQueryViewController: SKYChatParticipantListViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.queryMethod = .ByName
self.queryMethod = .byName
self.dataSource = self
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ extension UserQueryViewController: SKYChatParticipantListViewControllerDataSourc
options: nil,
progressBlock: nil) { (downloadedImage, error, url, data) in
guard error == nil else {
print("Failed to download image: \(error?.localizedDescription)")
print("Failed to download image: \(error?.localizedDescription ?? "")")
return
}

Expand Down
14 changes: 12 additions & 2 deletions SKYKitChat/Classes/Core/Records/SKYChatRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@

#import <SKYKit/SKYKit.h>

@interface SKYChatRecord : SKYRecord
@interface SKYChatRecord : NSObject

+ (instancetype)recordWithRecord:(SKYRecord *)record;
- (id _Nonnull)initWithRecordData:(SKYRecord *_Nonnull)record;

@property (copy, nonatomic, nonnull) SKYRecord *record;

- (NSString *_Nonnull)creatorUserRecordID;
- (NSDate *_Nonnull)creationDate;
- (SKYRecordID *_Nonnull)recordID;
- (NSString *_Nonnull)recordType;

- (void)setCreatorUserRecordID:(NSString *_Nonnull)recordID;
- (void)setCreationDate:(NSDate *_Nonnull)date;
- (NSDictionary *_Nonnull)dictionary;
@end
Loading

0 comments on commit 02c1cf0

Please sign in to comment.