2012-10-03 18:27:21 -07:00
|
|
|
//
|
|
|
|
// UnreadCountView.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 10/3/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
2012-10-03 18:27:21 -07:00
|
|
|
#import "UnreadCountView.h"
|
|
|
|
#import "UIView+TKCategory.h"
|
|
|
|
|
|
|
|
static UIFont *indicatorFont = nil;
|
|
|
|
static UIColor *indicatorWhiteColor = nil;
|
|
|
|
static UIColor *indicatorBlackColor = nil;
|
|
|
|
static UIColor *positiveBackgroundColor = nil;
|
|
|
|
static UIColor *neutralBackgroundColor = nil;
|
|
|
|
static UIColor *negativeBackgroundColor = nil;
|
|
|
|
|
|
|
|
@implementation UnreadCountView
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
@synthesize psWidth, psPadding, ntWidth, ntPadding;
|
2012-10-07 15:47:21 -04:00
|
|
|
@synthesize psCount, ntCount;
|
2012-10-03 18:27:21 -07:00
|
|
|
@synthesize rect;
|
|
|
|
|
|
|
|
+ (void) initialize {
|
|
|
|
if (self == [UnreadCountView class]) {
|
|
|
|
indicatorFont = [UIFont boldSystemFontOfSize:12];
|
|
|
|
indicatorWhiteColor = [UIColor whiteColor];
|
|
|
|
indicatorBlackColor = [UIColor blackColor];
|
|
|
|
|
|
|
|
UIColor *ps = UIColorFromRGB(0x3B7613);
|
|
|
|
UIColor *nt = UIColorFromRGB(0xF9C72A);
|
|
|
|
UIColor *ng = UIColorFromRGB(0xCC2A2E);
|
|
|
|
positiveBackgroundColor = ps;
|
|
|
|
neutralBackgroundColor = nt;
|
|
|
|
negativeBackgroundColor = ng;
|
|
|
|
// UIColor *psGrad = UIColorFromRGB(0x559F4D);
|
|
|
|
// UIColor *ntGrad = UIColorFromRGB(0xE4AB00);
|
|
|
|
// UIColor *ngGrad = UIColorFromRGB(0x9B181B);
|
|
|
|
// const CGFloat* psTop = CGColorGetComponents(ps.CGColor);
|
|
|
|
// const CGFloat* psBot = CGColorGetComponents(psGrad.CGColor);
|
|
|
|
// CGFloat psGradient[] = {
|
|
|
|
// psTop[0], psTop[1], psTop[2], psTop[3],
|
|
|
|
// psBot[0], psBot[1], psBot[2], psBot[3]
|
|
|
|
// };
|
|
|
|
// psColors = psGradient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
- (void)drawRect:(CGRect)r {
|
|
|
|
self.userInteractionEnabled = NO;
|
2012-10-04 15:44:25 -07:00
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
return [self drawInRect:r ps:psCount nt:ntCount listType:NBFeedListFolder];
|
2012-10-04 15:44:25 -07:00
|
|
|
}
|
|
|
|
|
2012-10-03 18:27:21 -07:00
|
|
|
- (void)drawInRect:(CGRect)r ps:(int)ps nt:(int)nt listType:(NBFeedListType)listType {
|
2012-10-04 15:44:25 -07:00
|
|
|
|
2012-10-03 18:27:21 -07:00
|
|
|
rect = CGRectInset(r, 12, 12);
|
|
|
|
rect.size.width -= 18; // Scrollbar padding
|
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
psCount = ps;
|
|
|
|
ntCount = nt;
|
|
|
|
[self calculateOffsets:ps nt:nt];
|
2012-10-03 18:27:21 -07:00
|
|
|
|
|
|
|
int psOffset = ps == 0 ? 0 : psWidth - 20;
|
2012-10-04 15:44:25 -07:00
|
|
|
int ntOffset = nt == 0 ? 0 : ntWidth - 20;
|
2012-10-03 18:27:21 -07:00
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
if (ps > 0) {
|
2012-10-03 18:27:21 -07:00
|
|
|
[positiveBackgroundColor set];
|
|
|
|
CGRect rr;
|
|
|
|
|
|
|
|
if (listType == NBFeedListSocial) {
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 14, psWidth, 17);
|
|
|
|
} else {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 10, psWidth, 17);
|
|
|
|
}
|
2012-10-04 15:44:25 -07:00
|
|
|
} else if (listType == NBFeedListFolder) {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset - 22, 5, psWidth, 17);
|
2012-10-03 18:27:21 -07:00
|
|
|
} else {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 7, psWidth, 17);
|
|
|
|
}
|
|
|
|
|
|
|
|
[UIView drawRoundRectangleInRect:rr withRadius:4];
|
|
|
|
|
|
|
|
[indicatorWhiteColor set];
|
|
|
|
|
|
|
|
NSString *psStr = [NSString stringWithFormat:@"%d", ps];
|
|
|
|
CGSize size = [psStr sizeWithFont:indicatorFont];
|
|
|
|
float x_pos = (rr.size.width - size.width) / 2;
|
|
|
|
float y_pos = (rr.size.height - size.height) / 2;
|
|
|
|
[psStr
|
|
|
|
drawAtPoint:CGPointMake(rr.origin.x + x_pos, rr.origin.y + y_pos)
|
|
|
|
withFont:indicatorFont];
|
|
|
|
}
|
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
if (nt > 0 && appDelegate.selectedIntelligence <= 0) {
|
2012-10-03 18:27:21 -07:00
|
|
|
[neutralBackgroundColor set];
|
|
|
|
|
|
|
|
CGRect rr;
|
|
|
|
if (listType == NBFeedListSocial) {
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 14, ntWidth, 17);
|
|
|
|
} else {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 10, ntWidth, 17);
|
|
|
|
}
|
2012-10-04 15:44:25 -07:00
|
|
|
} else if (listType == NBFeedListFolder) {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset - 22, 5, ntWidth, 17);
|
2012-10-03 18:27:21 -07:00
|
|
|
} else {
|
|
|
|
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 7, ntWidth, 17);
|
|
|
|
}
|
|
|
|
|
|
|
|
[UIView drawRoundRectangleInRect:rr withRadius:4];
|
|
|
|
// [UIView drawLinearGradientInRect:rr colors:ntColors];
|
|
|
|
|
|
|
|
[indicatorBlackColor set];
|
|
|
|
|
|
|
|
NSString *ntStr = [NSString stringWithFormat:@"%d", nt];
|
|
|
|
CGSize size = [ntStr sizeWithFont:indicatorFont];
|
|
|
|
float x_pos = (rr.size.width - size.width) / 2;
|
|
|
|
float y_pos = (rr.size.height - size.height) / 2;
|
|
|
|
[ntStr
|
|
|
|
drawAtPoint:CGPointMake(rr.origin.x + x_pos, rr.origin.y + y_pos)
|
|
|
|
withFont:indicatorFont];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 15:47:21 -04:00
|
|
|
- (void)calculateOffsets:(int)ps nt:(int)nt {
|
|
|
|
psWidth = ps == 0 ? 0 : ps < 10 ? 14 : ps < 100 ? 22 : 28;
|
|
|
|
ntWidth = nt == 0 ? 0 : nt < 10 ? 14 : nt < 100 ? 22 : 28;
|
|
|
|
|
|
|
|
psPadding = ps == 0 ? 0 : 2;
|
|
|
|
ntPadding = nt == 0 ? 0 : 2;
|
|
|
|
}
|
|
|
|
|
2012-10-03 18:27:21 -07:00
|
|
|
- (int)offsetWidth {
|
2012-10-07 15:47:21 -04:00
|
|
|
int width = 0;
|
|
|
|
if (self.psCount > 0) {
|
|
|
|
width += psWidth + psPadding;
|
|
|
|
}
|
|
|
|
if (self.ntCount > 0 && appDelegate.selectedIntelligence <= 0) {
|
|
|
|
width += ntWidth + ntPadding;
|
|
|
|
}
|
|
|
|
return width;
|
2012-10-03 18:27:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|