downloadFileBottomView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // downloadFileBottomView.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/8.
  6. //
  7. #import "downloadFileBottomView.h"
  8. #import <sys/mount.h>
  9. #include <sys/param.h>
  10. @interface downloadFileBottomView ()
  11. {
  12. long diskfreeSpace;//iphone 空间
  13. BOOL isCanDown;
  14. }
  15. @property(nonatomic,strong)UILabel *selectNumLabel;
  16. @property(nonatomic,strong)UILabel *uploadFileRoutelabel;
  17. @property(nonatomic,strong)UIButton *uploadButon;
  18. @end
  19. @implementation downloadFileBottomView
  20. - (id)initWithFrame:(CGRect)frame{
  21. self = [super initWithFrame:frame];
  22. [self setBackgroundColor:[UIColor whiteColor]];
  23. [self drawAnyView];
  24. _availableStorage = [self freeDiskSpaceInBytes];
  25. return self;
  26. }
  27. - (NSString *)freeDiskSpaceInBytes{
  28. struct statfs buf;
  29. unsigned long long freeSpace = -1;
  30. if (statfs("/var", &buf) >= 0) {
  31. freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
  32. diskfreeSpace = freeSpace;
  33. }
  34. NSString *str = [NSString stringWithFormat:@"%0.2f GB",freeSpace/1024.0/1024.0/1024.0];
  35. return str;
  36. }
  37. - (void)drawAnyView{
  38. _selectNumLabel = [[UILabel alloc] init];
  39. _selectNumLabel.font = [UIFont boldSystemFontOfSize:16.0];
  40. _selectNumLabel.textColor = [UIColor blackColor];
  41. [self addSubview:_selectNumLabel];
  42. [_selectNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.left.mas_equalTo(15);
  44. make.top.mas_equalTo(10);
  45. make.right.mas_equalTo(-110);
  46. make.height.mas_equalTo(20);
  47. }];
  48. //设置即得标题
  49. self.indexPathsForSelectedItems = [NSMutableArray new];
  50. _uploadFileRoutelabel = [[UILabel alloc] init];
  51. _uploadFileRoutelabel.font = [UIFont systemFontOfSize:14.0];
  52. _uploadFileRoutelabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  53. _uploadFileRoutelabel.text = NSLocalizedString(@"File_download_Path_default",nil);
  54. [self addSubview:_uploadFileRoutelabel];
  55. [_uploadFileRoutelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.left.mas_equalTo(15);
  57. make.top.mas_equalTo(_selectNumLabel.mas_bottom).offset(10);
  58. make.right.mas_equalTo(-110);
  59. make.height.mas_equalTo(20);
  60. }];
  61. //图片上传
  62. NSString* curImgUploadStr = [[NSString alloc] initWithFormat:@"%@ %@",@" ",NSLocalizedString(@"my_set_no_File_download",nil)];
  63. // gradient
  64. CAGradientLayer *gl_But = [CAGradientLayer layer];
  65. gl_But.frame = CGRectMake(0,0,88.f,40.f);
  66. gl_But.startPoint = CGPointMake(0, 0.5);
  67. gl_But.endPoint = CGPointMake(1, 0.5);
  68. gl_But.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  69. gl_But.locations = @[@(0), @(1.0f)];
  70. gl_But.cornerRadius = 8;
  71. UIButton *imageUploadBut = [[UIButton alloc] init];
  72. [imageUploadBut setImage:[UIImage imageNamed:@"donwload_file_white_100"] forState:UIControlStateNormal];
  73. [imageUploadBut setTitle:curImgUploadStr forState:UIControlStateNormal];
  74. [imageUploadBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  75. imageUploadBut.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
  76. imageUploadBut.tag = 2;
  77. [imageUploadBut addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
  78. [self addSubview:imageUploadBut];
  79. [imageUploadBut.layer insertSublayer:gl_But atIndex:0];
  80. [imageUploadBut bringSubviewToFront:imageUploadBut.imageView];
  81. self.uploadButon = imageUploadBut;
  82. //imageUploadBut.backgroundColor = [UIColor redColor];
  83. [imageUploadBut mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.right.mas_equalTo(-15);
  85. make.top.mas_equalTo(10);
  86. make.width.mas_equalTo(88);
  87. make.height.mas_equalTo(40);
  88. }];
  89. imageUploadBut.enabled = NO;
  90. imageUploadBut.alpha = 0.5;
  91. }
  92. #pragma mark 数据处理
  93. - (void)setIndexPathsForSelectedItems:(NSMutableArray *)indexPathsForSelectedItems
  94. {
  95. isCanDown = YES;
  96. _indexPathsForSelectedItems = indexPathsForSelectedItems;
  97. if(!_indexPathsForSelectedItems){
  98. _indexPathsForSelectedItems = [NSMutableArray new];
  99. }
  100. //TZAssetModel imageData (以字节为单位)
  101. NSUInteger allImageData = 0;
  102. for (couldPhoneFileModel* model in _indexPathsForSelectedItems) {
  103. allImageData += model.length;
  104. }
  105. if(diskfreeSpace < allImageData){
  106. isCanDown = NO;
  107. }
  108. allImageData /= 1024;
  109. NSString *byteStr = nil;
  110. NSString *unitStr = @"KB";
  111. if(allImageData < 1024){
  112. byteStr = [[NSString alloc] initWithFormat:@"%ld",allImageData];
  113. }
  114. else if( allImageData >= 1024 && allImageData < 1024*1024){
  115. byteStr = [[NSString alloc] initWithFormat:@"%.1f",allImageData/1024.0];
  116. unitStr = @"MB";
  117. }
  118. else{
  119. byteStr = [[NSString alloc] initWithFormat:@"%.2f",allImageData/1024.0/1024.0];
  120. unitStr = @"G";
  121. }
  122. if(!_availableStorage){
  123. _availableStorage = @"--";
  124. }
  125. NSString *curStr1 = [NSString stringWithFormat:@"已选%ld项",_indexPathsForSelectedItems.count];
  126. NSString *curStr2 = [NSString stringWithFormat:@"(%@%@,%@)",byteStr,unitStr,_availableStorage];
  127. NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",curStr1,curStr2];
  128. NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
  129. NSRange redRange = NSMakeRange([totalStr rangeOfString:curStr2].location, [totalStr rangeOfString:curStr2].length);
  130. UIColor *noteColor =[UIColor hwColor:@"#959799" alpha:1.0];
  131. if(!isCanDown){
  132. noteColor = [UIColor redColor];
  133. }
  134. [noteStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  135. [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:redRange];
  136. // 设置行间距
  137. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  138. [paragraphStyle setLineSpacing:5]; //设置行间距
  139. [noteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
  140. _selectNumLabel.attributedText = noteStr;
  141. if(_indexPathsForSelectedItems.count > 0 && isCanDown){
  142. self.uploadButon.enabled = YES;
  143. self.uploadButon.alpha = 1;
  144. }
  145. else
  146. {
  147. self.uploadButon.enabled = NO;
  148. self.uploadButon.alpha = 0.5;
  149. }
  150. }
  151. #pragma mark 按钮点击
  152. - (void)didClickButFun:(UIButton*)but
  153. {
  154. if(_didClickDownloadFile)
  155. {
  156. _didClickDownloadFile();
  157. }
  158. }
  159. @end