2
0
MasterPassword/platform-darwin/Source/iOS/MPPreferencesViewController.m

330 lines
12 KiB
Mathematica
Raw Normal View History

2017-04-05 20:56:22 +00:00
//==============================================================================
// This file is part of Master Password.
// Copyright (c) 2011-2017, Maarten Billemont.
//
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.
//
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.
//
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/>.
//==============================================================================
#import "MPPreferencesViewController.h"
#import "MPiOSAppDelegate.h"
2012-06-07 22:40:30 +00:00
#import "MPAppDelegate_Key.h"
#import "MPAppDelegate_Store.h"
#import "UIColor+Expanded.h"
2017-05-07 22:36:01 +00:00
#import "MPSitesViewController.h"
#import "MPAppDelegate_InApp.h"
2013-04-20 18:11:19 +00:00
@interface MPPreferencesViewController()
@end
@implementation MPPreferencesViewController
- (void)viewDidLoad {
2012-06-08 21:46:13 +00:00
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;
self.tableView.contentInset = UIEdgeInsetsMake( 64, 0, 49, 0 );
}
- (void)viewWillAppear:(BOOL)animated {
2012-06-08 21:46:13 +00:00
inf( @"Preferences will appear" );
[super viewWillAppear:animated];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tipped.passwordsPreferences"];
if (![[NSUserDefaults standardUserDefaults] synchronize])
wrn( @"Couldn't synchronize after preferences appearance." );
2016-01-14 14:58:04 +00:00
[self reload];
}
- (void)reload {
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserForMainThread];
2016-01-14 14:58:04 +00:00
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%lu", (unsigned long)activeUser.avatar )];
self.savePasswordSwitch.on = activeUser.saveKey;
self.touchIDSwitch.on = activeUser.touchID;
2016-01-14 16:22:15 +00:00
self.touchIDSwitch.enabled = self.savePasswordSwitch.on && [[MPiOSAppDelegate get] isFeatureUnlocked:MPProductTouchID];
MPResultType defaultType = activeUser.defaultType;
self.generated1TypeControl.selectedSegmentIndex = [self generated1SegmentIndexForType:defaultType];
self.generated2TypeControl.selectedSegmentIndex = [self generated2SegmentIndexForType:defaultType];
self.storedTypeControl.selectedSegmentIndex = [self storedSegmentIndexForType:defaultType];
PearlNotMainQueue( ^{
NSString *examplePassword = nil;
if (defaultType & MPResultTypeClassTemplate)
examplePassword = [MPAlgorithmDefault mpwTemplateForSiteNamed:@"test" ofType:defaultType
withCounter:1 usingKey:[MPiOSAppDelegate get].key];
PearlMainQueue( ^{
2017-05-07 22:36:01 +00:00
self.typeSamplePassword.text = [examplePassword length]? [NSString stringWithFormat:@"eg. %@", examplePassword]: nil;
} );
} );
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
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-20 18:11:19 +00:00
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2012-06-08 21:46:13 +00:00
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell == self.signOutCell) {
[self dismissPopup];
[[MPiOSAppDelegate get] signOutAnimated:YES];
}
if (cell == self.feedbackCell)
[[MPiOSAppDelegate get] showFeedbackWithLogs:YES forVC:self];
if (cell == self.exportCell)
[[MPiOSAppDelegate get] showExportForVC:self];
if (cell == self.showHelpCell) {
2017-05-07 22:36:01 +00:00
MPSitesViewController *passwordsVC = [self dismissPopup];
[passwordsVC performSegueWithIdentifier:@"guide" sender:self];
}
if (cell == self.checkInconsistencies)
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
if ([[MPiOSAppDelegate get] findAndFixInconsistenciesSaveInContext:context] == MPFixableResultNoProblems)
[PearlAlert showAlertWithTitle:@"No Inconsistencies" message:
2017-04-01 04:30:25 +00:00
@"No inconsistencies were detected in your sites."
viewStyle:UIAlertViewStyleDefault initAlert:nil
tappedButtonBlock:nil cancelTitle:[PearlStrings get].commonButtonOkay otherTitles:nil];
}];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - IBActions
- (IBAction)valueChanged:(id)sender {
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];
2016-01-14 14:58:04 +00:00
2017-04-01 04:30:25 +00:00
PearlMainQueue( ^{
2016-01-14 14:58:04 +00:00
[self reload];
2017-04-01 04:30:25 +00:00
} );
}];
if (sender == self.touchIDSwitch)
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
if ((activeUser.touchID = self.touchIDSwitch.on))
[[MPiOSAppDelegate get] storeSavedKeyFor:activeUser];
else
[[MPiOSAppDelegate get] forgetSavedKeyFor:activeUser];
[context saveToStore];
2016-01-14 14:58:04 +00:00
PearlMainQueue( ^{
[self reload];
} );
}];
if (sender == self.generated1TypeControl || sender == self.generated2TypeControl || sender == self.storedTypeControl) {
if (sender != self.generated1TypeControl)
self.generated1TypeControl.selectedSegmentIndex = -1;
if (sender != self.generated2TypeControl)
self.generated2TypeControl.selectedSegmentIndex = -1;
if (sender != self.storedTypeControl)
self.storedTypeControl.selectedSegmentIndex = -1;
MPResultType defaultType = [self typeForSelectedSegment];
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
2016-01-14 14:58:04 +00:00
[[MPiOSAppDelegate get] activeUserInContext:context].defaultType = defaultType;
[context saveToStore];
PearlMainQueue( ^{
2016-01-14 14:58:04 +00:00
[self reload];
} );
}];
}
}
- (IBAction)previousAvatar:(id)sender {
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
activeUser.avatar = (activeUser.avatar - 1 + MPAvatarCount) % MPAvatarCount;
[context saveToStore];
2016-01-14 14:58:04 +00:00
NSUInteger avatar = activeUser.avatar;
PearlMainQueue( ^{
2016-01-14 14:58:04 +00:00
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%lu", (unsigned long)avatar )];
} );
}];
}
- (IBAction)nextAvatar:(id)sender {
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPiOSAppDelegate get] activeUserInContext:context];
activeUser.avatar = (activeUser.avatar + 1 + MPAvatarCount) % MPAvatarCount;
[context saveToStore];
2016-01-14 14:58:04 +00:00
NSUInteger avatar = activeUser.avatar;
PearlMainQueue( ^{
2016-01-14 14:58:04 +00:00
self.avatarImage.image = [UIImage imageNamed:strf( @"avatar-%lu", (unsigned long)avatar )];
} );
}];
}
- (IBAction)homePageButton:(id)sender {
[[self dismissPopup].navigationController performSegueWithIdentifier:@"web" sender:
2018-06-03 20:26:08 +00:00
[NSURL URLWithString:@"https://masterpassword.app"]];
}
- (IBAction)securityButton:(id)sender {
[[self dismissPopup].navigationController performSegueWithIdentifier:@"web" sender:
2018-06-03 20:26:08 +00:00
[NSURL URLWithString:@"https://masterpassword.app/security.html"]];
}
- (IBAction)sourceButton:(id)sender {
[[self dismissPopup].navigationController performSegueWithIdentifier:@"web" sender:
[NSURL URLWithString:@"https://github.com/Lyndir/MasterPassword/"]];
}
- (IBAction)thanksButton:(id)sender {
[[self dismissPopup].navigationController performSegueWithIdentifier:@"web" sender:
[NSURL URLWithString:@"https://thanks.lhunath.com"]];
}
#pragma mark - Private
2017-05-07 22:36:01 +00:00
- (MPSitesViewController *)dismissPopup {
for (UIViewController *vc = self; (vc = vc.parentViewController);)
2017-05-07 22:36:01 +00:00
if ([vc isKindOfClass:[MPSitesViewController class]]) {
MPSitesViewController *passwordsVC = (MPSitesViewController *)vc;
[passwordsVC dismissPopdown:self];
return passwordsVC;
}
return nil;
}
- (MPResultType)typeForSelectedSegment {
NSInteger selectedGenerated1Index = self.generated1TypeControl.selectedSegmentIndex;
NSInteger selectedGenerated2Index = self.generated2TypeControl.selectedSegmentIndex;
NSInteger selectedStoredIndex = self.storedTypeControl.selectedSegmentIndex;
switch (selectedGenerated1Index) {
case 0:
return MPResultTypeTemplatePhrase;
case 1:
return MPResultTypeTemplateName;
default:
2017-05-07 22:36:01 +00:00
switch (selectedGenerated2Index) {
case 0:
return MPResultTypeTemplateMaximum;
case 1:
return MPResultTypeTemplateLong;
2017-05-07 22:36:01 +00:00
case 2:
return MPResultTypeTemplateMedium;
2017-05-07 22:36:01 +00:00
case 3:
return MPResultTypeTemplateBasic;
2017-05-07 22:36:01 +00:00
case 4:
return MPResultTypeTemplateShort;
2017-05-07 22:36:01 +00:00
case 5:
return MPResultTypeTemplatePIN;
default:
2017-05-07 22:36:01 +00:00
switch (selectedStoredIndex) {
case 0:
return MPResultTypeStatefulPersonal;
2017-05-07 22:36:01 +00:00
case 1:
return MPResultTypeStatefulDevice;
2017-05-07 22:36:01 +00:00
default:
Throw( @"unsupported selected type index: generated1=%ld, generated2=%ld, stored=%ld",
(long)selectedGenerated1Index, (long)selectedGenerated2Index, (long)selectedStoredIndex );
}
}
}
}
- (NSInteger)generated1SegmentIndexForType:(MPResultType)type {
switch (type) {
case MPResultTypeTemplatePhrase:
return 0;
case MPResultTypeTemplateName:
return 1;
default:
return -1;
}
}
2012-06-08 21:46:13 +00:00
- (NSInteger)generated2SegmentIndexForType:(MPResultType)type {
switch (type) {
case MPResultTypeTemplateMaximum:
return 0;
case MPResultTypeTemplateLong:
return 1;
case MPResultTypeTemplateMedium:
return 2;
case MPResultTypeTemplateBasic:
return 3;
case MPResultTypeTemplateShort:
return 4;
case MPResultTypeTemplatePIN:
return 5;
default:
return -1;
}
}
- (NSInteger)storedSegmentIndexForType:(MPResultType)type {
switch (type) {
case MPResultTypeStatefulPersonal:
return 0;
case MPResultTypeStatefulDevice:
return 1;
default:
return -1;
}
}
@end