mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			133 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			133 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
|  | 
 | ||
|  | #import "OWSProgressView.h" | ||
| 
											5 years ago
										 | #import <SessionUtilitiesKit/UIView+OWS.h> | ||
|  | #import <SessionUtilitiesKit/OWSMath.h> | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											9 years ago
										 | @interface OWSProgressView () | ||
|  | 
 | ||
|  | @property (nonatomic) CAShapeLayer *borderLayer; | ||
|  | @property (nonatomic) CAShapeLayer *progressLayer; | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
|  | @implementation OWSProgressView | ||
|  | 
 | ||
|  | - (id)init | ||
|  | { | ||
|  |     self = [super init]; | ||
|  |     if (self) { | ||
|  |         [self initCommon]; | ||
|  |     } | ||
|  | 
 | ||
|  |     return self; | ||
|  | } | ||
|  | 
 | ||
|  | - (id)initWithFrame:(CGRect)frame | ||
|  | { | ||
|  |     self = [super initWithFrame:frame]; | ||
|  |     if (self) { | ||
|  |         [self initCommon]; | ||
|  |     } | ||
|  |     return self; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)initCommon | ||
|  | { | ||
|  |     self.opaque = NO; | ||
|  |     self.userInteractionEnabled = NO; | ||
|  |     self.color = [UIColor whiteColor]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     // Prevent the shape layer from animating changes. | ||
|  |     [CATransaction begin]; | ||
| 
											8 years ago
										 |     [CATransaction setDisableActions:YES]; | ||
| 
											8 years ago
										 | 
 | ||
| 
											9 years ago
										 |     self.borderLayer = [CAShapeLayer new]; | ||
|  |     [self.layer addSublayer:self.borderLayer]; | ||
|  | 
 | ||
|  |     self.progressLayer = [CAShapeLayer new]; | ||
|  |     [self.layer addSublayer:self.progressLayer]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     [CATransaction commit]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     [self setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical]; | ||
|  |     [self setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)layoutSubviews | ||
|  | { | ||
|  |     [super layoutSubviews]; | ||
|  |     [self update]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setProgress:(CGFloat)progress | ||
|  | { | ||
|  |     if (_progress != progress) { | ||
|  |         _progress = progress; | ||
|  |         [self update]; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setColor:(UIColor *)color | ||
|  | { | ||
|  |     if (![_color isEqual:color]) { | ||
|  |         _color = color; | ||
|  |         [self update]; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | - (void)update | ||
|  | { | ||
| 
											8 years ago
										 |     // Prevent the shape layer from animating changes. | ||
|  |     [CATransaction begin]; | ||
| 
											8 years ago
										 |     [CATransaction setDisableActions:YES]; | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     CGFloat borderThickness = MAX(CGHairlineWidth(), self.bounds.size.height * 0.1f); | ||
|  |     CGFloat cornerRadius = MIN(self.bounds.size.width, self.bounds.size.height) * 0.5f; | ||
| 
											9 years ago
										 | 
 | ||
|  |     // Add the outer border. | ||
| 
											7 years ago
										 |     UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius]; | ||
| 
											9 years ago
										 |     self.borderLayer.path = borderPath.CGPath; | ||
| 
											7 years ago
										 |     self.borderLayer.strokeColor = self.color.CGColor; | ||
|  |     self.borderLayer.lineWidth = borderThickness; | ||
|  |     self.borderLayer.fillColor = [UIColor clearColor].CGColor; | ||
| 
											9 years ago
										 | 
 | ||
|  |     // Add the inner progress. | ||
| 
											7 years ago
										 |     CGRect progressRect = self.bounds; | ||
|  |     progressRect.size.width = cornerRadius * 2; | ||
|  |     CGFloat baseProgress = borderThickness * 2; | ||
|  |     CGFloat minProgress = baseProgress; | ||
|  |     CGFloat maxProgress = MAX(0, self.bounds.size.width - baseProgress); | ||
| 
											7 years ago
										 |     progressRect.size.width = CGFloatLerp(minProgress, maxProgress, CGFloatClamp01(self.progress)); | ||
| 
											7 years ago
										 |     UIBezierPath *progressPath = [UIBezierPath bezierPathWithRoundedRect:progressRect cornerRadius:cornerRadius]; | ||
| 
											9 years ago
										 |     self.progressLayer.path = progressPath.CGPath; | ||
|  |     self.progressLayer.fillColor = self.color.CGColor; | ||
| 
											8 years ago
										 | 
 | ||
|  |     [CATransaction commit]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | + (CGSize)defaultSize | ||
| 
											9 years ago
										 | { | ||
|  |     return CGSizeMake(150, 16); | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (CGSize)sizeThatFits:(CGSize)size | ||
|  | { | ||
|  |     return OWSProgressView.defaultSize; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (CGSize)intrinsicContentSize | ||
|  | { | ||
|  |     return CGSizeMake(UIViewNoIntrinsicMetric, 16); | ||
|  | } | ||
|  | 
 | ||
|  | @end | ||
| 
											9 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_END |