Skip to content

Commit 6b19e94

Browse files
Added “Remove” & “Report” for chat messages
1 parent b76dc8e commit 6b19e94

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed

Adamant/Assets/l18n/de.lproj/Localizable.strings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@
277277
/* Alert "Block this user?" */
278278
"Chats.BlockUser" = "Diesen Benutzer sperren?";
279279

280+
/* Button "Remove" */
281+
"Chats.Remove" = "Entfernen";
282+
283+
/* Alert "Delete this message?" */
284+
"Chats.RemoveMessage" = "Diese Nachricht löschen?";
285+
286+
/* Button "Report" */
287+
"Chats.Report" = "Bericht";
288+
289+
/* Alert "Report as inappropriate?" */
290+
"Chats.ReportMessage" = "Als unangemessen melden?";
291+
292+
/* Alert "Report has been sent" */
293+
"Chats.ReportSent" = "Bericht wurde gesendet";
294+
280295
/* Chat: inform user that he can't cancel transaction, that was sent */
281296
"ChatScene.Error.cancelError" = "Nachricht bereits versendet";
282297

Adamant/Assets/l18n/en.lproj/Localizable.strings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@
274274
/* Alert "Block this user?" */
275275
"Chats.BlockUser" = "Block this user?";
276276

277+
/* Button "Remove" */
278+
"Chats.Remove" = "Remove";
279+
280+
/* Alert "Delete this message?" */
281+
"Chats.RemoveMessage" = "Delete this message?";
282+
283+
/* Button "Report" */
284+
"Chats.Report" = "Report";
285+
286+
/* Alert "Report as inappropriate?" */
287+
"Chats.ReportMessage" = "Report as inappropriate?";
288+
289+
/* Alert "Report has been sent" */
290+
"Chats.ReportSent" = "Report has been sent";
291+
277292
/* Chat: inform user that he can't cancel transaction, that was sent */
278293
"ChatScene.Error.cancelError" = "Message is already sent";
279294

Adamant/Assets/l18n/ru.lproj/Localizable.strings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@
275275
/* Alert "Block this user?" */
276276
"Chats.BlockUser" = "Заблокировать пользователя?";
277277

278+
/* Button "Remove" */
279+
"Chats.Remove" = "Удалить";
280+
281+
/* Alert "Delete this message?" */
282+
"Chats.RemoveMessage" = "Удалить сообщение?";
283+
284+
/* Button "Report" */
285+
"Chats.Report" = "Пожаловаться";
286+
287+
/* Alert "Report as inappropriate?" */
288+
"Chats.ReportMessage" = "Пожаловаться на сообщение?";
289+
290+
/* Alert "Report has been sent" */
291+
"Chats.ReportSent" = "Запрос отправлен";
292+
278293
/* Chat: inform user that he can't cancel transaction, that was sent */
279294
"ChatScene.Error.cancelError" = "Сообщение уже отправлено";
280295

Adamant/CoreData/Chatroom+CoreDataClass.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Chatroom: NSManagedObject {
4848
semaphore?.signal()
4949
}
5050

