Skip to content

Commit

Permalink
Added Error/Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cneuwirt committed Feb 23, 2014
1 parent 13077a1 commit 46200b1
Show file tree
Hide file tree
Showing 29 changed files with 1,074 additions and 2 deletions.
116 changes: 116 additions & 0 deletions Miruken.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions Miruken/Cocoa/MKMasterDetail.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// MasterDetail.h
// Miruken
//
// Created by Venkat Palivela on 3/27/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol MKMasterDetail <NSObject>

@optional
- (id<Promise>)selectedDetail:(Class)detailClass;

- (id<Promise>)selectedDetails:(Class)detailClass;

- (void)selectDetail:(id)selectedDetail;

- (void)deselectDetail:(id)selectedDetail;

- (BOOL)hasPreviousDetail:(Class)detailClass;

- (BOOL)hasNextDetail:(Class)detailClass;

- (id<Promise>)previousDetail:(Class)detailClass;

- (id<Promise>)nextDetail:(Class)detailClass;

- (id<Promise>)addDetail:(id)detail;

- (id<Promise>)removeDetail:(id)detail delete:(BOOL)delete;

@end

#define MKMasterDetail(handler) ((id<MKMasterDetail>)(handler))

@protocol MKMasterDetailAware <NSObject>

@optional
- (void)masterChanged;

- (void)didSelectDetail:(id)detail;

- (void)didDeselectDetail:(id)detail;

- (void)didRemoveDetail:(id)detail;

@end

#define MKMasterDetailAware(handler) ((id<MKMasterDetailAware>)(handler))
26 changes: 26 additions & 0 deletions Miruken/Cocoa/MKViewRegion.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// ViewRegion.h
// Miruken
//
// Created by Craig Neuwirt on 2/27/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
This protocol represents a region on the screen where a view controller can
be rendered. It enables compositional view controllers.
*/

@protocol MKViewRegion <NSObject>

@optional
- (void)presentViewController:(UIViewController *)viewControllerToPresent;

- (void)presentNextViewController:(UIViewController *)viewControllerToPresent;

@end

#define MKViewRegion(handler) ((id<MKViewRegion>)(handler))
#define MKViewRegionHint(region,handler) (region ? region : MKViewRegion(handler))
13 changes: 12 additions & 1 deletion Miruken/Cocoa/MirukenCocoa.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@
// Copyright (c) 2014 Craig Neuwirt. All rights reserved. // Copyright (c) 2014 Craig Neuwirt. All rights reserved.
// //


#import "MKViewRegion.h"
#import "MKMasterDetail.h"
#import "MKPagingMixin.h"
#import "MKAlertViewMixin.h"
#import "MKActionSheetMixin.h"
#import "MKUserInteractionMixin.h"
#import "MKEndContextWhenAppResignsMixin.h"
#import "UIViewController_ContextualMixin.h" #import "UIViewController_ContextualMixin.h"
#import "UINavigationController_ContextualMixin.h" #import "UINavigationController_ContextualMixin.h"
#import "UIViewController_RotationMixin.h"
#import "UINavigationController_RotationMixin.h"
#import "UIScrollView+Motion.h"
#import "UIWindow+Rotation.h"
17 changes: 17 additions & 0 deletions Miruken/Cocoa/UIAlertView+Block.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIAlertView+Action.h
// Miruken
//
// Created by Craig Neuwirt on 2/11/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void (^MKAlertViewBlock)(NSInteger buttonIndex);

@interface UIAlertView (UIAlertView_Block)

- (void)showUsingBlock:(MKAlertViewBlock)block;

@end
55 changes: 55 additions & 0 deletions Miruken/Cocoa/UIAlertView+Block.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// UIAlertView+Block.m
// Miruken
//
// Created by Craig Neuwirt on 2/11/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import "UIAlertView+Block.h"

@interface MKAlertViewDelegate : NSObject<UIAlertViewDelegate>

@property (strong, atomic) MKAlertViewBlock block;

+ (void)show:(UIAlertView *)alertView withBlock:(MKAlertViewBlock)block;

@end


@implementation UIAlertView (UIAlertView_Block)

- (void)showUsingBlock:(MKAlertViewBlock)block
{
[MKAlertViewDelegate show:self withBlock:block];
}

@end


@implementation MKAlertViewDelegate

