2
0

WFCUMediaMessageCell.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #import "HWCircleView.h"
  11. @interface WFCUMediaMessageCell ()
  12. @property (nonatomic, strong)UIView *maskView;
  13. @property (nonatomic, strong)UIActivityIndicatorView *activityView;
  14. @property (nonatomic, strong)HWCircleView *progressView;
  15. @end
  16. @implementation WFCUMediaMessageCell
  17. - (void)setModel:(WFCUMessageModel *)model {
  18. [super setModel:model];
  19. __weak typeof(self)ws = self;
  20. [[NSNotificationCenter defaultCenter] addObserverForName:kMediaMessageStartDownloading object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  21. if ([note.object longLongValue] == ws.model.message.messageUid) {
  22. [ws onStartDownloading:ws];
  23. }
  24. }];
  25. [[NSNotificationCenter defaultCenter] addObserverForName:kMediaMessageDownloadFinished object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  26. if ([note.object longLongValue] == ws.model.message.messageUid) {
  27. [ws onDownloadFinished:ws];
  28. }
  29. }];
  30. [[NSNotificationCenter defaultCenter] addObserverForName:kUploadMediaMessageProgresse object:@(model.message.messageId) queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  31. float progress = [note.userInfo[@"progress"] floatValue];
  32. BOOL finish = [note.userInfo[@"finish"] boolValue];
  33. [ws updateUploadProgress:progress finish:finish];
  34. }];
  35. [[NSNotificationCenter defaultCenter] addObserverForName:kSendingMessageStatusUpdated object:@(model.message.messageId) queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  36. WFCCMessageStatus newStatus = (WFCCMessageStatus)[[note.userInfo objectForKey:@"status"] integerValue];
  37. if(newStatus == Message_Status_Sent || newStatus == Message_Status_Send_Failure) {
  38. [ws updateUploadProgress:1 finish:YES];
  39. }
  40. }];
  41. if (model.mediaDownloading) {
  42. [self onStartDownloading:self];
  43. } else {
  44. [self onDownloadFinished:self];
  45. }
  46. }
  47. - (void)updateUploadProgress:(float)progress finish:(BOOL)finish {
  48. if(finish) {
  49. self.progressView.hidden = YES;
  50. } else {
  51. UIView *parentView = [self getProgressParentView];
  52. self.progressView.hidden = NO;
  53. self.progressView.progress = progress;
  54. [parentView bringSubviewToFront:self.progressView];
  55. }
  56. }
  57. - (void)onStartDownloading:(id)sender {
  58. if (!_maskView) {
  59. _maskView = [[UIView alloc] init];
  60. [self.bubbleView addSubview:_maskView];
  61. [_maskView setBackgroundColor:[UIColor grayColor]];
  62. [_maskView setAlpha:0.5];
  63. [_maskView setClipsToBounds:YES];
  64. }
  65. _maskView.frame = self.bubbleView.bounds;
  66. [self.bubbleView bringSubviewToFront:_maskView];
  67. if (!_activityView) {
  68. _activityView = [[UIActivityIndicatorView alloc] init];
  69. [_maskView addSubview:_activityView];
  70. }
  71. _activityView.center = CGPointMake(self.maskView.bounds.size.width/2, self.maskView.bounds.size.height/2);
  72. [_activityView startAnimating];
  73. }
  74. - (void)onDownloadFinished:(id)sender {
  75. if (_maskView) {
  76. [_maskView removeFromSuperview];
  77. _maskView = nil;
  78. }
  79. if (_activityView) {
  80. [_activityView removeFromSuperview];
  81. [_activityView stopAnimating];
  82. _activityView = nil;
  83. }
  84. if ([sender isKindOfClass:[NSNotification class]]) {
  85. NSNotification *noti = (NSNotification *)sender;
  86. if ([noti.userInfo[@"result"] boolValue]) {
  87. [self setModel:self.model];
  88. }
  89. }
  90. }
  91. - (UIView *)progressView {
  92. if(!_progressView) {
  93. _progressView = [[HWCircleView alloc] initWithFrame:[self getProgressParentView].bounds];
  94. [[self getProgressParentView] addSubview:_progressView];
  95. }
  96. return _progressView;
  97. }
  98. - (UIView *)getProgressParentView {
  99. return nil;
  100. }
  101. - (void)dealloc {
  102. [[NSNotificationCenter defaultCenter] removeObserver:self];
  103. }
  104. @end