51-
if let transactions = transactions as? Set<ChatTransaction> {
51+
if let transactions = transactions?.filtered(using: NSPredicate(format: "isHidden == false")) as? Set<ChatTransaction> {
5252
if let newest = transactions.sorted(by: { (lhs: ChatTransaction, rhs: ChatTransaction) in
5353
guard let l = lhs.date as Date? else {
5454
return true

Adamant/ServiceProtocols/DataProviders/ChatsProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ protocol ChatsProvider: DataProvider {
196196
// MARK: - Tools
197197
func validateMessage(_ message: AdamantMessage) -> ValidateMessageResult
198198
func blockChat(with address: String)
199+
func removeMessage(with id: String)
199200

200201
// MARK: - Unconfirmed Transaction
201202
func addUnconfirmed(transactionId: UInt64, managedObjectId: NSManagedObjectID)

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class AdamantChatsProvider: ChatsProvider {
2020
var securedStore: SecuredStore! {
2121
didSet {
2222
self.blackList = self.securedStore.getArray("blackList") ?? []
23+
self.removedMessages = self.securedStore.getArray("removedMessages") ?? []
2324
}
2425
}
2526

@@ -34,6 +35,7 @@ class AdamantChatsProvider: ChatsProvider {
3435

3536
public var chatPositon: [String : Double] = [:]
3637
private(set) var blackList: [String] = []
38+
private(set) var removedMessages: [String] = []
3739

3840
private(set) var isInitiallySynced: Bool = false {
3941
didSet {
@@ -86,6 +88,7 @@ class AdamantChatsProvider: ChatsProvider {
8688
self?.dropStateData()
8789

8890
self?.blackList = []
91+
self?.removedMessages = []
8992
}
9093

9194
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantAccountService.stayInChanged, object: nil, queue: nil) { [weak self] notification in
@@ -97,6 +100,10 @@ class AdamantChatsProvider: ChatsProvider {
97100
if let blackList = self?.blackList {
98101
self?.securedStore.set(blackList, for: "blackList")
99102
}
103+
104+
if let removedMessages = self?.removedMessages {
105+
self?.securedStore.set(removedMessages, for: "removedMessages")
106+
}
100107
}
101108
}
102109
}
@@ -1148,6 +1155,10 @@ extension AdamantChatsProvider {
11481155
messageTransaction.fee = transaction.fee as NSDecimalNumber
11491156
messageTransaction.statusEnum = MessageStatus.delivered
11501157
messageTransaction.partner = partner
1158+
1159+
if let transactionId = messageTransaction.transactionId {
1160+
messageTransaction.isHidden = self.removedMessages.contains(transactionId)
1161+
}
11511162

11521163
return messageTransaction
11531164
}
@@ -1184,4 +1195,14 @@ extension AdamantChatsProvider {
11841195
}
11851196
}
11861197
}
1198+
1199+
public func removeMessage(with id: String) {
1200+
if !self.removedMessages.contains(id) {
1201+
self.removedMessages.append(id)
1202+
1203+
if self.accountService.hasStayInAccount {
1204+
self.securedStore.set(removedMessages, for: "removedMessages")
1205+
}
1206+
}
1207+
}
11871208
}

Adamant/Stories/Chats/ChatCell.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@ protocol ChatCell: class {
1313
// var bubbleStyle: MessageStyle { get set }
1414
var bubbleBackgroundColor: UIColor? { get set }
1515
}
16+
17+
extension MessageCollectionViewCell {
18+
@objc func remove(_ sender: Any?) {
19+
trigger(action: "remove:", with: sender)
20+
}
21+
22+
@objc func report(_ sender: Any?) {
23+
trigger(action: "report:", with: sender)
24+
}
25+
26+
func trigger(action: String, with sender: Any?) {
27+
if let collectionView = self.superview as? UICollectionView {
28+
if let indexPath = collectionView.indexPath(for: self) {
29+
collectionView.delegate?.collectionView?(collectionView, performAction: NSSelectorFromString(action), forItemAt: indexPath, withSender: sender)
30+
}
31+
}
32+
}
33+
}

Adamant/Stories/Chats/ChatViewController.swift

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ extension String.adamantLocalized {
3030
static let unsupportedUrlWarning = NSLocalizedString("ChatScene.Warning.UnsupportedUrl", comment: "Chat: warning message for opening unsupported url schemes")
3131

3232
static let block = NSLocalizedString("Chats.Block", comment: "Block")
33+
34+
static let remove = NSLocalizedString("Chats.Remove", comment: "Remove")
35+
static let removeMessage = NSLocalizedString("Chats.RemoveMessage", comment: "Delete this message?")
36+
static let report = NSLocalizedString("Chats.Report", comment: "Report")
37+
static let reportMessage = NSLocalizedString("Chats.ReportMessage", comment: "Report as inappropriate?")
38+
static let reportSent = NSLocalizedString("Chats.ReportSent", comment: "Report has been sent")
3339

3440
private init() { }
3541
}
@@ -405,8 +411,83 @@ class ChatViewController: MessagesViewController {
405411
scrollToBottomBtn.rightAnchor.constraint(equalTo: messagesCollectionView.rightAnchor, constant: -20),
406412
scrollToBottomBtnOffetConstraint!
407413
])
414+
415+
UIMenuController.shared.menuItems = [
416+
UIMenuItem(title: String.adamantLocalized.chat.remove, action: NSSelectorFromString("remove:")),
417+
UIMenuItem(title: String.adamantLocalized.chat.report, action: NSSelectorFromString("report:"))]
408418
}
409419

420+
override func canPerformAction(_ action: Selector, withSender sender: Any!) -> Bool {
421+
return false
422+
}
423+
424+
override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
425+
return true
426+
}
427+
428+
override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
429+
430+
switch action {
431+
case NSSelectorFromString("remove:"): return true
432+
case NSSelectorFromString("report:"): return true
433+
default:
434+
return super.collectionView(collectionView, canPerformAction: action, forItemAt: indexPath, withSender: sender)
435+
}
436+
}
437+
438+
override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
439+
440+
switch action {
441+
case NSSelectorFromString("remove:"): removeMessage(at: indexPath)
442+
case NSSelectorFromString("report:"): reportMessage(at: indexPath)
443+
default:
444+
super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
445+
}
446+
}
447+
448+
func removeMessage(at indexPath: IndexPath) {
449+
self.dialogService.showAlert(title: String.adamantLocalized.chat.removeMessage, message: nil, style: .alert, actions: [
450+
AdamantAlertAction(title: String.adamantLocalized.alert.ok, style: .destructive, handler: {
451+
self.hideMessage(at: indexPath)
452+
}),
453+
AdamantAlertAction(title: String.adamantLocalized.alert.cancel, style: .default, handler: {
454+
//
455+
})], from: nil)
456+
}
457+
458+
func reportMessage(at indexPath: IndexPath) {
459+
self.dialogService.showAlert(title: String.adamantLocalized.chat.reportMessage, message: nil, style: .alert, actions: [
460+
AdamantAlertAction(title: String.adamantLocalized.alert.ok, style: .destructive, handler: {
461+
self.hideMessage(at: indexPath, show: true)
462+
}),
463+
AdamantAlertAction(title: String.adamantLocalized.alert.cancel, style: .default, handler: {
464+
//
465+
})], from: nil)
466+
}
467+
468+
func hideMessage(at indexPath: IndexPath, show: Bool = false) {
469+
let message = messageForItem(at: indexPath, in: self.messagesCollectionView)
470+
471+
if let item = message as? ChatTransaction {
472+
print("\(message.messageId)")
473+
print(type(of: message.self))
474+
print(item.isHidden)
475+
476+
item.isHidden = true
477+
try? item.managedObjectContext?.save()
478+
479+
chatroom?.updateLastTransaction()
480+
481+
if let transactionId = item.transactionId {
482+
chatsProvider.removeMessage(with: transactionId)
483+
}
484+
485+
if show {
486+
self.dialogService.showToastMessage( String.adamantLocalized.chat.reportSent )
487+
}
488+
}
489+
}
490+
410491
@objc func scrollDown() {
411492
messagesCollectionView.scrollToBottom(animated: true)
412493
}

0 commit comments

Comments
 (0)