1//
  2//  BlockBackground.m
  3//  arrived
  4//
  5//  Created by Gustavo Ambrozio on 29/11/11.
  6//  Copyright (c) 2011 N/A. All rights reserved.
  7//
  8
  9#import "BlockBackground.h"
 10
 11@implementation BlockBackground
 12
 13@synthesize backgroundImage = _backgroundImage;
 14@synthesize vignetteBackground = _vignetteBackground;
 15
 16static BlockBackground *_sharedInstance = nil;
 17
 18+ (BlockBackground*)sharedInstance
 19{
 20    if (_sharedInstance != nil) {
 21        return _sharedInstance;
 22    }
 23
 24    @synchronized(self) {
 25        if (_sharedInstance == nil) {
 26            _sharedInstance = [[self alloc] init];
 27        }
 28    }
 29    
 30    return _sharedInstance;
 31}
 32
 33+ (id)allocWithZone:(NSZone*)zone
 34{
 35    @synchronized(self) {
 36        if (_sharedInstance == nil) {
 37            _sharedInstance = [super allocWithZone:zone];
 38            return _sharedInstance;
 39        }
 40    }
 41    NSAssert(NO, @ "[BlockBackground alloc] explicitly called on singleton class.");
 42    return nil;
 43}
 44
 45- (id)copyWithZone:(NSZone*)zone
 46{
 47    return self;
 48}
 49
 50- (id)retain
 51{
 52    return self;
 53}
 54
 55- (unsigned)retainCount
 56{
 57    return UINT_MAX;
 58}
 59
 60- (oneway void)release
 61{
 62}
 63
 64- (id)autorelease
 65{
 66    return self;
 67}
 68
 69- (void)setRotation:(NSNotification*)notification
 70{
 71    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
 72    
 73    CGRect orientationFrame = [UIScreen mainScreen].bounds;
 74    
 75    if(
 76       (UIInterfaceOrientationIsLandscape(orientation) && orientationFrame.size.height > orientationFrame.size.width) ||
 77       (UIInterfaceOrientationIsPortrait(orientation) && orientationFrame.size.width > orientationFrame.size.height)
 78       ) {
 79        float temp = orientationFrame.size.width;
 80        orientationFrame.size.width = orientationFrame.size.height;
 81        orientationFrame.size.height = temp;
 82    }
 83    
 84    self.transform = CGAffineTransformIdentity;
 85    self.frame = orientationFrame;
 86    
 87    CGFloat posY = orientationFrame.size.height/2;
 88    CGFloat posX = orientationFrame.size.width/2;
 89    
 90    CGPoint newCenter;
 91    CGFloat rotateAngle;
 92    
 93    switch (orientation) {
 94        case UIInterfaceOrientationPortraitUpsideDown:
 95            rotateAngle = M_PI;
 96            newCenter = CGPointMake(posX, orientationFrame.size.height-posY);
 97            break;
 98        case UIInterfaceOrientationLandscapeLeft:
 99            rotateAngle = -M_PI/2.0f;
100            newCenter = CGPointMake(posY, posX);
101            break;
102        case UIInterfaceOrientationLandscapeRight:
103            rotateAngle = M_PI/2.0f;
104            newCenter = CGPointMake(orientationFrame.size.height-posY, posX);
105            break;
106        default: // UIInterfaceOrientationPortrait
107            rotateAngle = 0.0;
108            newCenter = CGPointMake(posX, posY);
109            break;
110    }
111    
112    self.transform = CGAffineTransformMakeRotation(rotateAngle);
113    self.center = newCenter;
114    
115    [self setNeedsLayout];
116    [self layoutSubviews];
117}
118
119- (id)init
120{
121    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
122    if (self) {
123        self.windowLevel = UIWindowLevelStatusBar;
124        self.hidden = YES;
125        self.userInteractionEnabled = NO;
126        self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5f];
127        self.vignetteBackground = NO;
128        
129        [[NSNotificationCenter defaultCenter] addObserver:self
130                                                 selector:@selector(setRotation:)
131                                                     name:UIApplicationDidChangeStatusBarOrientationNotification
132                                                   object:nil];
133        [self setRotation:nil];
134
135    }
136    return self;
137}
138
139- (void)addToMainWindow:(UIView *)view
140{
141    [self setRotation:nil];
142    
143    if ([self.subviews containsObject:view]) return;
144
145    if (self.hidden)
146    {
147        _previousKeyWindow = [[[UIApplication sharedApplication] keyWindow] retain];
148        self.alpha = 0.0f;
149        self.hidden = NO;
150        [self makeKeyWindow];
151    }
152    
153    // if something's been added to this window, then this window should have interaction
154    self.userInteractionEnabled = YES;
155    
156    if (self.subviews.count > 0)
157    {
158        ((UIView*)[self.subviews lastObject]).userInteractionEnabled = NO;
159    }
160    
161    if (_backgroundImage)
162    {
163        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:_backgroundImage];
164        backgroundView.frame = self.bounds;
165        backgroundView.contentMode = UIViewContentModeScaleToFill;
166        [self addSubview:backgroundView];
167        [backgroundView release];
168        [_backgroundImage release];
169        _backgroundImage = nil;
170    }
171    
172    [self addSubview:view];
173}
174
175- (void)reduceAlphaIfEmpty
176{
177    if (self.subviews.count == 1 || (self.subviews.count == 2 && [[self.subviews objectAtIndex:0] isKindOfClass:[UIImageView class]]))
178    {
179        self.alpha = 0.0f;
180        self.userInteractionEnabled = NO;
181    }
182}
183
184- (void)removeView:(UIView *)view
185{
186    [view removeFromSuperview];
187
188    UIView *topView = [self.subviews lastObject];
189    if ([topView isKindOfClass:[UIImageView class]])
190    {
191        // It's a background. Remove it too
192        [topView removeFromSuperview];
193    }
194    
195    if (self.subviews.count == 0)
196    {
197        self.hidden = YES;
198        [_previousKeyWindow makeKeyWindow];
199        [_previousKeyWindow release];
200        _previousKeyWindow = nil;
201    }
202    else
203    {
204        ((UIView*)[self.subviews lastObject]).userInteractionEnabled = YES;
205    }
206}
207
208- (void)drawRect:(CGRect)rect 
209{    
210    if (_backgroundImage || !_vignetteBackground) return;
211    CGContextRef context = UIGraphicsGetCurrentContext();
212    
213	size_t locationsCount = 2;
214	CGFloat locations[2] = {0.0f, 1.0f};
215	CGFloat colors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f}; 
216	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
217	CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
218	CGColorSpaceRelease(colorSpace);
219	
220	CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
221	float radius = MIN(self.bounds.size.width , self.bounds.size.height) ;
222	CGContextDrawRadialGradient (context, gradient, center, 0, center, radius, kCGGradientDrawsAfterEndLocation);
223	CGGradientRelease(gradient);
224}
225
226
227
228
229@end