// // PBProjectListViewController.m // IphoneBIMe // // Created by zjf on 2018/7/13. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBProjectListViewController.h" #import "PBLoginViewController.h" #import "PBProjectModel.h" #import "PBProjectTableViewCell.h" #import "PBMineView.h" #import "MenuView.h" #import "PBTabBarController.h" #import "PBPromptView.h" #import "PBMessageController.h" #import "PBChangePWController.h" #import "PBRefreshGifHeader.h" static NSString *const cellID = @"cellID"; @interface PBProjectListViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *dataList; @property (nonatomic, weak) PBMineView *mineView; @property (nonatomic, strong) MenuView *menu; @property (nonatomic, strong) UISearchBar *searchBar; @property (nonatomic, strong) UIButton *maskBtn; @property (nonatomic, copy) NSString *fullName; @property (nonatomic, strong) PBPromptView *promptView; @property (nonatomic, strong) PBRefreshGifHeader *headerView; @end @implementation PBProjectListViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupNav]; [self setupUI]; [self setupRefresh]; if ([NSString getLoginData]) { [self.tableView.mj_header beginRefreshing]; }else{ [self toLogin]; } } - (void)toLogin { self.dataList = nil; [self.tableView reloadData]; PBLoginViewController *loginVC = [[PBLoginViewController alloc] init]; loginVC.LoginSuccessfulBlock = ^{ self.mineView.realName = Realname; self.searchBar.text = nil; self.fullName = nil; [self.tableView.mj_header beginRefreshing]; }; loginVC.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:loginVC animated:NO completion:nil]; } - (void)setupNav { UIBarButtonItem *leftNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Project_list_mine"] style:UIBarButtonItemStylePlain target:self action:@selector(leftNavItemAction)]; UIBarButtonItem *rightNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Project_list_news"] style:UIBarButtonItemStylePlain target:self action:@selector(rightNavItemAction)]; self.searchBar = [[UISearchBar alloc] init]; self.searchBar.placeholder = @"请搜索项目名称"; self.searchBar.delegate = self; self.searchBar.searchBarStyle = UISearchBarStyleMinimal; self.navigationItem.leftBarButtonItem = leftNavItem; self.navigationItem.rightBarButtonItem = rightNavItem; self.navigationItem.titleView = self.searchBar; } - (void)leftNavItemAction { [self.menu show]; } - (void)rightNavItemAction { PBMessageController *mesVC = [[PBMessageController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mesVC]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:nav animated:YES completion:nil]; } - (void)setupRefresh { // MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ // [self loadProjectList]; // }]; // header.lastUpdatedTimeLabel.hidden = YES; // header.stateLabel.textColor = TitleColor; // [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle]; // [header setTitle:@"释放更新" forState:MJRefreshStatePulling]; // [header setTitle:@"加载中..." forState:MJRefreshStateRefreshing]; // self.tableView.mj_header = header; self.tableView.mj_header = self.headerView; } - (void)loadProjectList { if (self.fullName == nil) { self.fullName = @""; } [[PBNetworkTools sharedTools] getProjectListWithKeyword:self.fullName andIsPublic:@"" andSort:@"CreateDate desc" andSkip:@"0" andtake:@"999" andOnlyFavorite:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if(error) { NSLog(@"%@",error); [self.tableView.mj_header endRefreshing]; [YJProgressHUD showMessage:@"加载项目列表失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { self.dataList = [NSArray yy_modelArrayWithClass:[PBProjectModel class] json:[networkModel.Data valueForKey:@"rows"]]; [self.tableView.mj_header endRefreshing]; [self.tableView reloadData]; if (self.dataList.count == 0) { self.promptView.hidden = NO; }else { self.promptView.hidden = YES; } }else { [self.tableView.mj_header endRefreshing]; [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } #pragma mark - UISearchBarDelegate - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.fullName = searchBar.text; [self.tableView.mj_header beginRefreshing]; [self.searchBar resignFirstResponder]; } //- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ // self.fullName = searchBar.text; // [self.tableView.mj_header beginRefreshing]; //} - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [self.view addSubview:self.maskBtn]; self.navigationItem.leftBarButtonItem.enabled = NO; self.navigationItem.rightBarButtonItem.enabled = NO; return YES; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { [self.maskBtn removeFromSuperview]; self.navigationItem.leftBarButtonItem.enabled = YES; self.navigationItem.rightBarButtonItem.enabled = YES; if ([searchBar.text isEqualToString:@""]) { self.fullName = @""; [self.tableView.mj_header beginRefreshing]; } } - (void)setupUI { self.view.backgroundColor = [UIColor whiteColor]; self.tableView = [[UITableView alloc] init]; self.tableView.rowHeight = 120; [self.tableView registerClass:[PBProjectTableViewCell class] forCellReuseIdentifier:cellID]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.right.equalTo(self.view); }]; PBMineView *mineView = [[PBMineView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth * 0.82, MainScreenHeight)]; mineView.customDelegate = self; mineView.realName = Realname; self.mineView = mineView; self.menu = [[MenuView alloc]initWithDependencyView:self.view MenuView:mineView isShowCoverView:YES]; [self.view addSubview:self.promptView]; self.promptView.imageV.image = [UIImage imageNamed:@"Doc_unable_preview"]; self.promptView.textL.text = @"抱歉,暂时没有项目"; self.promptView.hidden = YES; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PBProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; cell.projectModel = self.dataList[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { PBTabBarController *tabBarController = [[PBTabBarController alloc] init]; tabBarController.projectModel = self.dataList[indexPath.row]; tabBarController.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:tabBarController animated:YES completion:nil]; } #pragma mark - HomeMenuViewDelegate - (void)LeftMenuViewClick:(NSInteger)tag{ [self.menu hidenWithAnimation]; switch (tag) { case 0:{ //修改密码 PBChangePWController *changePwVC = [[PBChangePWController alloc] init]; changePwVC.ChangePWCompleteBlock = ^{ [self toLogin]; }; [self.navigationController pushViewController:changePwVC animated:YES]; } break; case 1:{ //清除缓存 [self clearCache]; } break; case 10:{ [self toLogin]; } break; default: break; } } - (void)clearCache { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"将删除所有缓存数据" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *clearAction = [UIAlertAction actionWithTitle:@"清除缓存" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) { [YJProgressHUD showProgress:@"正在删除" inView:self.view]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",cachePath); NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:cachePath]; for (NSString *fileName in fileEnumerator) { NSString *filePath = [cachePath stringByAppendingPathComponent:fileName]; [fileManager removeItemAtPath:filePath error:nil]; } [YJProgressHUD showMessage:@"删除完成" inView:self.view]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alertVC addAction:clearAction]; [alertVC addAction:cancelAction]; [self presentViewController:alertVC animated:YES completion:nil]; } - (UIButton *)maskBtn { if (_maskBtn == nil) { _maskBtn = [[UIButton alloc] initWithFrame:self.view.bounds]; _maskBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2]; [_maskBtn addTarget:self action:@selector(cancelMaskBtn) forControlEvents:UIControlEventTouchUpInside]; } return _maskBtn; } - (void)cancelMaskBtn { [self.searchBar resignFirstResponder]; } - (PBPromptView *)promptView { if (_promptView == nil) { _promptView = [[PBPromptView alloc] initWithFrame:self.view.bounds]; } return _promptView; } - (PBRefreshGifHeader *)headerView { if(_headerView == nil) { _headerView = [[PBRefreshGifHeader alloc] init]; __weak typeof(self) weakSelf = self; [self.headerView setRefreshingBlock:^{ [weakSelf loadProjectList]; }]; } return _headerView; } @end