123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- #import "YRMsgVC.h"
- #import <UU_Ent/Message.pbrpc.h>
- #import "YRUserInfo.h"
- #import "YRMsgMainModel.h"
- #import "YRMsgMainCell.h"
- #import "YRWebVC.h"
- #import "YRTabBar.h"
- #import "YRMsgHeader.h"
- @interface YRMsgVC ()<UITableViewDelegate,UITableViewDataSource>
- @property(strong,nonatomic)YRTableView *tableView;
- @property(strong,nonatomic)NSMutableArray *dataArr;
- @property(strong,nonatomic)YRMsgHeader *headerView;
- @property(strong,nonatomic)UIButton *rigBtn;
- @property(assign,nonatomic)BOOL isSpread;
- @end
- @implementation YRMsgVC
- - (instancetype)init
- {
- self = [super init];
- if (self) {
-
- [self regesterNotification];
-
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
- }
- -(void)viewWillAppear:(BOOL)animated{
-
- [super viewWillAppear:animated];
-
- [self setNavigationBarBackgroundImageWithAppear:YES];
-
- }
- -(void)createUI{
-
- self.navigationItem.title = @"消息";
-
- [self setNavigationBar];
-
- [self.view addSubview:self.tableView];
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.edges.equalTo(self.view);
-
- }];
-
-
- [self setUpHeaderView];
-
- [self loadData];
- }
- -(void)setUpHeaderView{
-
- self.headerView = [[YRMsgHeader alloc]initWithFrame:CGRectMake(0, -121, self_width, 121)];
-
- self.tableView.estimatedRowHeight = 0;
-
- [self.tableView insertSubview:self.headerView atIndex:1];
-
- self.headerView.hidden = YES;
-
- }
- -(void)setNavigationBarBackgroundImageWithAppear:(BOOL)appear{
-
- if (appear) {
-
- [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
-
- if(statusXLately){
-
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UU_newMsgNavBarX"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
-
- }else{
-
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UU_newMsgNavBar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
-
- }
-
- [self.navigationController.navigationBar setShadowImage:[UIImage new]];
-
- }
-
-
- }
- -(void)setNavigationBar{
-
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0]}];
-
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
- btn.frame = CGRectMake(0, 5, 22, 22);
- CGFloat imageY = 0;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0) {
- imageY = 5;
- }
- [btn setImage:[UIImage imageNamed:@"UU_msg_快捷打卡"] forState:UIControlStateNormal];
- self.rigBtn = btn;
- btn.tintColor = [UIColor whiteColor];
- [btn addTarget:self action:@selector(spreadOutHeaderOrNot:) forControlEvents:UIControlEventTouchUpInside];
-
- UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:btn];
-
- barButton.tintColor = [UIColor whiteColor];
-
- self.navigationItem.rightBarButtonItems = @[barButton];
-
- }
- -(void)spreadOutHeaderOrNot:(UIButton *)button{
-
- button.userInteractionEnabled = NO;
-
- if (!self.isSpread) {
-
- self.headerView.hidden = NO;
-
- [UIView animateWithDuration:1 animations:^{
-
- [self.tableView setContentOffset:CGPointMake(0, -121)];
-
- } completion:^(BOOL finished) {
-
- self.tableView.contentInset = UIEdgeInsetsMake(121, 0, 0,0);
-
- self.isSpread = !self.isSpread;
-
- button.userInteractionEnabled = YES;
-
- }];
-
-
- }else{
-
- [UIView animateWithDuration:1 animations:^{
-
- [self.tableView setContentOffset:CGPointMake(0, 0)];
-
- } completion:^(BOOL finished) {
-
- self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0,0);
-
- self.headerView.hidden = YES;
-
- self.isSpread = !self.isSpread;
-
- button.userInteractionEnabled = YES;
-
-
- }];
-
- }
-
-
-
- }
- #pragma mark - 通知
- -(void)regesterNotification{
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:kYRUserMasterChangedNotification object:nil];
-
- }
- #pragma mark - 网络请求
- -(void)loadData{
-
-
- DLog(@"这里是消息的数据刷新");
-
- NSDictionary *comDic = [YRUserInfo shareManager].userCompanyInfo;
-
- if (comDic) {
-
- self.dataArr = [YRMsgMainModel mj_objectArrayWithKeyValuesArray:comDic[@"messageConfigArray"]];
-
- NSMutableArray *unArr =comDic[@"unreadMessageCountArray"];
-
- for (YRMsgMainModel *model in self.dataArr) {
-
- for (NSDictionary *unDic in unArr) {
-
- if ([model.code isEqualToString:FORMAT(@"%@",unDic[@"code"])]) {
-
- model.remindCount = [unDic[@"count"] integerValue]+arc4random_uniform(100);
-
- }
-
- }
-
- }
-
- [self calculteRemindCount];
-
- [self.tableView reloadData];
-
- }else{
-
- [self getDataFromServer];
-
- }
-
-
- }
- -(void)getDataFromServer{
-
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
-
- GetMessageConfigRequest *request = [GetMessageConfigRequest message];
-
- GRPCProtoCall *call = [client RPCTogetConfigWithRequest:request handler:^(GetMessageConfigResponse * _Nullable response, NSError * _Nullable error) {
-
- if (!error) {
-
- NSDictionary *dict = response.mj_keyValues;
-
- self.dataArr = [YRMsgMainModel mj_objectArrayWithKeyValuesArray:dict[@"configArray"]];
-
- [self getUnreadMessage];
-
-
- }else{
-
-
-
- }
-
- }];
-
- call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
-
- [call start];
-
-
- }
- -(void)getUnreadMessage{
-
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
-
- GetUnreadMessageCountRequest *request = [GetUnreadMessageCountRequest message];
-
- GRPCProtoCall *call = [client RPCTogetUnreadCountWithRequest:request handler:^(GetUnreadMessageCountResponse * _Nullable response, NSError * _Nullable error) {
- [self.tableView.mj_header endRefreshing];
- for (YRMsgMainModel *model in self.dataArr) {
-
- for (UnreadMessageCount *msgUn in response.unreadArray) {
-
- if ([model.code isEqualToString:msgUn.code]) {
-
- model.remindCount = msgUn.count+arc4random_uniform(100);
-
- }
-
- }
-
- }
-
- [self calculteRemindCount];
-
- [self.tableView reloadData];
-
- }];
-
- call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
-
- [call start];
-
- }
- -(void)setMessageReadRequest:(YRMsgMainModel *)model{
-
- [GRPCCall useInsecureConnectionsForHost:kHostAddress];
-
- MessageService *client = [[MessageService alloc]initWithHost:kHostAddress];
-
- SetMessageReadRequest *request = [SetMessageReadRequest message];
-
- GRPCProtoCall *call = [client RPCTosetReadWithRequest:request handler:^(SetMessageReadResponse * _Nullable response, NSError * _Nullable error) {
-
- NSDictionary *dict = response.mj_keyValues;
-
- DLog(@"%@",dict);
-
- }];
-
- call.requestHeaders[@"authentication"] = [YRUserInfo shareManager].token;
-
- [call start];
-
-
- }
- #pragma mark - delegate && dataSource
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
-
- return 1;
-
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return self.dataArr.count;
-
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- YRMsgMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YRMsgMainCellReid"];
-
- if (!cell) {
-
- cell = [[YRMsgMainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YRMsgMainCellReid"];
-
- }
-
- if (indexPath.row < self.dataArr.count) {
-
- cell.model = self.dataArr[indexPath.row];
-
- }
-
- return cell;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return 66;
-
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
-
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- YRMsgMainModel *model = self.dataArr[indexPath.row];
-
- if (model.viewType == 0) {
-
- YRBaseVC *VC = (YRBaseVC *)[[NSClassFromString(model.desVC) alloc]init];
-
- if (!VC) {
-
- VC = [YRBaseVC new];
-
- }
-
- VC.title = model.name;
-
- [self.navigationController pushViewController:VC animated:YES];
-
- }else{
-
- YRWebVC *webVC = [[YRWebVC alloc]init];
-
- webVC.url = model.webURL;
-
- [self.navigationController pushViewController:webVC animated:YES];
-
- }
-
- }
- #pragma mark - 滑动
- -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
-
- if(!self.rigBtn.userInteractionEnabled){
-
- return;
-
- }
-
- if (self.isSpread) {
-
- if(scrollView.contentOffset.y < -121){
-
- [scrollView setContentOffset:CGPointMake(0, -121)];
-
- return;
-
- }
-
-
- if (scrollView.contentOffset.y > -70)
- {
-
- self.rigBtn.userInteractionEnabled = NO;
-
- scrollView.userInteractionEnabled = NO;
-
- self.isSpread = NO;
-
- [UIView animateWithDuration:0.6 animations:^{
-
- self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
-
- } completion:^(BOOL finished) {
-
- [self.tableView setContentOffset:CGPointMake(0, 0)];
-
- scrollView.userInteractionEnabled = YES;
- self.headerView.hidden = YES;
- self.rigBtn.userInteractionEnabled = YES;
-
- }];
-
- }
-
- }
-
- }
- #pragma mark - 计数
- -(void)calculteRemindCount{
-
- YRTabBar *tabbar = (YRTabBar *)self.tabBarController.tabBar;
-
- NSUInteger remind = 0;
-
- for (YRMsgMainModel *model in self.dataArr) {
-
- remind += model.remindCount;
-
- }
-
- tabbar.remindCount = remind;
- }
- -(YRTableView *)tableView{
-
- if (!_tableView) {
-
- _tableView = [[YRTableView alloc]initWithFrame:self.view.frame];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.autoNoDataSet = NO;
- _tableView.tableHeaderView = [UIView new];
- _tableView.tableFooterView = [UIView new];
- @weakify(self)
- _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- @strongify(self)
-
- [self getDataFromServer];
-
- }];
-
- }
-
- return _tableView;
- }
- -(NSMutableArray *)dataArr{
-
- if (!_dataArr) {
-
- _dataArr = [[NSMutableArray alloc]init];
-
- }
-
- return _dataArr;
- }
- -(void)dealloc{
-
- [[NSNotificationCenter defaultCenter] removeObserver:self name:kYRUserMasterChangedNotification object:nil];
-
- }
- @end
|