|
//
// TPWindow.m
// ThePaperHD
//
// Created by scar1900 on 15/2/5.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "TPWindow.h"
#import "AppDelegate.h"
#import "YRSideViewController.h"
@interface TPWindow()<UIAlertViewDelegate>
@property(nonatomic, weak)UIAlertView *alertView;
@end
@implementation TPWindow
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(linkWithPush:) name:LINKWITHPUSH object:nil];
}
return self;
}
- (UIView*)nightModeMaskView {
if (!_nightModeMaskView) {
_nightModeMaskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_nightModeMaskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
_nightModeMaskView.userInteractionEnabled = NO;
_nightModeMaskView.autoresizingMask = AutoresizingFull;
_nightModeMaskView.hidden = YES;
}
return _nightModeMaskView;
}
- (void)setMaskAfterOpenNightMode {
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
[self setNoNightModeMask];
self.nightModeMaskView.hidden = NO;
self.nightModeMaskView.frame = [UIScreen mainScreen].bounds;
[self addSubview:self.nightModeMaskView];
}else {
self.nightModeMaskView.hidden = YES;
[self setNoNightModeMask];
}
}
- (void)setNoNightModeMask {
[self.nightModeMaskView removeFromSuperview];
_nightModeMaskView = nil;
}
- (void)linkWithPush:(NSNotification*)notification {
TPLOG(@"接到通知或者广告点击:%@",[TPUserDefault instance].pushBO);
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
if ([[TPUserDefault instance].pushBO.isComeFromRemote intValue] != 1) {
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
}else {
if ([[TPUserDefault instance].isEnterFromBackground intValue] == 1) {
//推送:app运行中,连着收到两条或以上的推送,点击查看,只停留在首页并卡死(bug:6000)
if (_alertView) {
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
NSDictionary *dic = [TPUserDefault instance].pushBO.aps;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:dic[@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",nil];
alert.delegate = self;
[alert show];
_alertView = alert;
}else {
if (KEY_WINDOW.rootViewController.presentedViewController) {
[KEY_WINDOW.rootViewController dismissViewControllerAnimated:YES completion:^{
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
[TPUserDefault instance].pushBO = nil;
}];
}else {
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
[TPUserDefault instance].pushBO = nil;
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
if (buttonIndex != 0) {
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHCLICK object:nil userInfo:nil];
if (isPad) {
if (KEY_WINDOW.rootViewController.presentedViewController) {
[KEY_WINDOW.rootViewController dismissViewControllerAnimated:YES completion:^{
presentAfterPushOrClickAd();
[TPUserDefault instance].pushBO = nil;
}];
}else {
presentAfterPushOrClickAd();
[TPUserDefault instance].pushBO = nil;
}
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
[TPUserDefault instance].pushBO = nil;
}
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
|