Bladeren bron

点击文件预览逻辑优化

zwp 5 maanden geleden
bovenliggende
commit
198793c0c4

+ 2 - 3
AIIM/Controller/chat/chetCell/ChatMessageModel.h

@@ -89,9 +89,8 @@ typedef NS_ENUM(NSUInteger, ChatMessageType) {
 // 生成缩略图的方法,同时判断是否开始下载视频
 - (void)generateThumbnailWithCompletion:(void(^)(UIImage *thumbnail))completion;
 //下载文件保存本地
--(void)xiazaiwenjian:(void(^)(NSInteger persent))loading;
--(void)xiazaishipin:(void(^)(NSInteger persent))loading;
--(void)xiazaiyuyin:(void(^)(NSInteger persent))loading;
+-(void)downloadFileIfNeed:(void(^)(NSInteger persent))loading;
+
 
 
 //上传文件

+ 44 - 205
AIIM/Controller/chat/chetCell/ChatMessageModel.m

@@ -88,7 +88,7 @@
     }else{
         self.voiceDuration = duration/1000;
     }
-    self.voiceWidth = self.voiceDuration==0?0:[self voiceBubbleWidth];
+    self.voiceWidth = [self voiceBubbleWidth];
     
     //文件传输成功或失败
     self.fileError = 0;
@@ -386,224 +386,63 @@
 }
 
 //下载视频文件
