|
//
// TPcontentWebController.m
// ThePaperBase
//
// Created by Huixin on 15/8/7.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "TPcontentWebController.h"
#import "TPShareContentController.h"
#import "commentAndAskController.h"
#import "TPWebContentViewModel.h"
#import "loginHomeController.h"
@interface TPcontentWebController ()<UIWebViewDelegate, askDelegate, TPWebContentViewModelDelegate, TPShareContentControllerDelegate>
@property(nonatomic, strong)UIButton *commentButton;
@property(nonatomic, strong)UIButton *collectButton;
@property(nonatomic, strong)UIButton *shareButton;
@property(nonatomic, strong)commentAndAskController *commentController;
@property(nonatomic, strong)TPShareContentController *shareController;
@property(nonatomic, strong)TPWebContentViewModel *viewModel;
@property(nonatomic, strong)contDetailPageVO *detailContentBO;
@property(nonatomic, strong)contentObjectBO *contentBO;
@end
@implementation TPcontentWebController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.viewModel = [TPWebContentViewModel new];
self.viewModel.nodeID = self.contentId;
self.viewModel.delegate = self;
[self.viewModel remoteAction];
self.commentController.nodeId = self.contentId;
self.safariButton.hidden = YES;
[self.naviBar addSubview:self.commentButton];
[self.naviBar addSubview:self.collectButton];
[self.naviBar addSubview:self.shareButton];
// [self addChildViewController:self.shareController];
// [self.view addSubview:self.shareController.view];
// [self.shareController didMoveToParentViewController:self];
float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
[self.commentButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.top).offset(statusBarHeight);
make.bottom.equalTo(self.naviBar.bottom);
make.width.equalTo(self.commentButton.height);
}];
[self.collectButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.commentButton.top);
make.left.equalTo(self.commentButton.right);
make.bottom.equalTo(self.commentButton.bottom);
make.width.equalTo(self.collectButton.height);
}];
[self.shareButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.commentButton.top);
make.left.equalTo(self.collectButton.right);
make.bottom.equalTo(self.commentButton.bottom);
make.right.equalTo(self.naviBar.right).offset(-10);
make.width.equalTo(self.shareButton.height);
}];
// [self.shareController.view makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.naviBar.bottom);
// make.left.equalTo(self.view.left);
// make.right.equalTo(self.view.right);
// make.bottom.equalTo(self.view.bottom);
// }];
// self.shareController.view.hidden = YES;
[self addChildViewController:self.shareController];
[self.view addSubview:self.shareController.view];
}
- (UIButton*)commentButton {
if (!_commentButton) {
_commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
_commentButton.backgroundColor = [UIColor clearColor];
[_commentButton setImage:Image(@"Button/comment.png") forState:UIControlStateNormal];
[_commentButton setImage:Image(@"Button/comment_s.png") forState:UIControlStateHighlighted];
[_commentButton setImageEdgeInsets:UIEdgeInsetsMake(9.5, 9.5, 9.5, 9.5)];
[_commentButton addTarget:self action:@selector(gotoComment:) forControlEvents:UIControlEventTouchUpInside];
}
return _commentButton;
}
- (UIButton*)collectButton {
if (!_collectButton) {
_collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
_collectButton.backgroundColor = [UIColor clearColor];
[_collectButton setImage:Image(@"Button/collect.png") forState:UIControlStateNormal];
[_collectButton setImage:Image(@"Button/collect_s.png") forState:UIControlStateHighlighted];
[_collectButton setImage:Image(@"Button/collect_s.png") forState:UIControlStateSelected];
[_collectButton setImageEdgeInsets:UIEdgeInsetsMake(9.5, 9.5, 9.5, 9.5)];
[_collectButton addTarget:self action:@selector(collectHandler:) forControlEvents:UIControlEventTouchUpInside];
}
return _collectButton;
}
- (UIButton*)shareButton {
if (!_shareButton) {
_shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
_shareButton.backgroundColor = [UIColor clearColor];
[_shareButton setImage:Image(@"Button/share.png") forState:UIControlStateNormal];
[_shareButton setImage:Image(@"Button/share_s.png") forState:UIControlStateHighlighted];
[_shareButton setImage:Image(@"Button/share_s.png") forState:UIControlStateSelected];
[_shareButton setImageEdgeInsets:UIEdgeInsetsMake(9.5, 9.5, 9.5, 9.5)];
[_shareButton addTarget:self action:@selector(shareHandler:) forControlEvents:UIControlEventTouchUpInside];
}
return _shareButton;
}
- (commentAndAskController*)commentController {
if (!_commentController) {
_commentController = [[commentAndAskController alloc]init];
_commentController.view.frame = CGRectMake(0, 0, rect_screen.size.width, rect_screen.size.height);
_commentController.delegate = self;
[_commentController disablePanGesture];
}
return _commentController;
}
- (TPShareContentController*)shareController {
if (!_shareController) {
_shareController = [[TPShareContentController alloc] init];
_shareController.delegate = self;
_shareController.view.frame = CGRectMake(0, rect_screen.size.height, rect_screen.size.width, rect_screen.size.height-self.barHeight);
}
return _shareController;
}
- (void)setDetailContentBO:(contDetailPageVO *)detailBO {
if (detailBO) {
_detailContentBO = detailBO;
self.contentBO = setJsonDicToDataModel(detailBO.content, [contentObjectBO class]);
self.shareController.contentBO = self.contentBO;
NSString *isFavorited = detailBO.content[@"isFavorited"]?detailBO.content[@"isFavorited"]:@"";
if (!isBlankString(isFavorited) && [isFavorited intValue] == 1 && [TPUserDefault instance].userBO) {
[self.collectButton setSelected:YES];
}else {
[self.collectButton setSelected:NO];
}
if (isBlankString(self.url)) {
self.url = self.contentBO.shareUrl;
}
}
}
#pragma mark - remote handler
- (void)refreshPage {
[self.viewModel askForContentInfo];
}
#pragma mark - viewmodel dalegate
- (void)startHud {
[self.view addSubview:self.waitBackView];
[self.view bringSubviewToFront:self.waitBackView];
self.waitCursor.animating = YES;
}
- (void)stopHud {
[self.waitBackView removeFromSuperview];
self.waitBackView = nil;
self.waitCursor.animating = NO;
}
- (void)returnDetailPageInfo:(contDetailPageVO *)detailInfoBO {
self.detailContentBO = detailInfoBO;
}
- (void)returnCommentList:(NSMutableArray *)commentArray
nodeId:(NSString *)nodeId
nextUrl:(NSString *)nextUrl
commentTotalRecord:(NSInteger)recordTotal {
self.commentController.nodeId = nodeId;
self.commentController.commentList = commentArray;
self.commentController.firstCommentUrl = nextUrl;
self.commentController.commentSum = recordTotal;
}
- (void)returnHotAskDic:(NSMutableDictionary *)qaDic
nodeId:(NSString *)nodeId
nextUrl:(NSString *)nextUrl
askTotalRecord:(NSInteger)recordTotal {
self.commentController.qaNum = recordTotal;
self.commentController.dataDic = qaDic;
self.commentController.firstAskUrl = nextUrl;
}
- (void)collectResult:(BOOL)success {
if (success) {
[self.collectButton setSelected:YES];
}else {
[self.collectButton setSelected:NO];
}
}
- (void)contentHaveOffline {
[self contentHaveOfflined];
}
- (void)remoteFail:(BOOL)isTimeOut{
self.commentController.nodeId = self.contentId;
if (isTimeOut) {
[self networkTimeOut];
}
[self stopHud];
}
#pragma mark - button event
- (void)gotoComment:(UIButton*)btn {
[self.navigationController pushViewController:self.commentController animated:YES];
}
- (void)collectHandler:(UIButton*)btn {
if (!btn.selected) {
if (![TPUserDefault instance].userBO) {
loginHomeController *loginController = [loginHomeController new];
[self.navigationController pushViewController:loginController animated:YES];
}else {
[self collectContent];
}
}else {
if (![TPUserDefault instance].userBO) {
loginHomeController *loginController = [loginHomeController new];
[self.navigationController pushViewController:loginController animated:YES];
}else {
[self cancelCollect];
}
}
}
- (void)shareHandler:(UIButton*)btn {
btn.selected = !btn.selected;
if (btn.selected) {
[self.view addSubview:self.shareController.view];
// [CoreAnimationEffect animationPushUp:self.view];
[self.view bringSubviewToFront:self.shareController.view];
[UIView animateWithDuration:0.4 animations:^{
CGRect rect = self.shareController.view.frame;
rect.origin.y = self.barHeight;
self.shareController.view.frame = rect;
} completion:^(BOOL finished) {
}];
}else{
[UIView animateWithDuration:0.3 animations:^{
CGRect rect = self.shareController.view.frame;
rect.origin.y = rect_screen.size.height;
self.shareController.view.frame = rect;
} completion:^(BOOL finished) {
}];
// [self.shareController.view removeFromSuperview];
// [CoreAnimationEffect animationPushDown:self.view];
// self.shareController = nil;
}
// __weak typeof(self) Self = self;
// [UIView animateWithDuration:0.4 animations:^{
// Self.webView.alpha = 0.0;
// Self.shareController.view.alpha = 1.0;
// } completion:^(BOOL finished) {
// Self.webView.hidden = YES;
// Self.shareController.view.hidden = NO;
// }];
}
- (void)collectContent{
[self.viewModel collectContent];
}
- (void)cancelCollect {
[self.viewModel cancelCollect];
}
#pragma mark - sharecontroller delegate
- (void)openLinkWithSafari {
NSURL *newURL = [NSURL URLWithString:self.url];
[[UIApplication sharedApplication] openURL:newURL];
}
- (void)cancelShare:(BOOL)animate{
// CATransition *animation = [CATransition animation];
// animation.type = kCATransitionFade;
// animation.duration = 0.4;
// [self.shareController.view.layer addAnimation:animation forKey:nil];
//
// self.shareController.view.hidden = YES;
// self.webView.hidden = NO;
// __weak typeof(self) Self = self;
self.shareButton.selected = NO;
if (animate) {
[UIView animateWithDuration:0.3 animations:^{
CGRect rect = self.shareController.view.frame;
rect.origin.y = rect_screen.size.height;
self.shareController.view.frame = rect;
} completion:^(BOOL finished) {
}];
}else {
CGRect rect = self.shareController.view.frame;
rect.origin.y = rect_screen.size.height;
self.shareController.view.frame = rect;
}
}
#pragma mark - askdelegate
- (void)removeButtonHandler {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
NSLog(@"dealloc:%@",self.description);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"warning!!!!!!!!!");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
/*
#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
|