WFCUMediaMessageGridViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // WFCUMediaMessageGridViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/7/1.
  6. // Copyright © 2020 Tom Lee. All rights reserved.
  7. //
  8. #import "WFCUMediaMessageGridViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "MediaMessageGridViewCell.h"
  11. #import "SDPhotoBrowser.h"
  12. #import "WFCUBrowserViewController.h"
  13. #import "WFCUMediaMessageDownloader.h"
  14. #import "VideoPlayerKit.h"
  15. @interface WFCUMediaMessageGridViewController () <UICollectionViewDataSource, UICollectionViewDelegate, SDPhotoBrowserDelegate>
  16. @property(nonatomic, strong)UICollectionView *collectionView;
  17. @property(nonatomic, strong)NSMutableArray<WFCCMessage *> *mediaMessages;
  18. @property(nonatomic, strong)WFCCMessage *selectedMsg;
  19. @property(nonatomic, strong)VideoPlayerKit *videoPlayerViewController;
  20. @end
  21. @implementation WFCUMediaMessageGridViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
  25. CGFloat edgeInset = 5;
  26. int countInLine = 4;
  27. flowLayout.sectionInset = UIEdgeInsetsMake(edgeInset, edgeInset, edgeInset, edgeInset);
  28. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  29. width = width/countInLine - edgeInset - edgeInset;
  30. flowLayout.itemSize = CGSizeMake(width, width);
  31. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
  32. self.collectionView.dataSource = self;
  33. self.collectionView.delegate = self;
  34. [self.collectionView registerClass:[MediaMessageGridViewCell class] forCellWithReuseIdentifier:@"cell"];
  35. [self.view addSubview:self.collectionView];
  36. [self loadMediaMessages];
  37. [self.collectionView reloadData];
  38. }
  39. - (void)loadMediaMessages {
  40. self.mediaMessages = [[[WFCCIMService sharedWFCIMService] getMessages:self.conversation contentTypes:@[@(MESSAGE_CONTENT_TYPE_IMAGE), @(MESSAGE_CONTENT_TYPE_FILE), @(MESSAGE_CONTENT_TYPE_VIDEO)] from:0 count:20 withUser:nil] mutableCopy];
  41. }
  42. #pragma mark - UICollectionViewDataSource
  43. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  44. return self.mediaMessages.count;
  45. }
  46. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  47. MediaMessageGridViewCell *cell = (MediaMessageGridViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  48. WFCCMessage *msg = self.mediaMessages[indexPath.row];
  49. cell.mediaMessage = msg;
  50. return cell;
  51. }
  52. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  53. return 1;
  54. }
  55. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  56. self.selectedMsg = self.mediaMessages[indexPath.row];
  57. if ([self.selectedMsg.content isKindOfClass:[WFCCImageMessageContent class]]) {
  58. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  59. browser.sourceImagesContainerView = self.view;
  60. browser.showAll = NO;
  61. browser.imageCount = 1;
  62. browser.currentImageIndex = 0;
  63. browser.delegate = self;
  64. [browser show]; // 展示图片浏览器
  65. } else if([self.selectedMsg.content isKindOfClass:[WFCCFileMessageContent class]]) {
  66. WFCCFileMessageContent *fileContent = (WFCCFileMessageContent *)self.selectedMsg.content;
  67. WFCUBrowserViewController *bvc = [[WFCUBrowserViewController alloc] init];
  68. bvc.url = fileContent.remoteUrl;
  69. [self.navigationController pushViewController:bvc animated:YES];
  70. } else if([self.selectedMsg.content isKindOfClass:[WFCCVideoMessageContent class]]) {
  71. WFCCVideoMessageContent *videoMsg = (WFCCVideoMessageContent *)self.selectedMsg.content;
  72. if (self.selectedMsg.direction == MessageDirection_Receive && self.selectedMsg.status != Message_Status_Played) {
  73. [[WFCCIMService sharedWFCIMService] setMediaMessagePlayed:self.selectedMsg.messageId];
  74. }
  75. [self startPlayVideo:videoMsg];
  76. }
  77. }
  78. - (void)startPlayVideo:(WFCCVideoMessageContent *)videoMsg {
  79. NSURL *url = [NSURL URLWithString:videoMsg.remoteUrl];
  80. if (!self.videoPlayerViewController) {
  81. self.videoPlayerViewController = [VideoPlayerKit videoPlayerWithContainingView:self.view optionalTopView:nil hideTopViewWithControls:YES];
  82. self.videoPlayerViewController.allowPortraitFullscreen = YES;
  83. } else {
  84. [self.videoPlayerViewController.view removeFromSuperview];
  85. }
  86. [self.view addSubview:self.videoPlayerViewController.view];
  87. [self.videoPlayerViewController playVideoWithTitle:@" " URL:url videoID:nil shareURL:nil isStreaming:NO playInFullScreen:YES];
  88. }
  89. #pragma mark - SDPhotoBrowserDelegate
  90. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  91. WFCCImageMessageContent *imgCnt = (WFCCImageMessageContent *)self.selectedMsg.content;
  92. return imgCnt.thumbnail;
  93. }
  94. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  95. WFCCImageMessageContent *imgContent = (WFCCImageMessageContent *)self.selectedMsg.content;
  96. return [NSURL URLWithString:[imgContent.remoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  97. }
  98. - (void)photoBrowserDidDismiss:(SDPhotoBrowser *)browser {
  99. self.selectedMsg = nil;
  100. }
  101. @end