Skip to content

Commit

Permalink
capture image - can’t seem to pass image as a parameter - NSString er…
Browse files Browse the repository at this point in the history
…ror crash; setImage crash for navigation controller-x

share page -  done except that i cannot pass an image; maybe its because passing the image from a  dynamically created pageviewcontroller is difficult-x
solved this by copying the image in defaults and using it in shareviewcontroller -x
changes to text content UI -x
  • Loading branch information
guruhubb committed Apr 25, 2014
1 parent 19e7039 commit dc9850b
Show file tree
Hide file tree
Showing 15 changed files with 973 additions and 144 deletions.
77 changes: 77 additions & 0 deletions NSDate-Time-Ago/NSDate+NVTimeAgo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// NSDate+NVTimeAgo.h
// Adventures
//
// Created by Nikil Viswanathan on 4/18/13.
// Copyright (c) 2013 Nikil Viswanathan. All rights reserved.
//

/*
This NSDate category adds the facebook style "time ago" date formatting.
This mimics Facebook mobile (the desktop version has slightly different date formatting).
This assumes all dates are in the past.
To use this in your iOS project:
--------------------------------
1. Drag both NSDate+NVTimeAgo.m aand NSDate+NVTimeAgo.h into your iOS project in XCode
2. In the files that you want to use this functionality in,itType:
#import "NSDate+NVTimeAgo.h"
somewhere near the top of your file.
3. Use the date formatter on a date by calling:
[date formattedAsTimeAgo];
where date is an (NSDate *) and represents a date IN THE PAST (relative to now).
If you have a mysql datetime string and you want to convert it to the time ago format, do:
NSString *mysqlDatetime = <Get from the database>
NSString *timeAgoFormattedDate = [NSDate mysqlDatetimeFormattedAsTimeAgo:mysqlDatetime];
Made By Nikil Viswanathan
-------------------------
4/18/2013
You can contact me on: www.nikilster.com
Date Format
-----------
< 1 minute = "Just now"
< 1 hour = "x minutes ago"
Today = "x hours ago"
Yesterday = "Yesterday at 1:28pm"
< Last 7 days = "Friday at 1:48am"
< Last 30 days = "March 30 at 1:14 pm"
< 1 year = "September 15"
Anything else = "September 9, 2011"
*/

#import <Foundation/Foundation.h>

@interface NSDate (NVFacebookTimeAgo)

/*
Mysql Datetime Formatted As Time Ago
Takes in a mysql datetime string and returns the Time Ago date format
*/
+ (NSString*)mysqlDatetimeFormattedAsTimeAgo:(NSString *)mysqlDatetime;


/*
Formatted As Time Ago
Returns the time formatted as Time Ago (in the style of Facebook's mobile date formatting)
*/
- (NSString *)formattedAsTimeAgo;

@end
318 changes: 318 additions & 0 deletions NSDate-Time-Ago/NSDate+NVTimeAgo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
//
// NSDate+NVTimeAgo.m
// Adventures
//
// Created by Nikil Viswanathan on 4/18/13.
// Copyright (c) 2013 Nikil Viswanathan. All rights reserved.
//

#import "NSDate+NVTimeAgo.h"

@implementation NSDate (NVFacebookTimeAgo)


#define SECOND 1
#define MINUTE (SECOND * 60)
#define HOUR (MINUTE * 60)
#define DAY (HOUR * 24)
#define WEEK (DAY * 7)
#define MONTH (DAY * 31)
#define YEAR (DAY * 365.24)

/*
Mysql Datetime Formatted As Time Ago
Takes in a mysql datetime string and returns the Time Ago date format
*/
+ (NSString *)mysqlDatetimeFormattedAsTimeAgo:(NSString *)mysqlDatetime
{
//http://stackoverflow.com/questions/10026714/ios-converting-a-date-received-from-a-mysql-server-into-users-local-time
//If this is not in UTC, we don't have any knowledge about
//which tz it is. MUST BE IN UTC.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [formatter dateFromString:mysqlDatetime];

return [date formattedAsTimeAgo];

}


/*
Formatted As Time Ago
Returns the date formatted as Time Ago (in the style of the mobile time ago date formatting for Facebook)
*/
- (NSString *)formattedAsTimeAgo
{
//Now
NSDate *now = [NSDate date];
NSTimeInterval secondsSince = -(int)[self timeIntervalSinceDate:now];

//Should never hit this but handle the future case
if(secondsSince < 0)
return @"In The Future";


// < 1 minute = "Just now"
if(secondsSince < MINUTE)
return @"Just now";


// < 1 hour = "x minutes ago"
if(secondsSince < HOUR)
return [self formatMinutesAgo:secondsSince];


// Today = "x hours ago"
if([self isSameDayAs:now])
return [self formatAsToday:secondsSince];


// Yesterday = "Yesterday at 1:28 PM"
if([self isYesterday:now])
return [self formatAsYesterday];


// < Last 7 days = "Friday at 1:48 AM"
if([self isLastWeek:secondsSince])
return [self formatAsLastWeek];


// < Last 30 days = "March 30 at 1:14 PM"
if([self isLastMonth:secondsSince])
return [self formatAsLastMonth];

// < 1 year = "September 15"
if([self isLastYear:secondsSince])
return [self formatAsLastYear];

// Anything else = "September 9, 2011"
return [self formatAsOther];

}



/*
========================== Date Comparison Methods ==========================
*/

