| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //
- // TCLiveListModel.m
- // TCLVBIMDemo
- //
- // Created by annidyfeng on 16/8/3.
- // Copyright © 2016年 tencent. All rights reserved.
- //
- #import "TCLiveListModel.h"
- @implementation TCLiveUserInfo
- - (void) encodeWithCoder: (NSCoder *)coder {
- [coder encodeObject:_nickname forKey:@"nickname" ];
- [coder encodeObject:_headpic forKey:@"headpic" ];
- [coder encodeObject:_frontcover forKey:@"frontcover" ];
- [coder encodeObject:_location forKey:@"location" ];
- }
- - (id) initWithCoder: (NSCoder *) coder
- {
- self = [super init];
- if (self) {
- self.nickname = [coder decodeObjectForKey:@"nickname" ];
- self.headpic = [coder decodeObjectForKey:@"headpic" ];
- self.frontcover = [coder decodeObjectForKey:@"frontcover" ];
- self.location = [coder decodeObjectForKey:@"location" ];
- }
- return self;
- }
- @end
- @implementation TCLiveInfo
- - (void) encodeWithCoder: (NSCoder *)coder {
- [coder encodeObject:_userid forKey:@"userid" ];
- [coder encodeObject:_groupid forKey:@"groupid" ];
- [coder encodeObject:_title forKey:@"title" ];
- [coder encodeObject:_playurl forKey:@"playurl" ];
- [coder encodeObject:_fileid forKey:@"fileid" ];
- [coder encodeObject:_userinfo forKey:@"userinfo" ];
- }
- - (id) initWithCoder: (NSCoder *) coder
- {
- self = [super init];
- if (self) {
- self.userid = [coder decodeObjectForKey:@"userid" ];
- self.groupid = [coder decodeObjectForKey:@"groupid" ];
- self.title = [coder decodeObjectForKey:@"title" ];
- self.playurl = [coder decodeObjectForKey:@"playurl" ];
- self.fileid = [coder decodeObjectForKey:@"fileid" ];
- self.userinfo = [coder decodeObjectForKey:@"userinfo" ];
- }
- return self;
- }
- @end
- // -----------------------------------------------------------------------------
- #import <AFNetworking.h>
- #import <MJExtension/MJExtension.h>
- #define pageSize 20
- #define userDefaultsKey @"TCLiveListMgr"
- #define QUOTE(...) @#__VA_ARGS__
- //*
- NSString *json = QUOTE(
- {
- "returnValue": 0,
- "returnMsg": "return successfully!",
- "returnData": {
- "all_count": 1,
- "pusherlist": [
- {
- "userid" : "aaaa",
- "groupid" : "bbbb",
- "timestamp" : 1874483992,
- "type" : 1,
- "viewercount" : 1888,
- "likecount" : 888,
- "title" : "Testest",
- "playurl" : "rtmp://live.hkstv.hk.lxdns.com/live/hks",
- "userinfo" : {
- "nickname": "Testest",
- "userid" : "aaaa",
- "groupid" : "bbbb",
- "headpic" : "http://wx.qlogo.cn/mmopen/xxLzNxqMsxnlE4O0LjLaxTkiapbRU1HpVNPPvZPWb4MTicy1G1hJtEic0VGLbMFUrVA5ILoAnjQ2enNTSMYIe2hrQFkfRRfBccQ/132",
- "frontcover" : "http://wx.qlogo.cn/mmopen/xxLzNxqMsxnlE4O0LjLaxTkiapbRU1HpVNPPvZPWb4MTicy1G1hJtEic0VGLbMFUrVA5ILoAnjQ2enNTSMYIe2hrQFkfRRfBccQ/0",
- "location" : "深圳"
- }
- }
- ]
- }
- }
- );
- //*/
- NSString *const kTCLiveListNewDataAvailable = @"kTCLiveListNewDataAvailable";
- NSString *const kTCLiveListSvrError = @"kTCLiveListSvrError";
- NSString *const kTCLiveListUpdated = @"kTCLiveListUpdated";
- @interface TCLiveListMgr()
- @property NSMutableArray *allVodsArray;
- @property int totalCount;
- @property int currentPage;
- @property BOOL isLoading;
- @property BOOL isVideoTypeChange;
- @property AFHTTPSessionManager *httpSession;
- @property(nonatomic, copy) NSString* userId;
- @property(nonatomic) NSNumber* expired;
- @property(nonatomic) NSString* token;
- @end
- @implementation TCLiveListMgr
- - (instancetype)init {
- self = [super init];
- if (self) {
- _allVodsArray = [NSMutableArray new];
- _totalCount = 0;
- _isLoading = NO;
- _httpSession = [AFHTTPSessionManager manager];
- #ifdef NDEBUG
- _httpSession.requestSerializer.timeoutInterval = 5.f;
- #endif
- [_httpSession setRequestSerializer:[AFJSONRequestSerializer serializer]];
- [_httpSession setResponseSerializer:[AFJSONResponseSerializer serializer]];
- _httpSession.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
-
- }
- return self;
- }
- + (instancetype)sharedMgr {
- static TCLiveListMgr *mgr;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- if (mgr == nil) {
- mgr = [TCLiveListMgr new];
- }
- });
- return mgr;
- }
- - (void)setUserId:(NSString*)userId expires:(NSNumber*)expires token:(NSString*)token
- {
- self.userId = userId;
- self.expired = expires;
- self.token = token;
- }
- - (void)queryVideoList:(GetType)getType{
- _isLoading = YES;
-
- if (getType == GetType_Up) {
- _currentPage = 0;
- [self cleanAllVods];
- }
-
- [_httpSession.operationQueue cancelAllOperations];
-
- [self loadNextVods];
- }
- - (void)loadNextVods{
- _currentPage++;
- NSDictionary* params = @{@"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"index":@(_currentPage), @"count":@(20)};
- __weak typeof(self) weakSelf = self;
-
- [TCUtil asyncSendHttpRequest:@"get_ugc_list" token:@"" params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
- if (resultCode == 200) {
- NSArray* vodInfoArray = resultDict[@"list"];
- NSMutableArray* pusherList = [NSMutableArray array];
- for (NSDictionary* roomInfo in vodInfoArray) {
- TCLiveInfo* liveInfo = [TCLiveInfo new];
- liveInfo.userinfo = [TCLiveUserInfo new];
- liveInfo.userid = roomInfo[@"userid"];
- liveInfo.title = roomInfo[@"title"];
- liveInfo.playurl = roomInfo[@"play_url"];
- liveInfo.hls_play_url = roomInfo[@"hls_play_url"];
- liveInfo.fileid = roomInfo[@"file_id"];
- liveInfo.reviewStatus = (ReviewStatus)[roomInfo[@"review_status"] intValue];
- NSDate *date = [TCUtil timeToDate:roomInfo[@"create_time"]];
- liveInfo.timestamp = [date timeIntervalSince1970];
- liveInfo.userinfo.nickname = roomInfo[@"nickname"];
- liveInfo.userinfo.location = roomInfo[@"location"];
- liveInfo.userinfo.headpic = roomInfo[@"avatar"];
- liveInfo.userinfo.frontcover = roomInfo[@"frontcover"];
- [pusherList addObject:liveInfo];
- }
-
- @synchronized (self) {
- [_allVodsArray addObjectsFromArray:pusherList];
- _totalCount = (int)_allVodsArray.count;
- }
- _isLoading = NO;
- [self dumpLivesToArchive];
- [self postDataAvaliableNotify];
- }
- else {
- weakSelf.isLoading = NO;
- NSLog(@"finish loading");
- [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListSvrError object:[NSError errorWithDomain:@"VodVideoList" code:resultCode userInfo:@{@"errCode":@(resultCode), @"description": message}]];
- }
- }];
- }
- - (NSArray *)readVods:(NSRange)range finish:(BOOL *)finish {
- NSArray *res = nil;
-
- @synchronized (self) {
- if (range.location < _allVodsArray.count) {
- range.length = MIN(range.length, _allVodsArray.count - range.location);
- res = [_allVodsArray subarrayWithRange:range];
- }
- }
-
- if (res == nil && _allVodsArray.count > 0) {
- *finish = YES;
- } else {
- *finish = NO;
- }
- return res;
- }
- - (TCLiveInfo*)readVod:(NSString*)userId fileId:(NSString*)fileId
- {
- TCLiveInfo* info = nil;
- if (nil == userId)
- return nil;
-
- @synchronized (self) {
- for (TCLiveInfo* item in _allVodsArray)
- {
- if ([userId isEqualToString:item.userid] && [fileId isEqualToString:item.fileid])
- {
- info = item;
- break;
- }
- }
- }
- return info;
- }
- - (void)cleanAllVods {
- @synchronized (self) {
- [_allVodsArray removeAllObjects];
- }
- }
- - (void)postDataAvaliableNotify {
- [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListNewDataAvailable object:nil];
- }
- - (void)postSvrErrorNotify:(int)error reason:(NSString *)msg {
- NSError *e = [NSError errorWithDomain:NSOSStatusErrorDomain code:error userInfo:@{NSLocalizedFailureReasonErrorKey:msg}];
- [[NSNotificationCenter defaultCenter] postNotificationName:kTCLiveListSvrError object:e];
- }
- #pragma mark - 持久化存储
- - (void)loadVodsFromArchive {
- NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
- NSData *savedArray = [currentDefaults objectForKey:userDefaultsKey];
- if (savedArray != nil)
- {
- NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
- if (oldArray != nil) {
- @synchronized (self) {
- _allVodsArray = [[NSMutableArray alloc] initWithArray:oldArray];
- _totalCount = (int)_allVodsArray.count;
- }
- }
- }
- }
- - (void)dumpLivesToArchive {
- @synchronized (self) {
- if (_allVodsArray.count > 0) {
- [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_allVodsArray] forKey:userDefaultsKey];
- }
- }
- }
- @end
|