WeiboData.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // WeiboData.m
  3. // wq
  4. //
  5. // Created by weqia on 13-8-28.
  6. // Copyright (c) 2013年 Weqia. All rights reserved.
  7. //
  8. #import "WeiboData.h"
  9. #import "JSONKit.h"
  10. @implementation WeiboData
  11. @synthesize tag,height,linesLimit,uploadFailed,heightOflimit,shouldExtend,miniWidth,numberOfLineLimit,numberOfLinesTotal,willDisplay,imageHeight,replyHeight,fileHeight;
  12. @synthesize tMans;
  13. @synthesize minHeightForComment;
  14. @synthesize messageId;
  15. @synthesize userId;
  16. @synthesize userNickName;
  17. @synthesize content;
  18. @synthesize createTime;
  19. @synthesize deviceModel;
  20. @synthesize location;
  21. @synthesize type;
  22. @synthesize flag;
  23. @synthesize title;
  24. @synthesize visible;
  25. @synthesize loveCount;
  26. @synthesize playCount;
  27. @synthesize forwardCount;
  28. @synthesize shareCount;
  29. @synthesize praiseCount;
  30. @synthesize commentCount;
  31. @synthesize giftCount;
  32. @synthesize giftTotalPrice;
  33. @synthesize replys;
  34. @synthesize gifts;
  35. @synthesize praises;
  36. @synthesize larges;
  37. @synthesize smalls;
  38. @synthesize images;
  39. @synthesize audios;
  40. @synthesize videos;
  41. @synthesize files;
  42. @synthesize time;
  43. @synthesize remark;
  44. @synthesize address;
  45. @synthesize isPraise;
  46. @synthesize isCollect;
  47. @synthesize heightPraise;
  48. @synthesize isVideo;
  49. #pragma -mark 接口方法
  50. -(id)init{
  51. self = [super init];
  52. praises = [[NSMutableArray alloc]init];
  53. replys = [[NSMutableArray alloc]init];
  54. smalls = [[NSMutableArray alloc]init];
  55. larges = [[NSMutableArray alloc]init];
  56. videos = [[NSMutableArray alloc]init];
  57. audios = [[NSMutableArray alloc]init];
  58. files = [[NSMutableArray alloc]init];
  59. minHeightForComment = 0;
  60. height = 0;
  61. heightOflimit = 0;
  62. imageHeight = 0;
  63. replyHeight = 0;
  64. fileHeight = 0;
  65. heightPraise = 0;
  66. return self;
  67. }
  68. -(void)dealloc{
  69. self.images = nil;
  70. // [super dealloc];
  71. }
  72. +(NSString *)getPrimaryKey
  73. {
  74. return @"weiboId";
  75. }
  76. +(NSString *)getTableName
  77. {
  78. return @"WeiboData";
  79. }
  80. +(int)newTag
  81. {
  82. static int tag=1000;
  83. return tag++;
  84. }
  85. +(NSCache*)shareCacheForWeibo;
  86. {
  87. static NSCache * cache=nil;
  88. static dispatch_once_t onceToken;
  89. dispatch_once(&onceToken, ^{
  90. cache=[[NSCache alloc]init];
  91. cache.totalCostLimit=0.1*1024*1024;
  92. });
  93. return cache;
  94. }
  95. -(MatchParser*)getMatch
  96. {
  97. if (_match&&[_match isKindOfClass:[MatchParser class]]) {
  98. _match.data=self;
  99. return _match;
  100. }else{
  101. NSString *key=[NSString stringWithFormat:@"%@:local=%d:content=%@",self.messageId,self.local,self.content];
  102. MatchParser *parser=[[WeiboData shareCacheForWeibo] objectForKey:key];
  103. if (parser) {
  104. _match=parser;
  105. self.height=parser.height;
  106. self.heightOflimit=parser.heightOflimit;
  107. self.miniWidth=parser.miniWidth;
  108. self.numberOfLinesTotal=parser.numberOfTotalLines;
  109. self.numberOfLineLimit=parser.numberOfLimitLines;
  110. parser.data=self;
  111. if (_match.numberOfTotalLines>_match.numberOfLimitLines) {
  112. self.shouldExtend=YES;
  113. }else{
  114. self.shouldExtend=NO;
  115. }
  116. return parser;
  117. }else{
  118. //说说文本换行长度限制,但这方法未被调用
  119. parser=[self createMatch:JX_SCREEN_WIDTH -67-15];
  120. if (parser) {
  121. [[WeiboData shareCacheForWeibo] setObject:parser forKey:key];
  122. }
  123. return parser;
  124. }
  125. }
  126. }
  127. -(MatchParser*)getMatch:(void(^)(MatchParser *parser,id data))complete data:(id)data
  128. {
  129. if (_match&&[_match isKindOfClass:[MatchParser class]]) {
  130. _match.data=self;
  131. return _match;
  132. }else{
  133. NSString *key=[NSString stringWithFormat:@"%@:local=%d:content=%@",self.messageId,self.local,self.content];
  134. MatchParser *parser=[[WeiboData shareCacheForWeibo] objectForKey:key];
  135. if (parser) {
  136. _match=parser;
  137. self.height=parser.height;
  138. self.heightOflimit=parser.heightOflimit;
  139. self.miniWidth=parser.miniWidth;
  140. self.numberOfLinesTotal=parser.numberOfTotalLines;
  141. self.numberOfLineLimit=parser.numberOfLimitLines;
  142. parser.data=self;
  143. if (_match.numberOfTotalLines>_match.numberOfLimitLines) {
  144. self.shouldExtend=YES;
  145. }else{
  146. self.shouldExtend=NO;
  147. }
  148. return parser;
  149. }else{
  150. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  151. //说说文本换行长度限制
  152. MatchParser*parser=[self createMatch:JX_SCREEN_WIDTH - 67-15];
  153. if (parser) {
  154. _match=parser;
  155. [[WeiboData shareCacheForWeibo] setObject:parser forKey:key];
  156. if (complete) {
  157. complete(parser,data);
  158. }
  159. }
  160. });
  161. return nil;
  162. }
  163. }
  164. }
  165. -(void)setMatch
  166. {
  167. if (_match&&[_match isKindOfClass:[MatchParser class]]) {
  168. _match.data=self;
  169. return;
  170. }else{
  171. NSString *key=[NSString stringWithFormat:@"%@:local=%d:content=%@",self.messageId,self.local,self.content];
  172. MatchParser *parser=[[WeiboData shareCacheForWeibo] objectForKey:key];
  173. if (parser) {
  174. _match=parser;
  175. self.height=parser.height;
  176. self.heightOflimit=parser.heightOflimit;
  177. self.miniWidth=parser.miniWidth;
  178. self.numberOfLinesTotal=parser.numberOfTotalLines;
  179. self.numberOfLineLimit=parser.numberOfLimitLines;
  180. parser.data=self;
  181. if (_match.numberOfTotalLines>_match.numberOfLimitLines) {
  182. self.shouldExtend=YES;
  183. }else{
  184. self.shouldExtend=NO;
  185. }
  186. parser.data=self;
  187. }else{
  188. //说说文本换行长度限制
  189. MatchParser* parser=[self createMatch:JX_SCREEN_WIDTH -67-15];
  190. if (parser) {
  191. [[WeiboData shareCacheForWeibo] setObject:parser forKey:key];
  192. }
  193. }
  194. }
  195. }
  196. -(void)setMatch:(MatchParser *)match
  197. {
  198. _match=match;
  199. }
  200. -(MatchParser*)createMatch:(float)width
  201. {
  202. MatchParser * parser=[[MatchParser alloc]init];
  203. parser.keyWorkColor=[UIColor blueColor];
  204. parser.width=width;
  205. parser.numberOfLimitLines=5;
  206. self.numberOfLineLimit=5;
  207. [parser match:self.content atCallBack:^BOOL(NSString * string) {
  208. NSString *partInStr;
  209. if (![tMans isKindOfClass:[NSString class]]) {
  210. partInStr = [tMans JSONString];
  211. } else {
  212. partInStr = (NSString*)tMans;
  213. }
  214. return NO;
  215. }];
  216. _match=parser;
  217. self.height=_match.height;
  218. self.heightOflimit=parser.heightOflimit;
  219. self.miniWidth=parser.miniWidth;
  220. self.numberOfLinesTotal=parser.numberOfTotalLines;
  221. parser.data=self;
  222. if (_match.numberOfTotalLines>_match.numberOfLimitLines) {
  223. self.shouldExtend=YES;
  224. }else{
  225. self.shouldExtend=NO;
  226. }
  227. return parser;
  228. }
  229. -(void)updateMatch:(void(^)(NSMutableAttributedString * string, NSRange range))link
  230. {
  231. [_match match:self.content atCallBack:^BOOL(NSString * string) {
  232. NSString *partInStr;
  233. if (![tMans isKindOfClass:[NSString class]]) {
  234. partInStr = [tMans JSONString];
  235. } else {
  236. partInStr = (NSString*)tMans;
  237. }
  238. return NO;
  239. } title:nil link:link];
  240. }
  241. -(void)getWeiboReplysByType:(int)type1
  242. {
  243. static int replyId=0;
  244. NSMutableArray * datas=[[NSMutableArray alloc]init];
  245. WeiboReplyData * data=[[WeiboReplyData alloc]init];
  246. data.type=1;
  247. data.body=[NSString stringWithFormat:@"我说好! %d %d %d",replyId,replyId,replyId];
  248. data.messageId=self.messageId;
  249. [data setMatch];
  250. [datas addObject:data];
  251. data=[[WeiboReplyData alloc]init];
  252. data.type=1;
  253. data.body=[NSString stringWithFormat:@"[开心][酷] %d %d %d",replyId,replyId,replyId];
  254. data.messageId=self.messageId;
  255. [data setMatch];
  256. [datas addObject:data];
  257. self.replys=datas;
  258. // NSTimeInterval n = [[NSDate date] timeIntervalSinceReferenceDate];
  259. }
  260. -(void)deleteByReplyId:(NSString*)replyId
  261. {
  262. }
  263. -(void)updateRepleys
  264. {
  265. }
  266. -(NSString*)formatdate:(NSDate*)d format:(NSString*)str{
  267. NSDateFormatter* f=[[NSDateFormatter alloc]init];
  268. f.dateFormat = str;
  269. NSString* s = [f stringFromDate:d];
  270. // [f release];
  271. return s;
  272. }
  273. -(void)getDataFromDict:(NSDictionary*)dict{
  274. [images removeAllObjects];
  275. [larges removeAllObjects];
  276. [praises removeAllObjects];
  277. [replys removeAllObjects];
  278. [gifts removeAllObjects];
  279. [smalls removeAllObjects];
  280. // {"userId":100407,"flag":3,"visible":3,"body":{"text":"10099新商务圈改造完毕,视频是测试效果!!!10099","type":1,"videos":["http://192.168.1.240:8081/video/VID_20140519_190116.mp4"]},"count":{"play":0,"forward":0,"share":0,"collect":0,"praise":0,"comment":0,"money":0,"total":0},"time":1404444296250,"msgId":"53b61e88e4b0724d97d84804","comments":[],"praises":[],"gifts":[]}
  281. self.messageId = [dict objectForKey:@"msgId"];
  282. self.userId = [[dict objectForKey:@"userId"] stringValue];
  283. self.userNickName = [dict objectForKey:@"nickname"];
  284. self.createTime = [[dict objectForKey:@"time"] longLongValue];
  285. self.deviceModel = [dict objectForKey:@"model"];
  286. self.location = [dict objectForKey:@"location"];
  287. self.longitude = [dict objectForKey:@"longitude"];
  288. self.latitude = [dict objectForKey:@"latitude"];
  289. self.flag = [[dict objectForKey:@"flag"] intValue];
  290. self.visible = [[dict objectForKey:@"visible"] intValue];
  291. self.isPraise = [[dict objectForKey:@"isPraise"] boolValue];
  292. self.isCollect = [[dict objectForKey:@"isCollect"] boolValue];
  293. self.isAllowComment = [[dict objectForKey:@"isAllowComment"] intValue];
  294. self.loveCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"collect"] intValue];
  295. self.shareCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"share"] intValue];
  296. self.playCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"play"] intValue];
  297. self.forwardCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"forward"] intValue];
  298. self.praiseCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"praise"] intValue];
  299. self.commentCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"comment"] intValue];
  300. self.giftCount = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"money"] intValue];
  301. self.giftTotalPrice = [[(NSDictionary *)[dict objectForKey:@"count"] objectForKey:@"total"] intValue];
  302. self.title = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"title"];
  303. self.type = [[(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"type"] intValue];
  304. self.content= [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"text"];
  305. // self.audios = [[dict objectForKey:@"body"] objectForKey:@"audios"];
  306. // self.videos = [[dict objectForKey:@"body"] objectForKey:@"videos"];
  307. self.time = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"time"];
  308. self.address = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"address"];
  309. self.remark = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"remark"];
  310. if (self.type == weibo_dataType_share) {
  311. self.sdkUrl = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"sdkUrl"];
  312. self.sdkIcon = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"sdkIcon"];
  313. self.sdkTitle = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"sdkTitle"];
  314. }
  315. NSDictionary* row = nil;
  316. NSArray* p = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"images"];
  317. self.images = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"images"];
  318. for(int i=0;i<[p count];i++){
  319. row = [p objectAtIndex:i];
  320. ObjUrlData * url=[[ObjUrlData alloc] init];
  321. url.url= [row objectForKey:@"tUrl"];
  322. url.mime=@"image/pic";
  323. [smalls addObject:url];
  324. url =[[ObjUrlData alloc]init];
  325. url.url= [row objectForKey:@"oUrl"];
  326. url.mime=@"image/pic";
  327. [larges addObject:url];
  328. }
  329. p = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"audios"];
  330. for(int i=0;i<[p count];i++){
  331. row = [p objectAtIndex:i];
  332. ObjUrlData * url=[[ObjUrlData alloc] init];
  333. url.url= [row objectForKey:@"oUrl"];
  334. url.fileSize = [row objectForKey:@"size"];
  335. url.timeLen = [row objectForKey:@"length"];
  336. [audios addObject:url];
  337. }
  338. p = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"videos"];
  339. for(int i=0;i<[p count];i++){
  340. row = [p objectAtIndex:i];
  341. ObjUrlData * url=[[ObjUrlData alloc] init];
  342. url.url= [row objectForKey:@"oUrl"];
  343. url.fileSize = [row objectForKey:@"size"];
  344. url.timeLen = [row objectForKey:@"length"];
  345. [videos addObject:url];
  346. }
  347. p = [(NSDictionary *)[dict objectForKey:@"body"] objectForKey:@"files"];
  348. for(int i=0;i<[p count];i++){
  349. row = [p objectAtIndex:i];
  350. ObjUrlData * url=[[ObjUrlData alloc] init];
  351. url.url= [row objectForKey:@"oUrl"];
  352. url.fileSize = [row objectForKey:@"size"];
  353. url.timeLen = [row objectForKey:@"length"];
  354. if ([row objectForKey:@"oFileName"]) {
  355. url.name = [row objectForKey:@"oFileName"];
  356. }else {
  357. url.name = [url.url lastPathComponent];
  358. }
  359. [files addObject:url];
  360. }
  361. if( ([self.audios count]>0 || [self.videos count]>0) && [self.images count]<=0){//假如没图,则用头像代替
  362. ObjUrlData * url=[[ObjUrlData alloc]init];
  363. // url.url= @"http://www.feizl.com/upload2007/2013_02/130227014423722.jpg";
  364. url.url= [g_server getHeadImageOUrl:userId];
  365. url.mime=@"image/pic";
  366. [smalls addObject:url];
  367. }
  368. p = [dict objectForKey:@"praises"];
  369. for(int i=0;i<[p count];i++){
  370. WeiboReplyData * reply=[[WeiboReplyData alloc]init];
  371. reply.type=reply_data_praise;
  372. // reply.addHeight = self.minHeightForComment;
  373. reply.messageId=self.messageId;
  374. row = [p objectAtIndex:i];
  375. [reply getDataFromDict:row];
  376. [praises addObject:reply];
  377. }
  378. p = [dict objectForKey:@"gifts"];
  379. for(int i=0;i<[p count];i++){
  380. WeiboReplyData * reply=[[WeiboReplyData alloc]init];
  381. reply.type=reply_data_gift;
  382. // reply.addHeight = self.minHeightForComment;
  383. reply.messageId=self.messageId;
  384. row = [p objectAtIndex:i];
  385. [reply getDataFromDict:row];
  386. [gifts addObject:reply];
  387. }
  388. p = [dict objectForKey:@"comments"];
  389. for(NSInteger i = 0; i < p.count; i++){
  390. WeiboReplyData * reply=[[WeiboReplyData alloc]init];
  391. reply.type=reply_data_comment;
  392. reply.addHeight = self.minHeightForComment;
  393. reply.messageId=self.messageId;
  394. row = [p objectAtIndex:i];
  395. [reply getDataFromDict:row];
  396. [replys addObject:reply];
  397. }
  398. p = nil;
  399. row = nil;
  400. }
  401. -(NSString*)getLastReplyId:(int)page{
  402. NSString* lastId = @"";
  403. if(page > 0){
  404. NSInteger n = [replys count]-1;
  405. if(n>=0){
  406. WeiboReplyData* p = [replys objectAtIndex:n];
  407. lastId = p.replyId;
  408. p = nil;
  409. }
  410. }
  411. return lastId;
  412. }
  413. -(NSString*)getAllPraiseUsers{
  414. if([praises count]<=0)
  415. return nil;
  416. NSString* s=@"";
  417. NSInteger m = [praises count];
  418. NSInteger n = m;
  419. if(n>20)
  420. n = 20;
  421. WeiboReplyData* p = [praises objectAtIndex:0];
  422. s = p.userNickName;
  423. for(int i=1;i<n;i++){
  424. p = [praises objectAtIndex:i];
  425. s = [s stringByAppendingString:@","];
  426. s = [s stringByAppendingString:p.userNickName];
  427. }
  428. if(m>=20)
  429. s = [s stringByAppendingString:[NSString stringWithFormat:@" %d%@",self.praiseCount,Localized(@"WeiboData_PerZan1")]];
  430. else
  431. s = [s stringByAppendingString:[NSString stringWithFormat:@" %d%@",self.praiseCount,Localized(@"WeiboData_PerZan1")]];
  432. return s;
  433. }
  434. -(int)heightPraise{
  435. if([praises count]<=0){
  436. heightPraise = 0;
  437. return heightPraise;
  438. }
  439. WeiboReplyData* p = [[WeiboReplyData alloc]init];
  440. p.type = reply_data_praise;
  441. p.body = [self getAllPraiseUsers];
  442. [p getMatch];
  443. heightPraise = p.height+6;
  444. return heightPraise;
  445. }
  446. -(int) heightForReply
  447. {
  448. if([replys count]==0)
  449. return self.heightPraise;
  450. int m=6;
  451. int n = self.heightPraise;
  452. if(n>0)//评论和赞只加一个6
  453. m = 0;
  454. for(WeiboReplyData * data in replys){
  455. m+=data.height+4;
  456. }
  457. return m+n;
  458. }
  459. -(int) height2ForReply
  460. {
  461. if([replys count]==0)
  462. return 0;
  463. int n=0;
  464. for(WeiboReplyData * data in replys){
  465. [data getHeight2];
  466. n+=data.height2;
  467. }
  468. return n;
  469. }
  470. -(NSString*)getMediaURL{
  471. ObjUrlData* p=nil;
  472. if(self.isVideo){
  473. if([videos count]>0)
  474. p = [videos objectAtIndex:0];
  475. }
  476. else{
  477. if([audios count]>0)
  478. p = [audios objectAtIndex:0];
  479. }
  480. return p.url;
  481. }
  482. -(BOOL)isVideo{
  483. return [videos count]>0;
  484. return type == weibo_dataType_video;
  485. }
  486. #pragma -mark 私有方法
  487. @end