QMChatRoomFileCell.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // QMChatRoomFileCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/8/15.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomFileCell.h"
  9. #import "QMChatRoomShowFileController.h"
  10. #import "QMProfileManager.h"
  11. @interface QMChatRoomFileCell () {
  12. UIImageView *_fileImageView;
  13. UILabel *_fileName;
  14. UILabel *_fileSize;
  15. UILabel *_status;
  16. UIImageView *_trackTintColor;
  17. UIImageView *_progressTintColor;
  18. }
  19. @end
  20. @implementation QMChatRoomFileCell
  21. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  22. self = [super initWithStyle: style reuseIdentifier:reuseIdentifier];
  23. if (self) {
  24. [self createUI];
  25. }
  26. return self;
  27. }
  28. - (void)createUI {
  29. _fileImageView = [[UIImageView alloc] init];
  30. [self.chatBackgroudImage addSubview:_fileImageView];
  31. _fileName = [[UILabel alloc] init];
  32. _fileName.font = [UIFont systemFontOfSize:14];
  33. _fileName.backgroundColor = [UIColor clearColor];
  34. _fileName.numberOfLines = 1;
  35. [self.chatBackgroudImage addSubview:_fileName];
  36. _fileSize = [[UILabel alloc] init];
  37. _fileSize.font = [UIFont systemFontOfSize:12];
  38. _fileSize.alpha = 0.7;
  39. [self.chatBackgroudImage addSubview:_fileSize];
  40. _status = [[UILabel alloc] init];
  41. _status.font = [UIFont systemFontOfSize:12];
  42. _status.alpha = 0.7;
  43. [self.chatBackgroudImage addSubview:_status];
  44. _trackTintColor = [[UIImageView alloc] init];
  45. _trackTintColor.backgroundColor = [UIColor whiteColor];
  46. [self.chatBackgroudImage addSubview:_trackTintColor];
  47. _progressTintColor = [[UIImageView alloc] init];
  48. _progressTintColor.backgroundColor = [UIColor colorWithRed:3/255.0 green:216/255.0 blue:118/255.0 alpha:1];
  49. [self.chatBackgroudImage addSubview:_progressTintColor];
  50. }
  51. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  52. self.message = message;
  53. [super setData:message avater:avater];
  54. UITapGestureRecognizer * tapPressGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPressGesture:)];
  55. [self.chatBackgroudImage addGestureRecognizer:tapPressGesture];
  56. // 重写
  57. if ([message.fromType isEqualToString:@"0"]) {
  58. //发送
  59. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame)-5-200, CGRectGetMaxY(self.timeLabel.frame)+10, 200, 50);
  60. self.sendStatus.frame = CGRectMake(CGRectGetMinX(self.chatBackgroudImage.frame)-25, CGRectGetMinY(self.chatBackgroudImage.frame)+15, 20, 20);
  61. _fileImageView.frame = CGRectMake(self.chatBackgroudImage.frame.size.width - 50, 0, 50, 50);
  62. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_fileImageView.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(16, 16)];
  63. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  64. maskLayer.frame = _fileImageView.bounds;
  65. maskLayer.path = maskPath.CGPath;
  66. _fileImageView.layer.mask = maskLayer;
  67. _fileName.frame = CGRectMake(15, 8, self.chatBackgroudImage.frame.size.width - 15 - 8 - 50, 18);
  68. _fileName.textAlignment = NSTextAlignmentRight;
  69. _fileName.textColor = [UIColor whiteColor];
  70. _fileSize.frame = CGRectMake(self.chatBackgroudImage.frame.size.width - 108, CGRectGetMaxY(_fileName.frame) + 3, 50, 13);
  71. _fileSize.textAlignment = NSTextAlignmentRight;
  72. _fileSize.textColor = [UIColor whiteColor];
  73. _trackTintColor.frame = CGRectMake(15, 47, 127, 2);
  74. _progressTintColor.frame = CGRectMake(15, 47, 0, 2);
  75. _status.frame = CGRectMake(15, CGRectGetMaxY(_fileName.frame) + 3, 50, 13);
  76. _status.textAlignment = NSTextAlignmentLeft;
  77. _status.textColor = [UIColor whiteColor];
  78. }else {
  79. //接收
  80. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, 200, 50);
  81. _fileImageView.frame = CGRectMake(0, 0, 50, 50);
  82. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_fileImageView.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(16, 16)];
  83. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  84. maskLayer.frame = _fileImageView.bounds;
  85. maskLayer.path = maskPath.CGPath;
  86. _fileImageView.layer.mask = maskLayer;
  87. _fileName.frame = CGRectMake(58, 8, self.chatBackgroudImage.frame.size.width - 15 - 8 - 50, 18);
  88. _fileName.textAlignment = NSTextAlignmentLeft;
  89. _fileName.textColor = [UIColor blackColor];
  90. _fileSize.frame = CGRectMake(58, CGRectGetMaxY(_fileName.frame) + 3, 50, 13);
  91. _fileSize.textAlignment = NSTextAlignmentLeft;
  92. _fileSize.textColor = [UIColor blackColor];
  93. _trackTintColor.frame = CGRectMake(58, 47, 127, 2);
  94. _progressTintColor.frame = CGRectMake(58, 47, 0, 2);
  95. _status.frame = CGRectMake(self.chatBackgroudImage.frame.size.width - 65, CGRectGetMaxY(_fileName.frame) + 3, 50, 13);
  96. _status.textAlignment = NSTextAlignmentRight;
  97. _status.textColor = [UIColor blackColor];
  98. self.sendStatus.hidden = YES;
  99. }
  100. NSString *imageName = [self matchImageWithFileNameExtension: message.fileName.pathExtension.lowercaseString];
  101. _fileImageView.image = [UIImage imageNamed:imageName];
  102. if (message.fileName != nil) {
  103. _fileName.text = message.fileName;
  104. }else {
  105. _fileName.text = message.message;
  106. }
  107. if (message.fileSize == nil) {
  108. _fileSize.text = @"0 K";
  109. }else {
  110. _fileSize.text = message.fileSize;
  111. }
  112. if ([message.fromType isEqualToString:@"0"]) {
  113. if ([message.status isEqualToString:@"0"]) {
  114. _progressTintColor.hidden = YES;
  115. _trackTintColor.hidden = YES;
  116. _status.text = NSLocalizedString(@"title.uploaded", nil);
  117. }else if ([message.status isEqualToString:@"1"]) {
  118. _progressTintColor.hidden = YES;
  119. _trackTintColor.hidden = YES;
  120. _status.text = NSLocalizedString(@"title.uploadFailure", nil);
  121. }else {
  122. _progressTintColor.hidden = NO;
  123. _trackTintColor.hidden = NO;
  124. _status.text = NSLocalizedString(@"title.uploading", nil);
  125. }
  126. }else {
  127. if ([message.downloadState isEqualToString:@"2"]) {
  128. _progressTintColor.hidden = NO;
  129. _trackTintColor.hidden = NO;
  130. _status.text = NSLocalizedString(@"title.downloads", nil);
  131. }else if ([message.downloadState isEqualToString:@"1"]) {
  132. _progressTintColor.hidden = YES;
  133. _trackTintColor.hidden = YES;
  134. _status.text = NSLocalizedString(@"title.notDownloaded", nil);
  135. }else if ([message.downloadState isEqualToString:@"0"]) {
  136. _progressTintColor.hidden = YES;
  137. _trackTintColor.hidden = YES;
  138. _status.text = NSLocalizedString(@"title.downloaded", nil);
  139. }else {
  140. _progressTintColor.hidden = YES;
  141. _trackTintColor.hidden = YES;
  142. _status.text = NSLocalizedString(@"title.downloaded", nil);
  143. }
  144. }
  145. }
  146. - (void)setProgress: (float)progress {
  147. if ([self.message.fromType isEqualToString:@"0"]) {
  148. _progressTintColor.frame = CGRectMake(15, 47, progress*127, 2);
  149. }else {
  150. _progressTintColor.frame = CGRectMake(58, 47, progress*127, 2);
  151. }
  152. }
  153. - (void)tapPressGesture:(id)sender {
  154. if (self.message.localFilePath == nil) {
  155. NSString *localPath = [[QMProfileManager sharedInstance] checkFileExtension: self.message.fileName];
  156. __weak QMChatRoomFileCell *weakSelf = self;
  157. [QMConnect downloadFileWithMessage:self.message localFilePath:localPath progressHander:^(float progress) {
  158. dispatch_async(dispatch_get_main_queue(), ^{
  159. [weakSelf setProgress:progress];
  160. });
  161. } successBlock:^{
  162. // 图片或视频存储至相册
  163. _progressTintColor.frame = CGRectMake(15, 94, 0, 3);
  164. [weakSelf setProgress:0];
  165. } failBlock:^(NSString * _Nonnull error) {
  166. [weakSelf setProgress:0];
  167. }];
  168. }else {
  169. // 打开本地文件
  170. QMChatRoomShowFileController *showFile = [[QMChatRoomShowFileController alloc] init];
  171. showFile.filePath = self.message.localFilePath;
  172. UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  173. showFile.modalPresentationStyle = UIModalPresentationFullScreen;
  174. [vc presentViewController:showFile animated:YES completion:nil];
  175. }
  176. }
  177. - (NSString *)matchImageWithFileNameExtension: (NSString *)fileName {
  178. NSString * str;
  179. if ([fileName isEqualToString:@"doc"]||[fileName isEqualToString:@"docx"]) {
  180. str = @"doc";
  181. }else if ([fileName isEqualToString:@"xlsx"]||[fileName isEqualToString:@"xls"]) {
  182. str = @"xlsx";
  183. }else if ([fileName isEqualToString:@"ppt"]||[fileName isEqualToString:@"pptx"]) {
  184. str = @"pptx";
  185. }else if ([fileName isEqualToString:@"pdf"]) {
  186. str = @"pdf";
  187. }else if ([fileName isEqualToString:@"mp3"]) {
  188. str = @"mp3";
  189. }else if ([fileName isEqualToString:@"mov"]||[fileName isEqualToString:@"mp4"]) {
  190. str = @"mov";
  191. }else if ([fileName isEqualToString:@"png"]||[fileName isEqualToString:@"jpg"]||[fileName isEqualToString:@"bmp"]||[fileName isEqualToString:@"jpeg"]) {
  192. str = @"bmp";
  193. }else {
  194. str = @"other";
  195. }
  196. return [NSString stringWithFormat:@"custom_file_%@", str];
  197. }
  198. @end