diff --git a/Ring/Cartfile.resolved b/Ring/Cartfile.resolved index f395c801a2fa6fecbbffbaeaec0aca2280656465..2cc313e59c2634a244eb188e6ed36c38dc8a7393 100644 --- a/Ring/Cartfile.resolved +++ b/Ring/Cartfile.resolved @@ -1,5 +1,5 @@ github "ReactiveX/RxSwift" "3.5.0" -github "RxSwiftCommunity/RxDataSources" "1.0.3" +github "RxSwiftCommunity/RxDataSources" "1.0.4" github "RxSwiftCommunity/RxRealm" "0.6.0" github "pkluz/PKHUD" "4.2.3" github "realm/realm-cocoa" "v2.8.3" diff --git a/Ring/Ring/Conversations/ConversationViewModel.swift b/Ring/Ring/Conversations/ConversationViewModel.swift index 207becb88ff16ce4b8196c74d435fe60e2c30011..696773d4eb282ef532b1bc060124d58cd66ef0fc 100644 --- a/Ring/Ring/Conversations/ConversationViewModel.swift +++ b/Ring/Ring/Conversations/ConversationViewModel.swift @@ -49,8 +49,8 @@ class ConversationViewModel { //Create observable from sorted conversations and flatMap them to view models self.messages = self.conversationsService.conversations.map({ conversations in - return conversations.filter({ currentConversation in - return currentConversation.recipient == conversation.recipient + return conversations.filter({ conv in + return conv.isEqual(conversation) }).flatMap({ conversation in conversation.messages.map({ message in return MessageViewModel(withMessage: message) diff --git a/Ring/Ring/Services/ConversationsService.swift b/Ring/Ring/Services/ConversationsService.swift index 1d802fa97ea2d3f9d69b4368e935ab46af44d736..1d432a18de797b5262d92f1398b0af4271a910ac 100644 --- a/Ring/Ring/Services/ConversationsService.swift +++ b/Ring/Ring/Services/ConversationsService.swift @@ -129,6 +129,18 @@ class ConversationsService: MessagesAdapterDelegate { }) } + func deleteConversation(conversation: ConversationModel) { + try! realm.write { + + //Remove all messages from the conversation + for message in conversation.messages { + realm.delete(message) + } + + realm.delete(conversation) + } + } + //MARK: Message Adapter delegate func didReceiveMessage(_ message: Dictionary<String, String>, from senderAccount: String, diff --git a/Ring/Ring/Smartlist/SmartlistViewController.swift b/Ring/Ring/Smartlist/SmartlistViewController.swift index de1f4b8ebe22de9140856947fe8a6ac74311bc99..6b33ff06f85bbbd92118e03146c25869c00494a8 100644 --- a/Ring/Ring/Smartlist/SmartlistViewController.swift +++ b/Ring/Ring/Smartlist/SmartlistViewController.swift @@ -79,12 +79,19 @@ class SmartlistViewController: UIViewController { self.viewModel.hideNoConversationsMessage .bind(to: self.noConversationsView.rx.isHidden) .addDisposableTo(disposeBag) + + self.navigationItem.rightBarButtonItem = self.editButtonItem } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) } + override func setEditing(_ editing: Bool, animated: Bool) { + super.setEditing(editing, animated: animated) + self.conversationsTableView.setEditing(editing, animated: true) + } + func keyboardWillShow(withNotification notification: Notification) { let userInfo: Dictionary = notification.userInfo! let keyboardFrame: NSValue = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue @@ -125,6 +132,11 @@ class SmartlistViewController: UIViewController { return cell } + //Allows to delete + conversationsDataSource.canEditRowAtIndexPath = { _ in + return true + } + conversationsDataSource.configureCell = configureCell searchResultsDatasource.configureCell = configureCell @@ -171,11 +183,11 @@ class SmartlistViewController: UIViewController { }).addDisposableTo(disposeBag) //Deselect the rows - self.conversationsTableView.rx.itemSelected.asObservable().subscribe(onNext: { indexPath in + self.conversationsTableView.rx.itemSelected.subscribe(onNext: { [unowned self] indexPath in self.conversationsTableView.deselectRow(at: indexPath, animated: true) }).addDisposableTo(disposeBag) - self.searchResultsTableView.rx.itemSelected.asObservable().subscribe(onNext: { indexPath in + self.searchResultsTableView.rx.itemSelected.subscribe(onNext: { [unowned self] indexPath in self.searchResultsTableView.deselectRow(at: indexPath, animated: true) }).addDisposableTo(disposeBag) @@ -186,6 +198,12 @@ class SmartlistViewController: UIViewController { .addDisposableTo(disposeBag) self.searchResultsTableView.rx.setDelegate(self).addDisposableTo(disposeBag) + + //Swipe to delete action + self.conversationsTableView.rx.itemDeleted.subscribe(onNext:{ [unowned self] indexPath in + let convToDelete :ConversationViewModel = try! self.conversationsTableView.rx.model(at: indexPath) + self.viewModel.delete(conversationViewModel: convToDelete) + }).addDisposableTo(disposeBag) } func setupSearchBar() { diff --git a/Ring/Ring/Smartlist/SmartlistViewModel.swift b/Ring/Ring/Smartlist/SmartlistViewModel.swift index 79e3326de448319e9f796173686da40e083c3933..292c4ba49f7285c59a5947f9288e415d9308f4f1 100644 --- a/Ring/Ring/Smartlist/SmartlistViewModel.swift +++ b/Ring/Ring/Smartlist/SmartlistViewModel.swift @@ -108,7 +108,7 @@ class SmartlistViewModel { }).observeOn(MainScheduler.instance) //Observes search bar text - searchBarText.asObservable().subscribe(onNext: { [unowned self] text in + searchBarText.asObservable().observeOn(MainScheduler.instance).subscribe(onNext: { [unowned self] text in self.search(withText: text) }).addDisposableTo(disposeBag) @@ -181,4 +181,14 @@ class SmartlistViewModel { .addDisposableTo(disposeBag) } } + + func delete(conversationViewModel: ConversationViewModel) { + + if let index = self.conversationViewModels.index(where: ({ cvm in + cvm.conversation.recipient?.ringId == conversationViewModel.conversation.recipient?.ringId + })) { + self.conversationsService.deleteConversation(conversation: conversationViewModel.conversation) + self.conversationViewModels.remove(at: index) + } + } }