Fixing up notifier to actually show loading.

This commit is contained in:
Samuel Clay 2013-06-11 23:00:00 -07:00
parent 94ec5bd3ca
commit 2ff9dece60
3 changed files with 366 additions and 75 deletions

View file

@ -98,8 +98,6 @@
titleImageBarButton = [UIBarButtonItem alloc];
notifier = [[NBNotifier alloc] initWithFrame:self.view.frame];
[self.view addSubview:notifier];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
@ -124,7 +122,9 @@
self.finishedAnimatingIn = NO;
self.pageFinished = NO;
[MBProgressHUD hideHUDForView:self.view animated:YES];
[notifier hideIn:0];
// set center title
UILabel *titleLabel = (UILabel *)[appDelegate makeFeedTitle:appDelegate.activeFeed];
self.navigationItem.titleView = titleLabel;
@ -313,8 +313,13 @@
if (storyCount == 0) {
[self.storyTitlesTable reloadData];
[storyTitlesTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[self.notifier drawInView:self.view withText:@"Loading..." style:NBLoadingStyle];
[self.notifier setNeedsDisplay];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityIndicator startAnimating];
self.notifier = [[NBNotifier alloc] initWithTitle:@"Fetching stories..." inView:appDelegate.feedDetailViewController.view];
self.notifier.accessoryView = activityIndicator;
[self.notifier show];
}
if (appDelegate.isSocialView) {
theFeedDetailURL = [NSString stringWithFormat:@"http://%@/social/stories/%@/?page=%d",
@ -374,8 +379,7 @@
if (storyCount == 0) {
[self.storyTitlesTable reloadData];
[storyTitlesTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[self.notifier drawInView:self.view withText:@"Loading..." style:NBLoadingStyle];
[self.notifier setNeedsDisplay];
// [self.notifier initWithTitle:@"Loading more..." inView:self.view];
}
@ -565,7 +569,7 @@
[appDelegate.database commit];
NSLog(@"Inserting %d stories: %@", [confirmedNewStories count], [appDelegate.database lastErrorMessage]);
[self.notifier hideWithAnimation:NO];
[self.notifier hide];
}
#pragma mark -
@ -687,10 +691,8 @@
fleuron.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:fleuron];
} else if (self.feedPage > 1) {
NBNotifier *loadingNotifier = [[NBNotifier alloc] drawInView:self.view
withText:@"LOADING..."
style:NBLoadingStyle];
[cell addSubview:loadingNotifier];
NBNotifier *loadingNotifier = [[NBNotifier alloc] initWithTitle:@"Loading a page..." inView:cell];
[loadingNotifier showIn:0];
}
return cell;
@ -845,7 +847,7 @@
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (!self.pageFinished && self.feedPage > 1 && indexPath.row == [[appDelegate activeFeedStoryLocations] count]) {
return 20;
return 40;
} else if (appDelegate.isRiverView || appDelegate.isSocialView || appDelegate.isSocialRiverView) {
int height = kTableViewRiverRowHeight;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

View file

@ -8,7 +8,11 @@
#import <UIKit/UIKit.h>
@interface NBNotifier : UIView
@interface NBNotifier : UIView {
@protected
UILabel *_txtLabel;
}
typedef enum {
NBOfflineStyle = 1,
@ -16,11 +20,26 @@ typedef enum {
NBSyncingStyle = 3
} NBNotifierStyle;
@property (assign, nonatomic) NSString *_text;
@property (assign, nonatomic) NBNotifierStyle _style;
@property (assign, nonatomic) UIView *_view;
@property (nonatomic, strong) NSString *_text;
@property (nonatomic) NBNotifierStyle style;
@property (nonatomic, strong) UIView *_view;
@property (nonatomic, strong) UIView *accessoryView;
@property (nonatomic, strong) NSString *title;
- (id)drawInView:(UIView *)view withText:(NSString *)text style:(NBNotifierStyle)style;
- (void)hideWithAnimation:(BOOL)animate;
- (id)initWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style;
- (void)setAccessoryView:(UIView *)view animated:(BOOL)animated;
- (void)setTitle:(id)title animated:(BOOL)animated;
- (void)show;
- (void)showIn:(float)time;
- (void)showFor:(float)time;
- (void)hide;
- (void)hideAfter:(float)seconds;
- (void)hideIn:(float)seconds;
@end

View file

@ -6,11 +6,34 @@
// Copyright (c) 2013 NewsBlur. All rights reserved.
//
// Based on work by:
/*
Copyright 2012 Jonah Siegle
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "NBNotifier.h"
#import "UIView+TKCategory.h"
#include <QuartzCore/QuartzCore.h>
#define _displaytime 4.f
@implementation NBNotifier
@synthesize accessoryView, title = _title, style = _style;
+ (void)initialize {
if (self == [NBNotifier class]) {
@ -26,70 +49,317 @@
return self;
}
- (id)drawInView:(UIView *)view withText:(NSString *)text style:(NBNotifierStyle)style {
self._text = text;
self._style = style;
self._view = view;
return [self initWithFrame:view.frame];
- (id)initWithTitle:(NSString *)title {
return [self initWithTitle:title inView:[[[UIApplication sharedApplication] delegate] window]];
}
- (void)drawRect:(CGRect)r {
[[UIColor redColor] set];
r.size.height = 20;
[self setBackgroundColor:[UIColor clearColor]];
[self setAlpha:0.4];
- (id)initWithTitle:(NSString *)title inView:(UIView *)view {
return [self initWithTitle:title inView:view style:NBLoadingStyle];
}
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style {
[UIView drawLineInRect:CGRectMake(0, 0, r.size.width, 1) red:242 green:250 blue:230 alpha:1];
[UIView drawLineInRect:CGRectMake(0, 1, r.size.width, 1) red:255 green:255 blue:255 alpha:1];
[UIView drawLineInRect:CGRectMake(0, r.size.height-2, r.size.width, 1) red:255 green:255 blue:255 alpha:1];
[UIView drawLineInRect:CGRectMake(0, r.size.height-1, r.size.width, 1) red:242 green:250 blue:230 alpha:1];
// UIColor *psGrad = UIColorFromRGB(0x559F4D);
// UIColor *ngGrad = UIColorFromRGB(0x9B181B);
const CGFloat* psTop = CGColorGetComponents(UIColorFromRGB(0xE4AB00).CGColor);
const CGFloat* psBot = CGColorGetComponents(UIColorFromRGB(0xD9A200).CGColor);
CGFloat psGradient[] = {
psTop[0], psTop[1], psTop[2], psTop[3],
psBot[0], psBot[1], psBot[2], psBot[3]
};
NSLog(@"Drawing Notifier: %@", NSStringFromCGRect(r));
[UIView drawLinearGradientInRect:r colors:psGradient];
switch (self._style) {
case NBOfflineStyle: {
[self._text
drawAtPoint:CGPointMake(40, 4)
withFont:[UIFont boldSystemFontOfSize:12]];
break;
}
case NBSyncingStyle: {
[self._text
drawAtPoint:CGPointMake(40, 4)
withFont:[UIFont boldSystemFontOfSize:12]];
break;
}
case NBLoadingStyle: {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(6, 6, 4, 4);
spinner.center = CGPointMake(10, 10);
spinner.color = UIColorFromRGB(0x5060C0);
[self addSubview:spinner];
[spinner startAnimating];
[[UIColor darkGrayColor] set];
[self._text
drawAtPoint:CGPointMake(r.origin.x + 26, r.origin.y + 4)
withFont:[UIFont boldSystemFontOfSize:12]];
break;
}
if (self = [super initWithFrame:CGRectMake(0, view.bounds.size.height, view.bounds.size.width, 40)]){
self.backgroundColor = [UIColor clearColor];
self.style = style;
_txtLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 12, self.frame.size.width - 0, 20)];
[_txtLabel setFont:[UIFont fontWithName: @"Helvetica" size: 16]];
[_txtLabel setBackgroundColor:[UIColor clearColor]];
[_txtLabel setTextColor:[UIColor whiteColor]];
_txtLabel.layer.shadowOffset =CGSizeMake(0, -0.5);
_txtLabel.layer.shadowColor = [UIColor blackColor].CGColor;
_txtLabel.layer.shadowOpacity = 1.0;
_txtLabel.layer.shadowRadius = 1;
_txtLabel.layer.masksToBounds = NO;
[self addSubview:_txtLabel];
self.title= title;
[view addSubview:self];
}
self.frame = r;
return self;
}
- (void)hideWithAnimation:(BOOL)animate {
- (void)setAccessoryView:(UIView *)__accessoryView{
[[self viewWithTag:1]removeFromSuperview];
__accessoryView.tag = 1;
[__accessoryView setFrame:CGRectMake(12, ((self.frame.size.height -__accessoryView.frame.size.height)/2)+1, __accessoryView.frame.size.width, __accessoryView.frame.size.height)];
[self addSubview:__accessoryView];
if (__accessoryView)
[_txtLabel setFrame:CGRectMake(38, 12, self.frame.size.width - 38, 20)];
else
[_txtLabel setFrame:CGRectMake(8, 12, self.frame.size.width - 8, 20)];
}
- (void)setTitle:(NSString *)title{
[_txtLabel setText:title];
}
- (void)show {
[self showIn:(float)0.3f];
}
- (void)showIn:(float)time {
[UIView beginAnimations:nil context:nil];
self.alpha = 0;
[UIView setAnimationDuration:time];
CGRect move = self.frame;
move.origin.y -=40.f;
self.frame = move;
[UIView commitAnimations];
}
- (void)showFor:(float)time{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
CGRect move = self.frame;
move.origin.y -=40.f;
self.frame = move;
[UIView commitAnimations];
[self hideAfter:time];
}
- (void)hideAfter:(float)seconds{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationDelegate: self]; //or some other object that has necessary method
[UIView setAnimationDidStopSelector: @selector(removeFromSuperview)];
CGRect move = self.frame;
move.origin.y +=40.f;
self.frame = move;
[UIView commitAnimations];
});
}
- (void)hide {
[self hideIn:0.3f];
}
- (void)hideIn:(float)seconds {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:seconds];
[UIView setAnimationDelegate: self]; //or some other object that has necessary method
[UIView setAnimationDidStopSelector: @selector(removeFromSuperview)];
CGRect move = self.frame;
move.origin.y +=40.f;
self.frame = move;
[UIView commitAnimations];
}
- (void)setAccessoryView:(UIView *)view animated:(BOOL)animated{
if (!animated){
[[self viewWithTag:1]removeFromSuperview];
view.tag = 1;
}
[view setFrame:CGRectMake(12, ((self.frame.size.height -view.frame.size.height)/2)+1, view.frame.size.width, view.frame.size.height)];
[self addSubview:view];
if (animated) {
view.alpha = 0.0;
if ([self viewWithTag:1])
view.tag = 0;
else
view.tag = 2;
[UIView animateWithDuration:0.5
animations:^{
if ([self viewWithTag:1])
[self viewWithTag:1].alpha = 0.0;
else
view.alpha = 1.0;
}
completion:^(BOOL finished){
[[self viewWithTag:1]removeFromSuperview];
[UIView animateWithDuration:0.5
animations:^{
view.alpha = 1.0;
}
completion:^(BOOL finished){
view.tag = 1;
}];
}];
}
if (view)
[_txtLabel setFrame:CGRectMake(38, 12, self.frame.size.width - 38, 20)];
else
[_txtLabel setFrame:CGRectMake(8, 12, self.frame.size.width - 8, 20)];
}
- (void)setTitle:(id)title animated:(BOOL)animated{
float duration = 0.0;
if (animated)
duration = 0.5;
[UIView animateWithDuration:duration
animations:^{
_txtLabel.alpha = 0.0f;
}
completion:^(BOOL finished){
_txtLabel.text = title;
[UIView animateWithDuration:duration
animations:^{
_txtLabel.alpha = 1.0f;
}
completion:^(BOOL finished){
}];
}];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
//Background color
CGRect rectangle = CGRectMake(0,4,320,36);
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.6f].CGColor);
CGContextFillRect(context, rectangle);
//First whiteColor
CGContextSetLineWidth(context, 1.0);
CGFloat componentsWhiteLine[] = {1.0, 1.0, 1.0, 0.35};
CGColorRef Whitecolor = CGColorCreate(colorspace, componentsWhiteLine);
CGContextSetStrokeColorWithColor(context, Whitecolor);
CGContextMoveToPoint(context, 0, 4.5);
CGContextAddLineToPoint(context, 320, 4.5);
CGContextStrokePath(context);
CGColorRelease(Whitecolor);
//First whiteColor
CGContextSetLineWidth(context, 1.0);
CGFloat componentsBlackLine[] = {0.0, 0.0, 0.0, 1.0};
CGColorRef Blackcolor = CGColorCreate(colorspace, componentsBlackLine);
CGContextSetStrokeColorWithColor(context, Blackcolor);
CGContextMoveToPoint(context, 0, 3.5);
CGContextAddLineToPoint(context, 320, 3.5);
CGContextStrokePath(context);
CGColorRelease(Blackcolor);
//Draw Shadow
CGRect imageBounds = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 3.f);
CGRect bounds = CGRectMake(0, 0, 320, 3);
CGFloat alignStroke;
CGFloat resolution;
CGMutablePathRef path;
CGRect drawRect;
CGGradientRef gradient;
NSMutableArray *colors;
UIColor *color;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGPoint point;
CGPoint point2;
CGAffineTransform transform;
CGMutablePathRef tempPath;
CGRect pathBounds;
CGFloat locations[2];
resolution = 0.5f * (bounds.size.width / imageBounds.size.width + bounds.size.height / imageBounds.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, bounds.origin.x, bounds.origin.y);
CGContextScaleCTM(context, (bounds.size.width / imageBounds.size.width), (bounds.size.height / imageBounds.size.height));
// Layer 1
alignStroke = 0.0f;
path = CGPathCreateMutable();
drawRect = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 3.0f);
drawRect.origin.x = (roundf(resolution * drawRect.origin.x + alignStroke) - alignStroke) / resolution;
drawRect.origin.y = (roundf(resolution * drawRect.origin.y + alignStroke) - alignStroke) / resolution;
drawRect.size.width = roundf(resolution * drawRect.size.width) / resolution;
drawRect.size.height = roundf(resolution * drawRect.size.height) / resolution;
CGPathAddRect(path, NULL, drawRect);
colors = [NSMutableArray arrayWithCapacity:2];
color = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
[colors addObject:(id)[color CGColor]];
locations[0] = 0.0f;
color = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.18f];
[colors addObject:(id)[color CGColor]];
locations[1] = 1.0f;
gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, locations);
CGContextAddPath(context, path);
CGContextSaveGState(context);
CGContextEOClip(context);
transform = CGAffineTransformMakeRotation(-1.571f);
tempPath = CGPathCreateMutable();
CGPathAddPath(tempPath, &transform, path);
pathBounds = CGPathGetPathBoundingBox(tempPath);
point = pathBounds.origin;
point2 = CGPointMake(CGRectGetMaxX(pathBounds), CGRectGetMinY(pathBounds));
transform = CGAffineTransformInvert(transform);
point = CGPointApplyAffineTransform(point, transform);
point2 = CGPointApplyAffineTransform(point2, transform);
CGPathRelease(tempPath);
CGContextDrawLinearGradient(context, gradient, point, point2, (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation));
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGPathRelease(path);
CGContextRestoreGState(context);
CGColorSpaceRelease(space);
CGColorSpaceRelease(colorspace);
}
@end