Permalink
Browse files

Merge pull request #404 from koraktor/fix-event-links

Improve linking of event items
  • Loading branch information...
2 parents 12b06db + 961bfbf commit 3e9b21a7b8244b4c05f518d78ac6b11db4f308b7 @dennisreimann committed Jul 21, 2013
Showing with 37 additions and 38 deletions.
  1. +37 −38 Classes/IOCEventCell.m
View
@@ -63,43 +63,58 @@ - (void)setEvent:(GHEvent *)event {
[self.event addObserver:self forKeyPath:UserGravatarKeyPath options:NSKeyValueObservingOptionNew context:nil];
[self setGravatar:gravatar];
// actions
+ [self linkEventItems];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ if ([keyPath isEqualToString:UserGravatarKeyPath] && self.event.user.gravatar) {
+ [self setGravatar:self.event.user.gravatar];
+ }
+}
+
+- (void)setContentText:(NSString *)text {
+ self.contentLabel.text = text;
+ [self adjustContentTextHeight];
+}
+
+#pragma mark Helpers
+
+- (void)linkEventItems {
+ void (^addLinkToTitle)(NSString *, NSURL *);
+ addLinkToTitle = ^(NSString *linkText, NSURL *linkUrl) {
+ linkText = [NSString stringWithFormat:@"^%1$@\\s|\\s%1$@\\s|\\s%1$@$", linkText];
+ NSRange range = [self.titleLabel.text rangeOfString:linkText options:NSRegularExpressionSearch];
+ [self.titleLabel addLinkToURL:linkUrl withRange:range];
+ };
+
if (self.event.user) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.user.login];
- [self.titleLabel addLinkToURL:self.event.user.htmlURL withRange:range];
+ addLinkToTitle(self.event.user.login, self.event.user.htmlURL);
}
if (self.event.organization) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.organization.login];
- [self.titleLabel addLinkToURL:self.event.organization.htmlURL withRange:range];
+ addLinkToTitle(self.event.organization.login, self.event.organization.htmlURL);
}
if (self.event.otherUser) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.otherUser.login];
- [self.titleLabel addLinkToURL:self.event.otherUser.htmlURL withRange:range];
+ addLinkToTitle(self.event.otherUser.login, self.event.otherUser.htmlURL);
}
if (self.event.repository) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.repository.repoId];
- [self.titleLabel addLinkToURL:self.event.repository.htmlURL withRange:range];
+ addLinkToTitle(self.event.repository.repoId, self.event.repository.htmlURL);
}
if (self.event.otherRepository) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.otherRepository.repoId];
- [self.titleLabel addLinkToURL:self.event.otherRepository.htmlURL withRange:range];
+ addLinkToTitle(self.event.otherRepository.repoId, self.event.otherRepository.htmlURL);
}
if (self.event.issue && !self.event.pullRequest) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.issue.repoIdWithIssueNumber];
- [self.titleLabel addLinkToURL:self.event.issue.htmlURL withRange:range];
+ addLinkToTitle(self.event.issue.repoIdWithIssueNumber, self.event.issue.htmlURL);
}
if (self.event.pullRequest) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.pullRequest.repoIdWithIssueNumber];
- [self.titleLabel addLinkToURL:self.event.pullRequest.htmlURL withRange:range];
+ addLinkToTitle(self.event.pullRequest.repoIdWithIssueNumber, self.event.pullRequest.htmlURL);
}
if (self.event.gist) {
- NSRange range = [self.titleLabel.text rangeOfString:[NSString stringWithFormat:@"gist %@", self.event.gist.gistId]];
- [self.titleLabel addLinkToURL:self.event.gist.htmlURL withRange:range];
+ addLinkToTitle([NSString stringWithFormat:@"gist %@", self.event.gist.gistId], self.event.gist.htmlURL);
}
if (self.event.commits && self.event.commits.count > 0) {
GHCommit *commit = self.event.commits[0];
- NSRange range = [self.titleLabel.text rangeOfString:commit.shortenedSha];
- [self.titleLabel addLinkToURL:commit.htmlURL withRange:range];
- // commits
+ addLinkToTitle(commit.shortenedSha, commit.htmlURL);
+ //commits
for (GHCommit *commit in self.event.commits.items) {
NSRange range = [self.contentLabel.text rangeOfString:commit.shortenedSha];
[self.contentLabel addLinkToURL:commit.htmlURL withRange:range];
@@ -109,32 +124,16 @@ - (void)setEvent:(GHEvent *)event {
NSDictionary *wiki = self.event.pages[0];
NSString *pageName = [wiki ioc_stringForKey:@"page_name"];
NSURL *htmlURL = [wiki ioc_URLForKey:@"html_url"];
- NSRange range = [self.titleLabel.text rangeOfString:[NSString stringWithFormat:@"\"%@\"", pageName]];
- [self.titleLabel addLinkToURL:htmlURL withRange:range];
+ addLinkToTitle([NSString stringWithFormat:@"\"%@\"", pageName], htmlURL);
}
if (self.event.branch) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.ref];
- [self.titleLabel addLinkToURL:self.event.branch.htmlURL withRange:range];
+ addLinkToTitle(self.event.ref, self.event.branch.htmlURL);
}
if (self.event.tag) {
- NSRange range = [self.titleLabel.text rangeOfString:self.event.ref];
- [self.titleLabel addLinkToURL:self.event.tag.htmlURL withRange:range];
+ addLinkToTitle(self.event.ref, self.event.tag.htmlURL);
}
}
-- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- if ([keyPath isEqualToString:UserGravatarKeyPath] && self.event.user.gravatar) {
- [self setGravatar:self.event.user.gravatar];
- }
-}
-
-- (void)setContentText:(NSString *)text {
- self.contentLabel.text = text;
- [self adjustContentTextHeight];
-}
-
-#pragma mark Helpers
-
- (void)markAsNew {
UIColor *color = [UIColor colorWithHue:0.6 saturation:0.09 brightness:1.0 alpha:1.0];
[self setCustomBackgroundColor:color];

0 comments on commit 3e9b21a

Please sign in to comment.