Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / ShiftIfNecessary.m
Last active August 29, 2015 14:23
Shift Keyboard if Necessary (login view)
- (void)shiftKeyboardIfNecessary {
CGRect viewFrame = self.view.frame;
CGRect activeTextFieldFrame = self.activeTextField.frame;
CGFloat bottomPadding = 4;
// The lowest point visible on the screen is the height of the screen minus the height of the keyboard, then adjusted by the current position
// of the view. So, if the view has already been shifted upwards (negative origin), then more of the lower part of the screen is visible.
CGFloat lowestPointCoveredByKeyboard = -viewFrame.origin.y + viewFrame.size.height - self.keyboardFrame.size.height;
// The active text field's lowest point is its origin.y + its height (so, if it was at y=200, and is 21px tall, then the lowest point is 221.
@hsavit1
hsavit1 / JSON_RAC.m
Created June 18, 2015 08:07
Fetch JSON from URL with RAC
- (RACSignal *)fetchJSONFromURL:(NSURL *)url {
NSParameterAssert(url);
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
if (data) {
@hsavit1
hsavit1 / NSManagedObject+MVVM.m
Last active August 29, 2015 14:23
Category on NSManagedObject
#import "NSManagedObject+MVVM.h"
#import <RBCoreDataStack/NSManagedObject+RBCoreDataStack.h>
@implementation NSManagedObject (MVVM)
+ (RACSignal *)fetchAllSignalInContext:(NSManagedObjectContext *)context {
return [self fetchAllSignalWithPredicate:nil inContext:context];
}
+ (RACSignal *)fetchAllSignalWithPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context {
@hsavit1
hsavit1 / MVVMTableViewListDataSource.h
Last active August 29, 2015 14:23
MVVMTableViewListDataSource
@import Foundation;
@import UIKit;
typedef UITableViewCell *(^MVVMCellCreatorBlock)(UITableView * tableView, NSIndexPath * indexPath, id object);
@interface MVVMTableViewListDataSource : NSObject <UITableViewDataSource>
- (id)initWithObjects:(NSArray *)objects cellCreator:(MVVMCellCreatorBlock)block;
// show a network activity indicator when the search is being executed
RAC([UIApplication sharedApplication], networkActivityIndicatorVisible) =
self.viewModel.searchCommand.executing;
@hsavit1
hsavit1 / RAC_Comparison_Examples.m
Last active August 29, 2015 14:23
RAC vs The Standard Implementations
///////
//signal
///////
//conventional
- (BOOL)isFormValid {
return [self.usernameField.text length] > 0 &&
[self.emailField.text length] > 0 &&
[self.passwordField.text length] > 0 &&
[self.passwordField.text isEqual:self.passwordVerificationField.text];
// creates a signal that fetches an image in the background, delivering
// it on the UI thread. This signal 'cancels' itself if the cell is re-used before the
// image is downloaded.
-(RACSignal *)signalForImage:(NSURL *)imageUrl {
RACScheduler *scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground];
RACSignal *imageDownloadSignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSData *data = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:data];
- (id)init {
if (self = [super init]) {
[self initialize];
}
return self;
}
- (void) initialize {
// set up access to twitter
RAC(self, timestamp) = [[RACObserve(self, model.timestamp) map:^id(NSDate *value) {
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSUInteger days, hours, minutes, seconds;
absoluteTimeUnitsSinceDate(value, &days, &hours, &minutes, &seconds);
NSUInteger updateAfter = NSNotFound;
NSString *currentValue = nil;
if (days) {
currentValue = [NSString stringWithFormat:@"%ud", (unsigned int)days];
}
else if (hours) {
//the trick is to use replayLast or replayLazily signal operation, which basically creates buffer and hold in it cached value!
- (void)getData:(id)inputParam forKey:(NSString *)key withCompletionBlock:(void (^)(id data, NSError *error)) {
if (!_cache[key]) {
RACSignal *dataSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
id operation = _actualGetDataRequest(inputParam, ^(id data, NSError *error) {
if (error) {
[subscriber sendError:error];
}