Skip to content

Commit

Permalink
Merge pull request #620 from Simperium/develop
Browse files Browse the repository at this point in the history
Simperium Mk 1.7
  • Loading branch information
jleandroperez committed Mar 8, 2021
2 parents f4ffbb5 + 3a81343 commit c376773
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 65 deletions.
12 changes: 5 additions & 7 deletions Simperium-OSX/SPAuthenticationTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

@interface SPAuthenticationTextField : NSView

@property (assign) id delegate;
@property (strong) NSTextField *textField;
@property (nonatomic, strong, readonly) NSTextField *textField;
@property (nonatomic, weak, readwrite) id<NSTextFieldDelegate> delegate;
@property (nonatomic, copy, readwrite) NSString *stringValue;
@property (nonatomic, copy, readwrite) NSString *placeholderString;
@property (nonatomic, assign, readwrite, getter=isEnabled) BOOL enabled;

- (instancetype)initWithFrame:(NSRect)frame secure:(BOOL)secure;

- (void)setPlaceholderString:(NSString *)string;
- (NSString *)stringValue;
- (void)setStringValue:(NSString *)string;
- (void)setEnabled:(BOOL)enabled;

@end
74 changes: 22 additions & 52 deletions Simperium-OSX/SPAuthenticationTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,9 @@



#pragma mark ====================================================================================
#pragma mark Constants
#pragma mark ====================================================================================

static NSString* SPTextFieldDidBecomeFirstResponder = @"SPTextFieldDidBecomeFirstResponder";


#pragma mark ====================================================================================
#pragma mark Private Helper: SPTextField
#pragma mark ====================================================================================

@interface SPTextField : NSTextField

@end

@implementation SPTextField

- (BOOL)becomeFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:SPTextFieldDidBecomeFirstResponder object:self];
return [super becomeFirstResponder];
}

@end



#pragma mark ====================================================================================
#pragma mark Private Helper: SPSecureTextField
#pragma mark ====================================================================================

@interface SPSecureTextField : NSSecureTextField
@end

@implementation SPSecureTextField

- (BOOL)becomeFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:SPTextFieldDidBecomeFirstResponder object:self];
return [super becomeFirstResponder];
}

@end



#pragma mark - SPAuthenticationTextField

@interface SPAuthenticationTextField()
@property (nonatomic, assign) BOOL isWindowFistResponder;
@property (nonatomic, assign) BOOL secure;
@end

Expand All @@ -83,15 +38,14 @@ - (void)awakeFromNib {
}

- (void)setupInterface:(BOOL)secure {
// Center the textField vertically
NSRect frame = self.frame;
CGFloat paddingX = 10;
CGFloat fontSize = 20;
CGFloat fieldHeight = [[SPAuthenticationConfiguration sharedInstance] regularFontHeightForSize:fontSize];
CGFloat fieldY = (self.frame.size.height - fieldHeight) / 2;
CGRect textFrame = NSMakeRect(paddingX, fieldY, frame.size.width-paddingX*2, fieldHeight);

Class textFieldClass = secure ? [SPSecureTextField class] : [SPTextField class];
Class textFieldClass = secure ? [NSSecureTextField class] : [NSTextField class];
_textField = [[textFieldClass alloc] initWithFrame:textFrame];
NSFont *font = [NSFont fontWithName:[SPAuthenticationConfiguration sharedInstance].regularFontName size:fontSize];
[_textField setFont:font];
Expand All @@ -105,8 +59,8 @@ - (void)setupInterface:(BOOL)secure {
[self addSubview:_textField];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:SPTextFieldDidBecomeFirstResponder object:_textField];
[nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSControlTextDidEndEditingNotification object:_textField];
[nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:NSTextDidBeginEditingNotification object:nil];
[nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSTextDidEndEditingNotification object:nil];
}

- (void)setStringValue:(NSString *)string {
Expand All @@ -121,6 +75,10 @@ - (void)setPlaceholderString:(NSString *)string {
[[_textField cell] setPlaceholderString:string];
}

- (NSString *)placeholderString {
return [[_textField cell] placeholderString];
}

- (void)setDelegate:(id)delegate {
_textField.delegate = delegate;
}
Expand All @@ -134,11 +92,15 @@ - (void)setEnabled:(BOOL)enabled {
[_textField setEditable:enabled];
}

- (BOOL)isEnabled {
return _textField.enabled;
}

- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRect:self.bounds];
[betterBounds addClip];

