PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/WHAMBUSH/wbVideoCell.m

https://gitlab.com/urbanjunglestudio/whambush-ios
Objective C | 368 lines | 265 code | 55 blank | 48 comment | 38 complexity | 043b7c3c2c1498313cd6216e4b81e07c MD5 | raw file
  1. //
  2. // wbVideoCell.m
  3. // WHAMBUSH
  4. //
  5. // Created by Jari Kalinainen on 9/5/13.
  6. // Copyright (c) 2013 Jari Kalinainen. All rights reserved.
  7. //
  8. #import "wbVideoCell.h"
  9. #import "AFNetworking.h"
  10. @implementation wbVideoCell
  11. @synthesize cellContent;
  12. @synthesize rankNumber;
  13. @synthesize isTV;
  14. //- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. //self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // Initialization code
  21. isTV = NO;
  22. [self setBounds:CGRectMake(0, 0, self.bounds.size.width, 100)];
  23. [self setBackgroundColor:kBKGUICOLOR];
  24. videoRect = CGRectMake(0,(self.bounds.size.height-(150.0/16.0)*9.0)/2.0, 150,(150.0/16.0)*9.0);
  25. [[wbData sharedData] setThumbSize:videoRect.size];
  26. if (ai == nil) {
  27. ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  28. }
  29. [ai setCenter:CGPointMake(CGRectGetWidth(videoRect)/2,CGRectGetHeight(self.frame)/2)];
  30. [ai setHidesWhenStopped:YES];
  31. [ai startAnimating];
  32. }
  33. DPRINTCLASS;
  34. return self;
  35. }
  36. -(void)setCellContent:(NSMutableDictionary *)_cellContent
  37. {
  38. cellContent=_cellContent;
  39. [[wbData sharedData] saveWhambushUser:[_cellContent objectForKey:@"added_by"]];
  40. }
  41. -(void)showWait
  42. {
  43. [ai setHidden:YES];
  44. }
  45. -(void)hideWait
  46. {
  47. [ai stopAnimating];
  48. [ai removeFromSuperview];
  49. }
  50. -(void)setRankNumber:(NSInteger)_rankNumber
  51. {
  52. rankLabel = [NSString stringWithFormat:@"%ld.",(long)_rankNumber];
  53. rankNumber = _rankNumber;
  54. }
  55. #define kTVW 20
  56. -(void)drawMe
  57. {
  58. if (cellContent == nil) {
  59. DCMSG(cellContent);
  60. }
  61. if([self cellContent] != nil){
  62. [aiEmpty removeFromSuperview];
  63. if ([[cellContent objectForKey:@"deleted"] boolValue]) {
  64. } else {
  65. UIImageView *videoThumbViewBkg = [[UIImageView alloc] initWithFrame:videoRect];
  66. [videoThumbViewBkg setBackgroundColor:kBLACKUICOLOR];
  67. if (videoThumbView == nil) {
  68. videoThumbView = [[UIImageView alloc] initWithFrame:videoRect];
  69. [videoThumbView setContentMode:UIViewContentModeScaleAspectFit];
  70. __weak typeof(self) weakSelf = self;
  71. NSURL *url = [NSURL URLWithString:[[self cellContent] objectForKey:@"thumbnail_url"]];
  72. [videoThumbView setImageWithURLRequest:[NSMutableURLRequest requestWithURL:url] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
  73. __strong typeof(self) strongSelf = weakSelf;
  74. /*if (image != nil) {
  75. __strong typeof(self) strongSelf = weakSelf;
  76. float tbHeight = strongSelf->videoRect.size.height;
  77. float tbWidth = (image.size.width)/(image.size.height/tbHeight);
  78. if (tbWidth == videoRect.size.width) {
  79. [strongSelf->videoThumbView setFrame:CGRectMake(0, strongSelf->videoRect.origin.y, tbWidth,tbHeight)];
  80. } else {
  81. float newX = (videoRect.size.width/2)-(tbWidth/2);
  82. [strongSelf->videoThumbView setFrame:CGRectMake(newX, strongSelf->videoRect.origin.y, tbWidth,tbHeight)];
  83. }
  84. }*/
  85. //[videoThumbView setCenter:CGPointMake(videoThumbView.center.x, videoRect.size.height/2)];
  86. [strongSelf->videoThumbView setImage:image];
  87. [weakSelf hideWait];
  88. } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
  89. [weakSelf hideWait];
  90. }];
  91. /*[videoThumbView setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
  92. if (image != nil) {
  93. float tbHeight = videoRect.size.height;
  94. float tbWidth = (image.size.width)/(image.size.height/tbHeight);
  95. if (tbWidth == videoRect.size.width) {
  96. [videoThumbView setFrame:CGRectMake(0, videoRect.origin.y, tbWidth,tbHeight)];
  97. } else {
  98. float newX = (videoRect.size.width/2)-(tbWidth/2);
  99. [videoThumbView setFrame:CGRectMake(newX, videoRect.origin.y, tbWidth,tbHeight)];
  100. }
  101. }
  102. //[videoThumbView setCenter:CGPointMake(videoThumbView.center.x, videoRect.size.height/2)];
  103. [self hideWait];
  104. }];*/
  105. }
  106. UILabel *notProcessedLabel = [[UILabel alloc] initWithFrame:videoRect];
  107. [notProcessedLabel setText:NSLocalizedString(@"MAIN_VIDEO_IS_BEING_PROCESSED", @"")];
  108. [notProcessedLabel setTextAlignment:NSTextAlignmentCenter];
  109. [notProcessedLabel setFont:kFONT(16)];
  110. [notProcessedLabel setTextColor:kWHITEUICOLOR];
  111. [notProcessedLabel setNumberOfLines:0];
  112. //[notProcessedLabel setBackgroundColor:[UIColor yellowColor]];
  113. #define marginal 6
  114. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(160, 6, self.bounds.size.width-(160+marginal), self.bounds.size.height-12)];
  115. [containerView setBackgroundColor:kTRANSPARENTUICOLOR];
  116. [containerView setClipsToBounds:YES];
  117. if (videoTime == nil) {
  118. videoTime = [[UILabel alloc] initWithFrame:CGRectMake(0, (containerView.frame.size.height-11), containerView.frame.size.width, 10)];
  119. }
  120. [videoTime setFont:kFONTHelvetica(9)];
  121. [videoTime setTextColor:kLIGHTGRAYUICOLOR];
  122. [videoTime setBackgroundColor:kTRANSPARENTUICOLOR];
  123. [videoTime setText:[NSString stringWithFormat:@"%@",[[wbAPI sharedAPI] parsePostedTime:[[self cellContent] objectForKey:@"published_at"]]]];
  124. if (videoDescription == nil) {
  125. videoDescription = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(videoTime.frame), containerView.frame.size.width, 1)];
  126. }
  127. [videoDescription setText:[[self cellContent] objectForKey:@"description"]];
  128. [videoDescription setTextAlignment:NSTextAlignmentLeft];
  129. [videoDescription setFont:kFONTHelvetica(12)];
  130. [videoDescription setTextColor:kWHITEUICOLOR];
  131. [videoDescription setBackgroundColor:kTRANSPARENTUICOLOR];
  132. [videoDescription setNumberOfLines:2];
  133. [videoDescription sizeToFit];
  134. [videoDescription setFrame:CGRectMake(0, CGRectGetMinY(videoTime.frame)-videoDescription.frame.size.height, videoDescription.frame.size.width, videoDescription.frame.size.height)];
  135. if (videoUser == nil) {
  136. videoUser = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(videoDescription.frame)-12, containerView.frame.size.width, 12)];
  137. }
  138. [videoUser setFont:kFONTHelvetica(9)];
  139. [videoUser setTextColor:kLIGHTGRAYUICOLOR];
  140. [videoUser setBackgroundColor:kTRANSPARENTUICOLOR];
  141. [videoUser setText:[NSString stringWithFormat:@"%@ %@ ",[[[self cellContent] objectForKey:@"added_by"] objectForKey:@"username"],NSLocalizedString(@"MAIN_VIEWCOUNT_DELIM", @"")]];
  142. [videoUser sizeToFit];
  143. UIImageView *viewCountImg = [[UIImageView alloc] initWithImage:[UIImage ch_imageNamed:@"viewcount_small.png"]];
  144. [viewCountImg setFrame:CGRectMake(CGRectGetMaxX(videoUser.frame), CGRectGetMinY(videoDescription.frame)-12, 10, 10)];
  145. if (viewCount == nil) {
  146. viewCount = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(viewCountImg.frame)+3, CGRectGetMinY(videoDescription.frame)-12, 1, 12)];
  147. }
  148. [viewCount setFont:kFONTHelvetica(9)];
  149. [viewCount setTextColor:kLIGHTGRAYUICOLOR];
  150. [viewCount setBackgroundColor:kTRANSPARENTUICOLOR];
  151. [viewCount setText:[NSString stringWithFormat:@"%@",[[self cellContent] objectForKey:@"view_count"]]];
  152. [viewCount sizeToFit];
  153. if (videoTitle == nil) {
  154. videoTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(videoUser.frame)-25, containerView.frame.size.width, 25)];
  155. }
  156. [videoTitle setFont:kFONT(20)];
  157. [videoTitle setTextColor:kTITLEUICOLOR];
  158. [videoTitle setBackgroundColor:kTRANSPARENTUICOLOR];
  159. //DCMSG([[[super cellContent] objectForKey:@"mission_id"] description]);
  160. //DCMSG([[[self cellContent] objectForKey:@"mission_id"] class]);
  161. if (![[[self cellContent] objectForKey:@"mission_id"] isKindOfClass:[NSNull class]]) {
  162. if (rankLabel == nil) {
  163. [videoTitle setText:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"MAIN_MISSION_PREFIX",@""),[[[self cellContent] objectForKey:@"name"] uppercaseString]]];
  164. } else {
  165. [videoTitle setText:[NSString stringWithFormat:@"%@ %@ %@",rankLabel,NSLocalizedString(@"MAIN_MISSION_PREFIX",@""),[[[self cellContent] objectForKey:@"name"] uppercaseString]]];
  166. }
  167. // [videoTitle setText:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"MAIN_MISSION_PREFIX",@""),[[[self cellContent] objectForKey:@"name"] uppercaseString]]];
  168. } else {
  169. [videoTitle setText:[NSString stringWithFormat:@"%@",[[[self cellContent] objectForKey:@"name"] uppercaseString]]];
  170. }
  171. [videoTitle setNumberOfLines:1];
  172. [videoTitle sizeToFit];
  173. [videoTitle setFrame:CGRectMake(videoTitle.frame.origin.x, videoTitle.frame.origin.y, containerView.frame.size.width, videoTitle.frame.size.height+6)];
  174. //[videoDescription sizeToFit];
  175. //DCMSG([self cellContent]);
  176. [self addSubview:videoThumbViewBkg];
  177. BOOL processed = [[cellContent valueForKey:@"is_processed"] boolValue];
  178. if (processed) {
  179. [self addSubview:videoThumbView];
  180. } else {
  181. [self addSubview:notProcessedLabel];
  182. [self hideWait];
  183. }
  184. [self addSubview:ai];
  185. [containerView addSubview:videoTitle];
  186. [containerView addSubview:videoUser];
  187. [containerView addSubview:viewCountImg];
  188. [containerView addSubview:viewCount];
  189. [containerView addSubview:videoTime];
  190. [containerView addSubview:videoDescription];
  191. [self addSubview:containerView];
  192. if (isTV) {
  193. [videoTime sizeToFit];
  194. //DCMSG(cellContent);
  195. UIImage* tvImg = [UIImage ch_imageNamed:@"tv_feed.png"];
  196. UIImageView *tvImgView = [[UIImageView alloc] initWithImage:tvImg];
  197. [tvImgView setFrame:CGRectMake(videoRect.size.width-(kTVW+6), videoRect.size.height-(kTVW-2), kTVW, kTVW)];
  198. [tvImgView setBackgroundColor:kTRANSPARENTUICOLOR];
  199. [self addSubview:tvImgView];
  200. }
  201. UIView *comLike = [[UIView alloc] initWithFrame:CGRectMake(0, 6, 80, 16)];
  202. [comLike setBackgroundColor:kTRANSPARENTUICOLOR];
  203. UIImage *smallBubbleImg = [UIImage ch_imageNamed:@"comments_dark_small.png"];
  204. UIImageView *smallBubble = [[UIImageView alloc] initWithImage:smallBubbleImg];
  205. [smallBubble setFrame:CGRectMake(0, 0, 15, 15)];
  206. wbLabel *comCount = [[wbLabel alloc]initWithFrame:CGRectMake(20, 2, 20, 10)];
  207. [comCount setFont:kFONT(10)];
  208. [comCount setEdgeInsets:UIEdgeInsetsMake(3, 0, 0, 0)];
  209. NSString *comCountStr;
  210. NSInteger comCountInt = [[[self cellContent] valueForKey:@"comment_count"] integerValue];
  211. if (comCountInt > 999) {
  212. comCountStr = [NSString stringWithFormat:@"%ld",(long)ABS(comCountInt)/1000];
  213. } else {
  214. comCountStr = [NSString stringWithFormat:@"%ld",(long)ABS(comCountInt)];
  215. }
  216. [comCount setText:comCountStr];
  217. [comCount setTextColor:kBKGUICOLOR];
  218. NSInteger displayCount = [[[self cellContent] valueForKey:@"like_count"] integerValue] - [[[self cellContent] valueForKey:@"dislike_count"] integerValue];
  219. UIImageView *smallBanana;
  220. if (displayCount < 0) {
  221. UIImage *shitImg = [UIImage ch_imageNamed:@"shit_dark_small.png"];
  222. smallBanana = [[UIImageView alloc] initWithImage:shitImg];
  223. } else {
  224. UIImage *bananaImg = [UIImage ch_imageNamed:@"banana_dark_small.png"];
  225. smallBanana = [[UIImageView alloc] initWithImage:bananaImg];
  226. }
  227. [smallBanana setFrame:CGRectMake(35, 0, 15, 15)];
  228. NSString *countStr;
  229. if (displayCount > 999) {
  230. countStr = [NSString stringWithFormat:@"%ldk",(long)ABS(displayCount)/1000];
  231. } else {
  232. countStr = [NSString stringWithFormat:@"%ld",(long)ABS(displayCount)];
  233. }
  234. wbLabel *banCount = [[wbLabel alloc]initWithFrame:CGRectMake(55, 2, 20, 10)];
  235. [banCount setFont:kFONT(10)];
  236. [banCount setText:countStr];
  237. [banCount setTextColor:kBKGUICOLOR];
  238. [banCount setEdgeInsets:UIEdgeInsetsMake(3, 0, 0, 0)];
  239. [comLike addSubview:smallBubble];
  240. [comLike addSubview:comCount];
  241. [comLike addSubview:smallBanana];
  242. [comLike addSubview:banCount];
  243. [self addSubview:[self giveRibbon:comLike.frame]];
  244. [self addSubview:comLike];
  245. if (rankNumber > 0 && rankNumber < 4) {
  246. UIImage *medal = [UIImage ch_imageNamed:[NSString stringWithFormat:@"medal_%ld.png",(long)rankNumber]];
  247. UIImageView *medalView = [[UIImageView alloc] initWithImage:medal];
  248. [medalView setFrame:CGRectMake(CGRectGetMaxX(videoThumbView.frame)-(medal.size.width+5), CGRectGetMinY(videoThumbView.frame), medal.size.width, medal.size.height)];
  249. [medalView setBackgroundColor:kTRANSPARENTUICOLOR];
  250. [self addSubview:medalView];
  251. }
  252. }
  253. } else {
  254. DCMSG([self cellContent]);
  255. aiEmpty = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  256. [aiEmpty setCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)];
  257. [aiEmpty startAnimating];
  258. [self addSubview:aiEmpty];
  259. }
  260. }
  261. - (void)drawRect:(CGRect)rect
  262. {
  263. [self performSelectorOnMainThread:@selector(drawMe) withObject:NULL waitUntilDone:YES];
  264. }
  265. -(UIImageView*)giveRibbon:(CGRect)frame
  266. {
  267. #define W frame.size.width
  268. #define H frame.size.height
  269. //#define offset 10
  270. UIGraphicsBeginImageContextWithOptions(CGSizeMake(W,H),0.0,0.0);
  271. //this gets the graphic context
  272. CGContextRef context = UIGraphicsGetCurrentContext();
  273. //you can stroke and/or fill
  274. UIBezierPath *comLikeBkg = [UIBezierPath bezierPath];
  275. [comLikeBkg moveToPoint:CGPointMake(0, 0)];
  276. [comLikeBkg addLineToPoint:CGPointMake(W, 0)];
  277. [comLikeBkg addCurveToPoint:CGPointMake(W-H, H) controlPoint1:CGPointMake(W, H/2) controlPoint2:CGPointMake(W-(H/2), H)];
  278. [comLikeBkg addLineToPoint:CGPointMake(0, H)];
  279. [comLikeBkg closePath];
  280. CGContextSetFillColorWithColor(context, kGREENUICOLOR.CGColor);
  281. [comLikeBkg fill];
  282. UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();
  283. UIGraphicsEndImageContext();
  284. UIImageView *bezierImageView = [[UIImageView alloc] initWithImage:bezierImage];
  285. [bezierImageView setFrame:frame];
  286. comLikeBkg = nil;
  287. context = nil;
  288. bezierImage = nil;
  289. return bezierImageView;
  290. }
  291. -(void)dealloc
  292. {
  293. cellContent = nil;
  294. viewCount = nil;
  295. ai = nil;
  296. aiEmpty = nil;
  297. videoTitle = nil;
  298. videoUser = nil;
  299. videoDescription = nil;
  300. videoTime = nil;
  301. videoHash = nil;
  302. videoUserStr = nil;
  303. videoDescriptionStr = nil;
  304. videoTimeStr = nil;
  305. commentCount = nil;
  306. bananaCount = nil;
  307. disbananaCount = nil;
  308. videoThumbnail = nil;
  309. videoThumbView = nil;
  310. }
  311. @end