Przeglądaj źródła

文件取消自动下载

zwp 5 miesięcy temu
rodzic
commit
c226abc5c1

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

@@ -58,6 +58,8 @@ typedef NS_ENUM(NSUInteger, ChatMessageType) {
 @property (nonatomic, assign) CGFloat voiceWidth;
 @property (nonatomic, assign) NSInteger fileError;//0 = 成功 1 = 失败
 
+@property (nonatomic, copy, nullable) NSString *customFileSize;
+
 // 通话记录属性
 @property (nonatomic, assign) NSInteger callDuration;
 @property (nonatomic, assign) BOOL isCallSuccess;

+ 36 - 0
AIIM/Controller/chat/chetCell/ChatMessageModel.m

@@ -468,6 +468,7 @@
 //        NSLog(@"self.localurl2:%@",self.localurl);
         if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
 //            NSLog(@"文件已经存在本地");
+            self.customFileSize = [self getFileSize];
             if (loading) {
                 loading(100);
             }
@@ -492,6 +493,7 @@
                     self.localurl = documentsDirectoryURL.path;
                     NSDictionary *NewMsg = [mutableDict copy];
                     [ChatsStore.shareInstance reciveMsg:NewMsg];
+                    self.customFileSize = [self getFileSize];
                     if (loading) {
                         loading(100);
                     }
@@ -506,6 +508,7 @@
                             self.localurl = res[@"filePath"];
                             NSDictionary *NewMsg = [mutableDict copy];
                             [ChatsStore.shareInstance reciveMsg:NewMsg];
+                            self.customFileSize = [self getFileSize];
                             if (loading) {
                                 loading(100);
                             }
@@ -628,4 +631,37 @@
     return width;
 }
 
+- (NSString *)getFileSize {
+    NSString *filePath = self.localurl;
+
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+
+    // 获取文件属性
+    NSError *error = nil;
+    NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:&error];
+
+    NSString *string = @"0KB";
+    if (error) {
+        NSLog(@"获取文件属性失败: %@", error);
+    } else {
+        // 获取文件大小(单位:字节)
+        unsigned long long fileSize = [attributes fileSize];
+        NSLog(@"文件大小: %llu 字节", fileSize);
+
+        // 可选:转换为 KB、MB
+        double fileSizeKB = fileSize / 1024.0;
+        double fileSizeMB = fileSizeKB / 1024.0;
+        NSLog(@"文件大小: %.2f KB (%.2f MB)", fileSizeKB, fileSizeMB);
+        string = [NSString stringWithFormat:@"%.2fMB", fileSizeMB];
+    }
+    return string;
+}
+
+- (NSString *)customFileSize {
+    if (!_customFileSize) {
+        _customFileSize = [self getFileSize];
+    }
+    return _customFileSize;
+}
+
 @end

+ 56 - 64
AIIM/Controller/chat/chetCell/chatCellView.m

@@ -289,8 +289,8 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     [sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.equalTo(nameLabel);
         make.top.equalTo(nameLabel.mas_bottom).offset(4);
-        make.right.equalTo(nameLabel);
-        make.bottom.lessThanOrEqualTo(_fileContentView).offset(-8);
+//        make.right.equalTo(nameLabel);
+//        make.bottom.lessThanOrEqualTo(_fileContentView).offset(-8);
     }];
     
     [self.contentView addSubview:_fileContentView];
@@ -838,14 +838,10 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     }
     
     //没有有效本地图片 使用SDWebImage加载图片
-//    NSLog(@"UIImage------------4:%@",_messageModel.url);
-    NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
-    NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
-    UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
-    
+
     weakSelf(self);
     [_imageContentView sd_setImageWithURL:[NSURL URLWithString:_messageModel.url]
-                        placeholderImage:placeholder
+                        placeholderImage:[UIImage imageNamed:@"pictrue_placeholder"]
                                completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
         if (!error && image) {
             // 图片加载成功后可以更新布局(如果需要)
@@ -903,36 +899,6 @@ static const CGFloat kMediaCornerRadius = 4.0f;
             }
         }];
     }
