2012-06-04 11:27:02 +02:00
|
|
|
//
|
|
|
|
// MPPreferencesViewController.m
|
|
|
|
// MasterPassword-iOS
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 04/06/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPPreferencesViewController.h"
|
2013-04-24 21:26:04 -04:00
|
|
|
#import "MPiOSAppDelegate.h"
|
2012-06-08 00:40:30 +02:00
|
|
|
#import "MPAppDelegate_Key.h"
|
2012-06-11 16:15:49 +02:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2014-04-20 11:09:49 -04:00
|
|
|
#import "UIColor+Expanded.h"
|
2014-04-24 21:43:47 -04:00
|
|
|
#import "MPPasswordsViewController.h"
|
|
|
|
#import "MPCoachmarkViewController.h"
|
2012-06-04 11:27:02 +02:00
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@interface MPPreferencesViewController()
|
2012-06-04 11:27:02 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPPreferencesViewController
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-04 11:27:02 +02:00
|
|
|
[super viewDidLoad];
|
2014-04-20 11:09:49 -04:00
|
|
|
|
|
|
|
self.view.backgroundColor = [UIColor clearColor];
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-26 14:03:44 -04:00
|
|
|
inf( @"Preferences will appear" );
|
2014-04-20 11:09:49 -04:00
|
|
|
[super viewWillAppear:animated];
|
2012-06-06 00:59:09 +02:00
|
|
|
|
2013-06-16 12:39:52 -04:00
|
|
|
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserForMainThread];
|
2014-04-25 20:02:58 -04:00
|
|
|
self.generatedTypeControl.selectedSegmentIndex = [self generatedSegmentIndexForType:activeUser.defaultType];
|
|
|
|
self.storedTypeControl.selectedSegmentIndex = [self storedSegmentIndexForType:activeUser.defaultType];
|
2014-04-20 11:09:49 -04:00
|
|
|
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%ld", (long)activeUser.avatar )];
|
2013-04-17 22:00:15 -04:00
|
|
|
self.savePasswordSwitch.on = activeUser.saveKey;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
self.tableView.contentInset = UIEdgeInsetsMake( 64, 0, 49, 0 );
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
#pragma mark - UITableViewDelegate
|
2013-04-29 21:15:14 -04:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2013-04-29 21:15:14 -04:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
|
|
|
|
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
|
|
|
|
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRGBAHex:0x78DDFB33];
|
2013-04-29 21:15:14 -04:00
|
|
|
}
|
2013-04-20 14:11:19 -04:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
return cell;
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 00:59:09 +02:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-06 00:59:09 +02:00
|
|
|
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
|
2014-05-17 20:12:59 -04:00
|
|
|
if (cell == self.signOutCell) {
|
|
|
|
MPPasswordsViewController *passwordsVC = [self dismissPopup];
|
|
|
|
[[MPiOSAppDelegate get] signOutAnimated:YES];
|
|
|
|
}
|
2014-04-24 21:43:47 -04:00
|
|
|
if (cell == self.feedbackCell)
|
|
|
|
[[MPiOSAppDelegate get] showFeedbackWithLogs:YES forVC:self];
|
2012-06-06 00:59:09 +02:00
|
|
|
if (cell == self.exportCell)
|
2014-04-24 21:43:47 -04:00
|
|
|
[[MPiOSAppDelegate get] showExportForVC:self];
|
|
|
|
if (cell == self.coachmarksCell) {
|
2014-05-17 20:12:59 -04:00
|
|
|
MPPasswordsViewController *passwordsVC = [self dismissPopup];
|
|
|
|
passwordsVC.coachmark.coached = NO;
|
|
|
|
[passwordsVC performSegueWithIdentifier:@"coachmarks" sender:self];
|
2014-04-24 21:43:47 -04:00
|
|
|
}
|
2014-04-26 14:03:44 -04:00
|
|
|
if (cell == self.checkInconsistencies)
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
if ([[MPiOSAppDelegate get] findAndFixInconsistenciesSaveInContext:context] == MPFixableResultNoProblems)
|
|
|
|
[PearlAlert showAlertWithTitle:@"No Inconsistencies" message:
|
|
|
|
@"No inconsistencies were detected in your sites."
|
|
|
|
viewStyle:UIAlertViewStyleDefault initAlert:nil
|
|
|
|
tappedButtonBlock:nil cancelTitle:[PearlStrings get].commonButtonOkay otherTitles:nil];
|
|
|
|
}];
|
2012-06-06 00:59:09 +02:00
|
|
|
|
2012-06-06 22:38:43 +02:00
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
2012-06-06 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
#pragma mark - IBActions
|
2012-06-11 16:15:49 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
- (IBAction)valueChanged:(id)sender {
|
2012-06-11 16:15:49 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
if (sender == self.savePasswordSwitch)
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
|
|
|
|
if ((activeUser.saveKey = self.savePasswordSwitch.on))
|
|
|
|
[[MPiOSAppDelegate get] storeSavedKeyFor:activeUser];
|
|
|
|
else
|
|
|
|
[[MPiOSAppDelegate get] forgetSavedKeyFor:activeUser];
|
|
|
|
[context saveToStore];
|
|
|
|
}];
|
|
|
|
|
2014-04-25 20:02:58 -04:00
|
|
|
if (sender == self.generatedTypeControl || sender == self.storedTypeControl) {
|
|
|
|
if (sender == self.generatedTypeControl)
|
|
|
|
self.storedTypeControl.selectedSegmentIndex = -1;
|
|
|
|
else if (sender == self.storedTypeControl)
|
|
|
|
self.generatedTypeControl.selectedSegmentIndex = -1;
|
2014-04-26 14:03:44 -04:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-04-25 20:02:58 -04:00
|
|
|
MPElementType defaultType = [[MPiOSAppDelegate get] activeUserInContext:context].defaultType = [self typeForSelectedSegment];
|
2014-04-20 11:09:49 -04:00
|
|
|
[context saveToStore];
|
2014-04-25 20:02:58 -04:00
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.generatedTypeControl.selectedSegmentIndex = [self generatedSegmentIndexForType:defaultType];
|
|
|
|
self.storedTypeControl.selectedSegmentIndex = [self storedSegmentIndexForType:defaultType];
|
|
|
|
} );
|
2014-04-20 11:09:49 -04:00
|
|
|
}];
|
2014-04-25 20:02:58 -04:00
|
|
|
}
|
2014-04-20 11:09:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)previousAvatar:(id)sender {
|
2012-06-11 16:15:49 +02:00
|
|
|
|
2013-05-15 22:42:21 -04:00
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
|
2014-04-20 11:09:49 -04:00
|
|
|
activeUser.avatar = (activeUser.avatar - 1 + MPAvatarCount) % MPAvatarCount;
|
2013-05-15 22:42:21 -04:00
|
|
|
[context saveToStore];
|
2014-04-20 11:09:49 -04:00
|
|
|
|
|
|
|
long avatar = activeUser.avatar;
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%ld", avatar )];
|
|
|
|
} );
|
2013-05-15 22:42:21 -04:00
|
|
|
}];
|
2012-06-11 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
- (IBAction)nextAvatar:(id)sender {
|
2012-06-11 16:15:49 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
|
|
|
|
activeUser.avatar = (activeUser.avatar + 1 + MPAvatarCount) % MPAvatarCount;
|
|
|
|
[context saveToStore];
|
2012-06-11 16:15:49 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
long avatar = activeUser.avatar;
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%ld", avatar )];
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}
|
2012-06-06 00:59:09 +02:00
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2014-05-17 20:12:59 -04:00
|
|
|
- (MPPasswordsViewController *)dismissPopup {
|
|
|
|
|
|
|
|
for (UIViewController *vc = self; (vc = vc.parentViewController);)
|
|
|
|
if ([vc isKindOfClass:[MPPasswordsViewController class]]) {
|
|
|
|
MPPasswordsViewController *passwordsVC = (MPPasswordsViewController *)vc;
|
|
|
|
[passwordsVC dismissPopdown:self];
|
|
|
|
return passwordsVC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-04-20 11:09:49 -04:00
|
|
|
- (enum MPElementType)typeForSelectedSegment {
|
|
|
|
|
2014-04-25 20:02:58 -04:00
|
|
|
NSInteger selectedGeneratedIndex = self.generatedTypeControl.selectedSegmentIndex;
|
|
|
|
NSInteger selectedStoredIndex = self.storedTypeControl.selectedSegmentIndex;
|
|
|
|
|
|
|
|
switch (selectedGeneratedIndex) {
|
2014-04-20 11:09:49 -04:00
|
|
|
case 0:
|
|
|
|
return MPElementTypeGeneratedMaximum;
|
|
|
|
case 1:
|
|
|
|
return MPElementTypeGeneratedLong;
|
|
|
|
case 2:
|
|
|
|
return MPElementTypeGeneratedMedium;
|
|
|
|
case 3:
|
|
|
|
return MPElementTypeGeneratedBasic;
|
|
|
|
case 4:
|
|
|
|
return MPElementTypeGeneratedShort;
|
|
|
|
case 5:
|
|
|
|
return MPElementTypeGeneratedPIN;
|
|
|
|
default:
|
2014-04-25 20:02:58 -04:00
|
|
|
|
|
|
|
switch (selectedStoredIndex) {
|
|
|
|
case 0:
|
|
|
|
return MPElementTypeStoredPersonal;
|
|
|
|
case 1:
|
|
|
|
return MPElementTypeStoredDevicePrivate;
|
|
|
|
default:
|
2014-05-17 20:12:59 -04:00
|
|
|
Throw( @"unsupported selected type index: generated=%ld, stored=%ld", (long)selectedGeneratedIndex,
|
|
|
|
(long)selectedStoredIndex );
|
2014-04-25 20:02:58 -04:00
|
|
|
}
|
2014-04-20 11:09:49 -04:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-25 20:02:58 -04:00
|
|
|
- (NSInteger)generatedSegmentIndexForType:(MPElementType)type {
|
2014-04-20 11:09:49 -04:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MPElementTypeGeneratedMaximum:
|
|
|
|
return 0;
|
|
|
|
case MPElementTypeGeneratedLong:
|
|
|
|
return 1;
|
|
|
|
case MPElementTypeGeneratedMedium:
|
|
|
|
return 2;
|
|
|
|
case MPElementTypeGeneratedBasic:
|
|
|
|
return 3;
|
|
|
|
case MPElementTypeGeneratedShort:
|
|
|
|
return 4;
|
|
|
|
case MPElementTypeGeneratedPIN:
|
|
|
|
return 5;
|
|
|
|
default:
|
2014-04-25 20:02:58 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)storedSegmentIndexForType:(MPElementType)type {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MPElementTypeStoredPersonal:
|
|
|
|
return 0;
|
|
|
|
case MPElementTypeStoredDevicePrivate:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return -1;
|
2014-04-20 11:09:49 -04:00
|
|
|
}
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
2012-06-06 00:59:09 +02:00
|
|
|
|
2012-06-04 11:27:02 +02:00
|
|
|
@end
|