WFCUMediaMessageCell.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // MediaMessageCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/9.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUMediaMessageCell.h"
  9. #import "WFCUMediaMessageDownloader.h"
  10. @interface WFCUMediaMessageCell ()
  11. @property (nonatomic, strong)UIView *maskView;
  12. @property (nonatomic, strong)UIActivityIndicatorView *activityView;
  13. @end
  14. @implementation WFCUMediaMessageCell
  15. - (void)setModel:(WFCUMessageModel *)model {
  16. [super setModel:model];
  17. __weak typeof(self)ws = self;
  18. [[NSNotificationCenter defaultCenter] addObserverForName:kMediaMessageStartDownloading object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  19. if ([note.object longLongValue] == ws.model.message.messageUid) {
  20. [ws onStartDownloading:ws];
  21. }
  22. }];
  23. [[NSNotificationCenter defaultCenter] addObserverForName:kMediaMessageDownloadFinished object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  24. if ([note.object longLongValue] == ws.model.message.messageUid) {
  25. [ws onDownloadFinished:ws];
  26. }
  27. }];
  28. if (model.mediaDownloading) {
  29. [self onStartDownloading:self];
  30. } else {
  31. [self onDownloadFinished:self];
  32. }
  33. }
  34. - (void)onStartDownloading:(id)sender {
  35. if (!_maskView) {
  36. _maskView = [[UIView alloc] init];
  37. [self.bubbleView addSubview:_maskView];
  38. [_maskView setBackgroundColor:[UIColor grayColor]];
  39. [_maskView setAlpha:0.5];
  40. [_maskView setClipsToBounds:YES];
  41. }
  42. _maskView.frame = self.bubbleView.bounds;
  43. [self.bubbleView bringSubviewToFront:_maskView];
  44. if (!_activityView) {
  45. _activityView = [[UIActivityIndicatorView alloc] init];
  46. [_maskView addSubview:_activityView];
  47. }
  48. _activityView.center = CGPointMake(self.maskView.bounds.size.width/2, self.maskView.bounds.size.height/2);
  49. [_activityView startAnimating];
  50. }
  51. - (void)onDownloadFinished:(id)sender {
  52. if (_maskView) {
  53. [_maskView removeFromSuperview];
  54. _maskView = nil;
  55. }
  56. if (_activityView) {
  57. [_activityView removeFromSuperview];
  58. [_activityView stopAnimating];
  59. _activityView = nil;
  60. }
  61. if ([sender isKindOfClass:[NSNotification class]]) {
  62. NSNotification *noti = (NSNotification *)sender;
  63. if ([noti.userInfo[@"result"] boolValue]) {
  64. [self setModel:self.model];
  65. }
  66. }
  67. }
  68. - (void)dealloc {
  69. [[NSNotificationCenter defaultCenter] removeObserver:self];
  70. }
  71. @end