MessageDraftStore.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-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 "MessageDraftStore.h"
  21. #import "Contact.h"
  22. #import "Conversation.h"
  23. #import "NSString+Hex.h"
  24. #import "AppGroup.h"
  25. @implementation MessageDraftStore
  26. + (void)saveDraft:(NSString*)draft forConversation:(Conversation*)conversation {
  27. NSString *storeKey = [MessageDraftStore storeKeyForConversation:conversation];
  28. if (storeKey == nil)
  29. return;
  30. NSDictionary *messageDrafts = [[AppGroup userDefaults] dictionaryForKey:@"MessageDrafts"];
  31. if (messageDrafts == nil) {
  32. messageDrafts = [NSDictionary dictionary];
  33. }
  34. NSMutableDictionary *newMessageDrafts = [NSMutableDictionary dictionaryWithDictionary:messageDrafts];
  35. if (draft.length == 0) {
  36. [newMessageDrafts removeObjectForKey:storeKey];
  37. } else {
  38. [newMessageDrafts setObject:draft forKey:storeKey];
  39. }
  40. [[AppGroup userDefaults] setObject:newMessageDrafts forKey:@"MessageDrafts"];
  41. [[AppGroup userDefaults] synchronize];
  42. if (SYSTEM_IS_IPAD)
  43. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUpdateDraftForCell object:nil];
  44. }
  45. + (void)deleteDraftForConversation:(Conversation *)conversation {
  46. NSString *storeKey = [MessageDraftStore storeKeyForConversation:conversation];
  47. if (storeKey == nil)
  48. return;
  49. NSDictionary *messageDrafts = [[AppGroup userDefaults] dictionaryForKey:@"MessageDrafts"];
  50. if (messageDrafts == nil || [messageDrafts objectForKey:storeKey] == nil) {
  51. return;
  52. }
  53. NSMutableDictionary *newMessageDrafts = [NSMutableDictionary dictionaryWithDictionary:messageDrafts];
  54. [newMessageDrafts removeObjectForKey:storeKey];
  55. [[AppGroup userDefaults] setObject:newMessageDrafts forKey:@"MessageDrafts"];
  56. [[AppGroup userDefaults] synchronize];
  57. }
  58. + (NSString *)loadDraftForConversation:(Conversation *)conversation {
  59. NSString *storeKey = [MessageDraftStore storeKeyForConversation:conversation];
  60. if (storeKey == nil)
  61. return nil;
  62. NSDictionary *messageDrafts = [[AppGroup userDefaults] dictionaryForKey:@"MessageDrafts"];
  63. return [messageDrafts objectForKey:storeKey];
  64. }
  65. + (NSString*)storeKeyForConversation:(Conversation*)conversation {
  66. if ([conversation isGroup]) {
  67. NSString *creator = conversation.contact.identity;
  68. if (creator == nil)
  69. creator = @"*";
  70. return [NSString stringWithFormat:@"%@-%@", creator, [NSString stringWithHexData:conversation.groupId]];
  71. } else {
  72. return conversation.contact.identity;
  73. }
  74. }
  75. @end