2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-03-16 00:38:14 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2014-03-16 00:38:14 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2014-03-16 00:38:14 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2014-03-16 00:38:14 +00:00
|
|
|
|
|
|
|
#import "MPAvatarCell.h"
|
|
|
|
|
|
|
|
const long MPAvatarAdd = 10000;
|
|
|
|
|
|
|
|
@interface MPAvatarCell()
|
|
|
|
|
|
|
|
@property(strong, nonatomic) IBOutlet UIImageView *avatarImageView;
|
|
|
|
@property(strong, nonatomic) IBOutlet UILabel *nameLabel;
|
|
|
|
@property(strong, nonatomic) IBOutlet UIView *nameContainer;
|
2014-03-20 00:09:25 +00:00
|
|
|
@property(strong, nonatomic) IBOutlet UIImageView *spinner;
|
2014-03-20 12:12:33 +00:00
|
|
|
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *nameToCenterConstraint;
|
2014-03-20 00:09:25 +00:00
|
|
|
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *avatarSizeConstraint;
|
2014-03-20 12:12:33 +00:00
|
|
|
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *avatarToTopConstraint;
|
|
|
|
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *avatarRaisedConstraint;
|
2014-10-14 01:56:46 +00:00
|
|
|
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *keyboardHeightConstraint;
|
2014-03-16 00:38:14 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPAvatarCell {
|
2014-04-07 03:34:18 +00:00
|
|
|
CAAnimationGroup *_targetedShadowAnimation;
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)reuseIdentifier {
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
return NSStringFromClass( self );
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
#pragma mark - Life cycle
|
|
|
|
|
2014-03-16 00:38:14 +00:00
|
|
|
- (void)awakeFromNib {
|
|
|
|
|
|
|
|
[super awakeFromNib];
|
|
|
|
|
2017-04-21 02:29:10 +00:00
|
|
|
self.visible = NO;
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-03-16 00:38:14 +00:00
|
|
|
self.nameContainer.layer.cornerRadius = 5;
|
|
|
|
|
|
|
|
self.avatarImageView.layer.cornerRadius = self.avatarImageView.bounds.size.height / 2;
|
|
|
|
self.avatarImageView.layer.masksToBounds = NO;
|
|
|
|
self.avatarImageView.backgroundColor = [UIColor clearColor];
|
|
|
|
|
2014-09-16 04:34:22 +00:00
|
|
|
[self observeKeyPath:@"bounds" withBlock:^(id from, id to, NSKeyValueChange cause, MPAvatarCell *_self) {
|
|
|
|
_self.contentView.frame = _self.bounds;
|
2014-03-16 00:38:14 +00:00
|
|
|
}];
|
2014-09-16 04:34:22 +00:00
|
|
|
[self observeKeyPath:@"selected" withBlock:^(id from, id to, NSKeyValueChange cause, MPAvatarCell *_self) {
|
|
|
|
[_self updateAnimated:_self.superview != nil];
|
|
|
|
}];
|
|
|
|
[self observeKeyPath:@"highlighted" withBlock:^(id from, id to, NSKeyValueChange cause, MPAvatarCell *_self) {
|
|
|
|
[_self updateAnimated:_self.superview != nil];
|
2014-03-16 00:38:14 +00:00
|
|
|
}];
|
2014-10-14 01:56:46 +00:00
|
|
|
PearlAddNotificationObserver( UIKeyboardWillShowNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPAvatarCell *self, NSNotification *note) {
|
|
|
|
CGRect keyboardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
|
|
|
CGFloat keyboardHeight = CGRectGetHeight( self.window.screen.bounds ) - CGRectGetMinY( keyboardRect );
|
|
|
|
[self.keyboardHeightConstraint updateConstant:keyboardHeight];
|
|
|
|
} );
|
2014-03-16 00:38:14 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
CABasicAnimation *toShadowOpacityAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
|
|
|
|
toShadowOpacityAnimation.toValue = @0.2f;
|
|
|
|
toShadowOpacityAnimation.duration = 0.5f;
|
|
|
|
|
|
|
|
CABasicAnimation *pulseShadowOpacityAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
|
|
|
|
pulseShadowOpacityAnimation.fromValue = @0.2f;
|
|
|
|
pulseShadowOpacityAnimation.toValue = @0.6f;
|
|
|
|
pulseShadowOpacityAnimation.beginTime = 0.5f;
|
|
|
|
pulseShadowOpacityAnimation.duration = 2.0f;
|
|
|
|
pulseShadowOpacityAnimation.autoreverses = YES;
|
|
|
|
pulseShadowOpacityAnimation.repeatCount = MAXFLOAT;
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
_targetedShadowAnimation = [CAAnimationGroup new];
|
|
|
|
_targetedShadowAnimation.animations = @[ toShadowOpacityAnimation, pulseShadowOpacityAnimation ];
|
|
|
|
_targetedShadowAnimation.duration = MAXFLOAT;
|
2014-03-20 00:09:25 +00:00
|
|
|
self.avatarImageView.layer.shadowColor = [UIColor whiteColor].CGColor;
|
|
|
|
self.avatarImageView.layer.shadowOffset = CGSizeZero;
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prepareForReuse {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
[super prepareForReuse];
|
|
|
|
|
|
|
|
_newUser = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
_visibility = 0;
|
|
|
|
_mode = MPAvatarModeLowered;
|
|
|
|
_spinnerActive = NO;
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)dealloc {
|
2014-03-16 00:38:14 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
[self removeKeyPathObservers];
|
2014-10-14 01:56:46 +00:00
|
|
|
PearlRemoveNotificationObservers();
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
#pragma mark - Properties
|
|
|
|
|
2016-01-14 14:58:04 +00:00
|
|
|
- (void)setAvatar:(NSUInteger)avatar {
|
2014-03-16 00:38:14 +00:00
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
_avatar = avatar == MPAvatarAdd? MPAvatarAdd: (avatar + MPAvatarCount) % MPAvatarCount;
|
2014-03-16 00:38:14 +00:00
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
if (_avatar == MPAvatarAdd) {
|
2014-03-16 00:38:14 +00:00
|
|
|
self.avatarImageView.image = [UIImage imageNamed:@"avatar-add"];
|
2014-04-07 03:34:18 +00:00
|
|
|
self.name = strl( @"New User" );
|
|
|
|
_newUser = YES;
|
|
|
|
}
|
2014-03-16 00:38:14 +00:00
|
|
|
else
|
2016-01-14 14:58:04 +00:00
|
|
|
self.avatarImageView.image = [UIImage imageNamed:strf( @"avatar-%lu", (unsigned long)_avatar )];
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)name {
|
|
|
|
|
|
|
|
return self.nameLabel.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setName:(NSString *)name {
|
|
|
|
|
|
|
|
self.nameLabel.text = name;
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)setVisibility:(CGFloat)visibility {
|
|
|
|
|
|
|
|
[self setVisibility:visibility animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVisibility:(CGFloat)visibility animated:(BOOL)animated {
|
2014-03-16 00:38:14 +00:00
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
if (visibility == _visibility)
|
|
|
|
return;
|
2014-03-16 00:38:14 +00:00
|
|
|
_visibility = visibility;
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
[self updateAnimated:animated];
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setHighlighted:(BOOL)highlighted {
|
|
|
|
|
|
|
|
super.highlighted = highlighted;
|
|
|
|
|
2014-08-22 02:27:47 +00:00
|
|
|
self.avatarImageView.transform = highlighted? CGAffineTransformMakeScale( 1.1f, 1.1f ): CGAffineTransformIdentity;
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMode:(MPAvatarMode)mode {
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
[self setMode:mode animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMode:(MPAvatarMode)mode animated:(BOOL)animated {
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
if (mode == _mode)
|
|
|
|
return;
|
2014-03-16 00:38:14 +00:00
|
|
|
_mode = mode;
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
[self updateAnimated:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSpinnerActive:(BOOL)spinnerActive {
|
|
|
|
|
|
|
|
[self setSpinnerActive:spinnerActive animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSpinnerActive:(BOOL)spinnerActive animated:(BOOL)animated {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (_spinnerActive == spinnerActive)
|
|
|
|
return;
|
2014-03-20 00:09:25 +00:00
|
|
|
_spinnerActive = spinnerActive;
|
|
|
|
|
|
|
|
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
|
2014-08-22 02:27:47 +00:00
|
|
|
rotate.toValue = @(2 * M_PI);
|
2014-03-20 00:09:25 +00:00
|
|
|
rotate.duration = 5.0;
|
|
|
|
|
|
|
|
if (spinnerActive) {
|
|
|
|
rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
|
|
|
|
rotate.fromValue = @0.0;
|
|
|
|
rotate.repeatCount = MAXFLOAT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
|
|
|
|
rotate.repeatCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.spinner.layer removeAnimationForKey:@"rotation"];
|
|
|
|
[self.spinner.layer addAnimation:rotate forKey:@"rotation"];
|
|
|
|
|
|
|
|
[self updateAnimated:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)updateAnimated:(BOOL)animated {
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.contentView layoutIfNeeded];
|
2014-03-20 00:09:25 +00:00
|
|
|
[UIView animateWithDuration:animated? 0.2f: 0 animations:^{
|
2014-03-16 00:38:14 +00:00
|
|
|
self.avatarImageView.transform = CGAffineTransformIdentity;
|
|
|
|
}];
|
2014-09-12 04:41:17 +00:00
|
|
|
[UIView animateWithDuration:animated? 0.5f: 0 delay:0
|
2014-09-11 04:26:01 +00:00
|
|
|
options:UIViewAnimationOptionOverrideInheritedDuration | UIViewAnimationOptionBeginFromCurrentState
|
|
|
|
animations:^{
|
2017-04-21 02:29:10 +00:00
|
|
|
self.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
|
|
|
|
if (self.newUser) {
|
|
|
|
if (self.mode == MPAvatarModeLowered)
|
|
|
|
self.avatar = MPAvatarAdd;
|
|
|
|
else if (self.avatar == MPAvatarAdd)
|
|
|
|
self.avatar = arc4random() % MPAvatarCount;
|
|
|
|
}
|
|
|
|
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.alpha = self.visibility;
|
|
|
|
self.avatarImageView.alpha = self.visibility * 0.7f + 0.3f;
|
|
|
|
self.avatarImageView.layer.shadowRadius = 15 * self.visibility * self.visibility;
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
switch (self.mode) {
|
|
|
|
case MPAvatarModeLowered: {
|
2014-09-28 14:05:36 +00:00
|
|
|
[self.avatarSizeConstraint updateConstant:
|
|
|
|
self.avatarImageView.image.size.height * (self.visibility * 0.3f + 0.7f)];
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.avatarRaisedConstraint updatePriority:UILayoutPriorityDefaultLow];
|
|
|
|
[self.avatarToTopConstraint updatePriority:UILayoutPriorityDefaultLow];
|
|
|
|
[self.nameToCenterConstraint updatePriority:UILayoutPriorityDefaultLow];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
self.nameContainer.backgroundColor = [UIColor clearColor];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarImageView.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPAvatarModeRaisedButInactive: {
|
2014-09-28 14:05:36 +00:00
|
|
|
[self.avatarSizeConstraint updateConstant:
|
|
|
|
self.avatarImageView.image.size.height * (self.visibility * 0.7f + 0.3f)];
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.avatarRaisedConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
|
|
|
[self.avatarToTopConstraint updatePriority:UILayoutPriorityDefaultLow];
|
|
|
|
[self.nameToCenterConstraint updatePriority:UILayoutPriorityDefaultLow];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
self.nameContainer.backgroundColor = [UIColor clearColor];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarImageView.visible = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPAvatarModeRaisedAndActive: {
|
2014-09-28 14:05:36 +00:00
|
|
|
[self.avatarSizeConstraint updateConstant:
|
|
|
|
self.avatarImageView.image.size.height * (self.visibility * 0.7f + 0.3f)];
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.avatarRaisedConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
|
|
|
[self.avatarToTopConstraint updatePriority:UILayoutPriorityDefaultLow];
|
|
|
|
[self.nameToCenterConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
self.nameContainer.backgroundColor = [UIColor blackColor];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarImageView.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPAvatarModeRaisedAndHidden: {
|
2014-09-28 14:05:36 +00:00
|
|
|
[self.avatarSizeConstraint updateConstant:
|
|
|
|
self.avatarImageView.image.size.height * (self.visibility * 0.7f + 0.3f)];
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.avatarRaisedConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
|
|
|
[self.avatarToTopConstraint updatePriority:UILayoutPriorityDefaultLow];
|
|
|
|
[self.nameToCenterConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.visible = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
self.nameContainer.backgroundColor = [UIColor blackColor];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarImageView.visible = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPAvatarModeRaisedAndMinimized: {
|
|
|
|
[self.avatarSizeConstraint updateConstant:36];
|
|
|
|
[self.avatarRaisedConstraint updatePriority:UILayoutPriorityDefaultLow];
|
2014-10-14 01:56:46 +00:00
|
|
|
[self.avatarToTopConstraint updatePriority:UILayoutPriorityDefaultHigh + 2];
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.nameToCenterConstraint updatePriority:UILayoutPriorityDefaultHigh];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.nameContainer.visible = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
self.nameContainer.backgroundColor = [UIColor blackColor];
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarImageView.visible = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avatar minimized.
|
|
|
|
if (self.mode == MPAvatarModeRaisedAndMinimized)
|
|
|
|
[self.avatarImageView.layer removeAnimationForKey:@"targetedShadow"];
|
|
|
|
else if (![self.avatarImageView.layer animationForKey:@"targetedShadow"])
|
|
|
|
[self.avatarImageView.layer addAnimation:_targetedShadowAnimation forKey:@"targetedShadow"];
|
|
|
|
|
|
|
|
// Avatar selection and spinner.
|
|
|
|
if (self.mode != MPAvatarModeRaisedAndMinimized && (self.selected || self.highlighted) && !self.spinnerActive)
|
|
|
|
self.avatarImageView.backgroundColor = self.avatarImageView.tintColor;
|
|
|
|
else
|
|
|
|
self.avatarImageView.backgroundColor = [UIColor clearColor];
|
|
|
|
self.avatarImageView.layer.cornerRadius = self.avatarImageView.bounds.size.height / 2;
|
2017-04-21 02:29:10 +00:00
|
|
|
self.spinner.visible = self.spinnerActive;
|
2014-09-11 04:26:01 +00:00
|
|
|
|
|
|
|
[self.contentView layoutIfNeeded];
|
|
|
|
} completion:nil];
|
2014-03-16 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|