WFCUMediaMessageGridViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // WFCUMediaMessageGridViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/7/1.
  6. // Copyright © 2020 WildFireChat. 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, assign)BOOL hasMore;
  20. @property(nonatomic, strong)VideoPlayerKit *videoPlayerViewController;
  21. @end
  22. @implementation WFCUMediaMessageGridViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
  26. CGFloat edgeInset = 5;
  27. int countInLine = 4;
  28. flowLayout.sectionInset = UIEdgeInsetsMake(edgeInset, edgeInset, edgeInset, edgeInset);
  29. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  30. width = width/countInLine - edgeInset - edgeInset;
  31. flowLayout.itemSize = CGSizeMake(width, width);
  32. self.hasMore = YES;
  33. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
  34. self.collectionView.dataSource = self;
  35. self.collectionView.delegate = self;
  36. [self.collectionView registerClass:[MediaMessageGridViewCell class] forCellWithReuseIdentifier:@"cell"];
  37. [self.view addSubview:self.collectionView];
  38. [self loadMediaMessages];
  39. [self.collectionView reloadData];
  40. }
  41. - (void)loadMediaMessages {
  42. self.mediaMessages = [[[WFCCIMService sharedWFCIMService] getMessages:self.conversation contentTypes:@[@(MESSAGE_CONTENT_TYPE_IMAGE), @(MESSAGE_CONTENT_TYPE_FILE), @(MESSAGE_CONTENT_TYPE_VIDEO)] from:0 count:40 withUser:nil] mutableCopy];
  43. if (self.mediaMessages.count < 40) {
  44. self.hasMore = false;
  45. }
  46. }
  47. - (void)loadMoreMessage {
  48. NSMutableArray<WFCCMessage *> *moreMessages = [[[WFCCIMService sharedWFCIMService] getMessages:self.conversation contentTypes:@[@(MESSAGE_CONTENT_TYPE_IMAGE), @(MESSAGE_CONTENT_TYPE_FILE), @(MESSAGE_CONTENT_TYPE_VIDEO)] from:self.mediaMessages.lastObject.messageId count:20 withUser:nil] mutableCopy];
  49. if (moreMessages.count < 20) {
  50. self.hasMore = false;
  51. }
  52. [self.mediaMessages addObjectsFromArray:moreMessages];
  53. [self.collectionView reloadData];
  54. }
  55. - (void)startPlayVideo:(WFCCVideoMessageContent *)videoMsg {
  56. NSURL *url = [NSURL URLWithString:videoMsg.remoteUrl];
  57. if (!self.videoPlayerViewController) {
  58. self.videoPlayerViewController = [VideoPlayerKit videoPlayerWithContainingView:self.view optionalTopView:nil hideTopViewWithControls:YES];
  59. self.videoPlayerViewController.allowPortraitFullscreen = YES;
  60. } else {
  61. [self.videoPlayerViewController.view removeFromSuperview];
  62. }
  63. [self.view addSubview:self.videoPlayerViewController.view];
  64. [self.videoPlayerViewController playVideoWithTitle:@" " URL:url videoID:nil shareURL:nil isStreaming:NO playInFullScreen:YES];
  65. }
  66. #pragma mark - UICollectionViewDataSource
  67. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  68. return self.mediaMessages.count;
  69. }
  70. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  71. MediaMessageGridViewCell *cell = (MediaMessageGridViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  72. WFCCMessage *msg = self.mediaMessages[indexPath.row];
  73. cell.mediaMessage = msg;
  74. return cell;
  75. }
  76. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  77. return 1;
  78. }
  79. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  80. self.selectedMsg = self.mediaMessages[indexPath.row];
  81. if ([self.selectedMsg.content isKindOfClass:[WFCCImageMessageContent class]]) {
  82. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  83. browser.sourceImagesContainerView = self.view;
  84. browser.showAll = NO;
  85. browser.imageCount = 1;
  86. browser.currentImageIndex = 0;
  87. browser.delegate = self;
  88. [browser show]; // 展示图片浏览器
  89. } else if([self.selectedMsg.content isKindOfClass:[WFCCFileMessageContent class]]) {
  90. WFCCFileMessageContent *fileContent = (WFCCFileMessageContent *)self.selectedMsg.content;
  91. WFCUBrowserViewController *bvc = [[WFCUBrowserViewController alloc] init];
  92. bvc.url = fileContent.remoteUrl;
  93. [self.navigationController pushViewController:bvc animated:YES];
  94. } else if([self.selectedMsg.content isKindOfClass:[WFCCVideoMessageContent class]]) {
  95. WFCCVideoMessageContent *videoMsg = (WFCCVideoMessageContent *)self.selectedMsg.content;
  96. if (self.selectedMsg.direction == MessageDirection_Receive && self.selectedMsg.status != Message_Status_Played) {
  97. [[WFCCIMService sharedWFCIMService] setMediaMessagePlayed:self.selectedMsg.messageId];
  98. }
  99. [self startPlayVideo:videoMsg];
  100. }
  101. }
  102. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
  103. if (self.hasMore && ceil(targetContentOffset->y)+1 >= ceil(scrollView.contentSize.height - scrollView.bounds.size.height)) {
  104. [self loadMoreMessage];
  105. }
  106. }
  107. #pragma mark - SDPhotoBrowserDelegate
  108. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  109. WFCCImageMessageContent *imgCnt = (WFCCImageMessageContent *)self.selectedMsg.content;
  110. return imgCnt.thumbnail;
  111. }
  112. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  113. WFCCImageMessageContent *imgContent = (WFCCImageMessageContent *)self.selectedMsg.content;
  114. return [NSURL URLWithString:[imgContent.remoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  115. }
  116. - (void)photoBrowserDidDismiss:(SDPhotoBrowser *)browser {
  117. self.selectedMsg = nil;
  118. }
  119. @end