MailboxViewController.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import UIKit
  2. import DcCore
  3. class MailboxViewController: ChatViewController {
  4. override init(dcContext: DcContext, chatId: Int, highlightedMsg: Int? = nil) {
  5. super.init(dcContext: dcContext, chatId: chatId)
  6. hidesBottomBarWhenPushed = true
  7. tableView.allowsSelectionDuringEditing = false
  8. showCustomNavBar = false
  9. }
  10. required init?(coder _: NSCoder) {
  11. fatalError("init(coder:) has not been implemented")
  12. }
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. navigationItem.title = String.localized("menu_deaddrop")
  16. }
  17. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  18. askToChat(messageId: messageIds[indexPath.row])
  19. }
  20. override func imageTapped(indexPath: IndexPath) {
  21. askToChat(messageId: messageIds[indexPath.row])
  22. }
  23. override func avatarTapped(indexPath: IndexPath) {
  24. askToChat(messageId: messageIds[indexPath.row])
  25. }
  26. override func textTapped(indexPath: IndexPath) {
  27. askToChat(messageId: messageIds[indexPath.row])
  28. }
  29. // function builds the correct question when tapping on a deaddrop message.
  30. // returns a tuple (question, startButton, blockButton)
  31. public static func deaddropQuestion(context: DcContext, msg: DcMsg) -> (String, String, String) {
  32. let chat = context.getChat(chatId: msg.realChatId)
  33. if chat.isMailinglist {
  34. let question = String.localizedStringWithFormat(String.localized("ask_show_mailing_list"), chat.name)
  35. return (question, String.localized("yes"), String.localized("block"))
  36. } else {
  37. let contact = msg.fromContact
  38. let question = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), contact.nameNAddr)
  39. return (question, String.localized("start_chat"), String.localized("menu_block_contact"))
  40. }
  41. }
  42. func askToChat(messageId: Int) {
  43. if handleUIMenu() { return }
  44. let message = DcMsg(id: messageId)
  45. if message.isInfo {
  46. return
  47. }
  48. let (title, startButton, blockButton) = MailboxViewController.deaddropQuestion(context: dcContext, msg: message)
  49. let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
  50. alert.addAction(UIAlertAction(title: startButton, style: .default, handler: { _ in
  51. let chat = self.dcContext.decideOnContactRequest(messageId, DC_DECISION_START_CHAT)
  52. self.showChat(chatId: chat.id)
  53. }))
  54. alert.addAction(UIAlertAction(title: blockButton, style: .destructive, handler: { _ in
  55. self.dcContext.decideOnContactRequest(messageId, DC_DECISION_BLOCK)
  56. }))
  57. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
  58. present(alert, animated: true, completion: nil)
  59. }
  60. }