Created
June 19, 2015 08:05
-
-
Save hsavit1/156f529a858f3be332f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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