ChatViewControllerCache.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ChatViewControllerCache.h"
  21. #import "EntityManager.h"
  22. #import "AppDelegate.h"
  23. @implementation ChatViewControllerCache
  24. static NSMutableDictionary *cache;
  25. + (void)initialize {
  26. if (self == [ChatViewControllerCache self]) {
  27. cache = [NSMutableDictionary dictionary];
  28. }
  29. }
  30. + (ChatViewController *)newControllerForConversation:(Conversation *)conversation forceTouch:(BOOL)forceTouch {
  31. UIStoryboard *storyboard = [AppDelegate getMainStoryboard];
  32. ChatViewController *controller = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"chatViewController"];
  33. controller.conversation = conversation;
  34. controller.isOpenWithForceTouch = forceTouch;
  35. if (SYSTEM_IS_IPAD) {
  36. controller.hidesBottomBarWhenPushed = NO;
  37. }
  38. return controller;
  39. }
  40. + (ChatViewController *)controllerForConversation:(Conversation *)conversation {
  41. @synchronized(cache) {
  42. ChatViewController *controller = [cache objectForKey:conversation.objectID];
  43. if (controller == nil) {
  44. controller = [self newControllerForConversation:conversation forceTouch:NO];
  45. [cache setObject:controller forKey:conversation.objectID];
  46. }
  47. return controller;
  48. }
  49. }
  50. + (ChatViewController *)controllerForNotificationInfo:(NSDictionary *)info {
  51. Conversation *conversation = [self getConversationForNotificationInfo:info];
  52. ChatViewController *controller = [self controllerForConversation:conversation];
  53. if (controller) {
  54. [self setupController:controller withInfo:info];
  55. }
  56. return controller;
  57. }
  58. + (void)addInitializedController:(ChatViewController *)controller {
  59. @synchronized(cache) {
  60. [cache setObject:controller forKey:controller.conversation.objectID];
  61. }
  62. }
  63. + (void)clearCache {
  64. @synchronized(cache) {
  65. [cache removeAllObjects];
  66. }
  67. }
  68. + (void)refresh {
  69. for (ChatViewController *vc in [cache allValues]) {
  70. [vc refresh];
  71. }
  72. }
  73. + (void)clearConversation:(Conversation *)conversation {
  74. @synchronized(cache) {
  75. if (conversation) {
  76. [cache removeObjectForKey:conversation];
  77. }
  78. }
  79. }
  80. + (Conversation *)getConversationForNotificationInfo:(NSDictionary *)info {
  81. __block Conversation *conversation = [info objectForKey:kKeyConversation];
  82. if (conversation == nil) {
  83. EntityManager *entityManager = [[EntityManager alloc] init];
  84. [entityManager performSyncBlockAndSafe:^{
  85. Contact *contact = [info objectForKey:kKeyContact];
  86. if (contact) {
  87. conversation = [entityManager conversationForContact:contact createIfNotExisting:YES];
  88. }
  89. }];
  90. }
  91. return conversation;
  92. }
  93. #pragma mark - private
  94. + (void)setupController:(ChatViewController *)controller withInfo:(NSDictionary *)info {
  95. NSNumber *forceComposeNumber = [info objectForKey:kKeyForceCompose];
  96. BOOL forceCompose = forceComposeNumber ? forceComposeNumber.boolValue : NO;
  97. if (forceCompose) {
  98. controller.composing = YES;
  99. }
  100. NSString *text = [info objectForKey:kKeyText];
  101. if (text) {
  102. controller.messageText = text;
  103. }
  104. UIImage *image = [info objectForKey:kKeyImage];
  105. if (image) {
  106. controller.imageDataToSend = image;
  107. }
  108. }
  109. @end