downloadFileBottomView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. [self addSubview:_selectNumLabel];
  41. [_selectNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.mas_equalTo(15);
  43. make.top.mas_equalTo(10);
  44. make.right.mas_equalTo(-110);
  45. make.height.mas_equalTo(20);
  46. }];
  47. //设置即得标题
  48. self.indexPathsForSelectedItems = [NSMutableArray new];
  49. _uploadFileRoutelabel = [[UILabel alloc] init];
  50. _uploadFileRoutelabel.font = [UIFont systemFontOfSize:14.0];
  51. _uploadFileRoutelabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  52. _uploadFileRoutelabel.text = NSLocalizedString(@"File_download_Path_default",nil);
  53. [self addSubview:_uploadFileRoutelabel];
  54. [_uploadFileRoutelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.mas_equalTo(15);
  56. make.top.mas_equalTo(_selectNumLabel.mas_bottom).offset(10);
  57. make.right.mas_equalTo(-110);
  58. make.height.mas_equalTo(20);
  59. }];
  60. //图片上传
  61. NSString* curImgUploadStr = [[NSString alloc] initWithFormat:@"%@ %@",@" ",NSLocalizedString(@"my_set_no_File_download",nil)];
  62. // gradient
  63. CAGradientLayer *gl_But = [CAGradientLayer layer];
  64. gl_But.frame = CGRectMake(0,0,88.f,40.f);
  65. gl_But.startPoint = CGPointMake(0, 0.5);
  66. gl_But.endPoint = CGPointMake(1, 0.5);
  67. gl_But.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  68. gl_But.locations = @[@(0), @(1.0f)];
  69. gl_But.cornerRadius = 8;
  70. UIButton *imageUploadBut = [[UIButton alloc] init];
  71. [imageUploadBut setImage:[UIImage imageNamed:@"donwload_file_white_100"] forState:UIControlStateNormal];
  72. [imageUploadBut setTitle:curImgUploadStr forState:UIControlStateNormal];
  73. [imageUploadBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  74. imageUploadBut.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
  75. imageUploadBut.tag = 2;
  76. [imageUploadBut addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
  77. [self addSubview:imageUploadBut];
  78. [imageUploadBut.layer insertSublayer:gl_But atIndex:0];
  79. [imageUploadBut bringSubviewToFront:imageUploadBut.imageView];
  80. self.uploadButon = imageUploadBut;
  81. //imageUploadBut.backgroundColor = [UIColor redColor];
  82. [imageUploadBut mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.right.mas_equalTo(-15);
  84. make.top.mas_equalTo(10);
  85. make.width.mas_equalTo(88);
  86. make.height.mas_equalTo(40);
  87. }];
  88. imageUploadBut.enabled = NO;
  89. imageUploadBut.alpha = 0.5;
  90. }
  91. #pragma mark 数据处理
  92. - (void)setIndexPathsForSelectedItems:(NSMutableArray *)indexPathsForSelectedItems
  93. {
  94. isCanDown = YES;
  95. _indexPathsForSelectedItems = indexPathsForSelectedItems;
  96. if(!_indexPathsForSelectedItems){
  97. _indexPathsForSelectedItems = [NSMutableArray new];
  98. }
  99. //TZAssetModel imageData (以字节为单位)
  100. NSUInteger allImageData = 0;
  101. for (couldPhoneFileModel* model in _indexPathsForSelectedItems) {
  102. allImageData += model.length;
  103. }
  104. if(diskfreeSpace < allImageData){
  105. isCanDown = NO;
  106. }
  107. allImageData /= 1024;
  108. NSString *byteStr = nil;
  109. NSString *unitStr = @"KB";
  110. if(allImageData < 1024){
  111. byteStr = [[NSString alloc] initWithFormat:@"%ld",allImageData];
  112. }
  113. else if( allImageData >= 1024 && allImageData < 1024*1024){
  114. byteStr = [[NSString alloc] initWithFormat:@"%.1f",allImageData/1024.0];
  115. unitStr = @"MB";
  116. }
  117. else{
  118. byteStr = [[NSString alloc] initWithFormat:@"%.2f",allImageData/1024.0/1024.0];
  119. unitStr = @"G";
  120. }
  121. if(!_availableStorage){
  122. _availableStorage = @"--";
  123. }
  124. NSString *curStr1 = [NSString stringWithFormat:@"已选%ld项",_indexPathsForSelectedItems.count];
  125. NSString *curStr2 = [NSString stringWithFormat:@"(%@%@,%@)",byteStr,unitStr,_availableStorage];
  126. NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",curStr1,curStr2];
  127. NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
  128. NSRange redRange = NSMakeRange([totalStr rangeOfString:curStr2].location, [totalStr rangeOfString:curStr2].length);
  129. UIColor *noteColor =[UIColor hwColor:@"#959799" alpha:1.0];
  130. if(!isCanDown){
  131. noteColor = [UIColor redColor];
  132. }
  133. [noteStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  134. [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:redRange];
  135. // 设置行间距
  136. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  137. [paragraphStyle setLineSpacing:5]; //设置行间距
  138. [noteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
  139. _selectNumLabel.attributedText = noteStr;
  140. if(_indexPathsForSelectedItems.count > 0 && isCanDown){
  141. self.uploadButon.enabled = YES;
  142. self.uploadButon.alpha = 1;
  143. }
  144. else
  145. {
  146. self.uploadButon.enabled = NO;
  147. self.uploadButon.alpha = 0.5;
  148. }
  149. }
  150. #pragma mark 按钮点击
  151. - (void)didClickButFun:(UIButton*)but
  152. {
  153. if(_didClickDownloadFile)
  154. {
  155. _didClickDownloadFile();
  156. }
  157. }
  158. @end