Permalink
Browse files

Refactoring: Move Markdown to resource classes

  • Loading branch information...
1 parent b3c1c6a commit 2aca1250b458287bac90df81bb9727e4a1b7534c @dennisreimann committed Jun 16, 2013
View
@@ -1,13 +1,15 @@
#import "GHResource.h"
-@class GHUser;
+@class GHUser, GHRepository;
@interface GHComment : GHResource
+@property(nonatomic,weak)GHRepository *repository;
@property(nonatomic,strong)GHUser *user;
@property(nonatomic,assign)NSUInteger commentID;
@property(nonatomic,strong)NSString *body;
@property(nonatomic,strong)NSString *bodyWithoutEmailFooter;
+@property(nonatomic,strong)NSMutableAttributedString *attributedBody;
@property(nonatomic,strong)NSDate *createdAt;
@property(nonatomic,strong)NSDate *updatedAt;
@property(nonatomic,readonly)BOOL isNew;
View
@@ -1,5 +1,8 @@
#import "GHComment.h"
#import "GHUser.h"
+#import "GHRepository.h"
+#import "GHFMarkdown.h"
+#import "NSString+Emojize.h"
#import "NSDictionary+Extensions.h"
#import "iOctocat.h"
@@ -17,6 +20,10 @@ - (void)setValues:(id)dict {
}
}
+- (GHRepository *)repository {
+ return _repository;
+}
+
- (BOOL)isNew {
return !self.commentID ? YES : NO;
}
@@ -37,7 +44,17 @@ - (NSString *)bodyWithoutEmailFooter {
- (void)setBody:(NSString *)body {
_bodyWithoutEmailFooter = nil;
+ _attributedBody = nil;
_body = body;
}
+- (NSMutableAttributedString *)attributedBody {
+ if (!_attributedBody) {
+ NSString *text = self.bodyWithoutEmailFooter;
+ text = [text emojizedString];
+ _attributedBody = [text mutableAttributedStringFromGHFMarkdownWithContextRepoId:self.repository.repoId];
+ }
+ return _attributedBody;
+}
+
@end
View
@@ -23,6 +23,7 @@
@property(nonatomic,readonly)NSString *shortenedSha;
@property(nonatomic,readonly)NSString *shortenedMessage;
@property(nonatomic,readonly)NSString *extendedMessage;
+@property(nonatomic,readonly)NSMutableAttributedString *attributedMessage;
@property(nonatomic,readonly)BOOL hasExtendedMessage;
- (id)initWithRepository:(GHRepository *)repo andCommitID:(NSString *)commitID;
View
@@ -4,11 +4,18 @@
#import "GHRepository.h"
#import "GHRepoComments.h"
#import "iOctocat.h"
+#import "GHFMarkdown.h"
#import "NSURL+Extensions.h"
+#import "NSString+Emojize.h"
#import "NSString+Extensions.h"
#import "NSDictionary+Extensions.h"
+@interface GHCommit ()
+@property(nonatomic,strong)NSMutableAttributedString *attributedMessage;
+@end
+
+
@implementation GHCommit
- (id)initWithRepository:(GHRepository *)repo andCommitID:(NSString *)commitID {
@@ -79,6 +86,15 @@ - (GHRepoComments *)comments {
return _comments;
}
+- (NSMutableAttributedString *)attributedMessage {
+ if (!_attributedMessage) {
+ NSString *text = self.message;
+ text = [text emojizedString];
+ _attributedMessage = [text mutableAttributedStringFromGHFMarkdownWithContextRepoId:self.repository.repoId];
+ }
+ return _attributedMessage;
+}
+
- (NSString *)shortenedSha {
return [self.commitID substringToIndex:7];
}
View
@@ -23,6 +23,7 @@
@property(nonatomic,strong)GHTag *tag;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *content;
+@property(nonatomic,readonly)NSString *contentForDisplay;
@property(nonatomic,readonly)NSString *extendedEventType;
@property(nonatomic,readonly)BOOL isCommentEvent;
@property(nonatomic,readonly)BOOL read;
View
@@ -13,12 +13,15 @@
#import "GHRepoComment.h"
#import "GHIssueComment.h"
#import "iOctocat.h"
+#import "GHFMarkdown.h"
+#import "NSString+Emojize.h"
#import "NSString+Extensions.h"
#import "NSDictionary+Extensions.h"
@interface GHEvent ()
@property(nonatomic,readwrite)BOOL read;
+@property(nonatomic,strong)NSString *contentForDisplay;
@end
@@ -46,6 +49,17 @@ - (NSString *)extendedEventType {
return self.eventType;
}
+- (NSString *)contentForDisplay {
+ if (!_contentForDisplay && self.content) {
+ NSString *text = [self.content emojizedString];
+ NSMutableString *mutableText = [text mutableCopy];
+ [mutableText substituteGHFMarkdown];
+ text = [[mutableText stringByReplacingOccurrencesOfString:@"\n\n" withString:@" "] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
+ _contentForDisplay = text;
+ }
+ return _contentForDisplay;
+}
+
- (void)markAsRead {
self.read = YES;
}
View
@@ -21,6 +21,7 @@
@property(nonatomic,readonly)NSString *repoIdWithIssueNumber;
@property(nonatomic,readonly)BOOL isNew;
@property(nonatomic,readonly)BOOL isOpen;
+@property(nonatomic,readonly)NSMutableAttributedString *attributedBody;
- (id)initWithRepository:(GHRepository *)repo;
@end
View
@@ -6,11 +6,18 @@
#import "GHLabels.h"
#import "GHUser.h"
#import "iOctocat.h"
+#import "GHFMarkdown.h"
#import "NSURL+Extensions.h"
+#import "NSString+Emojize.h"
#import "NSString+Extensions.h"
#import "NSDictionary+Extensions.h"
+@interface GHIssue ()
+@property(nonatomic,strong)NSMutableAttributedString *attributedBody;
+@end
+
+
@implementation GHIssue
- (id)initWithRepository:(GHRepository *)repo {
@@ -49,6 +56,15 @@ - (NSURL *)htmlURL {
return _htmlURL;
}
+- (NSMutableAttributedString *)attributedBody {
+ if (!_attributedBody) {
+ NSString *text = self.body;
+ text = [text emojizedString];
+ _attributedBody = [text mutableAttributedStringFromGHFMarkdownWithContextRepoId:self.repository.repoId];
+ }
+ return _attributedBody;
+}
+
- (GHIssueComments *)comments {
if (!_comments) {
_comments = [[GHIssueComments alloc] initWithParent:self];
View
@@ -26,6 +26,7 @@
@property(nonatomic,readonly)BOOL isOpen;
@property(nonatomic,readonly)BOOL isMerged;
@property(nonatomic,readonly)BOOL isMergeable;
+@property(nonatomic,readonly)NSMutableAttributedString *attributedBody;
- (id)initWithRepository:(GHRepository *)repo;
- (void)mergePullRequest:(NSString *)commitMessage start:(resourceStart)start success:(resourceSuccess)success failure:(resourceFailure)failure;
View
@@ -7,14 +7,17 @@
#import "GHFiles.h"
#import "GHUser.h"
#import "iOctocat.h"
+#import "GHFMarkdown.h"
#import "NSURL+Extensions.h"
+#import "NSString+Emojize.h"
#import "NSString+Extensions.h"
#import "NSDictionary+Extensions.h"
@interface GHPullRequest ()
@property(nonatomic,assign)BOOL isMerged;
@property(nonatomic,assign)BOOL isMergeable;
+@property(nonatomic,strong)NSMutableAttributedString *attributedBody;
@end
@@ -77,6 +80,15 @@ - (NSString *)repoIdWithIssueNumber {
return [NSString stringWithFormat:@"%@#%d", self.repository.repoId, self.number];
}
+- (NSMutableAttributedString *)attributedBody {
+ if (!_attributedBody) {
+ NSString *text = self.body;
+ text = [text emojizedString];
+ _attributedBody = [text mutableAttributedStringFromGHFMarkdownWithContextRepoId:self.repository.repoId];
+ }
+ return _attributedBody;
+}
+
#pragma mark Loading
- (void)setValues:(id)dict {
View
@@ -9,7 +9,6 @@
@property(nonatomic,strong)NSString *path;
@property(nonatomic,assign)NSUInteger position;
@property(nonatomic,assign)NSUInteger line;
-@property(nonatomic,readonly)GHRepository *repository;
- (id)initWithRepo:(GHRepository *)repo;
@end
View
@@ -3,10 +3,6 @@
#import "NSDictionary+Extensions.h"
-@interface GHRepoComment ()
-@property(nonatomic,weak)GHRepository *repository;
-@end
-
@implementation GHRepoComment
- (id)initWithRepo:(GHRepository *)repo {
View
@@ -5,6 +5,7 @@
@interface GHRepository : GHResource
@property(nonatomic,readonly)NSString *repoId;
+@property(nonatomic,readonly)NSMutableAttributedString *attributedDescriptionText;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *owner;
@property(nonatomic,strong)NSString *descriptionText;
View
@@ -9,13 +9,15 @@
#import "GHEvents.h"
#import "GHReadme.h"
#import "GHBranches.h"
+#import "GHFMarkdown.h"
#import "NSURL+Extensions.h"
#import "NSDictionary+Extensions.h"
@interface GHRepository ()
@property(nonatomic,strong)GHRepository *parent;
+@property(nonatomic,strong)NSMutableAttributedString *attributedDescriptionText;
@end
@@ -45,6 +47,18 @@ - (NSString *)repoIdAndStatus {
return [NSString stringWithFormat:@"%@/%@/%@", self.owner, self.isPrivate ? @"private" : @"public", self.name];
}
+- (void)setDescriptionText:(NSString *)descriptionText {
+ _attributedDescriptionText = nil;
+ _descriptionText = descriptionText;
+}
+
+- (NSMutableAttributedString *)attributedDescriptionText {
+ if (!_attributedDescriptionText) {
+ _attributedDescriptionText = [self.descriptionText mutableAttributedStringFromGHFMarkdownWithContextRepoId:self.repoId];
+ }
+ return _attributedDescriptionText;
+}
+
- (NSURL *)htmlURL {
if (!_htmlURL) {
self.htmlURL = [NSURL URLWithFormat:@"/%@/%@", self.owner, self.name];
View
@@ -19,9 +19,6 @@ @implementation IOCCommentCell
- (void)awakeFromNib {
[super awakeFromNib];
- self.linksEnabled = YES;
- self.emojiEnabled = YES;
- self.markdownEnabled = YES;
self.gravatarButton.layer.cornerRadius = 3;
self.gravatarButton.layer.masksToBounds = YES;
self.userButton.titleLabel.opaque = YES;
@@ -37,7 +34,8 @@ - (void)setComment:(GHComment *)comment {
_comment = comment;
[self.comment addObserver:self forKeyPath:UserGravatarKeyPath options:NSKeyValueObservingOptionNew context:nil];
self.dateLabel.text = self.comment.createdAt.prettyDate;
- self.contentText = self.comment.bodyWithoutEmailFooter;
+ self.contentText = self.comment.attributedBody;
+ self.rawContentText = self.comment.body;
self.userLogin = self.comment.user.login;
self.gravatar = self.comment.user.gravatar ? self.comment.user.gravatar : [UIImage imageNamed:@"AvatarBackground32.png"];
}
@@ -80,10 +80,6 @@ - (void)viewDidLoad {
self.statusCell = [[IOCResourceStatusCell alloc] initWithResource:self.commit name:NSLocalizedString(@"commit", nil)];
self.commentsStatusCell = [[IOCResourceStatusCell alloc] initWithResource:self.commit.comments name:NSLocalizedString(@"comments", nil)];
self.messageCell.delegate = self;
- self.messageCell.linksEnabled = YES;
- self.messageCell.emojiEnabled = YES;
- self.messageCell.markdownEnabled = YES;
- self.messageCell.contextRepoId = self.commit.repository.repoId;
[self layoutTableHeader];
[self layoutTableFooter];
[self setupInfiniteScrolling];
@@ -141,7 +137,8 @@ - (void)displayCommit {
self.authorCell.accessoryType = self.commit.author ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
self.authoredCell.contentText = self.commit.authoredDate.prettyDate;
self.committedCell.contentText = self.commit.committedDate.prettyDate;
- self.messageCell.contentText = self.commit.message;
+ self.messageCell.contentText = self.commit.attributedMessage;
+ self.messageCell.rawContentText = self.commit.message;
self.tableView.showsInfiniteScrolling = self.commit.comments.hasNextPage;
[self.addedCell setFiles:self.commit.added description:@"added"];
[self.removedCell setFiles:self.commit.removed description:@"removed"];
@@ -269,7 +266,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:self options:nil];
cell = self.commentCell;
- cell.contextRepoId = self.commit.repository.repoId;
}
cell.delegate = self;
GHRepoComment *comment = self.commit.comments[indexPath.row];
View
@@ -12,9 +12,7 @@
#import "GHPullRequest.h"
#import "NSDate+Nibware.h"
#import "NSURL+Extensions.h"
-#import "NSString+Emojize.h"
#import "NSString+Extensions.h"
-#import "GHFMarkdown.h"
#import "NSDictionary+Extensions.h"
#import "TTTAttributedLabel.h"
@@ -38,8 +36,6 @@ @implementation IOCEventCell
- (void)awakeFromNib {
[super awakeFromNib];
self.linksEnabled = NO;
- self.emojiEnabled = YES;
- self.markdownEnabled = YES;
UIColor *linkColor = [UIColor colorWithRed:0.203 green:0.441 blue:0.768 alpha:1.000];
self.gravatarButton.layer.cornerRadius = 3;
self.gravatarButton.layer.masksToBounds = YES;
@@ -62,7 +58,7 @@ - (void)setEvent:(GHEvent *)event {
self.dateLabel.text = [self.event.date prettyDate];
self.contentLabel.lineBreakMode = self.event.isCommentEvent ? NSLineBreakByTruncatingTail : NSLineBreakByWordWrapping;
self.contentLabel.numberOfLines = self.event.isCommentEvent ? 3 : 0;
- self.contentText = self.event.content;
+ self.contentText = self.event.contentForDisplay;
NSString *icon = [NSString stringWithFormat:@"%@.png", self.event.extendedEventType];
self.iconView.image = [UIImage imageNamed:icon];
UIImage *gravatar = self.event.user.gravatar ? self.event.user.gravatar : [UIImage imageNamed:@"AvatarBackground32.png"];
@@ -137,12 +133,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
- (void)setContentText:(NSString *)text {
if ([self.contentText isEqualToString:text]) return;
_contentText = text;
- // parse and modify label text
- if (self.emojiEnabled) text = [text emojizedString];
- NSMutableString *mutableText = [text mutableCopy];
- [mutableText substituteGHFMarkdown];
- text = [[mutableText stringByReplacingOccurrencesOfString:@"\n\n" withString:@" "] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- self.contentLabel.text = [[NSAttributedString alloc] initWithString:text attributes:self.defaultAttributes];
+ self.contentLabel.text = text;
[self adjustContentTextHeight];
}
Oops, something went wrong.

0 comments on commit 2aca125

Please sign in to comment.