if (self.isWindowFistResponder) {
if (self.sp_isFirstResponder) {
[[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] setFill];
[betterBounds fill];

Expand All @@ -151,16 +113,24 @@ - (void)drawRect:(NSRect)dirtyRect {
}
}

- (BOOL)sp_isFirstResponder {
NSResponder *responder = [self.window firstResponder];
if (![responder isKindOfClass:[NSText class]]) {
return responder == self.textField;
}

NSText *fieldEditor = (NSText *)responder;
return fieldEditor.delegate == (id<NSTextDelegate>)self.textField;
}


#pragma mark - Notification Helpers

- (void)handleTextFieldDidBeginEditing:(NSNotification *)note {
self.isWindowFistResponder = YES;
[self setNeedsDisplay:YES];
}

- (void)handleTextFieldDidFinishEditing:(NSNotification *)note {
self.isWindowFistResponder = NO;
[self setNeedsDisplay:YES];
}

Expand Down
4 changes: 4 additions & 0 deletions Simperium/SPAuthenticationConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#endif


NS_ASSUME_NONNULL_BEGIN

#pragma mark ====================================================================================
#pragma mark SPAuthenticationConfiguration
#pragma mark ====================================================================================
Expand All @@ -36,3 +38,5 @@
- (float)regularFontHeightForSize:(float)size;

@end

NS_ASSUME_NONNULL_END
16 changes: 10 additions & 6 deletions Simperium/Simperium.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#endif


NS_ASSUME_NONNULL_BEGIN

@class Simperium;

#if TARGET_OS_IPHONE
Expand Down Expand Up @@ -79,7 +81,7 @@ typedef NS_ENUM(NSInteger, SPSimperiumErrors) {
context:(NSManagedObjectContext *)context
coordinator:(NSPersistentStoreCoordinator *)coordinator
label:(NSString *)label
bucketOverrides:(NSDictionary *)bucketOverrides;
bucketOverrides:(nullable NSDictionary *)bucketOverrides;

#if TARGET_OS_IPHONE
// Starts Simperium and displays the auth interface, if needed.
Expand Down Expand Up @@ -110,7 +112,7 @@ typedef void (^SimperiumBackgroundFetchCompletion)(UIBackgroundFetchResult resul

// Get a particular bucket (which, for Core Data, corresponds to a particular Entity name in your model).
// Once you have a bucket instance, you can set a SPBucketDelegate to react to changes.
- (SPBucket *)bucketForName:(NSString *)name;
- (nullable SPBucket *)bucketForName:(NSString *)name;

// Convenience methods for accessing the Core Data stack.
- (NSManagedObjectContext *)managedObjectContext;
Expand All @@ -132,7 +134,7 @@ typedef void (^SimperiumBackgroundFetchCompletion)(UIBackgroundFetchResult resul
// Clears all locally stored data from the device. Can be used to perform a manual sign out.
// Note: This method is now asynchronous. Please, listen to signout delegate calls, or implement a completion callback block.
typedef void (^SimperiumSignoutCompletion)(void);
- (void)signOutAndRemoveLocalData:(BOOL)remove completion:(SimperiumSignoutCompletion)completion;
- (void)signOutAndRemoveLocalData:(BOOL)remove completion:(nullable SimperiumSignoutCompletion)completion;

// Removes all of the sync'ing metadata. This helper method may be used after a faulty core data migration, to ensure
// sync'ing consistency: next time Simperium is authenticated, it will resync the entire database again.
Expand All @@ -143,7 +145,7 @@ typedef void (^SimperiumSignoutCompletion)(void);

// Alternative to setting delegates on each individual bucket (if you want a single handler for everything).
// If you need to, call this after starting Simperium.
- (void)setAllBucketDelegates:(id<SPBucketDelegate>)aDelegate;
- (void)setAllBucketDelegates:(nullable id<SPBucketDelegate>)aDelegate;

// Alternative to toggling propertyMismatchFailsafeE on each individual bucket (if you want the same behavior everywhere).
// When enabled, Simperium will capture any exceptions thrown while setting property values, and log the error.
Expand Down Expand Up @@ -184,7 +186,7 @@ typedef void (^SimperiumSignoutCompletion)(void);
@property (nonatomic, readwrite, assign) BOOL validatesObjects;

// Returns the currently authenticated Simperium user.
@property (nonatomic, readonly, strong) SPUser *user;
@property (nonatomic, readonly, strong, nullable) SPUser *user;

// The full URL used to communicate with Simperium.
@property (nonatomic, readonly, copy) NSString *appURL;
Expand All @@ -202,7 +204,7 @@ typedef void (^SimperiumSignoutCompletion)(void);
@property (nonatomic, readonly, copy) NSString *clientID;

// Remote Bucket Name Overrides!
@property (nonatomic, readonly, copy) NSDictionary *bucketOverrides;
@property (nonatomic, readonly, nullable, copy) NSDictionary *bucketOverrides;

// Returns the current SPAuthenticator instance.
@property (nonatomic, readwrite, strong) SPAuthenticator *authenticator;
Expand Down Expand Up @@ -239,3 +241,5 @@ typedef void (^SimperiumSignoutCompletion)(void);
#endif

@end

NS_ASSUME_NONNULL_END

0 comments on commit c376773

Please sign in to comment.