ShareViewController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import UIKit
  2. import Social
  3. import DcCore
  4. class ShareViewController: SLComposeServiceViewController {
  5. class SimpleLogger: Logger {
  6. func verbose(_ message: String) {
  7. print("ShareViewController", "verbose", message)
  8. }
  9. func debug(_ message: String) {
  10. print("ShareViewController", "debug", message)
  11. }
  12. func info(_ message: String) {
  13. print("ShareViewController", "info", message)
  14. }
  15. func warning(_ message: String) {
  16. print("ShareViewController", "warning", message)
  17. }
  18. func error(_ message: String) {
  19. print("ShareViewController", "error", message)
  20. }
  21. }
  22. let logger = SimpleLogger()
  23. let dcContext = DcContext.shared
  24. var selectedChatId: Int?
  25. var selectedChat: DcChat?
  26. let dbHelper = DatabaseHelper()
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. setupNavigationBar()
  30. // workaround for iOS13 bug
  31. if #available(iOS 13.0, *) {
  32. _ = NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { (_) in
  33. if let layoutContainerView = self.view.subviews.last {
  34. layoutContainerView.frame.size.height += 10
  35. }
  36. }
  37. }
  38. }
  39. override func presentationAnimationDidFinish() {
  40. if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
  41. dcContext.logger = self.logger
  42. dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
  43. selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
  44. if let chatId = selectedChatId {
  45. selectedChat = dcContext.getChat(chatId: chatId)
  46. }
  47. reloadConfigurationItems()
  48. } else {
  49. cancel()
  50. }
  51. }
  52. override func isContentValid() -> Bool {
  53. // Do validation of contentText and/or NSExtensionContext attachments here
  54. return !(contentText?.isEmpty ?? true)
  55. }
  56. private func setupNavigationBar() {
  57. guard let item = navigationController?.navigationBar.items?.first else { return }
  58. let button = UIBarButtonItem(
  59. title: String.localized("menu_send"),
  60. style: .done,
  61. target: self,
  62. action: #selector(appendPostTapped))
  63. item.rightBarButtonItem? = button
  64. item.titleView = UIImageView(image: UIImage(named: "ic_chat")?.scaleDownImage(toMax: 26))
  65. }
  66. /// Invoked when the user wants to post.
  67. @objc
  68. private func appendPostTapped() {
  69. if let chatId = self.selectedChatId {
  70. let message = DcMsg(viewType: DC_MSG_TEXT)
  71. message.text = self.contentText
  72. let chatListController = SendingController(chatId: chatId, dcMsg: message, dcContext: dcContext)
  73. chatListController.delegate = self
  74. self.pushConfigurationViewController(chatListController)
  75. }
  76. }
  77. func quit() {
  78. if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
  79. dcContext.closeDatabase()
  80. }
  81. // Inform the host that we're done, so it un-blocks its UI.
  82. self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
  83. }
  84. override func configurationItems() -> [Any]! {
  85. logger.debug("configurationItems")
  86. // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
  87. let item = SLComposeSheetConfigurationItem()
  88. item?.title = String.localized("forward_to")
  89. item?.value = selectedChat?.name
  90. logger.debug("configurationItems chat name: \(String(describing: selectedChat?.name))")
  91. item?.tapHandler = {
  92. let chatListController = ChatListController(dcContext: self.dcContext, chatListDelegate: self)
  93. self.pushConfigurationViewController(chatListController)
  94. }
  95. return [item as Any]
  96. }
  97. override func didSelectCancel() {
  98. quit()
  99. }
  100. }
  101. extension ShareViewController: ChatListDelegate {
  102. func onChatSelected(chatId: Int) {
  103. selectedChatId = chatId
  104. selectedChat = dcContext.getChat(chatId: chatId)
  105. reloadConfigurationItems()
  106. popConfigurationViewController()
  107. }
  108. }
  109. extension ShareViewController: SendingControllerDelegate {
  110. func onSendingAttemptFinished() {
  111. DispatchQueue.main.async {
  112. self.popConfigurationViewController()
  113. self.quit()
  114. }
  115. }
  116. }