--(void)xiazaishipin:(void(^)(NSInteger persent))loading{
-    NSLog(@"xiazaishipin--------");
-    
-    if(!self.isSender){
-        NSString *localFileurl = [self.localurl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
-        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:localFileurl]){
-//            NSLog(@"文件已经存在本地");
-            if (loading) {
-                loading(100);
-            }
-        }
-        else{
-//            NSLog(@"文件不存在");
-            if(self.url&&self.url.length != 0){
-                
-                NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-                NSURL *tempUrl = [NSURL URLWithString:self.url];
-                NSString *fileName = tempUrl.lastPathComponent;
-                NSString *fileExtension = [fileName pathExtension];
-                NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
-                documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
-                NSLog(@"文件已经存在本地1-:%@",documentsDirectoryURL.path);
-                if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
-                    NSLog(@"文件已经存在本地2-:%@",documentsDirectoryURL.path);
-                    NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                    NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                    [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
-                    [mutableDict setObject:extend forKey:@"extend"];
-                    self.localurl = documentsDirectoryURL.path;
-                    NSDictionary *NewMsg = [mutableDict copy];
-                    [ChatsStore.shareInstance reciveMsg:NewMsg];
-                    if (loading) {
-                        loading(100);
-                    }
-                }
-                else{
-                    [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
-                        if(res!=nil){
-                            NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                            NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                            [extend setObject:res[@"filePath"] forKey:@"localurl"];
-                            [mutableDict setObject:extend forKey:@"extend"];
-                            self.localurl = res[@"filePath"];
-                            NSDictionary *NewMsg = [mutableDict copy];
-                            [ChatsStore.shareInstance reciveMsg:NewMsg];
-                            if (loading) {
-                                loading(100);
-                            }
-                        }
-                        else{
-                            if (code>=0&&code<200) {
-                                if (loading) {
-                                    loading(code);
-                                }
-                            }
-                        }
-                    } fail:^(NSError * _Nonnull error) {
-                        NSLog(@"error:%@",error);
-                    }];
-                }
-            }
-            else{
-                if (loading) {
-                    loading(0);
-                }
-            }
+-(void)downloadFileIfNeed:(void(^)(NSInteger persent))loading{
+    NSString *localFileurl = [self.localurl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
+    if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:localFileurl]){
+        self.customFileSize = [self getFileSize];
+        if (loading) {
+            loading(100);
         }
-    }
-    else{
-        NSLog(@"downLoadWToken xiazaishipin-----");
-    }
-}
-
--(void)xiazaiwenjian:(void(^)(NSInteger persent))loading{
-    NSLog(@"xiazaiwenjian---");
-    if (self.isSender) {
         return;
     }
-    else{
-//        NSLog(@"self.localurl2:%@",self.localurl);
-        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
-//            NSLog(@"文件已经存在本地");
-            self.customFileSize = [self getFileSize];
-            if (loading) {
-                loading(100);
-            }
-            return;
-        }
-        else{
-            if(self.url&&self.url.length != 0){
-                
-                NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-                NSURL *tempUrl = [NSURL URLWithString:self.url];
-                NSString *fileName = tempUrl.lastPathComponent;
-                NSString *fileExtension = [fileName pathExtension];
-                NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
-                documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
-                NSLog(@"文件已经存在本地1-:%@",documentsDirectoryURL.path);
-                if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
-                    NSLog(@"文件已经存在本地2-:%@",documentsDirectoryURL.path);
-                    NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                    NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                    [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
-                    [mutableDict setObject:extend forKey:@"extend"];
-                    self.localurl = documentsDirectoryURL.path;
-                    NSDictionary *NewMsg = [mutableDict copy];
-                    [ChatsStore.shareInstance reciveMsg:NewMsg];
-                    self.customFileSize = [self getFileSize];
-                    if (loading) {
-                        loading(100);
-                    }
-                }
-                else{
-                    [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
-                        if(res!=nil){
-                            NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                            NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                            [extend setObject:res[@"filePath"] forKey:@"localurl"];
-                            [mutableDict setObject:extend forKey:@"extend"];
-                            self.localurl = res[@"filePath"];
-                            NSDictionary *NewMsg = [mutableDict copy];
-                            [ChatsStore.shareInstance reciveMsg:NewMsg];
-                            self.customFileSize = [self getFileSize];
-                            if (loading) {
-                                loading(100);
-                            }
-                        }
-                        else{
-                            if (code>=0&&code<200) {
-                                if (loading) {
-                                    loading(code);
-                                }
-                            }
-                        }
-                    } fail:^(NSError * _Nonnull error) {
-                        NSLog(@"error:%@",error);
-                    }];
-                }
-            }
-            else{
-                if (loading) {
-                    loading(0);
-                }
-            }
+    
+    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
+    NSURL *tempUrl = [NSURL URLWithString:self.url];
+    NSString *fileName = tempUrl.lastPathComponent;
+    NSString *fileExtension = [fileName pathExtension];
+    NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
+    documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
+    NSLog(@"文件本地路径:%@",documentsDirectoryURL.path);
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
+        NSLog(@"文件已经存在本地");
+        NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
+        NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
+        [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
+        [mutableDict setObject:extend forKey:@"extend"];
+        self.localurl = documentsDirectoryURL.path;
+        NSDictionary *NewMsg = [mutableDict copy];
+        [ChatsStore.shareInstance reciveMsg:NewMsg];
+        self.customFileSize = [self getFileSize];
+        if (loading) {
+            loading(100);
         }
-    }
-}
-
-
--(void)xiazaiyuyin:(void(^)(NSInteger persent))loading{
-    NSLog(@"xiazaiwenjian---");
-    if (self.isSender) {
         return;
     }
-    else{
-//        NSLog(@"self.localurl2:%@",self.localurl);
-        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
-//            NSLog(@"文件已经存在本地");
+    
+    [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
+        if(res!=nil){
+            NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
+            NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
+            [extend setObject:res[@"filePath"] forKey:@"localurl"];
+            [mutableDict setObject:extend forKey:@"extend"];
+            self.localurl = res[@"filePath"];
+            NSDictionary *NewMsg = [mutableDict copy];
+            [ChatsStore.shareInstance reciveMsg:NewMsg];
+            self.customFileSize = [self getFileSize];
             if (loading) {
                 loading(100);
             }
-            return;
         }
-        
-        if(self.url&&self.url.length != 0){
-            
-            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-            NSURL *tempUrl = [NSURL URLWithString:self.url];
-            NSString *fileName = tempUrl.lastPathComponent;
-            NSString *fileExtension = [fileName pathExtension];
-            NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
-            documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
-            NSLog(@"文件已经存在本地1-:%@",documentsDirectoryURL.path);
-            if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
-                NSLog(@"文件已经存在本地2-:%@",documentsDirectoryURL.path);
-                NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
-                [mutableDict setObject:extend forKey:@"extend"];
-                self.localurl = documentsDirectoryURL.path;
-                NSDictionary *NewMsg = [mutableDict copy];
-                [ChatsStore.shareInstance reciveMsg:NewMsg];
+        else{
+            if (code>=0&&code<200) {
                 if (loading) {
-                    loading(100);
+                    loading(code);
                 }
             }
-            else{
-                [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
-                    if(res!=nil){
-                        NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-                        NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-                        [extend setObject:res[@"filePath"] forKey:@"localurl"];
-                        [mutableDict setObject:extend forKey:@"extend"];
-                        self.localurl = res[@"filePath"];
-                        NSDictionary *NewMsg = [mutableDict copy];
-                        [ChatsStore.shareInstance reciveMsg:NewMsg];
-                        if (loading) {
-                            loading(100);
-                        }
-                    }
-                    else{
-                        if (code>=0&&code<200) {
-                            if (loading) {
-                                loading(code);
-                            }
-                        }
-                    }
-                } fail:^(NSError * _Nonnull error) {
-                    NSLog(@"error:%@",error);
-                }];
-            }
         }
-        else{
-            if (loading) {
-                loading(0);
-            }
-        }
-    }
+    } fail:^(NSError * _Nonnull error) {
+        NSLog(@"downLoadWToken error:%@",error);
+    }];
 }
 
 

+ 62 - 63
AIIM/Controller/chat/chetCell/chatCellView.m

@@ -1003,17 +1003,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         voiceIcon.animationImages = @[kImageMake(@"audio_receiver_animate1"),kImageMake(@"audio_receiver_animate2"),kImageMake(@"audio_receiver_animate3")];
     }
     
