|
//
// messageController.m
// ThePaperBase
//
// Created by YoungLee on 15/8/17.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "messageController.h"
#import "ATPagingView.h"
#import "MLNavigationController.h"
#import "letterController.h"
#import "RubberBandView.h"
#import "ZSAnimationView.h"
#import "messageViewModel.h"
#import "notificationController.h"
@interface messageController ()<ATPagingViewDelegate,UIGestureRecognizerDelegate,messageViewModelDelegate,notificationDelegate>
{
UIPanGestureRecognizer *panGestureRecognizer;
UISwipeGestureRecognizer *swipeGesture;
NSMutableDictionary *msgDic;
}
@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)ZSAnimationView *menuRubberView;
@property(nonatomic, strong)UILabel *markLabel;
@property(nonatomic, strong)UIView *belowLine;
@property(nonatomic, strong)UIView *shortLine1;
@end
@implementation messageController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
[MobClick event:@"21"];
self.titleStr = @"我的消息";
self.view.backgroundColor = [UIColor clearColor];
self.view.frame = CGRectMake(0, CGRectGetHeight(self.naviBar.bounds)-1, rect_screen.size.width, rect_screen.size.height-CGRectGetHeight(self.naviBar.bounds)+1);
self.backView.frame = self.view.frame;
[self.view addSubview:self.backView];
[self addTabButtns];
[self addPageView];
swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandler:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
swipeGesture.delegate = self;
[self.backView addGestureRecognizer:swipeGesture];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
// [self.backView addSubview:self.markLabel];
// Do any additional setup after loading the view.
}
- (void)needrefreshNightMode:(id)sender{
self.backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.pageView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.belowLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
self.shortLine1.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].msgMark];
[dic setObject:@(0) forKey:@"status"];
[dic setObject:@(0) forKey:@"questionMark"];
[dic setObject:@(0) forKey:@"attendMark"];
[dic setObject:@(0) forKey:@"replyedMark"];
[dic setObject:@(0) forKey:@"letterMark"];
[TPUserDefault instance].msgMark = [NSMutableDictionary dictionaryWithDictionary:dic];
}
- (void)swipeHandler:(UISwipeGestureRecognizer*)gestureRecognizer{
if (self.pageView.currentPageIndex == 0 ) {
[MobClick event:@"83"];
self.letterBtn.selected = !self.letterBtn.selected;
self.notificationBtn.selected = !self.notificationBtn.selected;
// 【倒退】我的话题和私信页面,不断切换列表,列表名下方的横线消失(bug:5164)
[self.pageView setCurrentPageIndexWithAnimation:1];
[self.menuRubberView animationStartWithOffsetX:3*(rect_screen.size.width)/4-20];
}else{
self.notificationBtn.selected = !self.notificationBtn.selected;
self.letterBtn.selected = !self.letterBtn.selected;
// 【倒退】我的话题和私信页面,不断切换列表,列表名下方的横线消失(bug:5164)
[self.pageView setCurrentPageIndexWithAnimation:0];
[self.menuRubberView animationStartWithOffsetX:(rect_screen.size.width)/4-20];
}
}
- (void)pan:(UIPanGestureRecognizer*)panGesture {
if ([delegate conformsToProtocol:@protocol(messageDelegate)] &&
[delegate respondsToSelector:@selector(removeAskView:)]) {
[delegate removeAskView:panGesture];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
//- (void)backHandler {
// if ([delegate conformsToProtocol:@protocol(messageDelegate)] &&
// [delegate respondsToSelector:@selector(removeButtonHandler)]) {
// [delegate removeButtonHandler];
// }
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView*)backView {
if (!_backView) {
_backView = [[UIView alloc]initWithFrame:CGRectMake(0,70,rect_screen.size.width, rect_screen.size.height-CGRectGetHeight(self.naviBar.bounds))];
_backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
}
return _backView;
}
- (UIButton*)notificationBtn {
if (!_notificationBtn) {
_notificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_notificationBtn setTitle:@"通知" forState:UIControlStateNormal];
_notificationBtn.titleLabel.font = appFont(16, NO);
[_notificationBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_notificationBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateSelected];
_notificationBtn.frame = CGRectMake(0, 0, rect_screen.size.width/2, 40);
[_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(16, NO);
[_letterBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_letterBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateSelected];
_letterBtn.frame = CGRectMake(CGRectGetMaxX(self.notificationBtn.frame),0 , rect_screen.size.width/2, 40);
[_letterBtn addTarget:self action:@selector(letterEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _letterBtn;
}
- (ATPagingView*)pageView {
if (!_pageView) {
_pageView = [[ATPagingView alloc]initWithFrame:CGRectMake(0,
40,
rect_screen.size.width,
CGRectGetHeight(self.backView.bounds) - 40)];
_pageView.gapBetweenPages = 0;
_pageView.recyclingEnabled = NO;
_pageView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_pageView.delegate = self;
_pageView.scrollView.bounces = NO;
_pageView.scrollView.scrollEnabled = NO;
}
return _pageView;
}
- (ZSAnimationView*)menuRubberView {
if (!_menuRubberView) {
_menuRubberView = [[ZSAnimationView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.notificationBtn.frame)/2 -20,40-3, 40, 3)];
_menuRubberView.backgroundColor = [UIColor colorWithHexString:BLUECOLOR];
}
return _menuRubberView;
}
- (UILabel*)markLabel {
if (!_markLabel) {
_markLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_markLabel.backgroundColor = [UIColor colorWithHexString:@"0xc32128"];
_markLabel.layer.cornerRadius = 15/2;
_markLabel.clipsToBounds = YES;
_markLabel.textAlignment = NSTextAlignmentCenter;
_markLabel.textColor = [UIColor whiteColor];
_markLabel.font = appFont(7, NO);
_markLabel.hidden = YES;
}
return _markLabel;
}
-(UIView *)belowLine{
if (!_belowLine) {
_belowLine = [UIView new];
_belowLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _belowLine;
}
-(UIView *)shortLine1{
if (!_shortLine1) {
_shortLine1 = [UIView new];
_shortLine1.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _shortLine1;
}
-(void)setCurrentIndex:(NSInteger)index{
_currentIndex = index;
if(_currentIndex == 1){
[self.menuRubberView animationStartWithOffsetX:3*rect_screen.size.width/4 - 20];
}else{
[self.menuRubberView animationStartWithOffsetX:rect_screen.size.width/4 - 20];
}
[self.pageView setCurrentPageIndexWithAnimation:index];
self.markLabel.hidden = YES;
}
#pragma mark - add subviews
- (void)addTabButtns {
[self.backView addSubview:self.notificationBtn];
[self.backView addSubview:self.letterBtn];
self.belowLine.frame = CGRectMake(0, 40-1, rect_screen.size.width, 1);
[self.backView addSubview:self.belowLine];
// UIView *shortLine1 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.notificationBtn.frame), 10, 1, 20)];
// shortLine1.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
self.shortLine1.frame = CGRectMake(CGRectGetMaxX(self.notificationBtn.frame), 10, 1, 20);
[self.backView addSubview:self.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)notificationEvent:(UIButton*)btn {
if (btn.selected == YES) {
return;
}
btn.selected = !btn.selected;
if (btn.selected) {
self.notificationBtn.selected = NO;
self.letterBtn.selected = NO;
// 【倒退】我的话题和私信页面,不断切换列表,列表名下方的横线消失(bug:5164)
[self.pageView setCurrentPageIndexWithAnimation:0];
[self.menuRubberView animationStartWithOffsetX:(rect_screen.size.width)/4-20];
}
}
- (void)letterEvent:(UIButton*)btn {
if ([msgDic[@"letterMark"] intValue] >0) { //我的消息:点击有红点的二级菜单,红点应立即消失,而不是从二级菜单返回后再消失(bug:5458)
[msgDic setObject:@"0" forKey:@"letterMark"];
[self appearMsgMark:msgDic];
}
if (btn.selected == YES) {
letterController *vc = self.pageViewControllers[1];
[vc scrollTableViewToTop];
return;
}
[MobClick event:@"83"];
btn.selected = !btn.selected;
if (btn.selected) {
self.notificationBtn.selected = NO;
self.letterBtn.selected = NO;
// 【倒退】我的话题和私信页面,不断切换列表,列表名下方的横线消失(bug:5164)
[self.pageView setCurrentPageIndexWithAnimation:1];
[self.menuRubberView animationStartWithOffsetX:3*(rect_screen.size.width)/4-20];
}
}
#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.delegate = self;
}else if(1 == index){
letterController *vc = self.pageViewControllers[1];
vc.isRequest = NO;
}
[self addChildViewController:vc];
}
return ((UIViewController*)self.pageViewControllers[index]).view;
}
- (void)currentPageDidChangeInPagingView:(ATPagingView *)pageView {
}
- (void)pagesDidChangeInPagingView:(ATPagingView *)pageView {
if (pageView.currentPageIndex == 0) {
self.notificationBtn.selected = YES;
self.letterBtn.selected = NO;
}else if (pageView.currentPageIndex == 1) {
//我的消息:点击有红点的二级菜单,红点应立即消失,而不是从二级菜单返回后再消失(bug:5458)
if ([msgDic[@"letterMark"] intValue] >0) {
[msgDic setObject:@"0" forKey:@"letterMark"];
[self appearMsgMark:msgDic];
}
letterController *vc = self.pageViewControllers[1];
[vc scrollTableViewToTop];
self.notificationBtn.selected = NO;
self.letterBtn.selected = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (!vc.isRequest) {//私信,切换私信和通知tab下方显示错误(bug:5061)
vc.isRequest = YES;
}
});
}
}
- (void)pagingViewWillBeginMoving:(ATPagingView *)pageView {
}
- (void)disablePanGesture {
panGestureRecognizer.enabled = NO;
}
#pragma mark -- notification delegate
-(void)appearMsgMark:(NSMutableDictionary *)dic{ //我的消息:点击有红点的二级菜单,红点应立即消失,而不是从二级菜单返回后再消失(bug:5458)
msgDic = [NSMutableDictionary dictionaryWithDictionary:dic];
if ([dic[@"letterMark"] intValue] >0) {
self.markLabel.hidden = NO;
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.hidden = YES;
}
[self.backView addSubview:self.markLabel];
[self.markLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.letterBtn.centerX).offset(30);
make.width.equalTo(@15);
make.centerY.equalTo(self.letterBtn.centerY);
make.height.equalTo(@15);
}];
}
@end
|