|
//
// messageCenterController.m
// ThePaperHD
//
// Created by liyuan on 15/7/15.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "messageCenterController.h"
#import "ATPagingView.h"
#import "RubberBandView.h"
#import "notificationController.h"
#import "letterController.h"
#import "pushMsgController.h"
@interface messageCenterController ()<ATPagingViewDelegate,letterListDelegate,notificationDelegate,pushMsgDelegate>
{
NSInteger lastIndex;
}
@property(nonatomic, strong)UIButton *closeBtn;
@property(nonatomic, strong)UIButton *editBtn;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)UIButton *notificationBtn;
@property(nonatomic, strong)UIButton *letterBtn;
@property(nonatomic, strong)ATPagingView *pageView;
@property(nonatomic, strong)NSMutableArray *pageViewControllers;
@property(nonatomic, strong)NSMutableArray *viewControllers;
@property(nonatomic, strong)UIView *menuRubberView;
@property(nonatomic, strong)UILabel *markLabel;
@property(nonatomic, strong)NSMutableDictionary *msgDic;
@property(nonatomic, strong)pushMsgController *pushMsgVC;
@end
@implementation messageCenterController
@synthesize msgDic;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.hidden = YES;
[self.view addSubview:self.closeBtn];
[self.view addSubview:self.titleLabel];
self.closeBtn.frame = CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 0, 50, 80);
[self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
self.titleLabel.frame = CGRectMake(0,
0,
hotAskListPopUpSize.width,
80);
self.backView.frame = CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 80, messagePopSize.width, messagePopSize.height-80);
[self.view addSubview:self.backView];
[self addTabButtns];
[self addPageView];
self.pushMsgVC = [pushMsgController new];
self.pushMsgVC.pushDelegate = self;
msgDic = [NSMutableDictionary dictionary];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)appearLettreMark:(NSMutableDictionary *)dic{
msgDic = [NSMutableDictionary dictionaryWithDictionary:dic];
if ([dic[@"letterMark"] intValue] >0) {
self.markLabel.hidden = NO;
self.markLabel.frame = CGRectMake(CGRectGetMaxX(self.letterBtn.frame)-90, CGRectGetHeight(self.letterBtn.frame)/2 - 10, 20, 20);
if ([dic[@"letterMark"] intValue] >9) {
self.markLabel.text = @"9+";
}else{
self.markLabel.text = [NSString stringWithFormat:@"%@",dic[@"letterMark"]];
}
[self.backView addSubview:self.markLabel];
}else{
self.markLabel.text = @"";
self.markLabel.hidden = YES;
[self.markLabel removeFromSuperview];
}
}
- (UIButton*)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_closeBtn setImage:Image(@"login/popUpCloseBtn.png") forState:UIControlStateNormal];
[_closeBtn setImage:Image(@"login/popUpCloseBtn_s.png") forState:UIControlStateHighlighted];
[_closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (UILabel*)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.text = @"我的消息";
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = appFont(30, NO);
}
return _titleLabel;
}
- (UILabel*)markLabel {
if (!_markLabel) {
_markLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_markLabel.backgroundColor = [UIColor colorWithHexString:@"0xc32128"];
_markLabel.layer.cornerRadius = 20/2;
_markLabel.clipsToBounds = YES;
_markLabel.textAlignment = NSTextAlignmentCenter;
_markLabel.textColor = [UIColor whiteColor];
_markLabel.font = appFont(11, NO);
_markLabel.hidden = NO;
}
return _markLabel;
}
- (UIView*)backView {
if (!_backView) {
_backView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame),messagePopSize.width, messagePopSize.height-80)];
_backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
}
return _backView;
}
- (UIButton*)notificationBtn {
if (!_notificationBtn) {
_notificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_notificationBtn setTitle:@"通知" forState:UIControlStateNormal];
_notificationBtn.titleLabel.font = appFont(20, NO);
[_notificationBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_notificationBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateSelected];
_notificationBtn.frame = CGRectMake(15, 0, (messagePopSize.width-30)/2, 114/2);
[_notificationBtn addTarget:self action:@selector(notificationEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _notificationBtn;
}
- (UIButton*)letterBtn {
if (!_letterBtn) {
_letterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_letterBtn setTitle:@"私信" forState:UIControlStateNormal];
_letterBtn.titleLabel.font = appFont(20, NO);
[_letterBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_letterBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateSelected];
_letterBtn.frame = CGRectMake(CGRectGetMaxX(self.notificationBtn.frame),0 , (messagePopSize.width-30)/2, 114/2);
[_letterBtn addTarget:self action:@selector(letterEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _letterBtn;
}
- (ATPagingView*)pageView {
if (!_pageView) {
_pageView = [[ATPagingView alloc]initWithFrame:CGRectMake(0,
114/2,
messagePopSize.width,
CGRectGetHeight(self.backView.bounds) - 114/2)];
_pageView.gapBetweenPages = 0;
_pageView.recyclingEnabled = NO;
_pageView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_pageView.delegate = self;
_pageView.scrollView.bounces = NO;
_pageView.scrollView.scrollEnabled = YES;
}
return _pageView;
}
- (UIView*)menuRubberView {
if (!_menuRubberView) {
_menuRubberView = [[UIView alloc]initWithFrame:CGRectMake(15,114/2-3, (messagePopSize.width-30)/2, 3)];
_menuRubberView.backgroundColor = [UIColor colorWithHexString:BLUECOLOR];
}
return _menuRubberView;
}
#pragma mark - add subviews
- (void)addTabButtns {
[self.backView addSubview:self.notificationBtn];
[self.backView addSubview:self.letterBtn];
UIView *belowLine = [[UIView alloc]initWithFrame:CGRectMake(15, 114/2-1, messagePopSize.width-30, 1)];
belowLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
[self.backView addSubview:belowLine];
UIView *shortLine1 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.notificationBtn.frame), 24, 1, 18)];
shortLine1.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
[self.backView addSubview:shortLine1];
[self.backView addSubview:self.menuRubberView];
}
- (void)addPageView {
self.viewControllers = [NSMutableArray arrayWithObjects:@"notificationController",@"letterController",nil];
[self.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (nil == self.pageViewControllers) {
self.pageViewControllers = [NSMutableArray array];
}
if ([obj isKindOfClass:[UIViewController class]]) {
[self.pageViewControllers addObject:obj];
[self addChildViewController:obj];
} else {
[self.pageViewControllers addObject:[NSNull null]];
}
}];
[self.backView addSubview:self.pageView];
self.notificationBtn.selected = YES;
[self.pageView reloadData];
}
#pragma mark - button handler
- (void)closeEvent:(UIButton*)btn {
[self dismissControllerAnimated:YES completion:nil];
}
- (void)notificationEvent:(UIButton*)btn {
if (btn.selected == YES) {
return;
}
btn.selected = !btn.selected;
if (btn.selected) {
self.pageView.currentPageIndex = 0;
self.notificationBtn.selected = NO;
self.letterBtn.selected = NO;
}
}
- (void)letterEvent:(UIButton*)btn {
[msgDic setValue:@(0) forKey:@"letterMark"];
[self appearLettreMark:msgDic];
// NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].msgMark];
// [dic setValue:@(0) forKey:@"letterMark"];
// [TPUserDefault instance].msgMark = dic;
// self.markLabel.hidden = YES;
if (btn.selected == YES) {
return;
}
btn.selected = !btn.selected;
if (btn.selected) {
self.pageView.currentPageIndex = 1;
self.notificationBtn.selected = NO;
self.letterBtn.selected = NO;
}
}
#pragma mark - pageView delegate
- (NSInteger)numberOfPagesInPagingView:(ATPagingView *)pagingView {
return self.viewControllers.count;
}
- (UIView *)viewForPageInPagingView:(ATPagingView *)pagingView atIndex:(NSInteger)index {
if (self.pageViewControllers[index] == [NSNull null]) {
NSString *className = self.viewControllers[index];
Class class = NSClassFromString(className);
UIViewController *vc = [[class alloc] init];
self.pageViewControllers[index] = vc;
if(0 == index){
notificationController *vc = self.pageViewControllers[0];
vc.notificationDele =self;
}else if(1 == index){
letterController *vc = self.pageViewControllers[1];
vc.letterlistDele =self;
vc.isRequest = NO;
}
[self addChildViewController:vc];
}
return ((UIViewController*)self.pageViewControllers[index]).view;
}
- (void)currentPageDidChangeInPagingView:(ATPagingView *)pageView {
if (pageView.currentPageIndex == 0) {
self.notificationBtn.selected = YES;
self.letterBtn.selected = NO;
}else if (pageView.currentPageIndex == 1) {
if([msgDic[@"letterMark"] intValue] > 0){
[msgDic setValue:@(0) forKey:@"letterMark"];
[self appearLettreMark:msgDic];
}
letterController *vc = self.pageViewControllers[1];
vc.isRequest = YES;
self.notificationBtn.selected = NO;
self.letterBtn.selected = YES;
}
}
- (void)pagesDidChangeInPagingView:(ATPagingView *)pageView {
}
- (void)pagingViewWillBeginMoving:(ATPagingView *)pageView {
}
- (void)pagingViewdDidScroll:(UIScrollView *)scrollerView {
CGFloat width = (messagePopSize.width-30);
CGFloat scrollOffsetX = scrollerView.contentOffset.x;
CGRect rect = self.menuRubberView.frame;
CGFloat scale = scrollOffsetX/(messagePopSize.width*2);
CGFloat offset = width*scale;
rect.origin.x = 15+offset;
self.menuRubberView.frame = rect;
}
#pragma mark -- delegate
-(void)listGotoOtherVC:(letterBO *)bo{
if ([self.messageCenterDelegate respondsToSelector:@selector(messageCenterGotoOtherVC:)]) {
[self.messageCenterDelegate messageCenterGotoOtherVC:bo];
}
}
-(void)notificationToNavi:(commentObjectVO *)comment{
if ([self.messageCenterDelegate respondsToSelector:@selector(notificationToCenterHome:)]) {
[self.messageCenterDelegate notificationToCenterHome:comment];
}
}
-(void)pushMsgTomessageCenter:(letterBO *)bo{
if ([self.messageCenterDelegate respondsToSelector:@selector(pushMsgToCenterHome:)]) {
[self.messageCenterDelegate pushMsgToCenterHome:bo];
}
}
-(void)notifiToUserGamb:(commentObjectVO *)bo{
if ([self.messageCenterDelegate respondsToSelector:@selector(messageToUserGamb:)]) {
[self.messageCenterDelegate messageToUserGamb:bo];
}
}
-(void)notifiToTopicList{
if ([self.messageCenterDelegate respondsToSelector:@selector(centerToTopicList)]) {
[self.messageCenterDelegate centerToTopicList];
}
}
-(void)notiToNews:(NSDictionary *)dic{//进入原新闻
if ([self.messageCenterDelegate respondsToSelector:@selector(centerToNews:)]) {
[self.messageCenterDelegate centerToNews:dic];
}
}
-(void)notiToTopic:(NSDictionary *)dic{//进入愿话题
if ([self.messageCenterDelegate respondsToSelector:@selector(centerToTopic:)]) {
[self.messageCenterDelegate centerToTopic:dic];
}
}
@end
|