TCLiveListModel.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // TCLiveListModel.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by annidyfeng on 16/8/3.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCLiveListModel.h"
  9. @implementation TCLiveUserInfo
  10. - (void) encodeWithCoder: (NSCoder *)coder {
  11. [coder encodeObject:_nickname forKey:@"nickname" ];
  12. [coder encodeObject:_headpic forKey:@"headpic" ];
  13. [coder encodeObject:_frontcover forKey:@"frontcover" ];
  14. [coder encodeObject:_location forKey:@"location" ];
  15. }
  16. - (id) initWithCoder: (NSCoder *) coder
  17. {
  18. self = [super init];
  19. if (self) {
  20. self.nickname = [coder decodeObjectForKey:@"nickname" ];
  21. self.headpic = [coder decodeObjectForKey:@"headpic" ];
  22. self.frontcover = [coder decodeObjectForKey:@"frontcover" ];
  23. self.location = [coder decodeObjectForKey:@"location" ];
  24. }
  25. return self;
  26. }
  27. @end
  28. @implementation TCLiveInfo
  29. - (void) encodeWithCoder: (NSCoder *)coder {
  30. [coder encodeObject:_userid forKey:@"userid" ];
  31. [coder encodeObject:_groupid forKey:@"groupid" ];
  32. [coder encodeObject:_title forKey:@"title" ];
  33. [coder encodeObject:_playurl forKey:@"playurl" ];
  34. [coder encodeObject:_fileid forKey:@"fileid" ];
  35. [coder encodeObject:_userinfo forKey:@"userinfo" ];
  36. }
  37. - (id) initWithCoder: (NSCoder *) coder
  38. {
  39. self = [super init];
  40. if (self) {
  41. self.userid = [coder decodeObjectForKey:@"userid" ];
  42. self.groupid = [coder decodeObjectForKey:@"groupid" ];
  43. self.title = [coder decodeObjectForKey:@"title" ];
  44. self.playurl = [coder decodeObjectForKey:@"playurl" ];
  45. self.fileid = [coder decodeObjectForKey:@"fileid" ];
  46. self.userinfo = [coder decodeObjectForKey:@"userinfo" ];
  47. }
  48. return self;
  49. }
  50. @end
  51. // -----------------------------------------------------------------------------
  52. #import <AFNetworking.h>
  53. #import <MJExtension/MJExtension.h>
  54. #define pageSize 20
  55. #define userDefaultsKey @"TCLiveListMgr"
  56. #define QUOTE(...) @#__VA_ARGS__
  57. //*
  58. NSString *json = QUOTE(
  59. {
  60. "returnValue": 0,
  61. "returnMsg": "return successfully!",
  62. "returnData": {
  63. "all_count": 1,
  64. "pusherlist": [
  65. {
  66. "userid" : "aaaa",
  67. "groupid" : "bbbb",
  68. "timestamp" : 1874483992,
  69. "type" : 1,
  70. "viewercount" : 1888,
  71. "likecount" : 888,
  72. "title" : "Testest",
  73. "playurl" : "rtmp://live.hkstv.hk.lxdns.com/live/hks",
  74. "userinfo" : {
  75. "nickname": "Testest",
  76. "userid" : "aaaa",
  77. "groupid" : "bbbb",
  78. "headpic" : "http://wx.qlogo.cn/mmopen/xxLzNxqMsxnlE4O0LjLaxTkiapbRU1HpVNPPvZPWb4MTicy1G1hJtEic0VGLbMFUrVA5ILoAnjQ2enNTSMYIe2hrQFkfRRfBccQ/132",
  79. "frontcover" : "http://wx.qlogo.cn/mmopen/xxLzNxqMsxnlE4O0LjLaxTkiapbRU1HpVNPPvZPWb4MTicy1G1hJtEic0VGLbMFUrVA5ILoAnjQ2enNTSMYIe2hrQFkfRRfBccQ/0",
  80. "location" : "深圳"
  81. }
  82. }
  83. ]
  84. }
  85. }
  86. );
  87. //*/
  88. NSString *const kTCLiveListNewDataAvailable = @"kTCLiveListNewDataAvailable";
  89. NSString *const kTCLiveListSvrError = @"kTCLiveListSvrError";
  90. NSString *const kTCLiveListUpdated = @"kTCLiveListUpdated";
  91. @interface TCLiveListMgr()
  92. @property NSMutableArray *allVodsArray;
  93. @property int totalCount;
  94. @property int currentPage;
  95. @property BOOL isLoading;
  96. @property BOOL isVideoTypeChange;
  97. @property AFHTTPSessionManager *httpSession;
  98. @property(nonatomic, copy) NSString* userId;
  99. @property(nonatomic) NSNumber* expired;
  100. @property(nonatomic) NSString* token;
  101. @end
  102. @implementation TCLiveListMgr
  103. - (instancetype)init {
  104. self = [super init];
  105. if (self) {
  106. _allVodsArray = [NSMutableArray new];
  107. _totalCount = 0;
  108. _isLoading = NO;
  109. _httpSession = [AFHTTPSessionManager manager];
  110. #ifdef NDEBUG
  111. _httpSession.requestSerializer.timeoutInterval = 5.f;
  112. #endif
  113. [_httpSession setRequestSerializer:[AFJSONRequestSerializer serializer]];
  114. [_httpSession setResponseSerializer:[AFJSONResponseSerializer serializer]];
  115. _httpSession.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
  116. }
  117. return self;
  118. }
  119. + (instancetype)sharedMgr {
  120. static TCLiveListMgr *mgr;
  121. static dispatch_once_t onceToken;
  122. dispatch_once(&onceToken, ^{
  123. if (mgr == nil) {
  124. mgr = [TCLiveListMgr new];
  125. }
  126. });
  127. return mgr;
  128. }
  129. - (void)setUserId:(NSString*)userId expires:(NSNumber*)expires token:(NSString*)token
  130. {
  131. self.userId = userId;
  132. self.expired = expires;
  133. self.token = token;
  134. }
  135. - (void)queryVideoList:(GetType)getType{
  136. _isLoading = YES;
  137. if (getType == GetType_Up) {
  138. _currentPage = 0;
  139. [self cleanAllVods];
  140. }
  141. [_httpSession.operationQueue cancelAllOperations];
  142. [self loadNextVods];
  143. }
  144. - (void)loadNextVods{
  145. _currentPage++;
  146. NSDictionary* params = @{@"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"index":@(_currentPage), @"count":@(20)};
  147. __weak typeof(self) weakSelf = self;
  148. [TCUtil asyncSendHttpRequest:@"get_ugc_list" token:@"" params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
  149. if (resultCode == 200) {
  150. NSArray* vodInfoArray = resultDict[@"list"];
  151. NSMutableArray* pusherList = [NSMutableArray array];
  152. for (NSDictionary* roomInfo in vodInfoArray) {
  153. TCLiveInfo* liveInfo = [TCLiveInfo new];
  154. liveInfo.userinfo = [TCLiveUserInfo new];
  155. liveInfo.userid = roomInfo[@"userid"];
  156. liveInfo.title = roomInfo[@"title"];
  157. liveInfo.playurl = roomInfo[@"play_url"];
  158. liveInfo.hls_play_url = roomInfo[@"hls_play_url"];
  159. liveInfo.fileid = roomInfo[@"file_id"];
  160. liveInfo.reviewStatus = (ReviewStatus)[roomInfo[@"review_status"] intValue];
  161. NSDate *date = [TCUtil timeToDate:roomInfo[@"create_time"]];
  162. liveInfo.timestamp = [date timeIntervalSince1970];
  163. liveInfo.userinfo.nickname = roomInfo[@"nickname"];
  164. liveInfo.userinfo.location = roomInfo[@"location"];
  165. liveInfo.userinfo.headpic = roomInfo[@"avatar"];
  166. liveInfo.userinfo.frontcover = roomInfo[@"frontcover"];
  167. [pusherList addObject:liveInfo];
  168. }
  169. @synchronized (self) {
  170. [_allVodsArray addObjectsFromArray:pusherList];
  171. _totalCount = (int)_allVodsArray.count;
  172. }
  173. _isLoading = NO;
  174. [self dumpLivesToArchive];
  175. [self postDataAvaliableNotify];
  176. }
  177. else {
  178. weakSelf.isLoading = NO;
  179. NSLog(@"finish loading");
  180. [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListSvrError object:[NSError errorWithDomain:@"VodVideoList" code:resultCode userInfo:@{@"errCode":@(resultCode), @"description": message}]];
  181. }
  182. }];
  183. }
  184. - (NSArray *)readVods:(NSRange)range finish:(BOOL *)finish {
  185. NSArray *res = nil;
  186. @synchronized (self) {
  187. if (range.location < _allVodsArray.count) {
  188. range.length = MIN(range.length, _allVodsArray.count - range.location);
  189. res = [_allVodsArray subarrayWithRange:range];
  190. }
  191. }
  192. if (res == nil && _allVodsArray.count > 0) {
  193. *finish = YES;
  194. } else {
  195. *finish = NO;
  196. }
  197. return res;
  198. }
  199. - (TCLiveInfo*)readVod:(NSString*)userId fileId:(NSString*)fileId
  200. {
  201. TCLiveInfo* info = nil;
  202. if (nil == userId)
  203. return nil;
  204. @synchronized (self) {
  205. for (TCLiveInfo* item in _allVodsArray)
  206. {
  207. if ([userId isEqualToString:item.userid] && [fileId isEqualToString:item.fileid])
  208. {
  209. info = item;
  210. break;
  211. }
  212. }
  213. }
  214. return info;
  215. }
  216. - (void)cleanAllVods {
  217. @synchronized (self) {
  218. [_allVodsArray removeAllObjects];
  219. }
  220. }
  221. - (void)postDataAvaliableNotify {
  222. [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListNewDataAvailable object:nil];
  223. }
  224. - (void)postSvrErrorNotify:(int)error reason:(NSString *)msg {
  225. NSError *e = [NSError errorWithDomain:NSOSStatusErrorDomain code:error userInfo:@{NSLocalizedFailureReasonErrorKey:msg}];
  226. [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListSvrError object:e];
  227. }
  228. #pragma mark - 持久化存储
  229. - (void)loadVodsFromArchive {
  230. NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
  231. NSData *savedArray = [currentDefaults objectForKey:userDefaultsKey];
  232. if (savedArray != nil)
  233. {
  234. NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
  235. if (oldArray != nil) {
  236. @synchronized (self) {
  237. _allVodsArray = [[NSMutableArray alloc] initWithArray:oldArray];
  238. _totalCount = (int)_allVodsArray.count;
  239. }
  240. }
  241. }
  242. }
  243. - (void)dumpLivesToArchive {
  244. @synchronized (self) {
  245. if (_allVodsArray.count > 0) {
  246. [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_allVodsArray] forKey:userDefaultsKey];
  247. }
  248. }
  249. }
  250. @end