TUIMediaView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // MenuView.m
  3. // UIKit
  4. //
  5. // Created by kennethmiao on 2018/9/18.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIMediaView.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TIMCommon/TUIMessageCell.h>
  11. #import <TUICore/TUIDarkModel.h>
  12. #import <TUICore/TUIGlobalization.h>
  13. #import "TUIChatConversationModel.h"
  14. #import "TUIImageCollectionCell.h"
  15. #import "TUIMessageMediaDataProvider.h"
  16. #import "TUIVideoCollectionCell.h"
  17. #define ANIMATION_TIME 0.2
  18. @interface TUIMediaView () <UICollectionViewDelegate,
  19. UICollectionViewDataSource,
  20. UICollectionViewDelegateFlowLayout,
  21. UIScrollViewDelegate,
  22. TUIMediaCollectionCellDelegate>
  23. @property(strong, nonatomic) TUIMessageMediaDataProvider *dataProvider;
  24. @property(strong, nonatomic) UICollectionView *menuCollectionView;
  25. @property(strong, nonatomic) UIImage *saveBackgroundImage;
  26. @property(strong, nonatomic) UIImage *saveShadowImage;
  27. @property(strong, nonatomic) UIImageView *imageView;
  28. @property(strong, nonatomic) UIImage *thumbImage;
  29. @property(strong, nonatomic) UIView *coverView;
  30. @property(strong, nonatomic) UIView *mediaView;
  31. @property(assign, nonatomic) CGRect thumbFrame;
  32. @property(assign, nonatomic) NSIndexPath *currentVisibleIndexPath;
  33. @end
  34. @implementation TUIMediaView {
  35. V2TIMMessage *_curMessage;
  36. }
  37. - (instancetype)init {
  38. self = [super init];
  39. if (self) {
  40. _currentVisibleIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  41. }
  42. return self;
  43. }
  44. - (void)setupViews {
  45. self.backgroundColor = [UIColor clearColor];
  46. self.coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width * 3, Screen_Height * 3)];
  47. self.coverView.backgroundColor = [UIColor blackColor];
  48. [self addSubview:self.coverView];
  49. self.mediaView = [[UIView alloc] initWithFrame:self.thumbFrame];
  50. self.mediaView.backgroundColor = [UIColor clearColor];
  51. [self addSubview:self.mediaView];
  52. TUICollectionRTLFitFlowLayout *menuFlowLayout = [[TUICollectionRTLFitFlowLayout alloc] init];
  53. menuFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  54. menuFlowLayout.minimumLineSpacing = 0;
  55. menuFlowLayout.minimumInteritemSpacing = 0;
  56. menuFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  57. self.menuCollectionView = [[UICollectionView alloc] initWithFrame:self.mediaView.bounds collectionViewLayout:menuFlowLayout];
  58. self.menuCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  59. [self.menuCollectionView registerClass:[TUIImageCollectionCell class] forCellWithReuseIdentifier:TImageMessageCell_ReuseId];
  60. [self.menuCollectionView registerClass:[TUIVideoCollectionCell class] forCellWithReuseIdentifier:TVideoMessageCell_ReuseId];
  61. self.menuCollectionView.pagingEnabled = YES;
  62. self.menuCollectionView.delegate = self;
  63. self.menuCollectionView.dataSource = self;
  64. self.menuCollectionView.showsHorizontalScrollIndicator = NO;
  65. self.menuCollectionView.showsVerticalScrollIndicator = NO;
  66. self.menuCollectionView.alwaysBounceHorizontal = YES;
  67. self.menuCollectionView.decelerationRate = UIScrollViewDecelerationRateFast;
  68. self.menuCollectionView.backgroundColor = [UIColor clearColor];
  69. self.menuCollectionView.hidden = YES;
  70. [self.mediaView addSubview:self.menuCollectionView];
  71. self.imageView = [[UIImageView alloc] initWithFrame:self.mediaView.bounds];
  72. self.imageView.layer.cornerRadius = 5.0;
  73. [self.imageView.layer setMasksToBounds:YES];
  74. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  75. self.imageView.backgroundColor = [UIColor clearColor];
  76. self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  77. self.imageView.image = self.thumbImage;
  78. [self.mediaView addSubview:self.imageView];
  79. [UIView animateWithDuration:ANIMATION_TIME
  80. animations:^{
  81. self.mediaView.frame = self.bounds;
  82. }];
  83. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ANIMATION_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  84. [self.imageView removeFromSuperview];
  85. self.menuCollectionView.hidden = NO;
  86. });
  87. [[NSNotificationCenter defaultCenter] postNotificationName:kEnableAllRotationOrientationNotification object:nil];
  88. [self setupRotaionNotifications];
  89. }
  90. - (void)setupRotaionNotifications {
  91. if (@available(iOS 16.0, *)) {
  92. [[NSNotificationCenter defaultCenter] addObserver:self
  93. selector:@selector(onDeviceOrientationChange:)
  94. name:TUIMessageMediaViewDeviceOrientationChangeNotification
  95. object:nil];
  96. } else {
  97. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  98. [[NSNotificationCenter defaultCenter] addObserver:self
  99. selector:@selector(onDeviceOrientationChange:)
  100. name:UIDeviceOrientationDidChangeNotification
  101. object:nil];
  102. }
  103. }
  104. - (void)setThumb:(UIImageView *)thumb frame:(CGRect)frame {
  105. self.thumbImage = thumb.image;
  106. self.thumbFrame = frame;
  107. [self setupViews];
  108. }
  109. - (void)setCurMessage:(V2TIMMessage *)curMessage {
  110. _curMessage = curMessage;
  111. TUIChatConversationModel *model = [[TUIChatConversationModel alloc] init];
  112. model.userID = _curMessage.userID;
  113. model.groupID = _curMessage.groupID;
  114. self.dataProvider = [[TUIMessageMediaDataProvider alloc] initWithConversationModel:model];
  115. [self.dataProvider loadMediaWithMessage:_curMessage];
  116. @weakify(self);
  117. [RACObserve(self.dataProvider, medias) subscribeNext:^(NSArray *x) {
  118. @strongify(self);
  119. [self.menuCollectionView reloadData];
  120. for (int i = 0; i < self.dataProvider.medias.count; i++) {
  121. TUIMessageCellData *data = self.dataProvider.medias[i];
  122. if ([data.innerMessage.msgID isEqualToString:self->_curMessage.msgID]) {
  123. self.currentVisibleIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
  124. [self.menuCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]
  125. atScrollPosition:(UICollectionViewScrollPositionLeft)animated:NO];
  126. return;
  127. }
  128. }
  129. }];
  130. }
  131. - (void)setCurMessage:(V2TIMMessage *)curMessage allMessages:(NSArray<V2TIMMessage *> *)allMessages {
  132. _curMessage = curMessage;
  133. NSMutableArray *medias = [NSMutableArray array];
  134. for (V2TIMMessage *message in allMessages) {
  135. TUIMessageCellData *data = [TUIMessageMediaDataProvider getMediaCellData:message];
  136. if (data) {
  137. [medias addObject:data];
  138. }
  139. }
  140. self.dataProvider = [[TUIMessageMediaDataProvider alloc] initWithConversationModel:nil];
  141. self.dataProvider.medias = medias;
  142. [self.menuCollectionView reloadData];
  143. for (int i = 0; i < self.dataProvider.medias.count; i++) {
  144. TUIMessageCellData *data = self.dataProvider.medias[i];
  145. if ([data.innerMessage.msgID isEqualToString:_curMessage.msgID]) {
  146. [self.menuCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]
  147. atScrollPosition:(UICollectionViewScrollPositionLeft)animated:NO];
  148. return;
  149. }
  150. }
  151. }
  152. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  153. return self.dataProvider.medias.count;
  154. }
  155. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  156. TUIMessageCellData *data = self.dataProvider.medias[indexPath.row];
  157. TUIMediaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:data.reuseId forIndexPath:indexPath];
  158. if (cell) {
  159. cell.delegate = self;
  160. [cell fillWithData:data];
  161. if ([cell isKindOfClass:[TUIVideoCollectionCell class]]) {
  162. TUIVideoCollectionCell *videoCell = (TUIVideoCollectionCell *)cell;
  163. [videoCell reloadAllView];
  164. }
  165. else if ([cell isKindOfClass:[TUIImageCollectionCell class]]) {
  166. TUIImageCollectionCell *imageCell = (TUIImageCollectionCell *)cell;
  167. [imageCell reloadAllView];
  168. }
  169. else {
  170. //empty
  171. }
  172. }
  173. return cell;
  174. }
  175. - (CGSize)collectionView:(UICollectionView *)collectionView
  176. layout:(UICollectionViewLayout *)collectionViewLayout
  177. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  178. return collectionView.frame.size;
  179. }
  180. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  181. [super traitCollectionDidChange:previousTraitCollection];
  182. [self.menuCollectionView.collectionViewLayout invalidateLayout];
  183. }
  184. - (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset {
  185. UICollectionViewLayoutAttributes *attrs = [collectionView layoutAttributesForItemAtIndexPath:self.currentVisibleIndexPath];
  186. CGPoint newOriginForOldIndex = attrs.frame.origin;
  187. return newOriginForOldIndex.x == 0 ? proposedContentOffset : newOriginForOldIndex;
  188. }
  189. #pragma mark TUIMediaCollectionCellDelegate
  190. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  191. CGPoint center = CGPointMake(scrollView.contentOffset.x + (scrollView.frame.size.width / 2), scrollView.frame.size.height / 2);
  192. NSIndexPath *ip = [self.menuCollectionView indexPathForItemAtPoint:center];
  193. if (ip) {
  194. self.currentVisibleIndexPath = ip;
  195. }
  196. NSArray *indexPaths = [self.menuCollectionView indexPathsForVisibleItems];
  197. NSIndexPath *indexPath = indexPaths.firstObject;
  198. TUIMessageCellData *data = self.dataProvider.medias[indexPath.row];
  199. _curMessage = data.innerMessage;
  200. if (indexPath.row <= self.dataProvider.pageCount / 2) {
  201. [self.dataProvider loadOlderMedia];
  202. }
  203. if (indexPath.row >= self.dataProvider.medias.count - self.dataProvider.pageCount / 2) {
  204. [self.dataProvider loadNewerMedia];
  205. }
  206. }
  207. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  208. NSArray *indexPaths = [self.menuCollectionView indexPathsForVisibleItems];
  209. NSIndexPath *indexPath = indexPaths.firstObject;
  210. UICollectionViewCell *cell = [self.menuCollectionView cellForItemAtIndexPath:indexPath];
  211. if ([cell isKindOfClass:[TUIVideoCollectionCell class]]) {
  212. TUIVideoCollectionCell *videoCell = (TUIVideoCollectionCell *)cell;
  213. [videoCell stopVideoPlayAndSave];
  214. }
  215. }
  216. #pragma mark TUIMediaCollectionCellDelegate
  217. - (void)onCloseMedia:(TUIMediaCollectionCell *)cell {
  218. if (self.onClose) {
  219. self.onClose();
  220. }
  221. if (self.superview) {
  222. [self removeFromSuperview];
  223. }
  224. [[NSNotificationCenter defaultCenter] postNotificationName:kDisableAllRotationOrientationNotification object:nil];
  225. }
  226. - (void)applyRotaionFrame {
  227. self.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
  228. self.coverView.frame = CGRectMake(0, 0, Screen_Width * 3, Screen_Height * 3);
  229. self.mediaView.frame = self.frame;
  230. self.mediaView.center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
  231. self.menuCollectionView.frame = self.mediaView.frame;
  232. self.menuCollectionView.center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
  233. [self.menuCollectionView setNeedsLayout];
  234. self.imageView.frame = self.mediaView.frame;
  235. self.imageView.center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
  236. [self.menuCollectionView
  237. performBatchUpdates:^{
  238. }
  239. completion:^(BOOL finished) {
  240. if (finished) {
  241. [self.menuCollectionView scrollToItemAtIndexPath:self.currentVisibleIndexPath atScrollPosition:(UICollectionViewScrollPositionLeft)animated:NO];
  242. }
  243. }];
  244. return;
  245. }
  246. - (void)onDeviceOrientationChange:(NSNotification *)noti {
  247. [UIView performWithoutAnimation:^{
  248. [self applyRotaionFrame];
  249. }];
  250. }
  251. @end