NewsBlur/clients/ios/Classes/UnreadCountView.m

165 lines
6.1 KiB
Mathematica
Raw Normal View History

//
// UnreadCountView.m
// NewsBlur
//
// Created by Samuel Clay on 10/3/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "UnreadCountView.h"
#import "UIView+TKCategory.h"
static UIFont *indicatorFont = nil;
@implementation UnreadCountView
const int COUNT_HEIGHT = 15;
@synthesize appDelegate;
@synthesize psWidth, psPadding, ntWidth, ntPadding;
@synthesize psCount, ntCount, blueCount;
@synthesize rect;
+ (void) initialize {
if (self == [UnreadCountView class]) {
indicatorFont = [UIFont boldSystemFontOfSize:12];
}
}
- (void)drawRect:(CGRect)r {
self.userInteractionEnabled = NO;
return [self drawInRect:r ps:psCount nt:ntCount listType:NBFeedListFolder];
}
- (void)drawInRect:(CGRect)r ps:(NSInteger)ps nt:(NSInteger)nt listType:(NBFeedListType)listType {
rect = CGRectInset(r, 12, 12);
rect.size.width -= 18; // Scrollbar padding
2016-03-30 21:46:22 -07:00
if (listType == NBFeedListSaved || (listType == NBFeedListFolder && self.blueCount)) {
blueCount = ps;
psCount = ps;
2016-03-26 21:27:25 -07:00
ntCount = 0;
} else {
2016-03-26 21:27:25 -07:00
blueCount = 0;
psCount = ps;
ntCount = nt;
}
[self calculateOffsets:ps nt:nt];
NSInteger psOffset = ps == 0 ? 0 : psWidth - 20;
NSInteger ntOffset = nt == 0 ? 0 : ntWidth - 20;
if (ps > 0 || blueCount) {
CGRect rr;
if (listType == NBFeedListSocial) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 7, psWidth, COUNT_HEIGHT);
} else {
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 7, psWidth, COUNT_HEIGHT);
}
} else if (listType == NBFeedListFolder) {
2013-02-15 16:41:20 -08:00
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset - 22, 8, psWidth, COUNT_HEIGHT);
} else {
rr = CGRectMake(rect.size.width + rect.origin.x - psOffset, 7, psWidth, COUNT_HEIGHT);
}
if (blueCount) {
2015-12-07 16:09:49 -08:00
[UIColorFromFixedRGB(0x01346B) set];
} else {
2015-12-07 16:09:49 -08:00
[UIColorFromFixedRGB(0x4E872A) set];
}
CGRect rrShadow = CGRectMake(rr.origin.x, rr.origin.y+1, rr.size.width, rr.size.height);
[UIView drawRoundRectangleInRect:rrShadow withRadius:4];
if (blueCount) {
2015-12-07 16:09:49 -08:00
[UIColorFromFixedRGB(0x11448B) set];
} else {
2015-12-07 16:09:49 -08:00
[UIColorFromFixedRGB(0x6EA74A) set];
}
[UIView drawRoundRectangleInRect:rr withRadius:4];
NSString *psStr = [NSString stringWithFormat:@"%ld", (long)ps];
CGSize size = [psStr sizeWithAttributes:@{NSFontAttributeName: indicatorFont}];
float x_pos = (rr.size.width - size.width) / 2;
float y_pos = (rr.size.height - size.height) / 2;
UIColor *psColor;
if (blueCount) {
2015-12-07 16:09:49 -08:00
psColor = UIColorFromFixedRGB(NEWSBLUR_BLACK_COLOR);
} else {
2015-12-07 16:09:49 -08:00
psColor = UIColorFromFixedRGB(0x4E872A);
}
[psStr
drawAtPoint:CGPointMake(rr.origin.x + x_pos, rr.origin.y + y_pos + 1)
withAttributes:@{NSFontAttributeName: indicatorFont,
NSForegroundColorAttributeName: psColor}];
[psStr
drawAtPoint:CGPointMake(rr.origin.x + x_pos, rr.origin.y + y_pos)
withAttributes:@{NSFontAttributeName: indicatorFont,
2015-12-07 16:09:49 -08:00
NSForegroundColorAttributeName: UIColorFromFixedRGB(NEWSBLUR_WHITE_COLOR)}];
}
if (nt > 0 && appDelegate.selectedIntelligence <= 0) {
CGRect rr;
if (listType == NBFeedListSocial) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 7, ntWidth, COUNT_HEIGHT);
} else {
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 7, ntWidth, COUNT_HEIGHT);
}
} else if (listType == NBFeedListFolder) {
2013-02-15 16:41:20 -08:00
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset - 22, 8, ntWidth, COUNT_HEIGHT);
} else {
rr = CGRectMake(rect.size.width + rect.origin.x - psWidth - psPadding - ntOffset, 7, ntWidth, COUNT_HEIGHT);
}
2016-02-27 14:34:10 -08:00
[UIColorFromLightDarkRGB(0x93968D, 0x53564D) set];
CGRect rrShadow = CGRectMake(rr.origin.x, rr.origin.y+1, rr.size.width, rr.size.height);
[UIView drawRoundRectangleInRect:rrShadow withRadius:4];
2016-02-27 14:34:10 -08:00
[UIColorFromLightDarkRGB(0xB3B6AD, 0xA3A69D) set];
2013-06-10 00:29:03 -07:00
[UIView drawRoundRectangleInRect:rr withRadius:4];
NSString *ntStr = [NSString stringWithFormat:@"%ld", (long)nt];
CGSize size = [ntStr sizeWithAttributes:@{NSFontAttributeName: 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 + 1)
withAttributes:@{NSFontAttributeName: indicatorFont,
2016-02-27 14:34:10 -08:00
NSForegroundColorAttributeName:UIColorFromLightSepiaMediumDarkRGB(0x93968D, 0x93968D, 0x93968D, 0x93968D)}];
[ntStr
drawAtPoint:CGPointMake(rr.origin.x + x_pos, rr.origin.y + y_pos)
withAttributes:@{NSFontAttributeName: indicatorFont,
2016-02-27 14:34:10 -08:00
NSForegroundColorAttributeName:UIColorFromLightSepiaMediumDarkRGB(0xFFFFFF, 0xF8F8E9, 0x606060, 0x000000)}];
}
}
- (void)calculateOffsets:(NSInteger)ps nt:(NSInteger)nt {
psWidth = ps == 0 ? 0 : ps < 10 ? 14 : ps < 100 ? 22 : ps < 1000 ? 28 : ps < 10000 ? 34 : 40;
ntWidth = nt == 0 ? 0 : nt < 10 ? 14 : nt < 100 ? 22 : nt < 1000 ? 28 : nt < 10000 ? 34 : 40;
psPadding = ps == 0 ? 0 : 2;
ntPadding = nt == 0 ? 0 : 2;
}
- (NSInteger)offsetWidth {
NSInteger width = 0;
if (self.psCount > 0) {
width += psWidth + psPadding;
}
if (self.ntCount > 0 && appDelegate.selectedIntelligence <= 0) {
width += ntWidth + ntPadding;
}
return width;
}
@end