Smartlist: Add Editing mode and swipe to delete conversations

Add Editing mode and swipe gesture on the smartlist to delete
conversations using RxDatasources itemDeleted and Realm delete.

Change-Id: I8c171d4da0c5ed3fc5bb61231501ef786d14c89b
parent 67e34610
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"
......@@ -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)
......
......@@ -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,
......
......@@ -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() {
......
......@@ -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)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment