|
//
// TPHttpController.m
// ThePaperDemo
//
// Created by scar1900 on 14-10-13.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPHttpController.h"
#import "AppDelegate.h"
#define timeOutTag 1024
@interface TPHttpController ()
@property(nonatomic, strong)UIView *maskBack;
@property(nonatomic, strong)UIImageView *snapBack;
@end
@implementation TPHttpController
@synthesize request;
@synthesize snapBack;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if (isIOS7) {
[self setNeedsStatusBarAppearanceUpdate];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needReloadNight:) name:REFRESHAFTERNIGHTMODE object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[MobClick beginLogPageView:@"PageOne"];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[MobClick endLogPageView:@"PageOne"];
}
- (BOOL)shouldAutorotate {
return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
return UIStatusBarStyleLightContent;
}else {
return UIStatusBarStyleDefault;
}
}
- (void)needReloadNight:(NSNotification*)noti {
if (self.timeOutView) {
self.timeOutView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
UILabel *timeOutLabel = (UILabel*)[self.timeOutView viewWithTag:timeOutTag];
timeOutLabel.textColor = [UIColor colorWithHexString:TextBlack];
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIViewController*) rootViewController {
return ((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController;
}
- (void) setRootViewController:(UIViewController *)rootViewController {
((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController = rootViewController;
}
- (UIView*)waitBackView {
if (!_waitBackView) {
_waitBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
_waitBackView.backgroundColor = [UIColor clearColor];
_waitBackView.userInteractionEnabled = YES;
[_waitBackView addSubview:self.waitCursor];
}
return _waitBackView;
}
- (SCGIFImageView*)waitCursor {
if (!_waitCursor) {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"/%@", Bundle(@"frontPage/loading.gif")];
NSData* imageData = [NSData dataWithContentsOfFile:filePath];
_waitCursor = [[SCGIFImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.waitBackView.bounds)/2-25/2,
(CGRectGetHeight(self.waitBackView.bounds))/2-25/2-32,
25,
25)];
_waitCursor.autoresizingMask = AutoresizingAuto;
_waitCursor.animating = NO;
[_waitCursor setData:imageData];
_waitCursor.hidden = NO;
}
return _waitCursor;
}
//页面需要被销毁,断开网络请求
- (void) stopRemoteRequestWhenPop {
[Remote removeWaitBack];
/**
* bug:5789(积分商城:弱网络环境下,进入积分商城,再立刻返回,loading会留在个人信息页)
*/
[self.request clearDelegatesAndCancel];
self.request.userInfo = nil;
self.request = nil;
}
- (void)presentController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
self.snapBack = nil;
UIToolbar *toolBar = [[UIToolbar alloc]init];
if ([toolBar respondsToSelector:@selector(setBarTintColor:)]) {
self.snapBack = [[UIImageView alloc]initWithFrame:self.view.bounds];
self.maskBack.backgroundColor = [UIColor clearColor];
self.snapBack.userInteractionEnabled = YES;
self.snapBack.image = customLightDarkBlurSnapshotFromView(self.view).image;
self.snapBack.alpha = 0;
}
toolBar = nil;
[UIView animateWithDuration:0.25 animations:^{
if (self.snapBack) {
[self.view addSubview:snapBack];
self.snapBack.alpha = 1;
}else {
[self.view addSubview:self.maskBack];
self.maskBack.alpha = 0.8;
}
} completion:^(BOOL finished) {
[self.view addSubview:viewControllerToPresent.view];
[CoreAnimationEffect animationPushUp:viewControllerToPresent.view];
if (completion) {
completion();
}
}];
}
- (void)dismissViewController:(UIViewController *)viewControllerToDismiss animated:(BOOL)flag completion:(void (^)(void))completion {
[UIView animateWithDuration:0.25 animations:^{
viewControllerToDismiss.view.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(viewControllerToDismiss.view.bounds));
self.snapBack.alpha = 0.f;
self.maskBack.alpha = 0.f;
} completion:^(BOOL finished) {
[viewControllerToDismiss.view removeFromSuperview];
[self.maskBack removeFromSuperview];
[self.snapBack removeFromSuperview];
if (completion) {
completion();
}
}];
}
- (UIView *)maskBack {
if (!_maskBack) {
_maskBack = [[UIView alloc]initWithFrame:self.view.bounds];
_maskBack.alpha = 0;
_maskBack.backgroundColor = [UIColor colorWithHexString:@"0x3e3c3d"];
}
return _maskBack;
}
- (void)startWaitCursor:(int)actionTag {
[self.view addSubview:self.waitBackView];
[self.view bringSubviewToFront:self.waitBackView];
self.waitCursor.animating = YES;
}
- (void)stopWaitCursor:(int)actionTag {
[self.waitBackView removeFromSuperview];
self.waitBackView = nil;
self.waitCursor.animating = NO;
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
ShowTextMessage(message);
}
#pragma mark - network time out
- (void)networkTimeOut {
[self.timeOutView removeFromSuperview];
self.timeOutView = nil;
self.timeOutView = [[UIView alloc]initWithFrame:CGRectZero];
self.timeOutView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
// self.timeOutView.frame = self.view.bounds;
// self.timeOutView.autoresizingMask = AutoresizingFull;
UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// refreshBtn.frame = self.timeOutView.bounds;
[refreshBtn setImage:Image(@"other/pageRefresh.png") forState:UIControlStateNormal];
[refreshBtn setImage:Image(@"other/pageRefresh_h.png") forState:UIControlStateHighlighted];
[refreshBtn addTarget:self action:@selector(refreshPageAfterTimeOut) forControlEvents:UIControlEventTouchUpInside];
[self.timeOutView addSubview:refreshBtn];
UILabel *timeOutLabel = [[UILabel alloc]initWithFrame:CGRectZero];
timeOutLabel.font = appFont(20, NO);
timeOutLabel.textAlignment = NSTextAlignmentCenter;
timeOutLabel.textColor = [UIColor colorWithHexString:TextBlack];
timeOutLabel.text = @"网络不给力,点击重新加载";
timeOutLabel.autoresizingMask = AutoresizingAuto;
timeOutLabel.tag = timeOutTag;
[self.timeOutView addSubview:timeOutLabel];
[self.view addSubview:self.timeOutView];
[self.timeOutView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[refreshBtn makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.timeOutView);
}];
[timeOutLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.timeOutView.centerX);
make.centerY.equalTo(self.timeOutView.centerY).offset(41/2+20);
make.width.equalTo(self.timeOutView.width);
make.height.equalTo(@25);
}];
}
- (void)backHandler {
((MLNavigationController *)self.navigationController).canDragBack = YES;
[self.navigationController popViewControllerAnimated:YES];
}
- (void)refreshPageAfterTimeOut {
if (![Remote IsEableNetwork]) {
return;
}
self.timeOutView.hidden = YES;
[self.timeOutView removeFromSuperview];
self.timeOutView = nil;
[self refreshPage];
}
- (void)refreshPage {
}
- (void)dealloc {
[self stopRemoteRequestWhenPop];
[[NSNotificationCenter defaultCenter]removeObserver:self];
TPLOG(@"*********controller dealloc:%@***********",self.description);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
clearImageMemCache();
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|