Skip to content

Commit

Permalink
Appearance proxy support for VSAlertController :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun Santhanam committed Oct 12, 2017
1 parent 99bd821 commit d5fcb70
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 88 deletions.
6 changes: 5 additions & 1 deletion VSAlert-Example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#import "AppDelegate.h"

#import "VSAlertController.h"
#import "VSAlertControllerAppearanceProxy.h"

@interface AppDelegate ()

@end
Expand All @@ -15,8 +18,9 @@ @implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

return YES;

}


Expand Down
10 changes: 10 additions & 0 deletions VSAlert.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
0710F9D41F8BCDBE00C6D084 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0710F9D21F8BCDB700C6D084 /* CoreGraphics.framework */; };
074538171F8C437700092DAF /* VSAlertControllerTransitionAnimator.h in Headers */ = {isa = PBXBuildFile; fileRef = 074538151F8C437700092DAF /* VSAlertControllerTransitionAnimator.h */; };
074538191F8C437700092DAF /* VSAlertControllerTransitionAnimator.m in Sources */ = {isa = PBXBuildFile; fileRef = 074538161F8C437700092DAF /* VSAlertControllerTransitionAnimator.m */; };
07F25AB81F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 07F25AB61F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.h */; };
07F25AB91F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 07F25AB71F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m */; };
07F25ABA1F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 07F25AB71F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m */; };
07FF9E171F8FEF8C002966A3 /* AlertExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 07FF9E111F8FEF03002966A3 /* AlertExample.m */; };
07FF9E181F8FEF8E002966A3 /* AlertExampleSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 07FF9E0F1F8FEF03002966A3 /* AlertExampleSettingsViewController.m */; };
07FF9E191F8FEF92002966A3 /* AlertExampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 07FF9E0E1F8FEF03002966A3 /* AlertExampleViewController.m */; };
Expand Down Expand Up @@ -85,6 +88,8 @@
07A7C4051F8D655100A71580 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
07A7C4071F8D655700A71580 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.0.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
07F00EFF1F8A972D006FAC96 /* libVSAlert.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libVSAlert.a; sourceTree = BUILT_PRODUCTS_DIR; };
07F25AB61F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VSAlertControllerAppearanceProxy.h; sourceTree = "<group>"; };
07F25AB71F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VSAlertControllerAppearanceProxy.m; sourceTree = "<group>"; };
07FF9DF01F8FEE85002966A3 /* VSAlert-Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "VSAlert-Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
07FF9E091F8FEEE3002966A3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
07FF9E0A1F8FEEE3002966A3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -134,6 +139,8 @@
0710F9C51F8B1E1000C6D084 /* VSAlertController.m */,
074538151F8C437700092DAF /* VSAlertControllerTransitionAnimator.h */,
074538161F8C437700092DAF /* VSAlertControllerTransitionAnimator.m */,
07F25AB61F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.h */,
07F25AB71F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m */,
);
name = Src;
sourceTree = "<group>";
Expand Down Expand Up @@ -217,6 +224,7 @@
0710F9C71F8B1E2000C6D084 /* VSAlertAction.h in Headers */,
074538171F8C437700092DAF /* VSAlertControllerTransitionAnimator.h in Headers */,
0710F9C81F8B1E2200C6D084 /* VSAlertController.h in Headers */,
07F25AB81F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -351,6 +359,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
07F25AB91F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m in Sources */,
0710F9C91F8B1E3500C6D084 /* VSAlertAction.m in Sources */,
074538191F8C437700092DAF /* VSAlertControllerTransitionAnimator.m in Sources */,
0710F9CA1F8B1E3700C6D084 /* VSAlertController.m in Sources */,
Expand All @@ -367,6 +376,7 @@
07FF9E4F1F8FF1D1002966A3 /* VSAlertControllerTransitionAnimator.m in Sources */,
07FF9E191F8FEF92002966A3 /* AlertExampleViewController.m in Sources */,
07FF9E1A1F8FEF98002966A3 /* main.m in Sources */,
07F25ABA1F8FFA98000B4784 /* VSAlertControllerAppearanceProxy.m in Sources */,
07FF9E181F8FEF8E002966A3 /* AlertExampleSettingsViewController.m in Sources */,
07FF9E1B1F8FEF9B002966A3 /* AppDelegate.m in Sources */,
);
Expand Down
26 changes: 1 addition & 25 deletions VSAlert/VSAlertController.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ typedef NS_ENUM(NSInteger, VSAlertControllerAnimationStyle) {
/**
VSAlertController is a drop-in replacement for UIAlertController with more features. It is created using the +alertControllerWithTitle:description:image:style: class method, and configured using instances of VSAlertAction. You can add text fields by calling -addTextField: on an instance of VSAlertController. Instantiate the controller, add your actions and textfieds. and any other configuration you might need. Present the controller modally using UIViewController's -presentViewController:animated:completion: method. VSAlertController respects the animation paramater of this call, and you configure the animation in question by setting your instances animationStyle property before presentation. You can also change this property in the handler of an action to use a different animation on dismissal.
*/
@interface VSAlertController : UIViewController
@interface VSAlertController : UIViewController<UIAppearance>

/**
@name Creating Alerts
Expand Down Expand Up @@ -240,28 +240,4 @@ typedef NS_ENUM(NSInteger, VSAlertControllerAnimationStyle) {
*/
@property (NS_NONATOMIC_IOSONLY, strong, readonly, nullable) UIImage *image;

/**
@name Customizing the Class Globally
*/

/**
Default title text color. Affects all instances instantiated after this change.
*/
@property (NS_NONATOMIC_IOSONLY, class, strong, nullable) UIColor *defaultTitleTextColor;

/**
Default description text color. Affects all instances instantiated after this change.
*/
@property (NS_NONATOMIC_IOSONLY, class, strong, nullable) UIColor *defaultMessageTextColor;

/**
Default title text font. Affects all instance instantiated after this change.
*/
@property (NS_NONATOMIC_IOSONLY, class, strong, nullable) UIFont *defaultTitleTextFont;

/**
Default description text font. Affects all instances instantiated after this change.
*/
@property (NS_NONATOMIC_IOSONLY, class, strong, nullable) UIFont *defaultMessageTextFont;

@end
93 changes: 32 additions & 61 deletions VSAlert/VSAlertController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import "VSAlertController.h"
#import "VSAlertControllerTransitionAnimator.h"
#import "VSAlertControllerAppearanceProxy.h"

NSString * const VSAlertControllerNotImplementedException = @"VSAlertControllerNotImplementedException";
NSString * const VSAlertControllerTextFieldInvalidException = @"VSAlertControllerTextFieldInvalidException";
Expand Down Expand Up @@ -55,12 +56,6 @@ @implementation VSAlertController {
// Log
static os_log_t alert_log;

// Static class vars
static UIColor *_defaultTitleTextColor;
static UIColor *_defaultMessageTextColor;
static UIFont *_defaultTitleTextFont;
static UIFont *_defaultMessageTextFont;

// Explicitly synthesize Ivars from header
@synthesize alertTitleTextColor = _alertTitleTextColor;
@synthesize alertMessageTextColor = _alertMessageTextColor;
Expand Down Expand Up @@ -118,56 +113,6 @@ + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)m

}

