bd37f1d6a7
[UPDATED] Make private moc parent of all private blocks to avoid blocking the main thread for writes, update the main moc on private moc updates. [FIXED] Don't cause a crash on elements with a bad type. [UPDATED] Improved cell handling and UI update handling. [UPDATED] Replace FontReplacer with moarfonts to fix issues in UICollectionViewCells.
140 lines
4.3 KiB
Objective-C
140 lines
4.3 KiB
Objective-C
/**
|
|
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
|
|
*
|
|
* See the enclosed file LICENSE for license information (LGPLv3). If you did
|
|
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
*
|
|
* @author Maarten Billemont <lhunath@lyndir.com>
|
|
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
*/
|
|
|
|
//
|
|
// MPPasswordLargeGeneratedCell.h
|
|
// MPPasswordLargeGeneratedCell
|
|
//
|
|
// Created by lhunath on 2014-03-19.
|
|
// Copyright, lhunath (Maarten Billemont) 2014. All rights reserved.
|
|
//
|
|
|
|
#import "MPPasswordLargeGeneratedCell.h"
|
|
#import "MPiOSAppDelegate.h"
|
|
#import "MPAppDelegate_Store.h"
|
|
#import "MPPasswordElementCell.h"
|
|
|
|
@implementation MPPasswordLargeGeneratedCell
|
|
|
|
- (void)awakeFromNib {
|
|
|
|
[super awakeFromNib];
|
|
|
|
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
|
|
initWithTarget:self action:@selector(doResetCounterRecognizer:)];
|
|
[self.counterButton addGestureRecognizer:gestureRecognizer];
|
|
}
|
|
|
|
- (void)reloadWithElement:(MPElementEntity *)mainElement {
|
|
|
|
[super reloadWithElement:mainElement];
|
|
|
|
MPElementGeneratedEntity *generatedElement = [self generatedElement:mainElement];
|
|
if (generatedElement)
|
|
self.counterLabel.text = strf( @"%lu", (unsigned long)generatedElement.counter );
|
|
else
|
|
self.counterLabel.text = @"1";
|
|
|
|
if (!mainElement || mainElement.requiresExplicitMigration) {
|
|
self.counterLabel.alpha = 0;
|
|
self.counterButton.alpha = 0;
|
|
}
|
|
else {
|
|
self.counterLabel.alpha = 1;
|
|
self.counterButton.alpha = 1;
|
|
}
|
|
}
|
|
|
|
- (void)resolveContentOfCellTypeForTransientSite:(NSString *)siteName usingKey:(MPKey *)key result:(void (^)(NSString *))resultBlock {
|
|
|
|
PearlNotMainQueue( ^{
|
|
resultBlock( [MPAlgorithmDefault generateContentNamed:siteName ofType:self.type withCounter:1 usingKey:key] );
|
|
} );
|
|
}
|
|
|
|
- (void)resolveContentOfCellTypeForElement:(MPElementEntity *)element usingKey:(MPKey *)key result:(void (^)(NSString *))resultBlock {
|
|
|
|
id<MPAlgorithm> algorithm = element.algorithm;
|
|
NSString *siteName = element.name;
|
|
|
|
PearlNotMainQueue( ^{
|
|
resultBlock( [algorithm generateContentNamed:siteName ofType:self.type withCounter:1 usingKey:key] );
|
|
} );
|
|
}
|
|
|
|
- (MPElementEntity *)saveContentTypeWithElement:(MPElementEntity *)element saveInContext:(NSManagedObjectContext *)context {
|
|
|
|
element = [super saveContentTypeWithElement:element saveInContext:context];
|
|
|
|
MPElementGeneratedEntity *generatedElement = [self generatedElement:element];
|
|
if (generatedElement) {
|
|
generatedElement.counter = [self.counterLabel.text intValue];
|
|
[context saveToStore];
|
|
}
|
|
|
|
return element;
|
|
}
|
|
|
|
#pragma mark - Actions
|
|
|
|
- (IBAction)doIncrementCounter:(UIButton *)sender {
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
MPElementGeneratedEntity *generatedElement = [self generatedElementInContext:context];
|
|
if (!generatedElement)
|
|
return;
|
|
|
|
++generatedElement.counter;
|
|
[context saveToStore];
|
|
|
|
PearlMainQueue( ^{
|
|
[self updateAnimated:YES];
|
|
[PearlOverlay showTemporaryOverlayWithTitle:@"Counter Incremented" dismissAfter:2];
|
|
} );
|
|
}];
|
|
}
|
|
|
|
- (void)doResetCounterRecognizer:(UILongPressGestureRecognizer *)gestureRecognizer {
|
|
|
|
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
|
|
return;
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
MPElementGeneratedEntity *generatedElement = [self generatedElementInContext:context];
|
|
if (!generatedElement)
|
|
return;
|
|
|
|
generatedElement.counter = 1;
|
|
[context saveToStore];
|
|
|
|
PearlMainQueue( ^{
|
|
[self updateAnimated:YES];
|
|
[PearlOverlay showTemporaryOverlayWithTitle:@"Counter Reset" dismissAfter:2];
|
|
} );
|
|
}];
|
|
}
|
|
|
|
#pragma mark - Properties
|
|
|
|
- (MPElementGeneratedEntity *)generatedElementInContext:(NSManagedObjectContext *)context {
|
|
|
|
return [self generatedElement:[[MPPasswordElementCell findAsSuperviewOf:self] elementInContext:context]];
|
|
}
|
|
|
|
- (MPElementGeneratedEntity *)generatedElement:(MPElementEntity *)element {
|
|
|
|
if (![element isKindOfClass:[MPElementGeneratedEntity class]])
|
|
return nil;
|
|
|
|
return (MPElementGeneratedEntity *)element;
|
|
}
|
|
|
|
@end
|