2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-03-20 00:09:25 +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-20 00:09:25 +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-20 00:09:25 +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-20 00:09:25 +00:00
|
|
|
|
2020-01-27 18:27:10 +00:00
|
|
|
#import <Countly/Countly.h>
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
#import "MPUsersViewController.h"
|
|
|
|
#import "MPEntities.h"
|
|
|
|
#import "MPAvatarCell.h"
|
|
|
|
#import "MPiOSAppDelegate.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
|
|
|
#import "MPAppDelegate_Key.h"
|
2014-05-10 13:18:46 +00:00
|
|
|
#import "MPWebViewController.h"
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
typedef NS_OPTIONS( NSUInteger, MPUsersTips ) {
|
|
|
|
MPUsersThanksTip = 1 << 0,
|
|
|
|
MPUsersAvatarTip = 1 << 1,
|
|
|
|
MPUsersMasterPasswordTip = 1 << 2,
|
|
|
|
MPUsersPreferencesTip = 1 << 3,
|
|
|
|
};
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
typedef NS_ENUM( NSUInteger, MPActiveUserState ) {
|
2014-03-20 00:09:25 +00:00
|
|
|
/** The users are all inactive */
|
|
|
|
MPActiveUserStateNone,
|
|
|
|
/** The selected user is activated and being logged in with */
|
|
|
|
MPActiveUserStateLogin,
|
|
|
|
/** The selected user is activated and its user name is being asked for */
|
|
|
|
MPActiveUserStateUserName,
|
|
|
|
/** The selected user is activated and its new master password is being asked for */
|
|
|
|
MPActiveUserStateMasterPasswordChoice,
|
|
|
|
/** The selected user is activated and the confirmation of the previously entered master password is being asked for */
|
|
|
|
MPActiveUserStateMasterPasswordConfirmation,
|
|
|
|
/** The selected user is activated displayed at the top with the rest of the UI inactive */
|
|
|
|
MPActiveUserStateMinimized,
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface MPUsersViewController()
|
|
|
|
|
|
|
|
@property(nonatomic) MPActiveUserState activeUserState;
|
|
|
|
@property(nonatomic, strong) NSArray *userIDs;
|
|
|
|
@property(nonatomic, strong) NSTimer *marqueeTipTimer;
|
|
|
|
@property(nonatomic, strong) NSArray *marqueeTipTexts;
|
|
|
|
@property(nonatomic) NSUInteger marqueeTipTextIndex;
|
2017-05-07 22:36:01 +00:00
|
|
|
@property(nonatomic, copy) NSString *masterPasswordChoice;
|
|
|
|
@property(nonatomic, strong) NSOperationQueue *afterUpdates;
|
|
|
|
@property(nonatomic, weak) id contextChangedObserver;
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
@end
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@implementation MPUsersViewController
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
self.afterUpdates = [NSOperationQueue new];
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
self.marqueeTipTexts = @[
|
2014-05-10 13:18:46 +00:00
|
|
|
strl( @"Thanks, lhunath ➚" ),
|
|
|
|
strl( @"Press and hold to delete or reset user." ),
|
2014-04-07 03:34:18 +00:00
|
|
|
strl( @"Shake for emergency generator." ),
|
2014-03-20 00:09:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
self.view.backgroundColor = [UIColor clearColor];
|
|
|
|
self.avatarCollectionView.allowsMultipleSelection = YES;
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.entryField addTarget:self action:@selector( textFieldEditingChanged: ) forControlEvents:UIControlEventEditingChanged];
|
2014-09-24 05:07:02 +00:00
|
|
|
|
2017-04-21 02:29:10 +00:00
|
|
|
self.preferencesTipContainer.visible = NO;
|
2014-09-29 02:15:55 +00:00
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
[self setActive:YES animated:NO];
|
2014-09-29 02:25:48 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"tipped.thanks"])
|
|
|
|
[self showTips:MPUsersThanksTip];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
2017-04-21 02:29:10 +00:00
|
|
|
self.userSelectionContainer.visible = NO;
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 00:31:23 +00:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
|
|
[self registerObservers];
|
|
|
|
[self reloadUsers];
|
|
|
|
|
|
|
|
[self.marqueeTipTimer invalidate];
|
|
|
|
self.marqueeTipTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector( firedMarqueeTimer: )
|
|
|
|
userInfo:nil repeats:YES];
|
|
|
|
[self firedMarqueeTimer:nil];
|
|
|
|
}
|
|
|
|
|
2014-09-12 21:13:33 +00:00
|
|
|
- (void)viewWillLayoutSubviews {
|
2014-04-13 19:45:08 +00:00
|
|
|
|
2014-09-12 21:13:33 +00:00
|
|
|
[self.avatarCollectionView.collectionViewLayout invalidateLayout];
|
|
|
|
[super viewWillLayoutSubviews];
|
|
|
|
}
|
|
|
|
|
2017-03-11 16:20:15 +00:00
|
|
|
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
2014-04-13 19:45:08 +00:00
|
|
|
|
|
|
|
[self.avatarCollectionView.collectionViewLayout invalidateLayout];
|
2017-03-11 16:20:15 +00:00
|
|
|
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
|
2014-04-13 19:45:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 13:18:46 +00:00
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
|
|
2017-04-30 22:45:08 +00:00
|
|
|
if ([segue.identifier isEqualToString:@"thanks"])
|
|
|
|
((MPWebViewController *)segue.destinationViewController).initialURL =
|
|
|
|
[NSURL URLWithString:@"https://thanks.lhunath.com"];
|
2014-05-10 13:18:46 +00:00
|
|
|
}
|
2014-04-13 19:45:08 +00:00
|
|
|
|
2017-04-29 19:01:24 +00:00
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
|
|
|
[self removeObservers];
|
|
|
|
|
|
|
|
[self.marqueeTipTimer invalidate];
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
#pragma mark - UITextFieldDelegate
|
|
|
|
|
|
|
|
- (void)textFieldDidEndEditing:(UITextField *)textField {
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
|
|
|
|
|
|
if (textField == self.entryField) {
|
|
|
|
switch (self.activeUserState) {
|
|
|
|
case MPActiveUserStateNone: {
|
|
|
|
[textField resignFirstResponder];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateLogin: {
|
2014-06-02 02:45:29 +00:00
|
|
|
self.entryField.enabled = NO;
|
2020-03-25 16:16:19 +00:00
|
|
|
MPAvatarCell *selectedAvatar = [self selectedAvatar];
|
|
|
|
selectedAvatar.spinnerActive = YES;
|
|
|
|
NSIndexPath *selectedPath = selectedAvatar? [self.avatarCollectionView indexPathForCell:selectedAvatar]: nil;
|
2016-01-14 14:58:04 +00:00
|
|
|
NSString *masterPassword = self.entryField.text;
|
|
|
|
if (![MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-03-20 00:09:25 +00:00
|
|
|
BOOL signedIn = NO, isNew = NO;
|
2020-03-25 16:16:19 +00:00
|
|
|
MPUserEntity *user = selectedPath? [self userForIndexPath:selectedPath inContext:context isNew:&isNew]: nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
if (!isNew && user)
|
|
|
|
signedIn = [[MPiOSAppDelegate get] signInAsUser:user saveInContext:context
|
2016-01-14 14:58:04 +00:00
|
|
|
usingMasterPassword:masterPassword];
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
2014-03-20 12:12:33 +00:00
|
|
|
self.entryField.text = @"";
|
2014-06-02 02:45:29 +00:00
|
|
|
self.entryField.enabled = YES;
|
2014-03-20 00:09:25 +00:00
|
|
|
[self selectedAvatar].spinnerActive = NO;
|
|
|
|
|
|
|
|
if (!signedIn) {
|
|
|
|
// Sign in failed.
|
2014-04-13 17:04:18 +00:00
|
|
|
[self showEntryTip:strl( @"Looks like a typo!\nTry again; that password was incorrect." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}];
|
2016-01-14 14:58:04 +00:00
|
|
|
}]) {
|
|
|
|
self.entryField.enabled = YES;
|
|
|
|
[self selectedAvatar].spinnerActive = NO;
|
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateUserName: {
|
|
|
|
NSString *userName = self.entryField.text;
|
|
|
|
if (![userName length]) {
|
|
|
|
// No name entered.
|
2014-04-13 17:04:18 +00:00
|
|
|
[self showEntryTip:strl( @"First, enter your name." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self selectedAvatar].name = userName;
|
|
|
|
self.activeUserState = MPActiveUserStateMasterPasswordChoice;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordChoice: {
|
|
|
|
NSString *masterPassword = self.entryField.text;
|
|
|
|
if (![masterPassword length]) {
|
|
|
|
// No password entered.
|
2014-04-13 17:04:18 +00:00
|
|
|
[self showEntryTip:strl( @"Pick a master password." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.activeUserState = MPActiveUserStateMasterPasswordConfirmation;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation: {
|
|
|
|
NSString *masterPassword = self.entryField.text;
|
|
|
|
if (![masterPassword length]) {
|
|
|
|
// No password entered.
|
2014-04-13 17:04:18 +00:00
|
|
|
[self showEntryTip:strl( @"Confirm your master password." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (![masterPassword isEqualToString:self.masterPasswordChoice]) {
|
2014-03-20 00:09:25 +00:00
|
|
|
// Master password confirmation failed.
|
2014-04-13 17:04:18 +00:00
|
|
|
[self showEntryTip:strl( @"Looks like a typo!\nTry again; enter your master password twice." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
self.activeUserState = MPActiveUserStateMasterPasswordChoice;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-06-02 02:45:29 +00:00
|
|
|
self.entryField.enabled = NO;
|
2014-04-12 18:43:41 +00:00
|
|
|
MPAvatarCell *avatarCell = [self selectedAvatar];
|
|
|
|
avatarCell.spinnerActive = YES;
|
2020-03-13 16:15:24 +00:00
|
|
|
NSIndexPath *avatarPath = [self.avatarCollectionView indexPathForCell:avatarCell];
|
2016-01-14 14:58:04 +00:00
|
|
|
NSUInteger newUserAvatar = avatarCell.avatar;
|
|
|
|
NSString *newUserName = avatarCell.name;
|
2014-04-12 18:43:41 +00:00
|
|
|
if (![MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-03-20 00:09:25 +00:00
|
|
|
BOOL isNew = NO;
|
2020-03-13 16:15:24 +00:00
|
|
|
MPUserEntity *user = [self userForIndexPath:avatarPath inContext:context isNew:&isNew];
|
2014-03-20 00:09:25 +00:00
|
|
|
if (isNew) {
|
2014-04-12 18:43:41 +00:00
|
|
|
user = [MPUserEntity insertNewObjectInContext:context];
|
2015-02-28 15:01:41 +00:00
|
|
|
user.algorithm = MPAlgorithmDefault;
|
2017-04-20 01:58:10 +00:00
|
|
|
user.defaultType = user.algorithm.defaultType;
|
2016-01-14 14:58:04 +00:00
|
|
|
user.avatar = newUserAvatar;
|
|
|
|
user.name = newUserName;
|
2017-04-30 03:03:50 +00:00
|
|
|
|
|
|
|
if ([[MPConfig get].sendInfo boolValue]) {
|
2020-01-27 18:27:10 +00:00
|
|
|
[Countly.sharedInstance recordEvent:@"new-user" segmentation:@{
|
|
|
|
@"algorithm": @(user.algorithm.version).description,
|
|
|
|
@"avatar" : @(user.avatar).description,
|
|
|
|
}];
|
2017-04-30 03:03:50 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 14:58:04 +00:00
|
|
|
BOOL signedIn = [[MPiOSAppDelegate get] signInAsUser:user saveInContext:context
|
|
|
|
usingMasterPassword:masterPassword];
|
2014-04-12 18:43:41 +00:00
|
|
|
PearlMainQueue( ^{
|
2014-03-20 12:12:33 +00:00
|
|
|
self.entryField.text = @"";
|
2014-06-02 02:45:29 +00:00
|
|
|
self.entryField.enabled = YES;
|
2016-01-14 14:58:04 +00:00
|
|
|
avatarCell.spinnerActive = NO;
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
if (!signedIn) {
|
|
|
|
// Sign in failed, shouldn't happen for a new user.
|
2014-04-12 18:43:41 +00:00
|
|
|
[self showEntryTip:strl( @"Couldn't create new user." )];
|
2014-03-20 00:09:25 +00:00
|
|
|
self.activeUserState = MPActiveUserStateNone;
|
|
|
|
return;
|
|
|
|
}
|
2014-04-12 18:43:41 +00:00
|
|
|
} );
|
2016-01-14 14:58:04 +00:00
|
|
|
}]) {
|
|
|
|
self.entryField.enabled = YES;
|
2014-04-12 18:43:41 +00:00
|
|
|
avatarCell.spinnerActive = NO;
|
2016-01-14 14:58:04 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMinimized: {
|
|
|
|
[textField resignFirstResponder];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This isn't really in UITextFieldDelegate. We fake it from UITextFieldTextDidChangeNotification.
|
|
|
|
- (void)textFieldEditingChanged:(UITextField *)textField {
|
|
|
|
|
2017-05-04 17:57:12 +00:00
|
|
|
if ([[textField.text lowercaseString] isEqualToString:@"hangtest"])
|
|
|
|
[NSThread sleepForTimeInterval:10];
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
if (textField == self.entryField) {
|
|
|
|
switch (self.activeUserState) {
|
|
|
|
case MPActiveUserStateNone:
|
|
|
|
break;
|
|
|
|
case MPActiveUserStateLogin:
|
|
|
|
break;
|
|
|
|
case MPActiveUserStateUserName: {
|
|
|
|
NSString *userName = self.entryField.text;
|
|
|
|
[self selectedAvatar].name = [userName length]? userName: strl( @"New User" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordChoice:
|
|
|
|
break;
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation:
|
|
|
|
break;
|
|
|
|
case MPActiveUserStateMinimized:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
#pragma mark - UICollectionViewDelegateFlowLayout
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-09-12 21:13:33 +00:00
|
|
|
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
|
referenceSizeForHeaderInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
CGSize parentSize = self.avatarCollectionView.bounds.size;
|
|
|
|
return CGSizeMake( parentSize.width / 4, parentSize.height );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
|
referenceSizeForFooterInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
CGSize parentSize = self.avatarCollectionView.bounds.size;
|
|
|
|
return CGSizeMake( parentSize.width / 4, parentSize.height );
|
|
|
|
}
|
|
|
|
|
2014-03-20 12:42:15 +00:00
|
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
|
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-09-12 21:13:33 +00:00
|
|
|
CGSize parentSize = self.avatarCollectionView.bounds.size;
|
|
|
|
return CGSizeMake( parentSize.width / 2, parentSize.height );
|
2014-03-20 12:42:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
#pragma mark - UICollectionViewDataSource
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
if (collectionView == self.avatarCollectionView)
|
|
|
|
return [self.userIDs count] + 1;
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
Throw( @"unexpected collection view: %@", collectionView );
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
if (collectionView == self.avatarCollectionView) {
|
|
|
|
MPAvatarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MPAvatarCell reuseIdentifier] forIndexPath:indexPath];
|
2014-09-12 04:41:17 +00:00
|
|
|
cell.contentView.frame = cell.bounds;
|
2014-09-11 04:26:01 +00:00
|
|
|
[cell addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector( didLongPress: )]];
|
2014-03-20 00:09:25 +00:00
|
|
|
[self updateModeForAvatar:cell atIndexPath:indexPath animated:NO];
|
2014-04-12 18:43:41 +00:00
|
|
|
[self updateVisibilityForAvatar:cell atIndexPath:indexPath animated:NO];
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
BOOL isNew = NO;
|
|
|
|
MPUserEntity *user = [self userForIndexPath:indexPath inContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]
|
|
|
|
isNew:&isNew];
|
2014-04-07 03:34:18 +00:00
|
|
|
if (isNew)
|
2014-09-11 04:26:01 +00:00
|
|
|
// New User
|
2014-03-20 00:09:25 +00:00
|
|
|
cell.avatar = MPAvatarAdd;
|
|
|
|
else {
|
|
|
|
// Existing User
|
|
|
|
cell.avatar = user.avatar;
|
|
|
|
cell.name = user.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
Throw( @"unexpected collection view: %@", collectionView );
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
if (collectionView == self.avatarCollectionView) {
|
2014-04-15 04:26:13 +00:00
|
|
|
[self.avatarCollectionView scrollToItemAtIndexPath:indexPath
|
|
|
|
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
// Deselect all other cells.
|
|
|
|
for (NSUInteger otherItem = 0; otherItem < [collectionView numberOfItemsInSection:indexPath.section]; ++otherItem)
|
|
|
|
if (otherItem != indexPath.item) {
|
|
|
|
NSIndexPath *otherIndexPath = [NSIndexPath indexPathForItem:otherItem inSection:indexPath.section];
|
|
|
|
[collectionView deselectItemAtIndexPath:otherIndexPath animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL isNew = NO;
|
2014-06-02 02:45:29 +00:00
|
|
|
NSManagedObjectContext *mainContext = [MPiOSAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
MPUserEntity *mainUser = [self userForIndexPath:indexPath inContext:mainContext isNew:&isNew];
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
if (isNew)
|
|
|
|
self.activeUserState = MPActiveUserStateUserName;
|
2014-06-02 02:45:29 +00:00
|
|
|
else if (!mainUser.keyID)
|
2014-03-20 00:09:25 +00:00
|
|
|
self.activeUserState = MPActiveUserStateMasterPasswordChoice;
|
2014-06-02 02:45:29 +00:00
|
|
|
else {
|
2014-03-20 00:09:25 +00:00
|
|
|
self.activeUserState = MPActiveUserStateLogin;
|
2014-06-02 02:45:29 +00:00
|
|
|
|
|
|
|
self.entryField.enabled = NO;
|
2016-01-14 14:58:04 +00:00
|
|
|
MPAvatarCell *userAvatar = [self selectedAvatar];
|
|
|
|
userAvatar.spinnerActive = YES;
|
|
|
|
if (!isNew && mainUser && [MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2017-04-30 03:52:57 +00:00
|
|
|
MPUserEntity *user = [MPUserEntity existingObjectWithID:mainUser.permanentObjectID inContext:context];
|
2016-06-04 14:44:43 +00:00
|
|
|
BOOL signedIn = [[MPiOSAppDelegate get] signInAsUser:user saveInContext:context usingMasterPassword:nil];
|
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
2016-01-14 14:58:04 +00:00
|
|
|
self.entryField.text = @"";
|
|
|
|
self.entryField.enabled = YES;
|
|
|
|
userAvatar.spinnerActive = NO;
|
|
|
|
|
|
|
|
if (!signedIn)
|
|
|
|
[self.entryField becomeFirstResponder];
|
2016-06-04 14:44:43 +00:00
|
|
|
} );
|
2016-01-14 14:58:04 +00:00
|
|
|
}])
|
|
|
|
return;
|
2014-06-02 02:45:29 +00:00
|
|
|
|
|
|
|
self.entryField.text = @"";
|
|
|
|
self.entryField.enabled = YES;
|
2016-01-14 14:58:04 +00:00
|
|
|
userAvatar.spinnerActive = NO;
|
2014-06-02 02:45:29 +00:00
|
|
|
|
2016-01-14 14:58:04 +00:00
|
|
|
[self.entryField becomeFirstResponder];
|
2014-06-02 02:45:29 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (collectionView == self.avatarCollectionView)
|
2014-03-20 00:09:25 +00:00
|
|
|
self.activeUserState = MPActiveUserStateNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UILongPressGestureRecognizer
|
|
|
|
|
|
|
|
- (void)didLongPress:(UILongPressGestureRecognizer *)recognizer {
|
|
|
|
|
|
|
|
if ([recognizer.view isKindOfClass:[MPAvatarCell class]]) {
|
|
|
|
if (recognizer.state != UIGestureRecognizerStateBegan)
|
2014-09-11 04:26:01 +00:00
|
|
|
// Don't show the action menu unless the state is Began.
|
2014-03-20 00:09:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
MPAvatarCell *avatarCell = (MPAvatarCell *)recognizer.view;
|
|
|
|
NSManagedObjectContext *mainContext = [MPiOSAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
|
|
|
|
BOOL isNew = NO;
|
2020-03-13 16:15:24 +00:00
|
|
|
MPUserEntity *user = [self userForIndexPath:[self.avatarCollectionView indexPathForCell:avatarCell]
|
|
|
|
inContext:mainContext isNew:&isNew];
|
2014-03-20 00:09:25 +00:00
|
|
|
if (isNew || !user)
|
|
|
|
return;
|
|
|
|
|
2017-04-30 03:52:57 +00:00
|
|
|
NSManagedObjectID *userID = user.permanentObjectID;
|
2020-01-14 18:59:32 +00:00
|
|
|
UIAlertController *controller = [UIAlertController alertControllerWithTitle:user.name message:nil preferredStyle:UIAlertControllerStyleActionSheet];
|
|
|
|
[controller addAction:[UIAlertAction actionWithTitle:@"Delete User" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
|
2020-03-05 16:29:10 +00:00
|
|
|
UIAlertController *controller_ = [UIAlertController alertControllerWithTitle:@"Deleting User" message:
|
|
|
|
@"The user and its sites will be deleted." preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
[controller_ addAction:[UIAlertAction actionWithTitle:@"Delete User" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
|
2020-01-14 18:59:32 +00:00
|
|
|
[self deleteUser:userID];
|
|
|
|
}]];
|
2020-03-05 16:29:10 +00:00
|
|
|
[controller_ addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
|
|
|
|
[self presentViewController:controller_ animated:YES completion:nil];
|
2020-01-14 18:59:32 +00:00
|
|
|
}]];
|
|
|
|
[controller addAction:[UIAlertAction actionWithTitle:@"Reset Password" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
2020-03-05 16:29:10 +00:00
|
|
|
UIAlertController *controller_ = [UIAlertController alertControllerWithTitle:@"Resetting User" message:
|
|
|
|
@"The user's master password will be reset." preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
[controller_ addAction:[UIAlertAction actionWithTitle:@"Reset User" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
|
2020-01-14 18:59:32 +00:00
|
|
|
[self resetUser:userID avatar:avatarCell];
|
|
|
|
}]];
|
2020-03-05 16:29:10 +00:00
|
|
|
[controller_ addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
|
|
|
|
[self presentViewController:controller_ animated:YES completion:nil];
|
2020-01-14 18:59:32 +00:00
|
|
|
}]];
|
|
|
|
[controller addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
|
|
|
|
[self presentViewController:controller animated:YES completion:nil];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
|
|
|
|
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity
|
|
|
|
targetContentOffset:(inout CGPoint *)targetContentOffset {
|
|
|
|
|
|
|
|
if (scrollView == self.avatarCollectionView) {
|
2014-04-07 03:34:18 +00:00
|
|
|
CGPoint offsetToCenter = self.avatarCollectionView.center;
|
2014-03-20 00:09:25 +00:00
|
|
|
NSIndexPath *avatarIndexPath = [self.avatarCollectionView indexPathForItemAtPoint:
|
|
|
|
CGPointPlusCGPoint( *targetContentOffset, offsetToCenter )];
|
|
|
|
CGPoint targetCenter = [self.avatarCollectionView layoutAttributesForItemAtIndexPath:avatarIndexPath].center;
|
|
|
|
*targetContentOffset = CGPointMinusCGPoint( targetCenter, offsetToCenter );
|
2014-09-11 04:26:01 +00:00
|
|
|
NSAssert( [self.avatarCollectionView indexPathForItemAtPoint:targetCenter].item == avatarIndexPath.item, @"should be same item" );
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
- (void)deleteUser:(NSManagedObjectID *)userID {
|
2014-04-12 18:43:41 +00:00
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2017-04-29 19:01:24 +00:00
|
|
|
MPUserEntity *user = [MPUserEntity existingObjectWithID:userID inContext:context];
|
|
|
|
if (!user)
|
2014-09-29 02:15:55 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-29 19:01:24 +00:00
|
|
|
[context deleteObject:user];
|
2014-09-29 02:15:55 +00:00
|
|
|
[context saveToStore];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resetUser:(NSManagedObjectID *)userID avatar:(MPAvatarCell *)avatarCell {
|
|
|
|
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2017-04-29 19:01:24 +00:00
|
|
|
MPUserEntity *user = [MPUserEntity existingObjectWithID:userID inContext:context];
|
|
|
|
if (!user)
|
2014-09-29 02:15:55 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-29 19:01:24 +00:00
|
|
|
[[MPiOSAppDelegate get] changeMasterPasswordFor:user saveInContext:context didResetBlock:^{
|
2014-09-29 02:15:55 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
NSIndexPath *avatarIndexPath = [self.avatarCollectionView indexPathForCell:avatarCell];
|
|
|
|
[self.avatarCollectionView selectItemAtIndexPath:avatarIndexPath animated:NO
|
|
|
|
scrollPosition:UICollectionViewScrollPositionNone];
|
|
|
|
[self collectionView:self.avatarCollectionView didSelectItemAtIndexPath:avatarIndexPath];
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showTips:(MPUsersTips)showTips {
|
2014-04-13 17:04:18 +00:00
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersThanksTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.thanksTipContainer.visible = YES;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersAvatarTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarTipContainer.visible = YES;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersMasterPasswordTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.entryTipContainer.visible = YES;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersPreferencesTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.preferencesTipContainer.visible = YES;
|
2014-04-12 18:43:41 +00:00
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
if (finished)
|
2014-09-29 02:15:55 +00:00
|
|
|
PearlMainQueueAfter( 5, ^{
|
2014-04-12 18:43:41 +00:00
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersThanksTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.thanksTipContainer.visible = NO;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersAvatarTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.avatarTipContainer.visible = NO;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersMasterPasswordTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.entryTipContainer.visible = NO;
|
2014-09-29 02:15:55 +00:00
|
|
|
if (showTips & MPUsersPreferencesTip)
|
2017-04-21 02:29:10 +00:00
|
|
|
self.preferencesTipContainer.visible = NO;
|
2014-04-12 18:43:41 +00:00
|
|
|
}];
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
- (void)showEntryTip:(NSString *)message {
|
|
|
|
|
|
|
|
NSUInteger newlineIndex = [message rangeOfString:@"\n"].location;
|
|
|
|
NSString *messageTitle = newlineIndex == NSNotFound? message: [message substringToIndex:newlineIndex];
|
|
|
|
NSString *messageSubtitle = newlineIndex == NSNotFound? nil: [message substringFromIndex:newlineIndex];
|
|
|
|
self.entryTipTitleLabel.text = messageTitle;
|
|
|
|
self.entryTipSubtitleLabel.text = messageSubtitle;
|
|
|
|
[self showTips:MPUsersMasterPasswordTip];
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)firedMarqueeTimer:(NSTimer *)timer {
|
|
|
|
|
2014-05-10 13:18:46 +00:00
|
|
|
NSString *nextMarqueeString = self.marqueeTipTexts[self.marqueeTipTextIndex++ % [self.marqueeTipTexts count]];
|
|
|
|
if ([nextMarqueeString isEqualToString:[self.marqueeButton titleForState:UIControlStateNormal]])
|
|
|
|
return;
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
[UIView animateWithDuration:timer? 0.5f: 0 animations:^{
|
2017-04-21 02:29:10 +00:00
|
|
|
self.marqueeButton.visible = NO;
|
2014-03-20 00:09:25 +00:00
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
if (!finished)
|
|
|
|
return;
|
|
|
|
|
2014-05-10 13:18:46 +00:00
|
|
|
[self.marqueeButton setTitle:nextMarqueeString forState:UIControlStateNormal];
|
2014-09-27 05:27:05 +00:00
|
|
|
[UIView animateWithDuration:timer? 0.5f: 0 animations:^{
|
2017-04-21 02:29:10 +00:00
|
|
|
self.marqueeButton.visible = YES;
|
2014-03-20 00:09:25 +00:00
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MPAvatarCell *)selectedAvatar {
|
|
|
|
|
|
|
|
NSArray *selectedIndexPaths = self.avatarCollectionView.indexPathsForSelectedItems;
|
|
|
|
if (![selectedIndexPaths count]) {
|
|
|
|
// No selected user.
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (MPAvatarCell *)[self.avatarCollectionView cellForItemAtIndexPath:selectedIndexPaths.firstObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MPUserEntity *)userForIndexPath:(NSIndexPath *)indexPath inContext:(NSManagedObjectContext *)context isNew:(BOOL *)isNew {
|
2020-03-25 16:16:19 +00:00
|
|
|
|
|
|
|
*isNew = NO;
|
|
|
|
if (!indexPath || (*isNew = indexPath.item >= [self.userIDs count]))
|
2014-03-20 00:09:25 +00:00
|
|
|
return nil;
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
return [MPUserEntity existingObjectWithID:self.userIDs[indexPath.item] inContext:context];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
- (void)updateAvatarVisibility {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
for (NSIndexPath *indexPath in self.avatarCollectionView.indexPathsForVisibleItems) {
|
|
|
|
MPAvatarCell *cell = (MPAvatarCell *)[self.avatarCollectionView cellForItemAtIndexPath:indexPath];
|
|
|
|
[self updateVisibilityForAvatar:cell atIndexPath:indexPath animated:NO];
|
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateModeForAvatar:(MPAvatarCell *)avatarCell atIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
|
|
|
|
|
|
|
|
switch (self.activeUserState) {
|
|
|
|
case MPActiveUserStateNone: {
|
|
|
|
[self.avatarCollectionView deselectItemAtIndexPath:indexPath animated:YES];
|
|
|
|
[avatarCell setMode:MPAvatarModeLowered animated:animated];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateLogin:
|
|
|
|
case MPActiveUserStateUserName:
|
|
|
|
case MPActiveUserStateMasterPasswordChoice:
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation: {
|
|
|
|
if ([self.avatarCollectionView.indexPathsForSelectedItems containsObject:indexPath])
|
|
|
|
[avatarCell setMode:MPAvatarModeRaisedAndActive animated:animated];
|
|
|
|
else
|
|
|
|
[avatarCell setMode:MPAvatarModeRaisedButInactive animated:animated];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMinimized: {
|
|
|
|
if ([self.avatarCollectionView.indexPathsForSelectedItems containsObject:indexPath])
|
|
|
|
[avatarCell setMode:MPAvatarModeRaisedAndMinimized animated:animated];
|
|
|
|
else
|
|
|
|
[avatarCell setMode:MPAvatarModeRaisedAndHidden animated:animated];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
- (void)updateVisibilityForAvatar:(MPAvatarCell *)cell atIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
|
|
|
|
|
|
|
|
CGFloat current = [self.avatarCollectionView layoutAttributesForItemAtIndexPath:indexPath].center.x -
|
|
|
|
self.avatarCollectionView.contentOffset.x;
|
|
|
|
CGFloat max = self.avatarCollectionView.bounds.size.width;
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
CGFloat visibility = MAX( 0, MIN( 1, 1 - ABS( current / (max / 2) - 1 ) ) );
|
2014-04-12 18:43:41 +00:00
|
|
|
[cell setVisibility:visibility animated:animated];
|
|
|
|
|
2017-04-21 02:29:10 +00:00
|
|
|
[UIView animateWithDuration:animated? 0.3f: 0 animations:^{
|
|
|
|
self.nextAvatarButton.visible = self.previousAvatarButton.visible = cell.newUser && cell.mode == MPAvatarModeRaisedAndActive;
|
|
|
|
self.nextAvatarButton.alpha = self.previousAvatarButton.alpha = visibility * 0.7f;
|
|
|
|
}];
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
- (void)afterUpdatesMainQueue:(void ( ^ )(void))block {
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.afterUpdates addOperationWithBlock:^{
|
2014-04-07 03:34:18 +00:00
|
|
|
PearlMainQueue( block );
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:01:24 +00:00
|
|
|
- (void)removeObservers {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
[self removeKeyPathObservers];
|
2017-04-29 19:01:24 +00:00
|
|
|
PearlRemoveNotificationObservers();
|
2017-05-07 22:36:01 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self.contextChangedObserver];
|
2017-04-29 19:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerObservers {
|
|
|
|
|
|
|
|
[self removeObservers];
|
2014-03-20 00:09:25 +00:00
|
|
|
[self observeKeyPath:@"avatarCollectionView.contentOffset" withBlock:
|
2017-05-07 22:36:01 +00:00
|
|
|
^(id from, id to, NSKeyValueChange cause, MPUsersViewController *self) {
|
2020-01-27 18:27:10 +00:00
|
|
|
PearlMainQueue( ^{ [self updateAvatarVisibility]; } );
|
2014-03-20 00:09:25 +00:00
|
|
|
}];
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
PearlAddNotificationObserver( UIApplicationDidEnterBackgroundNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
2017-04-21 02:29:10 +00:00
|
|
|
self.userSelectionContainer.visible = NO;
|
2014-09-27 05:27:05 +00:00
|
|
|
} );
|
|
|
|
PearlAddNotificationObserver( UIApplicationWillEnterForegroundNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
|
|
|
[self reloadUsers];
|
|
|
|
} );
|
|
|
|
PearlAddNotificationObserver( UIApplicationDidBecomeActiveNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
|
|
|
[UIView animateWithDuration:0.5f animations:^{
|
2017-04-21 02:29:10 +00:00
|
|
|
self.userSelectionContainer.visible = YES;
|
2014-09-27 05:27:05 +00:00
|
|
|
}];
|
|
|
|
} );
|
2014-10-14 01:56:46 +00:00
|
|
|
PearlAddNotificationObserver( UIKeyboardWillShowNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
|
|
|
CGRect keyboardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
|
|
|
CGFloat keyboardHeight = CGRectGetHeight( self.view.window.screen.bounds ) - CGRectGetMinY( keyboardRect );
|
|
|
|
[self.keyboardHeightConstraint updateConstant:keyboardHeight];
|
|
|
|
} );
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if ((self.contextChangedObserver
|
2018-02-11 04:29:55 +00:00
|
|
|
= [[MPiOSAppDelegate get] managedObjectContextChanged:^(NSDictionary<NSManagedObjectID *, NSString *> *affectedObjects) {
|
2017-05-07 22:36:01 +00:00
|
|
|
if ([[[affectedObjects allKeys] filteredArrayUsingPredicate:
|
|
|
|
[NSPredicate predicateWithBlock:^BOOL(NSManagedObjectID *objectID, NSDictionary *bindings) {
|
|
|
|
return [objectID.entity.name isEqualToString:NSStringFromClass( [MPUserEntity class] )];
|
|
|
|
}]] count])
|
|
|
|
[self reloadUsers];
|
|
|
|
}]))
|
2017-04-29 19:01:24 +00:00
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
|
|
|
self.avatarCollectionView.visible = YES;
|
|
|
|
[self.storeLoadingActivity stopAnimating];
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
|
|
|
self.avatarCollectionView.visible = NO;
|
|
|
|
[self.storeLoadingActivity startAnimating];
|
|
|
|
}];
|
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
PearlAddNotificationObserver( NSPersistentStoreCoordinatorStoresWillChangeNotification, [MPiOSAppDelegate get].storeCoordinator, nil,
|
2014-09-27 05:27:05 +00:00
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
|
|
|
self.userIDs = nil;
|
|
|
|
} );
|
2014-10-28 04:53:16 +00:00
|
|
|
PearlAddNotificationObserver( NSPersistentStoreCoordinatorStoresDidChangeNotification, [MPiOSAppDelegate get].storeCoordinator, nil,
|
2014-09-27 05:27:05 +00:00
|
|
|
^(MPUsersViewController *self, NSNotification *note) {
|
2017-04-29 19:01:24 +00:00
|
|
|
[self registerObservers];
|
|
|
|
[self reloadUsers];
|
2014-09-27 05:27:05 +00:00
|
|
|
} );
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reloadUsers {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
[self afterUpdatesMainQueue:^{
|
2014-09-15 05:00:23 +00:00
|
|
|
if (![MPiOSAppDelegate managedObjectContextForMainThreadPerformBlockAndWait:^(NSManagedObjectContext *mainContext) {
|
2014-04-07 03:34:18 +00:00
|
|
|
NSError *error = nil;
|
|
|
|
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPUserEntity class] )];
|
|
|
|
fetchRequest.sortDescriptors = @[
|
2014-09-11 04:26:01 +00:00
|
|
|
[NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector( @selector( lastUsed ) ) ascending:NO]
|
2014-04-07 03:34:18 +00:00
|
|
|
];
|
|
|
|
NSArray *users = [mainContext executeFetchRequest:fetchRequest error:&error];
|
|
|
|
if (!users) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Failed to load users." );
|
2014-04-07 03:34:18 +00:00
|
|
|
self.userIDs = nil;
|
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
NSMutableArray *userIDs = [NSMutableArray arrayWithCapacity:[users count]];
|
|
|
|
for (MPUserEntity *user in users)
|
2017-04-30 03:52:57 +00:00
|
|
|
[userIDs addObject:user.permanentObjectID];
|
2014-04-07 03:34:18 +00:00
|
|
|
self.userIDs = userIDs;
|
2014-09-15 05:00:23 +00:00
|
|
|
}])
|
|
|
|
self.userIDs = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Properties
|
|
|
|
|
|
|
|
- (void)setActive:(BOOL)active animated:(BOOL)animated {
|
|
|
|
|
|
|
|
_active = active;
|
|
|
|
|
|
|
|
if (active)
|
|
|
|
[self setActiveUserState:MPActiveUserStateNone animated:animated];
|
|
|
|
else
|
|
|
|
[self setActiveUserState:MPActiveUserStateMinimized animated:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUserIDs:(NSArray *)userIDs {
|
|
|
|
|
|
|
|
_userIDs = userIDs;
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
BOOL isNew = NO;
|
|
|
|
NSManagedObjectID *selectUserID = [MPiOSAppDelegate get].activeUserOID;
|
2020-03-25 16:16:19 +00:00
|
|
|
if (!selectUserID) {
|
|
|
|
selectUserID = [self userForIndexPath:self.avatarCollectionView.indexPathsForSelectedItems.firstObject
|
|
|
|
inContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]
|
|
|
|
isNew:&isNew].permanentObjectID;
|
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
[self.avatarCollectionView reloadData];
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
NSUInteger selectedAvatarItem = isNew? [self.userIDs count]: selectUserID? [self.userIDs indexOfObject:selectUserID]: NSNotFound;
|
2014-04-07 03:34:18 +00:00
|
|
|
if (selectedAvatarItem != NSNotFound)
|
|
|
|
[self.avatarCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:selectedAvatarItem inSection:0] animated:NO
|
|
|
|
scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
2017-04-21 02:29:10 +00:00
|
|
|
self.userSelectionContainer.visible = YES;
|
2014-03-20 00:09:25 +00:00
|
|
|
}];
|
2014-04-07 03:34:18 +00:00
|
|
|
} );
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setActiveUserState:(MPActiveUserState)activeUserState {
|
|
|
|
|
|
|
|
[self setActiveUserState:activeUserState animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setActiveUserState:(MPActiveUserState)activeUserState animated:(BOOL)animated {
|
|
|
|
|
|
|
|
_activeUserState = activeUserState;
|
2017-05-07 22:36:01 +00:00
|
|
|
self.masterPasswordChoice = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (activeUserState != MPActiveUserStateMinimized && (!self.active || [MPiOSAppDelegate get].activeUserOID)) {
|
2014-03-20 00:09:25 +00:00
|
|
|
[[MPiOSAppDelegate get] signOutAnimated:YES];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:26:01 +00:00
|
|
|
// Set the entry container's contents.
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.afterUpdates setSuspended:YES];
|
2014-04-07 03:34:18 +00:00
|
|
|
__block BOOL requestFirstResponder = NO;
|
2014-09-11 04:26:01 +00:00
|
|
|
[self.view layoutIfNeeded];
|
2014-04-12 18:43:41 +00:00
|
|
|
[UIView animateWithDuration:animated? 0.4f: 0 animations:^{
|
2014-03-20 00:09:25 +00:00
|
|
|
switch (activeUserState) {
|
|
|
|
case MPActiveUserStateNone:
|
|
|
|
break;
|
|
|
|
case MPActiveUserStateLogin: {
|
|
|
|
self.entryLabel.text = strl( @"Enter your master password:" );
|
|
|
|
self.entryField.secureTextEntry = YES;
|
2014-04-12 18:43:41 +00:00
|
|
|
self.entryField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
2014-06-06 01:43:06 +00:00
|
|
|
self.entryField.text = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateUserName: {
|
|
|
|
self.entryLabel.text = strl( @"Enter your full name:" );
|
|
|
|
self.entryField.secureTextEntry = NO;
|
2014-04-12 18:43:41 +00:00
|
|
|
self.entryField.autocapitalizationType = UITextAutocapitalizationTypeWords;
|
2014-06-06 01:43:06 +00:00
|
|
|
self.entryField.text = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordChoice: {
|
|
|
|
self.entryLabel.text = strl( @"Choose your master password:" );
|
|
|
|
self.entryField.secureTextEntry = YES;
|
2014-04-12 18:43:41 +00:00
|
|
|
self.entryField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
2014-06-06 01:43:06 +00:00
|
|
|
self.entryField.text = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation: {
|
2017-05-07 22:36:01 +00:00
|
|
|
self.masterPasswordChoice = self.entryField.text;
|
2014-03-20 00:09:25 +00:00
|
|
|
self.entryLabel.text = strl( @"Confirm your master password:" );
|
|
|
|
self.entryField.secureTextEntry = YES;
|
2014-04-12 18:43:41 +00:00
|
|
|
self.entryField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
2014-06-06 01:43:06 +00:00
|
|
|
self.entryField.text = nil;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMinimized:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manage the entry container depending on whether a user is activate or not.
|
|
|
|
switch (activeUserState) {
|
|
|
|
case MPActiveUserStateNone: {
|
|
|
|
self.avatarCollectionView.scrollEnabled = YES;
|
2017-04-21 02:29:10 +00:00
|
|
|
self.entryContainer.visible = NO;
|
|
|
|
self.footerContainer.visible = YES;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateLogin:
|
|
|
|
case MPActiveUserStateUserName:
|
|
|
|
case MPActiveUserStateMasterPasswordChoice:
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation: {
|
|
|
|
self.avatarCollectionView.scrollEnabled = NO;
|
2017-04-21 02:29:10 +00:00
|
|
|
self.entryContainer.visible = YES;
|
|
|
|
self.footerContainer.visible = YES;
|
2014-04-07 03:34:18 +00:00
|
|
|
requestFirstResponder = YES;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMinimized: {
|
|
|
|
self.avatarCollectionView.scrollEnabled = NO;
|
2017-04-21 02:29:10 +00:00
|
|
|
self.entryContainer.visible = NO;
|
|
|
|
self.footerContainer.visible = NO;
|
2014-03-20 00:09:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 02:27:47 +00:00
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
// Manage tip visibility.
|
|
|
|
switch (activeUserState) {
|
|
|
|
case MPActiveUserStateNone:
|
|
|
|
case MPActiveUserStateMasterPasswordConfirmation:
|
|
|
|
case MPActiveUserStateLogin: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateUserName: {
|
|
|
|
[self showTips:MPUsersAvatarTip];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMasterPasswordChoice: {
|
|
|
|
[self showEntryTip:strl( @"A short phrase makes a strong, memorable password." )];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPActiveUserStateMinimized: {
|
2014-09-29 02:25:48 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"tipped.passwordsPreferences"])
|
2014-09-29 02:15:55 +00:00
|
|
|
[self showTips:MPUsersPreferencesTip];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 02:27:47 +00:00
|
|
|
[self.view layoutIfNeeded];
|
2014-03-20 00:09:25 +00:00
|
|
|
} completion:^(BOOL finished) {
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.afterUpdates setSuspended:NO];
|
2014-03-20 00:09:25 +00:00
|
|
|
}];
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-06-22 01:59:14 +00:00
|
|
|
[self.entryField resignFirstResponder];
|
2014-04-07 03:34:18 +00:00
|
|
|
if (requestFirstResponder)
|
|
|
|
[self.entryField becomeFirstResponder];
|
2014-09-11 04:26:01 +00:00
|
|
|
|
|
|
|
// Set avatar modes.
|
|
|
|
MPAvatarCell *selectedAvatar = [self selectedAvatar];
|
|
|
|
for (NSIndexPath *indexPath in [self.avatarCollectionView indexPathsForVisibleItems]) {
|
|
|
|
MPAvatarCell *avatarCell = (MPAvatarCell *)[self.avatarCollectionView cellForItemAtIndexPath:indexPath];
|
|
|
|
[self updateModeForAvatar:avatarCell atIndexPath:indexPath animated:animated];
|
|
|
|
[self updateVisibilityForAvatar:avatarCell atIndexPath:indexPath animated:animated];
|
|
|
|
|
|
|
|
if (selectedAvatar && avatarCell == selectedAvatar)
|
|
|
|
[self.avatarCollectionView scrollToItemAtIndexPath:indexPath
|
|
|
|
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
|
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
- (IBAction)changeAvatar:(UIButton *)sender {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
if (sender == self.previousAvatarButton)
|
|
|
|
--[self selectedAvatar].avatar;
|
|
|
|
if (sender == self.nextAvatarButton)
|
|
|
|
++[self selectedAvatar].avatar;
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|