Skip to content

Instantly share code, notes, and snippets.

@manmal
manmal / functional-nerdgasm.swift
Last active September 12, 2015 19:10
Functional Nerdgasm in Swift
typealias DistanceRange = (from: Double, to: Double) // meters
typealias WaterListResult = (water: Water, lodging: Lodging, distance: Double, price: Float?)
typealias LodgingAndDistance = (lodging: Lodging, distance: Double?)
extension Water {
// Returns waters that are sorted by their nearest (nearest to `center`) lodging's distance to `center`.
static func watersByProximity(distanceRange: DistanceRange, center: CLLocation) -> [WaterListResult] {
return ModelsStorage.waters
.flatMap { (water: Water) -> [WaterListResult] in // Map to water's lodgings combined with their distances to center
@manmal
manmal / SomeAbstractLayer.swift
Last active August 29, 2015 14:25
Completion Handlers in Swift
import Foundation
typealias someCompletionHandler = (success: Bool) -> ()
class SomeAbstractLayer {
static let sharedInstance = SomeAbstractLayer()
var subscribers: [someCompletionHandler]
@manmal
manmal / letdriven.swift
Created June 26, 2015 14:15
let-driven programming - if you think that's a bad idea, punish me at @manuelmaly 😛
// Good old UIWebView delegate
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let noArticle = request.URL?.absoluteString?.rangeOfString("article.html")?.isEmpty {
if !noArticle,
let foundURL = request.URL,
let components = NSURLComponents(URL: foundURL, resolvingAgainstBaseURL: false),
let queryItems = components.queryItems as? [NSURLQueryItem],
let articleIdQueryItem = filter(queryItems, { $0.name == "article_id" }).first,
let articleId = articleIdQueryItem.value?.toInt() {
@manmal
manmal / TestHelper.m
Created March 30, 2015 19:04
asyncWaitFor
BOOL asyncWaitFor(BOOL *flag, NSTimeInterval timeout) {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
do {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
if ([timeoutDate timeIntervalSinceNow] < 0.0) {
break;
}
}
while (!*flag);
@manmal
manmal / upload_to_crashlytics
Created March 12, 2015 18:31
Upload an .ipa to Crashlytics
#!/bin/bash
set -x
cd ${0%/*}/..
ARCHIVEPATH=`pwd`/archive
IPA_PATH=("${ARCHIVEPATH}"/*.ipa)
DSYM_PATH=("${ARCHIVEPATH}"/*.dSYM.zip)
CRASHLYTICS_API_KEY=XXXX
// Just a quick hack to achieve something like this: https://www.dropbox.com/s/6a6lsqasee0zp1q/bgview.png?dl=0
@interface MMGradientBackgroundView()
@property (strong, nonatomic) UIImage *noiseImage;
@end
@implementation MMGradientBackgroundView
- (instancetype)initWithFrame:(CGRect)frame {
@manmal
manmal / MMReactiveSearchTableViewModel.m
Created January 23, 2015 16:02
Reactive Search Table View Model
@interface MMBaseReactiveSearchTableViewModel()
// Most of these will actually be public/header file
@property (copy, nonatomic) NSArray *searchResults;
@property (copy, nonatomic) NSString *searchTerm;
@property (strong, nonatomic) NSError *error;
@property (assign, nonatomic) BOOL isLoading;
@property (strong, nonatomic) RACSubject *forceRestartServerRequestSubject;
@end
static UIView *PSPDFViewWithSuffix(UIView *view, NSString *classNameSuffix) {
if (!view || classNameSuffix.length == 0) return nil;
UIView *theView = nil;
for (__unsafe_unretained UIView *subview in view.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:classNameSuffix]) {
return subview;
}else {
if ((theView = PSPDFViewWithSuffix(subview, classNameSuffix))) break;
}
@manmal
manmal / comment.coffee
Created November 14, 2014 07:09
Ember Promised Properties
# /app/controllers/comment.coffee
`import promisedProperty from "../utils/promised_property"`
CommentController = Ember.ObjectController.extend
needs: ["application"]
currentUser: Ember.computed.alias('controllers.application.currentUser')
isCommentController: true
@manmal
manmal / MMTableViewWithParallaxHeader.m
Created January 15, 2014 08:54
Forwards hit testing on a UITableView to certain views which are behind the tableview (siblings of UITableView). Quite hacky :/ If you know a better way, please tweet @manuelmaly
@interface MMTableViewWithParallaxHeader : UITableView
@property (strong, nonatomic) NSArray *forwardTouchesOnTransparentCellToViews;
@end
@implementation MMTableViewWithParallaxHeader
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {