|
//
// TPHttpController.m
// ThePaperDemo
//
// Created by scar1900 on 14-10-13.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPHttpController.h"
#import "KGModal.h"
#import "SDWebImage/SDImageCache.h"
@interface TPHttpController ()<UIGestureRecognizerDelegate>
@property(nonatomic,strong)UISwipeGestureRecognizer *popBackSwipeGesture;
@end
@implementation TPHttpController
@synthesize request;
@synthesize hud;
@synthesize Frame = _Frame;
@synthesize isPresent;
@synthesize popBackSwipeGesture;
- (id)init {
self = [super init];
if (self) {
self.isPresent = NO;
}
return self;
}
- (void)setFrame:(CGRect)rect {
_Frame = rect;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if (self.view.bounds.size.width > self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}else {
if (self.view.bounds.size.width < self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
appFont(20, NO),UITextAttributeFont,nil];
self.navigationController.navigationBar.titleTextAttributes = dic;
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
self.popBackSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipeGestureHandler:)];
[self.popBackSwipeGesture delaysTouchesBegan];
self.popBackSwipeGesture .numberOfTouchesRequired = 1;
self.popBackSwipeGesture .direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.popBackSwipeGesture ];
[self disablePopBackGesture];
//隐藏状态栏
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self setNeedsStatusBarAppearanceUpdate];
}else {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
return UIStatusBarStyleLightContent;
}else {
return UIStatusBarStyleDefault;
}
}
- (void)enablePopBackGesture {
self.popBackSwipeGesture.enabled = YES;
}
- (void)disablePopBackGesture {
self.popBackSwipeGesture.enabled = NO;
}
- (void)rightSwipeGestureHandler:(UISwipeGestureRecognizer*)gesture {
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[MobClick beginLogPageView:@"PageOne"];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[MobClick endLogPageView:@"PageOne"];
}
- (void)naviBarTapHandler:(UIButton*)btn {
}
//- (void)setBackButton:(BOOL)isBlack {
//
// self.navigationController.navigationBar.hidden = NO;
//
// UIImage *backButtonBackgroundImage = Image(@"Button/backBarBtn.png");
//
// if (isBlack) {
// backButtonBackgroundImage = Image(@"Button/backBarBtnBlack.png");
//
// }
//
// // The background should be pinned to the left and not stretch.
//
// backButtonBackgroundImage = [backButtonBackgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButtonBackgroundImage.size.width-18, 0, 0)];
//
//// 定义你想在哪些类中使用这个效果
//
// id appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil];
//
// [appearance setBackButtonBackgroundImage:backButtonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//
// // Provide an empty backBarButton to hide the 'Back' text present by
//
// // default in the back button.
//
// //
//
// // NOTE: You do not need to provide a target or action. These are set
//
// // by the navigation bar.
//
// // NOTE: Setting the title of this bar button item to ' ' (space) works
//
// // around a bug in iOS 7.0.x where the background image would be
//
// // horizontally compressed if the back button title is empty.
//
// UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
//
// self.navigationItem.backBarButtonItem = backBarButton;
//}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [UIApplication sharedApplication].statusBarOrientation;
}
- (void)orientChange:(NSNotification *)noti
{
// NSDictionary* ntfDict = [noti userInfo];
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
/*
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down */
switch (orient)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
}
}
- (void)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右
{
//
}
if (
orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
{
//
}
if (orientation == UIInterfaceOrientationPortrait)
{
//
}
if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//
}
}
- (UIViewController*) rootViewController {
return ((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController;
}
- (void) setRootViewController:(UIViewController *)rootViewController {
((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController = rootViewController;
}
//页面需要被销毁,断开网络请求
- (void) stopRemoteRequestWhenPop {
[self.request clearDelegatesAndCancel];
self.request.userInfo = nil;
self.request = nil;
}
- (void)presentController:(TPHttpController *)viewControllerToPresent animated:(BOOL)flag presentSize:(CGSize)size completion:(void (^)(void))completion{
viewControllerToPresent.view.frame = CGRectMake(0, 0, size.width, size.height);
viewControllerToPresent.Frame = CGRectMake(0, 0, size.width, size.height);
[KGModal sharedInstance].modalBackgroundColor = [UIColor clearColor];
[KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
[KGModal sharedInstance].tapOutsideToDismiss = YES;
[[KGModal sharedInstance] showWithContentViewController:viewControllerToPresent andAnimated:YES completion:^{
if (completion) {
completion();
}
}];
}
- (void)presentController:(TPHttpController *)viewControllerToPresent animated:(BOOL)flag presentSize:(CGSize)size completion:(void (^)(void))completion dismiss:(void (^)(void))dismiss{
viewControllerToPresent.view.frame = CGRectMake(0, 0, size.width, size.height);
viewControllerToPresent.Frame = CGRectMake(0, 0, size.width, size.height);
[KGModal sharedInstance].modalBackgroundColor = [UIColor clearColor];
[KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
[KGModal sharedInstance].tapOutsideToDismiss = YES;
[[KGModal sharedInstance] showWithContentViewController:viewControllerToPresent andAnimated:YES completion:^{
if (completion) {
completion();
}
} dismiss:^{
if (dismiss) {
dismiss();
}
} tapHandler:nil];
}
- (void)presentController:(TPHttpController *)viewControllerToPresent animated:(BOOL)flag presentSize:(CGSize)size completion:(void (^)(void))completion dismiss:(void (^)(void))dismiss tapHandler:(void (^)(void))tapHandler{
viewControllerToPresent.view.frame = CGRectMake(0, 0, size.width, size.height);
viewControllerToPresent.Frame = CGRectMake(0, 0, size.width, size.height);
[KGModal sharedInstance].modalBackgroundColor = [UIColor clearColor];
[KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
[KGModal sharedInstance].tapOutsideToDismiss = NO;
[[KGModal sharedInstance] showWithContentViewController:viewControllerToPresent andAnimated:YES completion:^{
if (completion) {
completion();
}
} dismiss:^{
if (dismiss) {
dismiss();
}
} tapHandler:^{
if (tapHandler) {
tapHandler();
}
}];
}
- (void)dismissControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
[[KGModal sharedInstance] hideAnimated:YES withCompletionBlock:^{
if (completion) {
completion();
}
}];
}
- (void)enablePopBackTap {
[KGModal sharedInstance].tapOutsideToDismiss = YES;
}
- (void)disablePopBackTap {
[KGModal sharedInstance].tapOutsideToDismiss = NO;
}
#pragma mark - remote delegate
- (void)startWaitCursor:(int)actionTag {
if (!self.hud) {
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.mode = MBProgressHUDModeIndeterminate;
self.hud.userInteractionEnabled = YES;
self.hud.dimBackground = NO;
UIColor *hudColor = [UIColor colorWithRed:76.5f/255.f green:76.5f/255.f blue:76.5f/255.f alpha:0.9f];
self.hud.color = hudColor;
self.hud.cornerRadius = 4;
self.hud.removeFromSuperViewOnHide = YES;
}
}
- (void)stopWaitCursor:(int)actionTag {
[self.hud hide:YES];
self.hud = nil;
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
ShowTextMessage([NSString stringWithFormat:@"%@:%@",self.description,message]);
}
- (void)dealloc {
[self stopRemoteRequestWhenPop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
TPLOG(@"*********controller dealloc:%@***********",self.description);
}
- (void) leftIconButton:(NSString*)aIcon highlited:(NSString*)highlitedIcon selector:(SEL)sel {
UIImage* icon = Image(aIcon);
UIImage* icon_h = Image(highlitedIcon);
UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setBackgroundImage:icon forState:UIControlStateNormal];
[leftBtn setBackgroundImage:icon_h forState:UIControlStateHighlighted];
leftBtn.frame = CGRectMake(0, 7, 20, 20);
if (nil != sel) {
[leftBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = button;
}
+ (UIBarButtonItem*)barButtonItemWithIcon:(NSString*)aIcon target:(id)target selector:(SEL)sel {
UIImage* icon = Image(aIcon);
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:icon forState:UIControlStateNormal];
button.frame = CGRectMake(0, 10, icon.size.width*24/icon.size.height, 24);
if (nil != sel) {
[button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
}
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
+ (UIBarButtonItem*)barButtonItemWithIcon:(NSString*)aIcon highlited:(NSString*)hIcon target:(id)target selector:(SEL)sel {
UIBarButtonItem *item = [TPHttpController barButtonItemWithIcon:aIcon target:target selector:sel];
[(UIButton*)item.customView setImage:Image(hIcon) forState:UIControlStateHighlighted];
return item;
}
+ (UIBarButtonItem*)barButtonItemWithTitle:(NSString*)aTitle target:(id)target selector:(SEL)sel {
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:aTitle
style:UIBarButtonItemStylePlain
target:target
action:sel];
item.tintColor = [UIColor whiteColor];
return item;
}
- (void)rightIconButton:(NSString*)aIcon highlited:(NSString*)hIcon selector:(SEL)sel {
UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBtn setImage:Image(aIcon) forState:UIControlStateNormal];
[rightBtn setImage:Image(hIcon) forState:UIControlStateHighlighted];
rightBtn.frame = CGRectMake(0, 7, 20, 20);
[rightBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = button;
}
- (void) onGoBackEvent:(UIButton*)button {
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//收到内存警报立刻清除图片内存缓存:针对内存过多闪退问题
SDImageCache *sd_cache = [SDImageCache sharedImageCache];
[sd_cache clearMemory];
}
/**
* 文章已下线逻辑
*
*/
#pragma mark - content Offlined
- (void)contentHaveOfflined{
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBarHidden = YES;
[self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
CGFloat statusH = 0;
if (isIOS7) {
statusH = 18;
}
UILabel *textLabel = [[UILabel alloc]initWithFrame:self.view.bounds];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = appFont(18, NO);
textLabel.textColor = [UIColor colorWithHexString:TextBlack];
textLabel.text = @"此文章已下线";
textLabel.autoresizingMask = AutoresizingFull;
[self.view addSubview:textLabel];
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0,statusH, 70, 70);
[back setImage:Image(@"detailPage/contentBackImg.png") forState:UIControlStateNormal];
[back setImage:Image(@"detailPage/contentBackImg_s.png") forState:UIControlStateHighlighted];
[back setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[back addTarget:self action:@selector(offlineBackBtnHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
- (void)topicHaveOfflined:(BOOL)isHaveNavigaionBar{
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBarHidden = !isHaveNavigaionBar;
[self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
CGFloat statusH = 0;
if (isIOS7) {
statusH = 18;
}
UILabel *textLabel = [[UILabel alloc]initWithFrame:self.view.bounds];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = appFont(18, NO);
textLabel.textColor = [UIColor colorWithHexString:TextBlack];
textLabel.text = @"此话题已下线";
textLabel.autoresizingMask = AutoresizingFull;
[self.view addSubview:textLabel];
// UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
// back.frame = CGRectMake(0,statusH, 70, 70);
// [back setImage:Image(@"detailPage/contentBackImg.png") forState:UIControlStateNormal];
// [back setImage:Image(@"detailPage/contentBackImg_s.png") forState:UIControlStateHighlighted];
// [back setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
// [back addTarget:self action:@selector(offlineBackBtnHandler:) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:back];
}
- (void)offlineBackBtnHandler:(UIButton*)btn {
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#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];
refreshBtn.autoresizingMask = AutoresizingFull;
[self.timeOutView addSubview:refreshBtn];
UILabel *timeOutLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.timeOutView.bounds)/2+41/2+20, CGRectGetWidth(self.timeOutView.bounds), 25)];
timeOutLabel.font = appFont(20, NO);
timeOutLabel.textAlignment = NSTextAlignmentCenter;
timeOutLabel.textColor = [UIColor colorWithHexString:TextBlack];
timeOutLabel.text = @"网络不给力,点击重新加载";
timeOutLabel.autoresizingMask = AutoresizingAuto;
[self.timeOutView addSubview:timeOutLabel];
[self.view addSubview:self.timeOutView];
}
- (void)refreshPageAfterTimeOut {
[self.timeOutView removeFromSuperview];
self.timeOutView = nil;
[self refreshPage];
}
- (void)refreshPage {
}
#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
|