2011-10-17 09:28:15 -07:00
|
|
|
//
|
|
|
|
// Utilities.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 10/17/11.
|
|
|
|
// Copyright (c) 2011 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Utilities.h"
|
2013-06-22 19:34:12 -07:00
|
|
|
#import <CommonCrypto/CommonCrypto.h>
|
2011-10-17 09:28:15 -07:00
|
|
|
|
2012-07-23 10:57:11 -07:00
|
|
|
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor,
|
|
|
|
CGColorRef endColor) {
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGFloat locations[] = { 0.0, 1.0 };
|
|
|
|
|
|
|
|
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
|
|
|
|
|
|
|
|
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
|
|
|
|
(__bridge CFArrayRef) colors, locations);
|
|
|
|
|
|
|
|
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
|
|
|
|
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
|
|
|
|
|
|
|
|
CGContextSaveGState(context);
|
|
|
|
CGContextAddRect(context, rect);
|
|
|
|
CGContextClip(context);
|
|
|
|
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
|
|
|
|
CGContextRestoreGState(context);
|
|
|
|
|
|
|
|
CGGradientRelease(gradient);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
}
|
|
|
|
|
2011-10-17 09:28:15 -07:00
|
|
|
@implementation Utilities
|
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
static NSMutableDictionary *imageCache;
|
2011-10-17 09:28:40 -07:00
|
|
|
|
|
|
|
+ (void)saveImage:(UIImage *)image feedId:(NSString *)filename {
|
2011-10-18 08:56:13 -07:00
|
|
|
if (!imageCache) {
|
2012-07-15 15:06:06 -07:00
|
|
|
imageCache = [NSMutableDictionary dictionary];
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
// Save image to memory-based cache, for performance when reading.
|
2012-08-09 12:31:09 -07:00
|
|
|
// NSLog(@"Saving %@", [imageCache allKeys]);
|
2013-02-28 17:07:51 -08:00
|
|
|
if (image && [filename class] != [NSNull class]) {
|
2012-07-12 00:10:42 -07:00
|
|
|
[imageCache setObject:image forKey:filename];
|
|
|
|
} else {
|
2012-11-08 17:39:32 -08:00
|
|
|
// NSLog(@"%@ has no image!!!", filename);
|
2012-07-12 00:10:42 -07:00
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIImage *)getImage:(NSString *)filename {
|
2012-07-18 15:26:55 -07:00
|
|
|
return [self getImage:filename isSocial:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIImage *)getImage:(NSString *)filename isSocial:(BOOL)isSocial {
|
2011-10-17 09:28:40 -07:00
|
|
|
UIImage *image;
|
2013-10-18 14:17:01 -07:00
|
|
|
if (filename && [imageCache objectForKey:filename]) {
|
2013-04-23 11:52:34 -07:00
|
|
|
image = [imageCache objectForKey:filename];
|
|
|
|
}
|
2011-10-18 08:56:13 -07:00
|
|
|
|
2013-02-28 17:07:51 -08:00
|
|
|
if (!image || [image class] == [NSNull class]) {
|
2011-10-18 08:56:13 -07:00
|
|
|
// Image not in cache, search on disk.
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
2011-10-17 09:28:40 -07:00
|
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
2013-06-23 22:19:08 -07:00
|
|
|
if (isSocial) {
|
|
|
|
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"avatars"];
|
|
|
|
} else {
|
|
|
|
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"favicons"];
|
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
|
|
|
|
|
|
|
|
image = [UIImage imageWithContentsOfFile:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image) {
|
|
|
|
return image;
|
|
|
|
} else {
|
2012-08-13 18:45:06 -07:00
|
|
|
if (isSocial) {
|
2012-08-13 17:35:04 -07:00
|
|
|
// return [UIImage imageNamed:@"user_light.png"];
|
2012-08-13 18:45:06 -07:00
|
|
|
return nil;
|
|
|
|
} else {
|
|
|
|
return [UIImage imageNamed:@"world.png"];
|
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-22 09:12:02 -07:00
|
|
|
+ (void)drawLinearGradientWithRect:(CGRect)rect startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor {
|
|
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGFloat locations[] = { 0.0, 1.0 };
|
|
|
|
|
|
|
|
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
|
|
|
|
|
|
|
|
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
|
|
|
|
(__bridge CFArrayRef) colors, locations);
|
|
|
|
|
|
|
|
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
|
|
|
|
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
|
|
|
|
|
|
|
|
CGContextSaveGState(context);
|
|
|
|
CGContextAddRect(context, rect);
|
|
|
|
CGContextClip(context);
|
|
|
|
|
|
|
|
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
|
|
|
|
CGContextRestoreGState(context);
|
|
|
|
|
|
|
|
CGGradientRelease(gradient);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
}
|
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
+ (void)saveimagesToDisk {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
|
|
|
|
|
2012-07-15 15:06:06 -07:00
|
|
|
dispatch_async(queue, [^{
|
2012-06-26 11:45:42 -07:00
|
|
|
for (NSString *filename in [imageCache allKeys]) {
|
2011-10-18 08:56:13 -07:00
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
2013-06-23 22:19:08 -07:00
|
|
|
if ([filename hasPrefix:@"social"]) {
|
|
|
|
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"avatars"];
|
|
|
|
} else {
|
|
|
|
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"favicons"];
|
|
|
|
}
|
2011-10-18 08:56:13 -07:00
|
|
|
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
|
|
|
|
|
|
|
|
// Save image to disk
|
|
|
|
UIImage *image = [imageCache objectForKey:filename];
|
2012-07-15 16:46:46 -07:00
|
|
|
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
2012-07-15 15:06:06 -07:00
|
|
|
} copy]);
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
+ (UIImage *)roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
|
|
|
|
UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
|
|
|
|
[[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size}
|
|
|
|
cornerRadius:r] addClip];
|
|
|
|
[orig drawInRect:(CGRect){CGPointZero, orig.size}];
|
|
|
|
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
|
|
|
|
UIGraphicsEndImageContext();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-22 19:34:12 -07:00
|
|
|
+ (NSString *)md5:(NSString *)string {
|
|
|
|
const char *cStr = [string UTF8String];
|
|
|
|
unsigned char result[16];
|
2013-09-25 17:43:00 -07:00
|
|
|
CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); // This is the md5 call
|
2013-06-22 19:34:12 -07:00
|
|
|
return [NSString stringWithFormat:
|
|
|
|
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
|
|
|
|
result[0], result[1], result[2], result[3],
|
|
|
|
result[4], result[5], result[6], result[7],
|
|
|
|
result[8], result[9], result[10], result[11],
|
|
|
|
result[12], result[13], result[14], result[15]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-10-10 12:58:40 -07:00
|
|
|
+ (NSString *)formatLongDateFromTimestamp:(NSInteger)timestamp {
|
2013-10-01 14:19:12 -07:00
|
|
|
if (!timestamp) timestamp = [[NSDate date] timeIntervalSince1970];
|
2013-10-10 12:58:40 -07:00
|
|
|
|
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(double)timestamp];
|
|
|
|
static NSDateFormatter *dateFormatter = nil;
|
|
|
|
static NSDateFormatter *todayFormatter = nil;
|
|
|
|
static NSDateFormatter *yesterdayFormatter = nil;
|
|
|
|
static NSDateFormatter *formatterPeriod = nil;
|
|
|
|
|
|
|
|
NSDate *today = [NSDate date];
|
|
|
|
NSDateComponents *components = [[NSCalendar currentCalendar]
|
|
|
|
components:NSIntegerMax
|
|
|
|
fromDate:today];
|
|
|
|
[components setHour:0];
|
|
|
|
[components setMinute:0];
|
|
|
|
[components setSecond:0];
|
|
|
|
NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
|
|
|
|
NSDate *yesterday = [NSDate dateWithTimeInterval:-60*60*24 sinceDate:midnight];
|
|
|
|
|
|
|
|
if (!dateFormatter || !todayFormatter || !yesterdayFormatter || !formatterPeriod) {
|
|
|
|
dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[dateFormatter setDateFormat:@"EEEE, MMMM d'Sth', y h:mm"];
|
|
|
|
todayFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[todayFormatter setDateFormat:@"'Today', MMMM d'Sth' h:mm"];
|
|
|
|
yesterdayFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[yesterdayFormatter setDateFormat:@"'Yesterday', MMMM d'Sth' h:mm"];
|
|
|
|
formatterPeriod = [[NSDateFormatter alloc] init];
|
|
|
|
[formatterPeriod setDateFormat:@"a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *dateString;
|
|
|
|
if ([date compare:midnight] == NSOrderedDescending) {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[todayFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
} else if ([date compare:yesterday] == NSOrderedDescending) {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[yesterdayFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
} else {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[dateFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
}
|
|
|
|
dateString = [dateString stringByReplacingOccurrencesOfString:@"Sth"
|
|
|
|
withString:[Utilities suffixForDayInDate:date]];
|
2013-10-01 14:19:12 -07:00
|
|
|
|
2013-10-10 12:58:40 -07:00
|
|
|
return dateString;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)formatShortDateFromTimestamp:(NSInteger)timestamp {
|
|
|
|
if (!timestamp) timestamp = [[NSDate date] timeIntervalSince1970];
|
|
|
|
|
2013-10-01 14:19:12 -07:00
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(double)timestamp];
|
2013-10-10 12:58:40 -07:00
|
|
|
static NSDateFormatter *dateFormatter = nil;
|
|
|
|
static NSDateFormatter *todayFormatter = nil;
|
|
|
|
static NSDateFormatter *yesterdayFormatter = nil;
|
|
|
|
static NSDateFormatter *formatterPeriod = nil;
|
|
|
|
|
|
|
|
NSDate *today = [NSDate date];
|
|
|
|
NSDateComponents *components = [[NSCalendar currentCalendar]
|
|
|
|
components:NSIntegerMax
|
|
|
|
fromDate:today];
|
|
|
|
[components setHour:0];
|
|
|
|
[components setMinute:0];
|
|
|
|
[components setSecond:0];
|
|
|
|
NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
|
|
|
|
NSDate *yesterday = [NSDate dateWithTimeInterval:-60*60*24 sinceDate:midnight];
|
|
|
|
|
|
|
|
if (!dateFormatter || !todayFormatter || !yesterdayFormatter || !formatterPeriod) {
|
|
|
|
dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[dateFormatter setDateFormat:@"dd LLL y, h:mm"];
|
|
|
|
todayFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[todayFormatter setDateFormat:@"h:mm"];
|
|
|
|
yesterdayFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[yesterdayFormatter setDateFormat:@"'Yesterday', h:mm"];
|
|
|
|
formatterPeriod = [[NSDateFormatter alloc] init];
|
|
|
|
[formatterPeriod setDateFormat:@"a"];
|
|
|
|
}
|
2013-10-01 14:19:12 -07:00
|
|
|
|
2013-10-10 12:58:40 -07:00
|
|
|
NSString *dateString;
|
|
|
|
if ([date compare:midnight] == NSOrderedDescending) {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[todayFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
} else if ([date compare:yesterday] == NSOrderedDescending) {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[yesterdayFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
} else {
|
|
|
|
dateString = [NSString stringWithFormat:@"%@%@",
|
|
|
|
[dateFormatter stringFromDate:date],
|
|
|
|
[[formatterPeriod stringFromDate:date] lowercaseString]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateString;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)suffixForDayInDate:(NSDate *)date {
|
|
|
|
NSInteger day = [[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] components:NSDayCalendarUnit fromDate:date] day];
|
2013-12-02 16:52:17 -08:00
|
|
|
if (day == 11 || day == 12 || day == 13) {
|
2013-10-10 12:58:40 -07:00
|
|
|
return @"th";
|
|
|
|
} else if (day % 10 == 1) {
|
|
|
|
return @"st";
|
|
|
|
} else if (day % 10 == 2) {
|
|
|
|
return @"nd";
|
|
|
|
} else if (day % 10 == 3) {
|
|
|
|
return @"rd";
|
|
|
|
} else {
|
|
|
|
return @"th";
|
|
|
|
}
|
2013-10-01 14:19:12 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 10:33:39 -07:00
|
|
|
@end
|