Skip to content

Commit

Permalink
added support for form strings on CCL games
Browse files Browse the repository at this point in the history
  • Loading branch information
akeaswaransfdc committed Mar 3, 2020
1 parent 0aa4c1a commit b9729b0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
2 changes: 2 additions & 0 deletions mls-bar/Core/SharedUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
+ (NSColor *)pickColorBasedOnContrastWithBackground:(NSColor *)backgroundColor color1:(NSColor *)color1 color2:(NSColor *)color2;
+ (NSComparisonResult)compareEvents:(id)obj1 obj2:(id)obj2;
+ (NSInteger)retrieveCurrentUpdateInterval;
+ (NSAttributedString *)formattedFormString:(NSString *)formString;
+ (NSAttributedString *)formattedFormString:(NSString *)formString extraAttributes:(NSDictionary *)attrs;
@end
35 changes: 35 additions & 0 deletions mls-bar/Core/SharedUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ + (NSComparisonResult)compareEvents:(id)obj1 obj2:(id)obj2 {
return [[NSNumber numberWithInteger:[objA[@"id"] integerValue]] compare:[NSNumber numberWithInteger:[objB[@"id"] integerValue]]];
}

+ (NSAttributedString *)formattedFormString:(NSString *)formString {
return [self formattedFormString:formString extraAttributes:@{}];
}

+ (NSAttributedString *)formattedFormString:(NSString *)formString extraAttributes:(NSDictionary *)attrs {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes addEntriesFromDictionary:@{NSParagraphStyleAttributeName : paragraphStyle}];
[attributes addEntriesFromDictionary:attrs];

NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:formString attributes:attributes];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"W|L|D" options:NSRegularExpressionCaseInsensitive error:nil];

NSArray *arrayOfAllMatches = [regex matchesInString:formString options:0 range:NSMakeRange(0, formString.length)];

for (NSTextCheckingResult *match in arrayOfAllMatches) {
if ([[formString substringWithRange:[match range]] isEqualToString:@"W"]) {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor systemGreenColor] range:match.range];
} else if ([[formString substringWithRange:[match range]] isEqualToString:@"L"]) {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor systemRedColor] range:match.range];
} else {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor secondaryLabelColor] range:match.range];
}
}

if (goodText.length == 0) {
goodText = [[NSMutableAttributedString alloc] initWithString:@"N/A" attributes:@{NSForegroundColorAttributeName : [NSColor secondaryLabelColor]}];
}

return goodText;
}


+ (NSInteger)retrieveCurrentUpdateInterval {
NSInteger curUpdateInterval = [[NSUserDefaults standardUserDefaults] integerForKey:DNV_UPDATE_INTERVAL_KEY];
if (curUpdateInterval <= 0) {
Expand Down
31 changes: 2 additions & 29 deletions mls-bar/View Controllers/PregameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
} else if ([stat[@"name"] isEqualToString:@"Recent Form"]) {
[cellView.homeStatLabel setAlphaValue:1.0];
[cellView.awayStatLabel setAlphaValue:1.0];
[cellView.homeStatLabel setAttributedStringValue:[self formattedFormString:stat[@"homeTeam"][@"value"]]];
[cellView.awayStatLabel setAttributedStringValue:[self formattedFormString:stat[@"awayTeam"][@"value"]]];
[cellView.homeStatLabel setAttributedStringValue:[SharedUtils formattedFormString:stat[@"homeTeam"][@"value"]]];
[cellView.awayStatLabel setAttributedStringValue:[SharedUtils formattedFormString:stat[@"awayTeam"][@"value"]]];
} else {
if ([stat[@"homeTeam"][@"value"] intValue] > [stat[@"awayTeam"][@"value"] intValue]) {
[cellView.awayStatLabel setTextColor:[NSColor labelColor]];
Expand Down Expand Up @@ -272,33 +272,6 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
}
}

-(NSAttributedString *)formattedFormString:(NSString *)formString {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSParagraphStyleAttributeName : paragraphStyle};
NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:formString attributes:attributes];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"W|L|D" options:NSRegularExpressionCaseInsensitive error:nil];

NSArray *arrayOfAllMatches = [regex matchesInString:formString options:0 range:NSMakeRange(0, formString.length)];

for (NSTextCheckingResult *match in arrayOfAllMatches) {
if ([[formString substringWithRange:[match range]] isEqualToString:@"W"]) {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor systemGreenColor] range:match.range];
} else if ([[formString substringWithRange:[match range]] isEqualToString:@"L"]) {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor systemRedColor] range:match.range];
} else {
[goodText addAttribute:NSForegroundColorAttributeName value:[NSColor secondaryLabelColor] range:match.range];
}
}

if (goodText.length == 0) {
goodText = [[NSMutableAttributedString alloc] initWithString:@"N/A" attributes:@{NSForegroundColorAttributeName : [NSColor secondaryLabelColor]}];
}

return goodText;
}

-(BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row {
return NO;
}
Expand Down
10 changes: 8 additions & 2 deletions mls-bar/View Controllers/ScoresViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn

[cellView.awayRecordLabel setStringValue:[NSString stringWithFormat:@"%@ %@ (%@)", [item.awayCompetitor points], awayPtsNoun, item.awayCompetitor.records[0][@"summary"]]];
} else {
[cellView.awayRecordLabel setStringValue:[NSString stringWithFormat:@"Form: %@", item.awayCompetitor.form]];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentRight;
[cellView.awayRecordLabel setAttributedStringValue:[SharedUtils formattedFormString:[NSString stringWithFormat:@"Form: %@", item.awayCompetitor.form] extraAttributes:@{NSParagraphStyleAttributeName : paragraphStyle}]];
}

if (item.homeCompetitor.records[0][@"summary"] != nil) {
Expand All @@ -239,7 +241,9 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn

[cellView.homeRecordLabel setStringValue:[NSString stringWithFormat:@"%@ %@ (%@)", [item.homeCompetitor points], homePtsNoun, item.homeCompetitor.records[0][@"summary"]]];
} else {
[cellView.homeRecordLabel setStringValue:[NSString stringWithFormat:@"Form: %@", item.homeCompetitor.form]];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentLeft;
[cellView.homeRecordLabel setAttributedStringValue:[SharedUtils formattedFormString:[NSString stringWithFormat:@"Form: %@", item.homeCompetitor.form] extraAttributes:@{NSParagraphStyleAttributeName : paragraphStyle}]];
}


Expand All @@ -259,6 +263,8 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
[cellView.statusField setStringValue:[item.startDate formattedDateWithFormat:@"h:mm a"]];
} else if (item.status == GameStateFinal && [item.statusDescription containsString:@"45"]) {
[cellView.statusField setStringValue:@"HT"];
} else if (item.status == GameStateFinal && [item.statusDescription containsString:@"9"]) {
[cellView.statusField setStringValue:@"FT"];
} else {
[cellView.statusField setStringValue:item.statusDescription];
}
Expand Down

0 comments on commit b9729b0

Please sign in to comment.