-    [_messageModel xiazaiyuyin:^(NSInteger persent) {
-//        NSLog(@"setupFileContent----------");
-        if(persent>=100){
-            NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
-        }
-        else{
-//            NSLog(@"setupFileContent 下载中:%ld",(long)persent);
-        }
-    }];
-    
-    
+
     _currentContentView = _voiceContentView;
 }
 
@@ -1529,7 +1519,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         }
         
         weakSelf(self);
-        [_messageModel xiazaishipin:^(NSInteger persent) {
+        [_messageModel downloadFileIfNeed:^(NSInteger persent) {
             if(persent>=100){
                 dispatch_async(dispatch_get_main_queue(), ^{
                     if(weakself.jinduLbl){
@@ -1584,7 +1574,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         UILabel *sizeLabel = _fileContentView.subviews[2];
 
         weakSelf(self)
-        [_messageModel xiazaiwenjian:^(NSInteger persent) {
+        [_messageModel downloadFileIfNeed:^(NSInteger persent) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
                 if(persent>=100){
@@ -1600,66 +1590,75 @@ static const CGFloat kMediaCornerRadius = 4.0f;
 }
 
 - (void)handleVoiceTap {
-    UIImageView *voiceIcon = _voiceContentView.subviews[0];
     if (_messageModel.messageType == ChatMessageTypeVoice) {
         // 处理语音点击
         NSLog(@"点击了语音: %@", _messageModel.url);
-        
-        if (self.player) {
-            AVPlayerItem *playerItem = self.player.currentItem;
-            CMTime currentTime = playerItem.currentTime;
-            CMTime duration = playerItem.duration;
-            [voiceIcon stopAnimating];
-            if (self.player.rate == 0.0) {
-                if (CMTimeCompare(currentTime, duration) == 0 ||
-                    CMTimeCompare(currentTime, duration) == 1) {
-                    // 播放完毕
-                    NSLog(@"Playback finished");
-                } else {
-                    // 暂停
-                    NSLog(@"Playback paused");
-                    [self.player play];
-                    return;
+        [_messageModel downloadFileIfNeed:^(NSInteger persent) {
+            dispatch_main_async_safe(^{
+                if (persent >= 100) {
+                    [self playVoice];
                 }
+            })
+        }];
+    }
+}
+
+- (void)playVoice {
+    UIImageView *voiceIcon = _voiceContentView.subviews[0];
+    if (self.player) {
+        AVPlayerItem *playerItem = self.player.currentItem;
+        CMTime currentTime = playerItem.currentTime;
+        CMTime duration = playerItem.duration;
+        [voiceIcon stopAnimating];
+        if (self.player.rate == 0.0) {
+            if (CMTimeCompare(currentTime, duration) == 0 ||
+                CMTimeCompare(currentTime, duration) == 1) {
+                // 播放完毕
+                NSLog(@"Playback finished");
             } else {
-                // 正在播放
-                NSLog(@"Playback in progress");
-                [self.player pause];
+                // 暂停
+                NSLog(@"Playback paused");
+                [self.player play];
                 return;
             }
+        } else {
+            // 正在播放
+            NSLog(@"Playback in progress");
+            [self.player pause];
+            return;
         }
+    }
+    
+    
+    NSString *urlstr = _messageModel.url;
+    NSURL *Url = [NSURL URLWithString:urlstr];
+    
+    NSLog(@"本地路径:%@",_messageModel.localurl);
+    if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
+        NSLog(@"本地路径:");
+        urlstr=_messageModel.localurl;
+        Url = [NSURL fileURLWithPath:urlstr];
+    }
+    else{
         
-        
-        NSString *urlstr = _messageModel.url;
-        NSURL *Url = [NSURL URLWithString:urlstr];
-        
-        NSLog(@"本地路径:%@",_messageModel.localurl);
-        if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
-            NSLog(@"本地路径:");
-            urlstr=_messageModel.localurl;
-            Url = [NSURL fileURLWithPath:urlstr];
-        }
-        else{
-            
-        }
-        NSLog(@"播放: %@", Url);
-        
-        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
-        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
-        [audioSession setActive:YES error:nil];
-        AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
-        
-        //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
-        
-        _player = [AVPlayer playerWithPlayerItem:pitem];
-        [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
-        [_player play];
-        [[NSNotificationCenter defaultCenter] addObserver:self
-                                                 selector:@selector(playerItemDidReachEnd:)
-                                                     name:AVPlayerItemDidPlayToEndTimeNotification
-                                                   object:pitem];
-        [voiceIcon startAnimating];
     }
+    NSLog(@"播放: %@", Url);
+    
+    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
+    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
+    [audioSession setActive:YES error:nil];
+    AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
+    
+    //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
+    
+    _player = [AVPlayer playerWithPlayerItem:pitem];
+    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
+    [_player play];
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(playerItemDidReachEnd:)
+                                                 name:AVPlayerItemDidPlayToEndTimeNotification
+                                               object:pitem];
+    [voiceIcon startAnimating];
 }
 
 - (void)playerItemDidReachEnd:(NSNotification *)notification {