downloadFileBottomView.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. 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. //多国语言适配
  84. NSString *languageCode = [NSLocale preferredLanguages][0];
  85. if([languageCode rangeOfString:@"ja-"].location != NSNotFound)
  86. {
  87. imageUploadBut.titleLabel.font = [UIFont systemFontOfSize:10.0];
  88. }
  89. [imageUploadBut mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.right.mas_equalTo(-10);
  91. make.top.mas_equalTo(10);
  92. make.width.mas_equalTo(98);
  93. make.height.mas_equalTo(40);
  94. }];
  95. imageUploadBut.enabled = NO;
  96. imageUploadBut.alpha = 0.5;
  97. }
  98. #pragma mark 数据处理
  99. - (void)setIndexPathsForSelectedItems:(NSMutableArray *)indexPathsForSelectedItems
  100. {
  101. isCanDown = YES;
  102. _indexPathsForSelectedItems = indexPathsForSelectedItems;
  103. if(!_indexPathsForSelectedItems){
  104. _indexPathsForSelectedItems = [NSMutableArray new];
  105. }
  106. //TZAssetModel imageData (以字节为单位)
  107. NSUInteger allImageData = 0;
  108. for (couldPhoneFileModel* model in _indexPathsForSelectedItems) {
  109. allImageData += model.length;
  110. }
  111. if(diskfreeSpace < allImageData){
  112. isCanDown = NO;
  113. }
  114. allImageData /= 1024;
  115. NSString *byteStr = nil;
  116. NSString *unitStr = @"KB";
  117. if(allImageData < 1024){
  118. byteStr = [[NSString alloc] initWithFormat:@"%ld",allImageData];
  119. }
  120. else if( allImageData >= 1024 && allImageData < 1024*1024){
  121. byteStr = [[NSString alloc] initWithFormat:@"%.1f",allImageData/1024.0];
  122. unitStr = @"MB";
  123. }
  124. else{
  125. byteStr = [[NSString alloc] initWithFormat:@"%.2f",allImageData/1024.0/1024.0];
  126. unitStr = @"G";
  127. }
  128. if(!_availableStorage){
  129. _availableStorage = @"--";
  130. }
  131. NSString *curStr1 = [NSString stringWithFormat:@"已选%ld项",_indexPathsForSelectedItems.count];
  132. NSString *curStr2 = [NSString stringWithFormat:@"(%@%@,%@)",byteStr,unitStr,_availableStorage];
  133. NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",curStr1,curStr2];
  134. NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
  135. NSRange redRange = NSMakeRange([totalStr rangeOfString:curStr2].location, [totalStr rangeOfString:curStr2].length);
  136. UIColor *noteColor =[UIColor hwColor:@"#959799" alpha:1.0];
  137. if(!isCanDown){
  138. noteColor = [UIColor redColor];
  139. }
  140. [noteStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  141. [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:redRange];
  142. // 设置行间距
  143. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  144. [paragraphStyle setLineSpacing:5]; //设置行间距
  145. [noteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
  146. _selectNumLabel.attributedText = noteStr;
  147. if(_indexPathsForSelectedItems.count > 0 && isCanDown){
  148. self.uploadButon.enabled = YES;
  149. self.uploadButon.alpha = 1;
  150. }
  151. else
  152. {
  153. self.uploadButon.enabled = NO;
  154. self.uploadButon.alpha = 0.5;
  155. }
  156. }
  157. #pragma mark 按钮点击
  158. - (void)didClickButFun:(UIButton*)but
  159. {
  160. if(_didClickDownloadFile)
  161. {
  162. _didClickDownloadFile();
  163. }
  164. }
  165. @end