EMDemoOptions.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // EMDemoOptions.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2018/12/17.
  6. // Copyright © 2018 XieYajie. All rights reserved.
  7. //
  8. #import "EMDemoOptions.h"
  9. #import <Hyphenate/EMOptions+PrivateDeploy.h>
  10. static EMDemoOptions *sharedOptions = nil;
  11. @implementation EMDemoOptions
  12. - (instancetype)init
  13. {
  14. self = [super init];
  15. if (self) {
  16. [self _initServerOptions];
  17. self.isDeleteMessagesWhenExitGroup = NO;
  18. self.isAutoAcceptGroupInvitation = NO;
  19. self.isAutoTransferMessageAttachments = YES;
  20. self.isAutoDownloadThumbnail = YES;
  21. self.isSortMessageByServerTime = NO;
  22. self.isPriorityGetMsgFromServer = NO;
  23. self.isAutoLogin = NO;
  24. self.loggedInUsername = @"";
  25. self.loggedInPassword = @"";
  26. self.isChatTyping = NO;
  27. self.isAutoDeliveryAck = NO;
  28. self.isOfflineHangup = NO;
  29. self.isShowCallInfo = NO;
  30. self.isUseBackCamera = NO;
  31. self.willRecord = NO;
  32. self.willMergeStrem = NO;
  33. }
  34. return self;
  35. }
  36. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  37. {
  38. if (self = [super init]) {
  39. NSString *appkey = [aDecoder decodeObjectForKey:kOptions_Appkey];
  40. if ([appkey length] == 0) {
  41. appkey = DEF_APPKEY;
  42. }
  43. self.appkey = appkey;
  44. self.apnsCertName = [aDecoder decodeObjectForKey:kOptions_ApnsCertname];
  45. self.usingHttpsOnly = [aDecoder decodeBoolForKey:kOptions_HttpsOnly];
  46. self.specifyServer = [aDecoder decodeBoolForKey:kOptions_SpecifyServer];
  47. self.chatPort = [aDecoder decodeIntForKey:kOptions_IMPort];
  48. self.chatServer = [aDecoder decodeObjectForKey:kOptions_IMServer];
  49. self.restServer = [aDecoder decodeObjectForKey:kOptions_RestServer];
  50. self.isDeleteMessagesWhenExitGroup = [aDecoder decodeBoolForKey:kOptions_DeleteChatExitGroup];
  51. self.isAutoAcceptGroupInvitation = [aDecoder decodeBoolForKey:kOptions_AutoAcceptGroupInvitation];
  52. self.isAutoTransferMessageAttachments = [aDecoder decodeBoolForKey:kOptions_AutoTransMsgFile];
  53. self.isAutoDownloadThumbnail = [aDecoder decodeBoolForKey:kOptions_AutoDownloadThumb];
  54. self.isSortMessageByServerTime = [aDecoder decodeBoolForKey:kOptions_SortMessageByServerTime];
  55. self.isPriorityGetMsgFromServer = [aDecoder decodeBoolForKey:kOptions_PriorityGetMsgFromServer];
  56. self.isAutoLogin = [aDecoder decodeBoolForKey:kOptions_AutoLogin];
  57. self.loggedInUsername = [aDecoder decodeObjectForKey:kOptions_LoggedinUsername];
  58. self.loggedInPassword = [aDecoder decodeObjectForKey:kOptions_LoggedinPassword];
  59. self.isChatTyping = [aDecoder decodeBoolForKey:kOptions_ChatTyping];
  60. self.isAutoDeliveryAck = [aDecoder decodeBoolForKey:kOptions_AutoDeliveryAck];
  61. self.isOfflineHangup = [aDecoder decodeBoolForKey:kOptions_OfflineHangup];
  62. self.isShowCallInfo = [aDecoder decodeBoolForKey:kOptions_ShowCallInfo];
  63. self.isUseBackCamera = [aDecoder decodeBoolForKey:kOptions_UseBackCamera];
  64. self.willRecord = [aDecoder decodeBoolForKey:kOptions_WillRecord];
  65. self.willMergeStrem = [aDecoder decodeBoolForKey:kOptions_WillMergeStrem];
  66. }
  67. return self;
  68. }
  69. - (void)encodeWithCoder:(NSCoder *)aCoder
  70. {
  71. [aCoder encodeObject:self.appkey forKey:kOptions_Appkey];
  72. [aCoder encodeObject:self.apnsCertName forKey:kOptions_ApnsCertname];
  73. [aCoder encodeBool:self.usingHttpsOnly forKey:kOptions_HttpsOnly];
  74. [aCoder encodeBool:self.specifyServer forKey:kOptions_SpecifyServer];
  75. [aCoder encodeInt:self.chatPort forKey:kOptions_IMPort];
  76. [aCoder encodeObject:self.chatServer forKey:kOptions_IMServer];
  77. [aCoder encodeObject:self.restServer forKey:kOptions_RestServer];
  78. [aCoder encodeBool:self.isDeleteMessagesWhenExitGroup forKey:kOptions_DeleteChatExitGroup];
  79. [aCoder encodeBool:self.isAutoAcceptGroupInvitation forKey:kOptions_AutoAcceptGroupInvitation];
  80. [aCoder encodeBool:self.isAutoTransferMessageAttachments forKey:kOptions_AutoTransMsgFile];
  81. [aCoder encodeBool:self.isAutoDownloadThumbnail forKey:kOptions_AutoDownloadThumb];
  82. [aCoder encodeBool:self.isSortMessageByServerTime forKey:kOptions_SortMessageByServerTime];
  83. [aCoder encodeBool:self.isPriorityGetMsgFromServer forKey:kOptions_PriorityGetMsgFromServer];
  84. [aCoder encodeBool:self.isAutoLogin forKey:kOptions_AutoLogin];
  85. [aCoder encodeObject:self.loggedInUsername forKey:kOptions_LoggedinUsername];
  86. [aCoder encodeObject:self.loggedInPassword forKey:kOptions_LoggedinPassword];
  87. [aCoder encodeBool:self.isChatTyping forKey:kOptions_ChatTyping];
  88. [aCoder encodeBool:self.isAutoDeliveryAck forKey:kOptions_AutoDeliveryAck];
  89. [aCoder encodeBool:self.isOfflineHangup forKey:kOptions_OfflineHangup];
  90. [aCoder encodeBool:self.isShowCallInfo forKey:kOptions_ShowCallInfo];
  91. [aCoder encodeBool:self.isUseBackCamera forKey:kOptions_UseBackCamera];
  92. [aCoder encodeBool:self.willRecord forKey:kOptions_WillRecord];
  93. [aCoder encodeBool:self.willMergeStrem forKey:kOptions_WillMergeStrem];
  94. }
  95. - (id)copyWithZone:(nullable NSZone *)zone
  96. {
  97. EMDemoOptions *retModel = [[[self class] alloc] init];
  98. retModel.appkey = self.appkey;
  99. retModel.apnsCertName = self.apnsCertName;
  100. retModel.usingHttpsOnly = self.usingHttpsOnly;
  101. retModel.specifyServer = self.specifyServer;
  102. retModel.chatPort = self.chatPort;
  103. retModel.chatServer = self.chatServer;
  104. retModel.restServer = self.restServer;
  105. retModel.isDeleteMessagesWhenExitGroup = self.isDeleteMessagesWhenExitGroup;
  106. retModel.isAutoAcceptGroupInvitation = self.isAutoAcceptGroupInvitation;
  107. retModel.isAutoTransferMessageAttachments = self.isAutoTransferMessageAttachments;
  108. retModel.isAutoDownloadThumbnail = self.isAutoDownloadThumbnail;
  109. retModel.isSortMessageByServerTime = self.isSortMessageByServerTime;
  110. retModel.isPriorityGetMsgFromServer = self.isPriorityGetMsgFromServer;
  111. retModel.isAutoLogin = self.isAutoLogin;
  112. retModel.loggedInUsername = self.loggedInUsername;
  113. retModel.loggedInPassword = self.loggedInPassword;
  114. retModel.isChatTyping = self.isChatTyping;
  115. retModel.isAutoDeliveryAck = self.isAutoDeliveryAck;
  116. retModel.isOfflineHangup = self.isOfflineHangup;
  117. retModel.isShowCallInfo = self.isShowCallInfo;
  118. retModel.isUseBackCamera = self.isUseBackCamera;
  119. retModel.willRecord = self.willRecord;
  120. retModel.willMergeStrem = self.willMergeStrem;
  121. return retModel;
  122. }
  123. - (void)setLoggedInUsername:(NSString *)loggedInUsername
  124. {
  125. if (![_loggedInUsername isEqualToString:loggedInUsername]) {
  126. _loggedInUsername = loggedInUsername;
  127. _loggedInPassword = @"";
  128. }
  129. }
  130. #pragma mark - Private
  131. - (void)_initServerOptions
  132. {
  133. self.appkey = DEF_APPKEY;
  134. #if DEBUG
  135. self.apnsCertName = @"chatdemoui_dev";
  136. #else
  137. self.apnsCertName = @"chatdemoui";
  138. #endif
  139. self.usingHttpsOnly = NO;
  140. self.specifyServer = NO;
  141. self.chatServer = @"msync-im1.sandbox.easemob.com";
  142. self.chatPort = 6717;
  143. self.restServer = @"a1.sdb.easemob.com";
  144. }
  145. #pragma mark - Public
  146. - (void)archive
  147. {
  148. NSString *fileName = @"emdemo_options.data";
  149. NSString *file = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:fileName];
  150. [NSKeyedArchiver archiveRootObject:self toFile:file];
  151. }
  152. - (EMOptions *)toOptions
  153. {
  154. EMOptions *retOpt = [EMOptions optionsWithAppkey:self.appkey];
  155. retOpt.apnsCertName = self.apnsCertName;
  156. retOpt.usingHttpsOnly = self.usingHttpsOnly;
  157. retOpt.enableConsoleLog = NO;
  158. if (self.specifyServer) {
  159. retOpt.enableDnsConfig = NO;
  160. retOpt.chatPort = self.chatPort;
  161. retOpt.chatServer = self.chatServer;
  162. retOpt.restServer = self.restServer;
  163. }
  164. retOpt.isAutoLogin = self.isAutoLogin;
  165. retOpt.isDeleteMessagesWhenExitGroup = self.isDeleteMessagesWhenExitGroup;
  166. retOpt.isAutoAcceptGroupInvitation = self.isAutoAcceptGroupInvitation;
  167. retOpt.isAutoTransferMessageAttachments = self.isAutoTransferMessageAttachments;
  168. retOpt.isAutoDownloadThumbnail = self.isAutoDownloadThumbnail;
  169. retOpt.sortMessageByServerTime = self.isSortMessageByServerTime;
  170. retOpt.enableDeliveryAck = self.isAutoDeliveryAck;
  171. retOpt.enableConsoleLog = YES;
  172. return retOpt;
  173. }
  174. #pragma mark - Class Methods
  175. + (instancetype)sharedOptions
  176. {
  177. static dispatch_once_t onceToken;
  178. dispatch_once(&onceToken, ^{
  179. sharedOptions = [EMDemoOptions getOptionsFromLocal];
  180. });
  181. return sharedOptions;
  182. }
  183. + (EMDemoOptions *)getOptionsFromLocal
  184. {
  185. EMDemoOptions *retModel = nil;
  186. NSString *fileName = @"emdemo_options.data";
  187. NSString *file = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:fileName];
  188. retModel = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
  189. if (!retModel) {
  190. retModel = [[EMDemoOptions alloc] init];
  191. [retModel archive];
  192. }
  193. return retModel;
  194. }
  195. + (void)reInitAndSaveServerOptions
  196. {
  197. EMDemoOptions *demoOptions = [EMDemoOptions sharedOptions];
  198. [demoOptions _initServerOptions];
  199. [demoOptions archive];
  200. }
  201. + (void)updateAndSaveServerOptions:(NSDictionary *)aDic
  202. {
  203. NSString *appkey = [aDic objectForKey:kOptions_Appkey];
  204. NSString *apns = [aDic objectForKey:kOptions_ApnsCertname];
  205. BOOL httpsOnly = [[aDic objectForKey:kOptions_HttpsOnly] boolValue];
  206. if ([appkey length] == 0) {
  207. appkey = DEF_APPKEY;
  208. }
  209. if ([apns length] == 0) {
  210. #if DEBUG
  211. apns = @"chatdemoui_dev";
  212. #else
  213. apns = @"chatdemoui";
  214. #endif
  215. }
  216. EMDemoOptions *demoOptions = [EMDemoOptions sharedOptions];
  217. demoOptions.appkey = appkey;
  218. demoOptions.apnsCertName = apns;
  219. demoOptions.usingHttpsOnly = httpsOnly;
  220. int specifyServer = [[aDic objectForKey:kOptions_SpecifyServer] intValue];
  221. demoOptions.specifyServer = NO;
  222. if (specifyServer != 0) {
  223. demoOptions.specifyServer = YES;
  224. NSString *imServer = [aDic objectForKey:kOptions_IMServer];
  225. NSString *imPort = [aDic objectForKey:kOptions_IMPort];
  226. NSString *restServer = [aDic objectForKey:kOptions_RestServer];
  227. if ([imServer length] > 0 && [restServer length] > 0 && [imPort length] > 0) {
  228. demoOptions.chatPort = [imPort intValue];
  229. demoOptions.chatServer = imServer;
  230. demoOptions.restServer = restServer;
  231. }
  232. }
  233. [demoOptions archive];
  234. }
  235. @end