Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Created June 19, 2015 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsavit1/156f529a858f3be332f0 to your computer and use it in GitHub Desktop.
Save hsavit1/156f529a858f3be332f0 to your computer and use it in GitHub Desktop.
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) {
currentValue = [NSString stringWithFormat:@"%uh", (unsigned int)hours];
}
else if (minutes) {
currentValue = [NSString stringWithFormat:@"%um", (unsigned int)minutes];
updateAfter = 60;
}
else {
if (seconds < 5.0) {
currentValue = @"just now";
}
else {
currentValue = [NSString stringWithFormat:@"%us", (unsigned int)seconds];
}
updateAfter = 1;
}
[subscriber sendNext:currentValue];
RACSerialDisposable *disposable = [RACSerialDisposable serialDisposableWithDisposable:nil];
if (updateAfter != NSNotFound) {
disposable.disposable = [[RACScheduler currentScheduler] afterDelay:updateAfter schedule:^{
[subscriber sendCompleted];
}];
}
return disposable;
}] repeat];
}] switchToLatest];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment