Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/fix-an-collection-controller-tests
Browse files Browse the repository at this point in the history
* develop:
  iOS-69-add-timeout-validator: added timeout validator for ANTableController & ANCollectionController

# Conflicts:
#	Example/Alister-Example.xcodeproj/project.pbxproj
  • Loading branch information
maxim-eremenko committed Feb 16, 2017
2 parents 9f70553 + a30e1c9 commit c6058fb
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 554 deletions.
47 changes: 47 additions & 0 deletions Alister/ANActionTimeoutValidator/ANActionTimeOutValidator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ADActionTimeoutValidator.h
// ANODA
//
// Created by ANODA on 3/18/16.
// Copyright © 2016 ANODA. All rights reserved.
//

typedef void (^ANCodeBlock)(void);

@interface ANActionTimeOutValidator : NSObject

/**
* custom init with delay.
*
* @param delay delay in seconds
*
* @return ADActionTimeoutValidator instance
*/
- (instancetype)initWithTimeoutDelay:(CGFloat)delay;

/**
* check is action enabled.
*
* @return return bool value, that meaning is time delay expired or not.
*/

- (BOOL)isActionEnabled;


/**
* reset current time delay
*/
- (void)reset;

/**
* required method for user timeout validator
*
* @param delay delay in seconds
* @param completion block that execute when action enabled
* @param skipBlock block that execute when action disabled, time delay has not expired
*/
- (void)handleTimeoutWithDelayInSeconds:(CGFloat)delay
completion:(ANCodeBlock)completion
skipBlock:(ANCodeBlock)skipBlock;

@end
81 changes: 81 additions & 0 deletions Alister/ANActionTimeoutValidator/ANActionTimeOutValidator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// ADActionTimeoutValidator.m
// ANODA
//
// Created by ANODA on 3/18/16.
// Copyright © 2016 ANODA. All rights reserved.
//

#import "ANActionTimeOutValidator.h"

static CGFloat const kADActionTimeOutValidatorDefaultDelay = 1;

@interface ANActionTimeOutValidator ()

@property (nonatomic, assign) CGFloat delay;
@property (nonatomic, strong) NSDate *initialDate;
@property (nonatomic, assign) BOOL isFirstRequest;

@end

@implementation ANActionTimeOutValidator

- (instancetype)init
{
return [self initWithTimeoutDelay:kADActionTimeOutValidatorDefaultDelay];
}

- (instancetype)initWithTimeoutDelay:(CGFloat)delay
{
self = [super init];
{
self.delay = delay;
self.initialDate = [NSDate date];
self.isFirstRequest = YES;
}
return self;
}

- (BOOL)isActionEnabled
{
NSTimeInterval timeInterval = [self.initialDate timeIntervalSinceDate:[NSDate date]];
CGFloat value = fabs(timeInterval);
BOOL isEnabled = (value > self.delay);
if (isEnabled)
{
self.initialDate = [NSDate date];
}
return isEnabled;
}

- (void)reset
{
self.initialDate = [[NSDate alloc] initWithTimeIntervalSince1970:1000];
}

- (void)handleTimeoutWithDelayInSeconds:(CGFloat)delay completion:(ANCodeBlock)completion skipBlock:(ANCodeBlock)skipBlock
{
self.delay = delay;
if (self.isFirstRequest)
{
self.initialDate = [NSDate date];
self.isFirstRequest = NO;
if (completion)
{
completion();
}
}
else
{
if ([self isActionEnabled] && completion)
{
completion();
}
else if (skipBlock)
{
skipBlock();
}
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
#import "ANListViewInterface.h"
#import "ANListCollectionView.h"
#import "ANListController+Interitance.h"
#import "ANActionTimeOutValidator.h"
#import "Alister.h"

@interface ANCollectionController ()

@property (nonatomic, strong) ANActionTimeOutValidator* timeOutValidator;

@end

@implementation ANCollectionController

+ (instancetype)controllerWithCollectionView:(UICollectionView*)collectionView
Expand Down Expand Up @@ -90,11 +97,14 @@ - (UICollectionViewCell*)collectionView:(__unused UICollectionView*)collectionVi
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.selectionBlock)
{
id model = [self.currentStorage objectAtIndexPath:indexPath];
self.selectionBlock(model, indexPath);
}

[self.timeOutValidator handleTimeoutWithDelayInSeconds:ANListDefaultActionTimeOut completion:^{
if (self.selectionBlock)
{
id model = [self.currentStorage objectAtIndexPath:indexPath];
self.selectionBlock(model, indexPath);
}
} skipBlock:nil];
}


Expand All @@ -120,4 +130,16 @@ - (BOOL)_isExistMappingForSection:(NSInteger)section kind:(NSString*)kind
// return existingKind;
//}


#pragma mark - Lazy Load

- (ANActionTimeOutValidator*)timeOutValidator
{
if (!_timeOutValidator)
{
_timeOutValidator = [[ANActionTimeOutValidator alloc] initWithTimeoutDelay:ANListDefaultActionTimeOut];
}
return _timeOutValidator;
}

@end
2 changes: 2 additions & 0 deletions Alister/ANListController/Headers/Alister.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ typedef void (^ANListControllerUpdatesFinishedTriggerBlock)();

static NSString* const ANListDefaultHeaderKind = @"ANStorageHeaderKind";
static NSString* const ANListDefaultFooterKind = @"ANStorageFooterKind";

static CGFloat const ANListDefaultActionTimeOut = 0.3f;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#import "ANListTableView.h"
#import "ANListController+Interitance.h"
#import "ANListTableView.h"
#import "ANActionTimeOutValidator.h"

@interface ANTableController ()

@property (nonatomic, strong) ANActionTimeOutValidator* timeOutValidator;

@end

@implementation ANTableController

Expand Down Expand Up @@ -102,11 +109,14 @@ - (UITableViewCell*)tableView:(__unused UITableView*)tableView cellForRowAtIndex
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.selectionBlock)
{
id model = [self.currentStorage objectAtIndexPath:indexPath];
self.selectionBlock(model, indexPath);
}

[self.timeOutValidator handleTimeoutWithDelayInSeconds:ANListDefaultActionTimeOut completion:^{
if (self.selectionBlock)
{
id model = [self.currentStorage objectAtIndexPath:indexPath];
self.selectionBlock(model, indexPath);
}
} skipBlock:nil];
}

- (void)tableView:(__unused UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath
Expand Down Expand Up @@ -193,4 +203,16 @@ - (CGFloat)_heightForSupplementaryIndex:(NSInteger)index kind:(NSString*)kind
return height;
}


#pragma mark - Lazy Load

- (ANActionTimeOutValidator*)timeOutValidator
{
if (!_timeOutValidator)
{
_timeOutValidator = [[ANActionTimeOutValidator alloc] initWithTimeoutDelay:ANListDefaultActionTimeOut];
}
return _timeOutValidator;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Oksana Kovalchuk. All rights reserved.
//

#import <XCTest/XCTest.h"
#import <XCTest/XCTest.h>
#import "ANTableController.h"
#import "ANStorage.h"
#import "ANTestTableCell.h"
Expand Down
Loading

0 comments on commit c6058fb

Please sign in to comment.