downloadFileBottomView.m 6.9 KB

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