-
-    [_messageModel xiazaishipin:^(NSInteger persent) {
-        if(persent>=100){
-            dispatch_async(dispatch_get_main_queue(), ^{
-                if(weakself.jinduLbl){
-                    weakself.jinduLbl.alpha = 0;
-                    weakself.loadingImg.alpha = 0;
-                }
-            });
-            
-            NSLog(@"下载完成:%@",self->_messageModel.localurl);
-        }
-        else{
-//            NSLog(@"下载中:%ld",(long)persent);
-            dispatch_async(dispatch_get_main_queue(), ^{
-                if(weakself.jinduLbl){
-                    if(weakself.loadingImg.alpha!=1||!weakself.loadingImg.image){
-                        NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
-                        NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
-                        UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
-                        weakself.loadingImg.image = placeholder;
-                        weakself.loadingImg.alpha = 1;
-                    }
-                    weakself.jinduLbl.alpha = 1;
-                    weakself.jinduLbl.text = [NSString stringWithFormat:@"%ld",(long)persent];
-                }
-            });
-            
-        }
-    }];
     
 
     _messageModel.uploadPersentChange = ^(NSInteger index,NSInteger localtime) {
@@ -990,30 +956,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     // 设置文件内容
     iconView.image = [UIImage imageNamed: @"file_icon_black"];
     nameLabel.text = _messageModel.fileName ?: @"未知文件";
-    
-    sizeLabel.alpha = 0;
-
-    
-    [_messageModel xiazaiwenjian:^(NSInteger persent) {
-//        NSLog(@"setupFileContent----------");
-        if(persent>=100){
-            NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
-            dispatch_async(dispatch_get_main_queue(), ^{
-                if(sizeLabel){
-                    sizeLabel.alpha = 0;
-                }
-            });
-        }
-        else{
-//            NSLog(@"setupFileContent 下载中:%ld",(long)persent);
-            dispatch_async(dispatch_get_main_queue(), ^{
-                if(sizeLabel){
-                    sizeLabel.alpha = 1;
-                    sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
-                }
-            });
-        }
-    }];
+    sizeLabel.text = _messageModel.customFileSize ?: @"0KB";
     
     _currentContentView = _fileContentView;
 }
@@ -1584,7 +1527,40 @@ static const CGFloat kMediaCornerRadius = 4.0f;
             [MBProgressHUD showWithText:@"视频正在上传"];
             return;
         }
-        [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
+        
+        weakSelf(self);
+        [_messageModel xiazaishipin:^(NSInteger persent) {
+            if(persent>=100){
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    if(weakself.jinduLbl){
+                        weakself.jinduLbl.alpha = 0;
+                        weakself.loadingImg.alpha = 0;
+                    }
+                    [FilePreviewer.shared previewFileWithLocalPath:weakself.messageModel.localurl
+                                                         remoteURL:getURL(weakself.messageModel.url)
+                                                fromViewController:weakself.parentViewController];
+                });
+                
+                NSLog(@"下载完成:%@",self->_messageModel.localurl);
+            }
+            else{
+    //            NSLog(@"下载中:%ld",(long)persent);
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    if(weakself.jinduLbl){
+                        if(weakself.loadingImg.alpha!=1||!weakself.loadingImg.image){
+                            NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
+                            NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
+                            UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
+                            weakself.loadingImg.image = placeholder;
+                            weakself.loadingImg.alpha = 1;
+                        }
+                        weakself.jinduLbl.alpha = 1;
+                        weakself.jinduLbl.text = [NSString stringWithFormat:@"%ld",(long)persent];
+                    }
+                });
+                
+            }
+        }];
     }
 }
 
@@ -1603,7 +1579,23 @@ static const CGFloat kMediaCornerRadius = 4.0f;
             [MBProgressHUD showWithText:@"文件正在上传"];
             return;
         }
-        [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
+        
+        
+        UILabel *sizeLabel = _fileContentView.subviews[2];
+
+        weakSelf(self)
+        [_messageModel xiazaiwenjian:^(NSInteger persent) {
+            dispatch_async(dispatch_get_main_queue(), ^{
+                sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
+                if(persent>=100){
+                    sizeLabel.text = weakself.messageModel.customFileSize;
+                    NSLog(@"setupFileContent:下载完成:%@",weakself.messageModel.localurl);
+                    [FilePreviewer.shared previewFileWithLocalPath:weakself.messageModel.localurl
+                                                         remoteURL:getURL(weakself.messageModel.url)
+                                                fromViewController:weakself.parentViewController];
+                }
+            });
+        }];
     }
 }