Skip to content

Instantly share code, notes, and snippets.

@rnystrom
rnystrom / gist:5811387
Last active December 18, 2015 16:29
Example of logging into Github and getting user info using ReactiveCocoa and Octokit.
- (void)viewDidLoad {
[super viewDidLoad];
RAC(self.loginButton, enabled) = [RACSignal combineLatest:@[
self.usernameTextField.rac_textSignal,
self.passwordTextField.rac_textSignal]
reduce:^(NSString *username, NSString *password) {
return @([username length] > 0 && [password length] > 0);
}];
@rnystrom
rnystrom / gist:5831736
Created June 21, 2013 14:53
Login to Github and display user info
- (void)viewDidLoad {
[super viewDidLoad];
RAC(self.nameLabel, text) = RACAble([GHDataStore sharedStore], client.user.name);
RAC(self.emailLabel, text) = RACAble([GHDataStore sharedStore], client.user.email);
RAC(self.companyLabel, text) = RACAble([GHDataStore sharedStore], client.user.company);
RAC(self.loginLabel, text) = RACAble([GHDataStore sharedStore], client.user.login);
RAC(self.reposLabel, text) = [[RACSignal combineLatest:@[
RACAble([GHDataStore sharedStore], client.user.publicRepoCount),
RACAble([GHDataStore sharedStore], client.user.privateRepoCount)
// get user settings
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean doUpdates = sharedPref.getBoolean(SettingsActivity.UPDATES_KEY, true);
boolean doRally = sharedPref.getBoolean(SettingsActivity.RALLY_KEY, true);
boolean doLeads = sharedPref.getBoolean(SettingsActivity.LEAD_KEY, true);
// subscribe to channels based on user settings
if (doUpdates) PushService.subscribe(this, Update.UPDATES_NOTIFICATION_KEY, UpdatesDetail.class);
if (doRally) PushService.subscribe(this, Update.RALLY_NOTIFICATION_KEY, UpdatesDetail.class);
if (doLeads) PushService.subscribe(this, Update.LEAD_NOTIFICATION_KEY, UpdatesDetail.class);
@rnystrom
rnystrom / coloroverlay
Last active December 22, 2015 22:39
Overlay a directory of PNGs with a solid color. You'll have to escape quotes (any solution to this?).
#!/bin/bash
# ex:
# coloroverlay black
# coloroverlay 'rgba\\(0,0,0,0.5\\)'
COLOR=$1
ls *.png | awk '{print("convert "$1" \\( -clone 0 -fill '$COLOR' -draw \"color 0,0 reset\" \\) -compose atop -composite "$1)}' | /bin/sh
- (NSManagedObjectContext *)managedObjectContext {
if (! _managedObjectContext) {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Moviedo" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"Moviedo.sqlite"];
NSDictionary *options = @{
@rnystrom
rnystrom / :(
Created November 3, 2014 16:37
A friendly challenge to anyone w/ functional chops to improve this function. No holds barred: op overloading, enum/struct/class, everything-is-a-singleton... go for it! The only rule is no forced unwrapping (e.g. !).
import Foundation
import ImageIO
func datesFromImagesInDir(dir: String) -> [NSDate] {
let fm = NSFileManager.defaultManager()
var error: NSError?
let df = NSDateFormatter()
df.dateFormat = "yyyy:MM:dd HH:mm:ss"
func showScheme(sender: ScalingPageControl) {
performSegueWithIdentifier(SegueIdentifier.PresentSchemeSegue.rawValue/* :( */, sender: pageControl)
}
@rnystrom
rnystrom / iOS @3x assets script
Created November 26, 2014 01:52
Save this shell script to generate @2x and 1x PNG assets for your apps. Requires Imagemagick (http://www.imagemagick.org/). These will probably look shitty and blurred.
#!/bin/sh
# Example usage
# cd into directory that has @3x images
# ./whatever-you-name-this.sh
# Remember to chmod +x the script
resize () {
ls *@3x.png | awk '{print("convert "$1" -filter box -resize '$1' "$1)}' | sed 's/@3x/'$2'/2' | /bin/sh
}
@rnystrom
rnystrom / RN3DTouchGestureRecognizer.swift
Last active February 3, 2018 22:58
RN3DTouchGestureRecognizer
//
// RN3DTouchGestureRecognizer.swift
//
// Created by Ryan Nystrom on 10/10/15.
// Copyright © 2015 Ryan Nystrom. All rights reserved.
//
import UIKit.UIGestureRecognizerSubclass
class RN3DTouchGestureRecognizer: UIGestureRecognizer {
// iterate newResultsArray in ascending order
// if na[i] == oa[j] and na[i+1] == oa[j+1], assign them to the opposite index+1
// pass 4 is for ascending and 5 descending, block covers logic for both
void (^pass4And5Block)(BOOL, vector<IGListRecord>&, vector<IGListRecord>&) = ^(BOOL ascending, vector<IGListRecord> &newResults, vector<IGListRecord> &oldResults) {
const NSInteger offset = ascending ? 1 : -1;
for (NSInteger i = ascending ? 0 : newCount - 1;
(ascending && i < newCount - 1) || (!ascending && i > 0);
ascending ? i++ : i--) {
// don't check block moves for new elements