#pragma mark - Class Property Access Methods

+ (UIColor *)defaultTitleTextColor {

return _defaultTitleTextColor;

}

+ (void)setDefaultTitleTextColor:(UIColor *)defaultTitleTextColor {

_defaultTitleTextColor = defaultTitleTextColor;

}

+ (UIColor *)defaultMessageTextColor {

return _defaultMessageTextColor;

}

+ (void)setDefaultMessageTextColor:(UIColor *)defaultMessageTextColor {

_defaultMessageTextColor = defaultMessageTextColor;

}

+ (UIFont *)defaultTitleTextFont {

return _defaultTitleTextFont;

}

+ (void)setDefaultTitleTextFont:(UIFont *)defaultTitleTextFont {

_defaultTitleTextFont = defaultTitleTextFont;

}

+ (UIFont *)defaultMessageTextFont {

return _defaultMessageTextFont;

}

+ (void)setDefaultMessageTextFont:(UIFont *)defaultMessageTextFont {

_defaultMessageTextFont = defaultMessageTextFont;

}

#pragma mark - Overridden Instance Methods

- (instancetype)init {
Expand Down Expand Up @@ -344,6 +289,32 @@ - (CGFloat)alertStackViewHeight {

}

#pragma mark - UIAppearance

+ (instancetype)appearance {

return [VSAlertControllerAppearanceProxy appearance];

}

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait {

return [VSAlertControllerAppearanceProxy appearanceForTraitCollection:trait];

}

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class<UIAppearanceContainer>> *)containerTypes {

return [VSAlertControllerAppearanceProxy appearanceForTraitCollection:trait whenContainedInInstancesOfClasses:containerTypes];

}

+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class<UIAppearanceContainer>> *)containerTypes {

return [VSAlertControllerAppearanceProxy appearanceWhenContainedInInstancesOfClasses:containerTypes];

}

