-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSWDiskUsageView.m
More file actions
94 lines (77 loc) · 3.62 KB
/
SWDiskUsageView.m
File metadata and controls
94 lines (77 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#import "SWDiskUsageView.h"
#import <objc/runtime.h>
extern uint64_t getTotalDiskSpace(void);
extern uint64_t getFreeDiskSpace(void);
UIColor *colorWithRGB(unsigned char red, unsigned char green, unsigned char blue)
{
return [UIColor colorWithRed:red/255.f green:green/255.f blue:blue/255.f alpha:1.0];
}
static inline double GBFromBytes(double bytes)
{
return (bytes / (1000 * 1000 * 1000));
}
@implementation SWDiskUsageView
-(UILabel *)usageLabel {
return _usageLabel;
}
-(UIView *)diskBarView
{
return _diskBarView;
}
-(CGSize)intrinsicContentSize
{
return CGSizeMake(0, 40);
}
-(void)setup
{
_backgroundView = [UIView new];
_backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
_backgroundView.layer.cornerRadius = 3;
_diskBarView = [UIView new];
_diskBarView.layer.cornerRadius = 3;
_diskBarView.translatesAutoresizingMaskIntoConstraints = NO;
_usageLabel = [UILabel new];
_usageLabel.text = @"-- GB / --- GB";
_usageLabel.textAlignment = NSTextAlignmentCenter;
_usageLabel.font = [UIFont boldSystemFontOfSize:9];
_usageLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview: _backgroundView];
[self addSubview: _diskBarView];
[self addSubview: _usageLabel];
[_backgroundView.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:15].active = YES;
[_backgroundView.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-15].active = YES;
[_backgroundView.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-5].active = YES;
[_backgroundView.topAnchor constraintEqualToAnchor: self.bottomAnchor constant:-12.5].active = YES;
[_diskBarView.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:15].active = YES;
//[_diskBarView.widthAnchor constraintEqualToAnchor: _backgroundView.widthAnchor multiplier: diskMultiplier].active = YES;
//[self.diskBarView.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-15].active = YES;
[_diskBarView.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-5].active = YES;
[_diskBarView.topAnchor constraintEqualToAnchor: self.bottomAnchor constant:-12.5].active = YES;
[_usageLabel.centerXAnchor constraintEqualToAnchor: self.centerXAnchor].active = YES;
[_usageLabel.bottomAnchor constraintEqualToAnchor: _backgroundView.topAnchor constant:-5].active = YES;
[self setBarBackgroundColor];
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self setBarBackgroundColor];
}
-(void)setBarBackgroundColor {
if (@available(iOS 13, *)) {
if ([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleLight) {
_backgroundView.backgroundColor = colorWithRGB(209, 209, 209);
_diskBarView.backgroundColor = colorWithRGB(70, 70, 70);
} else {
_backgroundView.backgroundColor = colorWithRGB(79, 79, 79);
_diskBarView.backgroundColor = colorWithRGB(224, 224, 224);
}
} else {
_backgroundView.backgroundColor = colorWithRGB(209, 209, 209);
_diskBarView.backgroundColor = colorWithRGB(70, 70, 70);
}
}
-(void)updateDiskBarForUsedDiskSpace:(NSUInteger)usedSpace totalSpace:(NSUInteger)totalSpace {
_usageLabel.text = [NSString stringWithFormat:@"%.01f GB / %.00f GB", GBFromBytes(usedSpace),GBFromBytes(totalSpace)];
CGFloat diskMultiplier = (CGFloat)usedSpace / (CGFloat)totalSpace;
[_diskBarView.widthAnchor constraintEqualToAnchor: _backgroundView.widthAnchor multiplier: diskMultiplier].active = YES;
}
@end