/*
Is Same Day As
Checks to see if the dates are the same calendar day
*/
- (BOOL)isSameDayAs:(NSDate *)comparisonDate
{
//Check by matching the date strings
NSDateFormatter *dateComparisonFormatter = [[NSDateFormatter alloc] init];
[dateComparisonFormatter setDateFormat:@"yyyy-MM-dd"];

//Return true if they are the same
return [[dateComparisonFormatter stringFromDate:self] isEqualToString:[dateComparisonFormatter stringFromDate:comparisonDate]];
}




/*
If the current date is yesterday relative to now
Pasing in now to be more accurate (time shift during execution) in the calculations
*/
- (BOOL)isYesterday:(NSDate *)now
{
return [self isSameDayAs:[now dateBySubtractingDays:1]];
}


//From https://github.com/erica/NSDate-Extensions/blob/master/NSDate-Utilities.m
- (NSDate *) dateBySubtractingDays: (NSInteger) numDays
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + DAY * -numDays;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}


/*
Is Last Week
We want to know if the current date object is the first occurance of
that day of the week (ie like the first friday before today
- where we would colloquially say "last Friday")
( within 6 of the last days)
TODO: make this more precise (1 week ago, if it is 7 days ago check the exact date)
*/
- (BOOL)isLastWeek:(NSTimeInterval)secondsSince
{
return secondsSince < WEEK;
}


/*
Is Last Month
Previous 31 days?
TODO: Validate on fb
TODO: Make last day precise
*/
- (BOOL)isLastMonth:(NSTimeInterval)secondsSince
{
return secondsSince < MONTH;
}


/*
Is Last Year
TODO: Make last day precise
*/

- (BOOL)isLastYear:(NSTimeInterval)secondsSince
{
return secondsSince < YEAR;
}

/*
=============================================================================
*/





/*
========================== Formatting Methods ==========================
*/


// < 1 hour = "x minutes ago"
- (NSString *)formatMinutesAgo:(NSTimeInterval)secondsSince
{
//Convert to minutes
int minutesSince = (int)secondsSince / MINUTE;

//Handle Plural
if(minutesSince == 1)
return @"1 minute ago";
else
return [NSString stringWithFormat:@"%d minutes ago", minutesSince];
}


// Today = "x hours ago"
- (NSString *)formatAsToday:(NSTimeInterval)secondsSince
{
//Convert to hours
int hoursSince = (int)secondsSince / HOUR;

//Handle Plural
if(hoursSince == 1)
return @"1 hour ago";
else
return [NSString stringWithFormat:@"%d hours ago", hoursSince];
}


// Yesterday = "Yesterday at 1:28 PM"
- (NSString *)formatAsYesterday
{
//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"h:mm a"];
return [NSString stringWithFormat:@"Yesterday at %@", [dateFormatter stringFromDate:self]];
}


// < Last 7 days = "Friday at 1:48 AM"
- (NSString *)formatAsLastWeek
{
//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"EEEE 'at' h:mm a"];
return [dateFormatter stringFromDate:self];
}


// < Last 30 days = "March 30 at 1:14 PM"
- (NSString *)formatAsLastMonth
{
//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"MMMM d 'at' h:mm a"];
return [dateFormatter stringFromDate:self];
}


// < 1 year = "September 15"
- (NSString *)formatAsLastYear
{
//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"MMMM d"];
return [dateFormatter stringFromDate:self];
}


// Anything else = "September 9, 2011"
- (NSString *)formatAsOther
{
//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"LLLL d, yyyy"];
return [dateFormatter stringFromDate:self];
}


/*
=======================================================================
*/





/*
========================== Test Method ==========================
*/

/*
Test the format
TODO: Implement unit tests
*/
+ (void)runTests
{
NSLog(@"1 Second in the future: %@\n", [[NSDate dateWithTimeIntervalSinceNow:1] formattedAsTimeAgo]);
NSLog(@"Now: %@\n", [[NSDate dateWithTimeIntervalSinceNow:0] formattedAsTimeAgo]);
NSLog(@"1 Second: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-1] formattedAsTimeAgo]);
NSLog(@"10 Seconds: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-10] formattedAsTimeAgo]);
NSLog(@"1 Minute: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-60] formattedAsTimeAgo]);
NSLog(@"2 Minutes: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-120] formattedAsTimeAgo]);
NSLog(@"1 Hour: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-HOUR] formattedAsTimeAgo]);
NSLog(@"2 Hours: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-2*HOUR] formattedAsTimeAgo]);
NSLog(@"1 Day: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-1*DAY] formattedAsTimeAgo]);
NSLog(@"1 Day + 3 seconds: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-1*DAY-3] formattedAsTimeAgo]);
NSLog(@"2 Days: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-2*DAY] formattedAsTimeAgo]);
NSLog(@"3 Days: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-3*DAY] formattedAsTimeAgo]);
NSLog(@"5 Days: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-5*DAY] formattedAsTimeAgo]);
NSLog(@"6 Days: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-6*DAY] formattedAsTimeAgo]);
NSLog(@"7 Days - 1 second: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-7*DAY+1] formattedAsTimeAgo]);
NSLog(@"10 Days: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-10*DAY] formattedAsTimeAgo]);
NSLog(@"1 Month + 1 second: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-MONTH-1] formattedAsTimeAgo]);
NSLog(@"1 Year - 1 second: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-YEAR+1] formattedAsTimeAgo]);
NSLog(@"1 Year + 1 second: %@\n", [[NSDate dateWithTimeIntervalSinceNow:-YEAR+1] formattedAsTimeAgo]);
}
/*
=======================================================================
*/



@end
Loading

0 comments on commit dc9850b

Please sign in to comment.