2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-09-17 05:34:58 +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-09-17 05:34:58 +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-09-17 05:34:58 +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-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
#import "MPStoreViewController.h"
|
|
|
|
#import "MPiOSAppDelegate.h"
|
|
|
|
#import "UIColor+Expanded.h"
|
2014-09-21 14:29:18 +00:00
|
|
|
#import "MPAppDelegate_InApp.h"
|
2014-09-29 02:15:55 +00:00
|
|
|
#import "MPPasswordsViewController.h"
|
2014-09-17 05:34:58 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
PearlEnum( MPDevelopmentFuelConsumption,
|
|
|
|
MPDevelopmentFuelConsumptionQuarterly, MPDevelopmentFuelConsumptionMonthly, MPDevelopmentFuelWeekly );
|
|
|
|
|
|
|
|
@interface MPStoreViewController()<MPInAppDelegate>
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
@property(nonatomic, strong) NSNumberFormatter *currencyFormatter;
|
2014-09-26 04:32:07 +00:00
|
|
|
@property(nonatomic, strong) NSArray *products;
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPStoreViewController
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
+ (NSString *)latestStoreFeatures {
|
|
|
|
|
|
|
|
NSMutableString *features = [NSMutableString string];
|
|
|
|
NSArray *storeVersions = @[
|
2016-01-15 05:19:07 +00:00
|
|
|
@"Generated Usernames\nSecurity Question Answers",
|
|
|
|
@"TouchID Support"
|
2014-09-29 02:15:55 +00:00
|
|
|
];
|
|
|
|
NSInteger storeVersion = [[NSUserDefaults standardUserDefaults] integerForKey:@"storeVersion"];
|
|
|
|
for (; storeVersion < [storeVersions count]; ++storeVersion)
|
|
|
|
[features appendFormat:@"%@\n", storeVersions[storeVersion]];
|
|
|
|
if (![features length])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger:storeVersion forKey:@"storeVersion"];
|
2014-10-24 04:35:05 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] synchronize])
|
|
|
|
wrn( @"Couldn't synchronize store version update." );
|
2014-09-29 02:15:55 +00:00
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2014-09-17 05:34:58 +00:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
|
|
self.currencyFormatter = [NSNumberFormatter new];
|
|
|
|
self.currencyFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
self.tableView.tableHeaderView = [UIView new];
|
|
|
|
self.tableView.tableFooterView = [UIView new];
|
2016-01-14 15:03:30 +00:00
|
|
|
self.tableView.rowHeight = UITableViewAutomaticDimension;
|
|
|
|
self.tableView.estimatedRowHeight = 400;
|
2014-09-17 05:34:58 +00:00
|
|
|
self.view.backgroundColor = [UIColor clearColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
self.tableView.contentInset = UIEdgeInsetsMake( 64, 0, 49, 0 );
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
[self reloadCellsHiding:self.allCellsBySection[0] showing:@[ self.loadingCell ]];
|
2014-09-17 05:34:58 +00:00
|
|
|
[self.allCellsBySection[0] enumerateObjectsUsingBlock:^(MPStoreProductCell *cell, NSUInteger idx, BOOL *stop) {
|
|
|
|
if ([cell isKindOfClass:[MPStoreProductCell class]]) {
|
|
|
|
cell.purchasedIndicator.alpha = 0;
|
|
|
|
[cell.activityIndicator stopAnimating];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
PearlAddNotificationObserver( NSUserDefaultsDidChangeNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPStoreViewController *self, NSNotification *note) {
|
2014-09-26 04:32:07 +00:00
|
|
|
[self updateProducts];
|
|
|
|
[self updateFuel];
|
2014-09-27 05:27:05 +00:00
|
|
|
} );
|
2014-09-26 04:32:07 +00:00
|
|
|
[[MPiOSAppDelegate get] registerProductsObserver:self];
|
|
|
|
[self updateFuel];
|
2014-09-17 05:34:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
|
|
|
PearlRemoveNotificationObservers();
|
|
|
|
}
|
|
|
|
|
2014-09-30 12:20:35 +00:00
|
|
|
#pragma mark - UITableViewDataSource
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
- (MPStoreProductCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
MPStoreProductCell *cell = (MPStoreProductCell *)[super tableView:tableView cellForRowAtIndexPath:indexPath];
|
2014-09-21 14:29:18 +00:00
|
|
|
if (indexPath.section == 0)
|
2014-09-26 04:32:07 +00:00
|
|
|
cell.selectionStyle = [[MPiOSAppDelegate get] isFeatureUnlocked:[self productForCell:cell].productIdentifier]?
|
2014-09-29 02:15:55 +00:00
|
|
|
UITableViewCellSelectionStyleNone: UITableViewCellSelectionStyleDefault;
|
2014-09-21 14:29:18 +00:00
|
|
|
|
2014-09-17 05:34:58 +00:00
|
|
|
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
|
|
|
|
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
|
|
|
|
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRGBAHex:0x78DDFB33];
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2014-09-30 12:20:35 +00:00
|
|
|
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2016-01-14 15:03:30 +00:00
|
|
|
return UITableViewAutomaticDimension;
|
2014-09-24 05:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 05:34:58 +00:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
MPStoreProductCell *cell = (MPStoreProductCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
if (cell.selectionStyle == UITableViewCellSelectionStyleNone)
|
2014-09-21 14:29:18 +00:00
|
|
|
return;
|
|
|
|
|
2014-09-17 05:34:58 +00:00
|
|
|
SKProduct *product = [self productForCell:cell];
|
2014-09-29 02:15:55 +00:00
|
|
|
if (product && ![[MPAppDelegate_Shared get] isFeatureUnlocked:product.productIdentifier])
|
2014-09-26 04:32:07 +00:00
|
|
|
[[MPAppDelegate_Shared get] purchaseProductWithIdentifier:product.productIdentifier
|
|
|
|
quantity:[self quantityForProductIdentifier:product.productIdentifier]];
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
}
|
|
|
|
|
2014-09-18 00:50:27 +00:00
|
|
|
#pragma mark - Actions
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (IBAction)toggleFuelConsumption:(id)sender {
|
|
|
|
|
|
|
|
NSUInteger fuelConsumption = [[MPiOSConfig get].developmentFuelConsumption unsignedIntegerValue];
|
|
|
|
[MPiOSConfig get].developmentFuelConsumption = @((fuelConsumption + 1) % MPDevelopmentFuelConsumptionCount);
|
|
|
|
[self updateProducts];
|
|
|
|
}
|
|
|
|
|
2014-09-18 00:50:27 +00:00
|
|
|
- (IBAction)restorePurchases:(id)sender {
|
|
|
|
|
|
|
|
[PearlAlert showAlertWithTitle:@"Restore Previous Purchases" message:
|
|
|
|
@"This will check with Apple to find and activate any purchases you made from other devices."
|
|
|
|
viewStyle:UIAlertViewStyleDefault initAlert:nil
|
|
|
|
tappedButtonBlock:^(UIAlertView *alert, NSInteger buttonIndex) {
|
|
|
|
if (buttonIndex == [alert cancelButtonIndex])
|
|
|
|
return;
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
[[MPAppDelegate_Shared get] restoreCompletedTransactions];
|
2014-09-18 00:50:27 +00:00
|
|
|
} cancelTitle:@"Cancel" otherTitles:@"Find Purchases", nil];
|
|
|
|
}
|
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
- (IBAction)sendThanks:(id)sender {
|
|
|
|
|
|
|
|
[[self dismissPopup].navigationController performSegueWithIdentifier:@"web" sender:
|
|
|
|
[NSURL URLWithString:@"http://thanks.lhunath.com"]];
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
#pragma mark - MPInAppDelegate
|
|
|
|
|
|
|
|
- (void)updateWithProducts:(NSArray *)products {
|
|
|
|
|
|
|
|
self.products = products;
|
|
|
|
|
|
|
|
[self updateProducts];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateWithTransaction:(SKPaymentTransaction *)transaction {
|
|
|
|
|
|
|
|
MPStoreProductCell *cell = [self cellForProductIdentifier:transaction.payment.productIdentifier];
|
|
|
|
if (!cell)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (transaction.transactionState) {
|
|
|
|
case SKPaymentTransactionStatePurchasing:
|
|
|
|
[cell.activityIndicator startAnimating];
|
|
|
|
break;
|
|
|
|
case SKPaymentTransactionStatePurchased:
|
|
|
|
[cell.activityIndicator stopAnimating];
|
|
|
|
break;
|
|
|
|
case SKPaymentTransactionStateFailed:
|
|
|
|
[cell.activityIndicator stopAnimating];
|
|
|
|
break;
|
|
|
|
case SKPaymentTransactionStateRestored:
|
|
|
|
[cell.activityIndicator stopAnimating];
|
|
|
|
break;
|
|
|
|
case SKPaymentTransactionStateDeferred:
|
|
|
|
[cell.activityIndicator startAnimating];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 05:34:58 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2014-09-29 02:15:55 +00: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-09-17 05:34:58 +00:00
|
|
|
- (SKProduct *)productForCell:(MPStoreProductCell *)cell {
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
for (SKProduct *product in self.products)
|
2014-09-17 05:34:58 +00:00
|
|
|
if ([self cellForProductIdentifier:product.productIdentifier] == cell)
|
|
|
|
return product;
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MPStoreProductCell *)cellForProductIdentifier:(NSString *)productIdentifier {
|
|
|
|
|
|
|
|
if ([productIdentifier isEqualToString:MPProductGenerateLogins])
|
|
|
|
return self.generateLoginCell;
|
2014-09-21 16:54:48 +00:00
|
|
|
if ([productIdentifier isEqualToString:MPProductGenerateAnswers])
|
|
|
|
return self.generateAnswersCell;
|
2014-09-29 02:15:55 +00:00
|
|
|
if ([productIdentifier isEqualToString:MPProductOSIntegration])
|
|
|
|
return self.iOSIntegrationCell;
|
|
|
|
if ([productIdentifier isEqualToString:MPProductTouchID])
|
|
|
|
return self.touchIDCell;
|
2014-09-26 04:32:07 +00:00
|
|
|
if ([productIdentifier isEqualToString:MPProductFuel])
|
|
|
|
return self.fuelCell;
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (void)updateProducts {
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
NSMutableArray *showCells = [NSMutableArray array];
|
|
|
|
NSMutableArray *hideCells = [NSMutableArray array];
|
2017-04-14 20:10:11 +00:00
|
|
|
[hideCells addObjectsFromArray:[self.allCellsBySection[0] array]];
|
2014-09-29 02:15:55 +00:00
|
|
|
[hideCells addObject:self.loadingCell];
|
2014-09-17 05:34:58 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
for (SKProduct *product in self.products) {
|
2014-09-21 14:39:09 +00:00
|
|
|
[self showCellForProductWithIdentifier:MPProductGenerateLogins ifProduct:product showingCells:showCells];
|
2014-09-21 16:54:48 +00:00
|
|
|
[self showCellForProductWithIdentifier:MPProductGenerateAnswers ifProduct:product showingCells:showCells];
|
2014-09-29 02:15:55 +00:00
|
|
|
[self showCellForProductWithIdentifier:MPProductOSIntegration ifProduct:product showingCells:showCells];
|
|
|
|
[self showCellForProductWithIdentifier:MPProductTouchID ifProduct:product showingCells:showCells];
|
2014-09-26 04:32:07 +00:00
|
|
|
[self showCellForProductWithIdentifier:MPProductFuel ifProduct:product showingCells:showCells];
|
2014-09-17 05:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[hideCells removeObjectsInArray:showCells];
|
2014-09-29 02:15:55 +00:00
|
|
|
[self reloadCellsHiding:hideCells showing:showCells];
|
2014-09-17 05:34:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (void)updateFuel {
|
|
|
|
|
|
|
|
CGFloat weeklyFuelConsumption = [self weeklyFuelConsumption]; /* consume x fuel / week */
|
2014-09-30 04:01:33 +00:00
|
|
|
CGFloat fuelRemaining = [[MPiOSConfig get].developmentFuelRemaining floatValue]; /* x fuel left */
|
|
|
|
CGFloat fuelInvested = [[MPiOSConfig get].developmentFuelInvested floatValue]; /* x fuel left */
|
2014-09-28 14:05:36 +00:00
|
|
|
NSDate *now = [NSDate date];
|
2014-09-29 02:15:55 +00:00
|
|
|
NSTimeInterval fuelSecondsElapsed = -[[MPiOSConfig get].developmentFuelChecked timeIntervalSinceDate:now];
|
2014-09-28 14:05:36 +00:00
|
|
|
if (fuelSecondsElapsed > 3600 || ![MPiOSConfig get].developmentFuelChecked) {
|
2014-09-26 04:32:07 +00:00
|
|
|
NSTimeInterval weeksElapsed = fuelSecondsElapsed / (3600 * 24 * 7 /* 1 week */); /* x weeks elapsed */
|
2014-09-30 04:01:33 +00:00
|
|
|
NSTimeInterval fuelConsumed = weeklyFuelConsumption * weeksElapsed;
|
|
|
|
fuelRemaining -= fuelConsumed;
|
|
|
|
fuelInvested += fuelConsumed;
|
2014-09-28 14:05:36 +00:00
|
|
|
[MPiOSConfig get].developmentFuelChecked = now;
|
2014-09-30 04:01:33 +00:00
|
|
|
[MPiOSConfig get].developmentFuelRemaining = @(fuelRemaining);
|
|
|
|
[MPiOSConfig get].developmentFuelInvested = @(fuelInvested);
|
2014-09-26 04:32:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 04:01:33 +00:00
|
|
|
CGFloat fuelRatio = weeklyFuelConsumption == 0? 0: fuelRemaining / weeklyFuelConsumption; /* x weeks worth of fuel left */
|
2014-09-27 05:27:05 +00:00
|
|
|
[self.fuelMeterConstraint updateConstant:MIN( 0.5f, fuelRatio - 0.5f ) * 160]; /* -80pt = 0 weeks left, 80pt = >=1 week left */
|
2014-09-30 04:01:33 +00:00
|
|
|
self.fuelStatusLabel.text = strf( @"fuel left: %0.1f work hours\ninvested: %0.1f work hours", fuelRemaining, fuelInvested );
|
|
|
|
self.fuelStatusLabel.hidden = (fuelRemaining + fuelInvested) == 0;
|
2014-09-26 04:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)weeklyFuelConsumption {
|
|
|
|
|
|
|
|
switch ((MPDevelopmentFuelConsumption)[[MPiOSConfig get].developmentFuelConsumption unsignedIntegerValue]) {
|
|
|
|
case MPDevelopmentFuelConsumptionQuarterly:
|
|
|
|
[self.fuelSpeedButton setTitle:@"1h / quarter" forState:UIControlStateNormal];
|
|
|
|
return 1.f / 12 /* 12 weeks */;
|
|
|
|
case MPDevelopmentFuelConsumptionMonthly:
|
|
|
|
[self.fuelSpeedButton setTitle:@"1h / month" forState:UIControlStateNormal];
|
|
|
|
return 1.f / 4 /* 4 weeks */;
|
|
|
|
case MPDevelopmentFuelWeekly:
|
|
|
|
[self.fuelSpeedButton setTitle:@"1h / week" forState:UIControlStateNormal];
|
|
|
|
return 1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)showCellForProductWithIdentifier:(NSString *)productIdentifier ifProduct:(SKProduct *)product
|
|
|
|
showingCells:(NSMutableArray *)showCells {
|
2014-09-17 05:34:58 +00:00
|
|
|
|
|
|
|
if (![product.productIdentifier isEqualToString:productIdentifier])
|
|
|
|
return;
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
MPStoreProductCell *cell = [self cellForProductIdentifier:productIdentifier];
|
2014-09-17 05:34:58 +00:00
|
|
|
[showCells addObject:cell];
|
|
|
|
|
|
|
|
self.currencyFormatter.locale = product.priceLocale;
|
2014-09-26 04:32:07 +00:00
|
|
|
BOOL purchased = [[MPiOSAppDelegate get] isFeatureUnlocked:productIdentifier];
|
|
|
|
NSInteger quantity = [self quantityForProductIdentifier:productIdentifier];
|
|
|
|
cell.priceLabel.text = purchased? @"": [self.currencyFormatter stringFromNumber:@([product.price floatValue] * quantity)];
|
2014-09-18 00:50:27 +00:00
|
|
|
cell.purchasedIndicator.alpha = purchased? 1: 0;
|
2014-09-17 05:34:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (NSInteger)quantityForProductIdentifier:(NSString *)productIdentifier {
|
|
|
|
|
|
|
|
if ([productIdentifier isEqualToString:MPProductFuel])
|
2014-09-30 04:01:33 +00:00
|
|
|
return (NSInteger)(MP_FUEL_HOURLY_RATE * [self weeklyFuelConsumption] + .5f);
|
2014-09-26 04:32:07 +00:00
|
|
|
|
|
|
|
return 1;
|
2014-09-17 05:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPStoreProductCell
|
|
|
|
@end
|