iOS開發之聊天模塊--內容保存邏輯實現

需求詳解:html

  在實際開發中,有多是在後期優化的時候,會有這麼須要優化的需求:聊天輸入框保存以前輸入的文本,提升用戶的良好體驗。佈局

   在聊天模塊中,用戶可能會在輸入框輸入若干字符,可是沒有點擊發送就點擊退出聊天,或者要點擊用戶頭像確認用戶的信息,或者好比須要向好友發送另外一個好 友的ID不得不暫時退出當前好友聊天界面跳轉找到別的界面找ID,然而當前聊天輸入框也已經輸入好了若干字符,用戶固然不但願退出以後就刪除以前輸入好的 文字。因此這裏就須要暫時保存用戶輸入好的可是沒有發送出去的字符串。post

  可是,還須要知足一、徹底殺掉或者徹底退出應用就須要清除這個暫時保存的字符串,二、發送出去以後,確定就要delegate以前暫時保存的字符串嘍。學習

 

開始:優化

這部分邏輯的實現一開始我沒怎麼比較好的頭緒,只想到本地序列化,但實際上這個還不算是最好的思路,由於本地序列化用到這裏有點小題大作了,其實只要用全局靜態變量的字典就能夠了。編碼

具體實現的邏輯,我也特地閱讀研究了Coding項目的實現,畢竟這個項目是比較成熟的項目,聊天模塊也作的很不錯,因此學學別人的思想,正所謂站在巨人的肩膀上,也是很好的哦。url

 那麼下面,我就直接解讀Coding源碼(學習Coding-iOS開源項目日誌(一))在這個聊天模塊內容保存的邏輯吧,就不拿本身工做開發的項目來說了。spa

一、首先聲明全局static的變量,Coding中用inputStrDict存儲輸入框的字符串,而inputMediaDict我暫時不知道它具體存什麼的,應該是media之類的元素:日誌

 

二、而後將不少邏輯封裝在這個UIMessageInputView類中,方法都不用公開,徹底利用UIMessageInputView活動週期的邏輯就能夠了。code

 

  1 #pragma mark remember input  2  3 - (NSMutableDictionary *)shareInputStrDict{  4 if (!_inputStrDict) {  5 _inputStrDict = [[NSMutableDictionary alloc] init];  6  }  7 return _inputStrDict;  8 }  9  10 - (NSMutableDictionary *)shareInputMediaDict{  11 if (!_inputMediaDict) {  12 _inputMediaDict = [[NSMutableDictionary alloc] init];  13  }  14 return _inputMediaDict;  15 }  16  17 - (NSString *)inputKey{  18 NSString *inputKey = nil;  19 if (_contentType == UIMessageInputViewContentTypePriMsg) {  20 inputKey = [NSString stringWithFormat:@"privateMessage_%@", self.toUser.global_key];  21 }else{  22 if (_commentOfId) {  23 switch (_contentType) {  24 case UIMessageInputViewContentTypeTweet:  25 inputKey = [NSString stringWithFormat:@"tweet_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];  26 break;  27 case UIMessageInputViewContentTypeTopic:  28 inputKey = [NSString stringWithFormat:@"topic_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];  29 break;  30 case UIMessageInputViewContentTypeTask:  31 inputKey = [NSString stringWithFormat:@"task_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];  32 break;  33 default:  34 break;  35  }  36  }  37  }  38 return inputKey;  39 }  40  41 - (NSString *)inputStr{  42 NSString *inputKey = [self inputKey];  43 if (inputKey) {  44 DebugLog(@"inputStr_get:%@",[[self shareInputStrDict] objectForKey:inputKey]);  45 return [[self shareInputStrDict] objectForKey:inputKey];  46  }  47 return nil;  48 }  49  50 - (void)deleteInputData{  51 NSString *inputKey = [self inputKey];  52 DebugLog(@"inputKey_delegate:%@",inputKey);  53 if (inputKey) {  54  [[self shareInputStrDict] removeObjectForKey:inputKey];  55  [[self shareInputMediaDict] removeObjectForKey:inputKey];  56  }  57 }  58  59 - (void)saveInputStr{  60 NSString *inputStr = _inputTextView.text;  61 NSString *inputKey = [self inputKey];  62 DebugLog(@"inputKey_save:%@",inputKey);  63 if (inputKey && inputKey.length > 0) {  64 if (inputStr && inputStr.length > 0) {  65  [[self shareInputStrDict] setObject:inputStr forKey:inputKey];  66 }else{  67  [[self shareInputStrDict] removeObjectForKey:inputKey];  68  }  69  }  70 }  71  72 - (void)saveInputMedia{  73 NSString *inputKey = [self inputKey];  74 if (inputKey && inputKey.length > 0) { 75 if (_mediaList.count > 0) { 76 [[self shareInputMediaDict] setObject:_mediaList forKey:inputKey]; 77 }else{ 78 [[self shareInputMediaDict] removeObjectForKey:inputKey]; 79 } 80 } 81 } 82 83 - (NSMutableArray *)inputMedia{ 84 NSString *inputKey = [self inputKey]; 85 if (inputKey) { 86 return [[self shareInputMediaDict] objectForKey:inputKey]; 87 } 88 return nil; 89 } 90 91 - (void)setToUser:(User *)toUser{ 92 _toUser = toUser; 93 NSString *inputStr = [self inputStr]; 94 if (_inputTextView) { 95 if (_contentType != UIMessageInputViewContentTypePriMsg) { 96 self.placeHolder = _toUser? [NSString stringWithFormat:@"回覆 %@", _toUser.name]: @"撰寫評論"; 97 }else{ 98 self.placeHolder = @"請輸入私信內容"; 99 } 100 _inputTextView.selectedRange = NSMakeRange(0, _inputTextView.text.length); 101 [_inputTextView insertText:inputStr? inputStr: @""]; 102 103 _mediaList = [self inputMedia]; 104 [self mediaListChenged]; 105 } 106 }

上面無非就是經過聊天對象的名字拼接成key值,而後對應存儲當前輸入框的字符串到全局static的字典中,而後是取出、刪除的幾個方法。

三、再看看那哪些地方調用了這些方法:

保 存的方法,放在frame重寫的方法裏,由於輸入框會隨着鍵盤的現實和隱藏而切換frame,不過我公司的項目一開始聊天模塊是我同事開發的,我發現他用 Masonry的佈局代碼去變換輸入框的位置,選擇了佈局約束也就意味着放棄了frame,因此何處調用save方法仍是要根據實際需求和實際的編碼實 現。另外,其實在最開始開發這個輸入框的時候,能夠考慮其運做的週期:開始編輯->正在編輯->結束編輯,這些運做週期是能夠實現出各自的方 法,就和一個控制器的生命週期同樣。總之思路不少,作好是能實現出好管理好維護的邏輯。

 而後找找刪除的方法,刪除的方法是放在將字符串發出去的最前面,由於已經發送出去了,是能夠將字典中存儲的元素刪除了去。

另外,在建立key的時候,這個key字符串是依賴當前聊天對象的,由於當前輸入框的內容要和當前好友對象一一對應,不能我保存了當前好友對應的輸入框內容,跳到別的好友卻又出現了同樣的內容。因此key值須要依據當前好友的字符串來決定,因此Coding源碼中重寫了ToUser屬性的set方法:

 

尊重勞動成果,轉載註明出處:http://www.cnblogs.com/goodboy-heyang/p/5782201.html

相關文章
相關標籤/搜索