#pragma mark - UIViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
Expand Down Expand Up @@ -464,11 +435,11 @@ - (void)_setUpAlertController {
_presentAnimator = [[VSAlertControllerTransitionAnimator alloc] init];
_dismissAnimator = [[VSAlertControllerTransitionAnimator alloc] init];

// Set instance properties without accessors (to respect UIAppearance) [Use class defaults for now, as this class doesn't actually work with UIAppearance]
_alertTitleTextColor = [self class].defaultTitleTextColor ? [self class].defaultTitleTextColor : [UIColor blackColor];
_alertTitleTextFont = [self class].defaultTitleTextFont ? [self class].defaultTitleTextFont : [UIFont systemFontOfSize:17.0f weight:UIFontWeightMedium];
_alertMessageTextColor = [self class].defaultMessageTextColor ? [self class].defaultMessageTextColor : [UIColor blackColor];
_alertMessageTextFont = [UIFont systemFontOfSize:15.0f weight:UIFontWeightRegular];
// Set up propertie without accessors for use with UIAppearance
_alertTitleTextColor = [VSAlertController appearance].alertTitleTextColor ? [VSAlertController appearance].alertTitleTextColor : [UIColor blackColor];
_alertTitleTextFont = [VSAlertController appearance].alertTitleTextFont ? [VSAlertController appearance].alertTitleTextFont : [UIFont systemFontOfSize:17.0f weight:UIFontWeightSemibold];
_alertMessageTextColor = [VSAlertController appearance].alertMessageTextColor ? [VSAlertController appearance].alertMessageTextColor : [UIColor blackColor];
_alertMessageTextFont = [VSAlertController appearance].alertMessageTextFont ? [VSAlertController appearance].alertMessageTextFont : [UIFont systemFontOfSize:15.0f weight:UIFontWeightRegular];

// Set instance read-only properties
_style = VSAlertControllerStyleAlert;
Expand Down
35 changes: 35 additions & 0 deletions VSAlert/VSAlertControllerAppearanceProxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// VSAlertControllerAppearanceProxy.h
// VSAlert
//
// Created by Varun Santhanam on 10/12/17.
//

#import <UIKit/UIKit.h>

/**
VSAlertControllerAppearanceProxy is a class used to support the UIAppearance protocol in VSAlertController, because VSAlertController does not inherit from an object that includes this support. This classes interface is described in a project header, and thus does not appear in the library documentation.
*/
@interface VSAlertControllerAppearanceProxy : UIView<UIAppearance>

/**
The color of the alert's title
*/
@property (NS_NONATOMIC_IOSONLY, strong, nonnull) UIColor *alertTitleTextColor UI_APPEARANCE_SELECTOR;

/**
The color of the alert's message
*/
@property (NS_NONATOMIC_IOSONLY, strong, nonnull) UIColor *alertMessageTextColor UI_APPEARANCE_SELECTOR;

/**
The font of the alert's title
*/
@property (NS_NONATOMIC_IOSONLY, strong, nonnull) UIFont *alertTitleTextFont UI_APPEARANCE_SELECTOR;

/**
The font of the aler's message
*/
@property (NS_NONATOMIC_IOSONLY, strong, nonnull) UIFont *alertMessageTextFont UI_APPEARANCE_SELECTOR;

@end
17 changes: 17 additions & 0 deletions VSAlert/VSAlertControllerAppearanceProxy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// VSAlertControllerAppearanceProxy.m
// VSAlert
//
// Created by Varun Santhanam on 10/12/17.
//

#import "VSAlertControllerAppearanceProxy.h"

@implementation VSAlertControllerAppearanceProxy

@synthesize alertTitleTextColor = _alertTitleTextColor;
@synthesize alertTitleTextFont = _alertTitleTextFont;
@synthesize alertMessageTextColor = _alertMessageTextColor;
@synthesize alertMessageTextFont = _alertMessageTextFont;

@end
17 changes: 16 additions & 1 deletion VSAlert/VSAlertControllerTransitionAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@
@import UIKit;

#import "VSAlertController.h"

/**
Exception thrown when a transition that has yet to be implemented is triggered at runtime.
*/
extern NSString * _Nonnull const VSAlertControllerTransitionAnimatorNotImplementedException;

/**
An exception thrown when a transition that doesn't work for a given controller is triggered at runttime
*/
extern NSString * _Nonnull const VSAlertControllerTransitionAnimatorInvalidUsageException;

/**
VSAlertControllerTransitionAnimator is a class used to manage modal transitions to and from VSAlertController objects. The class's interface is described in a project header, and thus does not appear in the library's documentation/
*/
@interface VSAlertControllerTransitionAnimator : NSObject<UIViewControllerAnimatedTransitioning>

/**
Create a transition animator
@param actionStyle The style of the action that trigged the animation, during dismissal transitions
@return The instantiated animator object.
*/
- (nullable instancetype)initWithActionStyle:(VSAlertActionStyle)actionStyle NS_DESIGNATED_INITIALIZER;

@end

0 comments on commit d5fcb70

Please sign in to comment.