+ (void)show:(UIAlertView *)alertView withBlock:(MKAlertViewBlock)block
{
__block MKAlertViewDelegate *delegate = [MKAlertViewDelegate new];

alertView.delegate = delegate;
delegate.block = ^(NSInteger buttonIndex) {
alertView.delegate = nil;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
delegate = nil;
#pragma clang diagnostic pop
};

[alertView show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (_block)
_block(buttonIndex);
}

@end
17 changes: 17 additions & 0 deletions Miruken/Error/MKCallbackHandler+Recoverable.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MKCallbackHandler+Recoverable.h
// Miruken
//
// Created by Craig Neuwirt on 11/30/12.
// Copyright (c) 2012 Craig Neuwirt. All rights reserved.
//

#import "MKCallbackHandler.h"

@interface MKCallbackHandler (MKCallbackHandler_Recoverable)

- (instancetype)recoverable;

- (instancetype)recoverableInContext:(void *)context;

@end
50 changes: 50 additions & 0 deletions Miruken/Error/MKCallbackHandler+Recoverable.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// MKCallbackHandler+Recoverable.m
// Miruken
//
// Created by Craig Neuwirt on 11/30/12.
// Copyright (c) 2012 Craig Neuwirt. All rights reserved.
//

#import "MKCallbackHandler+Recoverable.h"
#import "MKCallbackHandlerFilter.h"
#import "NSObject+ResolvePromise.h"
#import "MKErrors.h"

@implementation MKCallbackHandler (MKCallbackHandler_Recoverable)

- (instancetype)recoverable
{
return [self recoverableInContext:NULL];
}

- (instancetype)recoverableInContext:(void *)context
{
return [MKCallbackHandlerFilter for:self
filter:^(id callback, id<MKCallbackHandler> composer, BOOL(^proceed)()) {
id<MKPromise> promise = nil;
@try {
BOOL handled = proceed();
if (handled && (promise = [callback effectivePromise]))
{
__block id failureReason = nil;
__block BOOL *failureHandled = nil;
[[promise fail:^(id reason, BOOL *handled) {
failureReason = reason;
failureHandled = handled;
}]
always:^{
if (failureHandled != nil && *failureHandled == NO)
[MKErrors(composer) handleFailure:failureReason context:context];
failureReason = nil;
}];
}
return handled;
}
@catch (id exception) {
[MKErrors(composer) handleFailure:exception context:context];
}
}];
}

@end
19 changes: 19 additions & 0 deletions Miruken/Error/MKEndContextOnError.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// MKEndContextOnError.h
// Miruken
//
// Created by Craig Neuwirt on 9/3/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import "MKErrors.h"
#import "MKContextual.h"

@interface MKEndContextOnError : NSObject <MKErrors, MKContextual>
@end

@interface MKContext (MKContext_EndContextOnError)

- (MKContext *)endContextOnError;

@end
53 changes: 53 additions & 0 deletions Miruken/Error/MKEndContextOnError.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// MKEndContextOnError.m
// Miruken
//
// Created by Craig Neuwirt on 9/3/13.
// Copyright (c) 2013 Craig Neuwirt. All rights reserved.
//

#import "MKEndContextOnError.h"
#import "MKContext+Subscribe.h"
#import "UIAlertView+Block.h"
#import "MKDeferred.h"
#import "MKMixin.h"

@implementation MKEndContextOnError

+ (void)initialize
{
if (self == MKEndContextOnError.class)
[MKEndContextOnError mixinFrom:MKContextualMixin.class];
}

- (id<MKPromise>)reportError:(NSError *)error message:(NSString *)message
title:(NSString *)title context:(void *)context
{
MKDeferred *deferred = [MKDeferred new];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message
delegate:self cancelButtonTitle:@"Continue"
otherButtonTitles:nil];

[alert showUsingBlock:^(NSInteger buttonIndex) {
[self endContext];
[deferred resolve:nil];
}];

return [deferred promise];
}

@end

@implementation MKContext (MKCallbackHandler_EndContextOnError)

- (MKContext *)endContextOnError
{
__block MKEndContextOnError *end = [[MKEndContextOnError allocInContext:self] init];
[end.context subscribeDidEnd:^(id<MKContext> context) {
end = nil; // keeps refresh alive until context ends
}];
return end.context;
}

@end
12 changes: 12 additions & 0 deletions Miruken/Error/MKError.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// MKError.h
// Miruken
//
// Created by Craig Neuwirt on 2/23/14.
// Copyright (c) 2014 Craig Neuwirt. All rights reserved.
//

#import "MKErrors.h"
#import "MKCallbackHandler+Recoverable.h"
#import "MKEndContextOnError.h"
#import "MKWellKnownErrorResults.h"
14 changes: 14 additions & 0 deletions Miruken/Error/MKErrorCallbackHandler.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ErrorCallbackHandler.h
// Miruken
//
// Created by Craig Neuwirt on 11/16/12.
// Copyright (c) 2012 Craig Neuwirt. All rights reserved.
//

#import "MKErrors.h"
#import "MKDynamicCallbackHandler.h"

@interface MKErrorCallbackHandler : MKDynamicCallbackHandler <MKErrors>

@end
Loading

0 comments on commit 46200b1

Please sign in to comment.