// // PBModelCategoryViewController.m // IphoneBIMe // // Created by zjf on 2018/7/23. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBModelCategoryViewController.h" #import "PBModelsModel.h" #import "PBPhaseLabel.h" #import "PBViewsModel.h" #import "PBDrawingsModel.h" #import "PBViewPointModel.h" #import "PBTextTableViewCell.h" #import "PBImageTableViewCell.h" #import "PBLoadModelDisplayController.h" #import "PBDrawingsShowController.h" #import "PBViewPointDetailsController.h" #import "PBMoreTabViewController.h" #import "PBAlertController.h" #import "PBMoreFunctionView.h" #import "PBAddIssueController.h" #import "PBNavigationController.h" #import "PBPromptView.h" #import "PBShareView.h" #import "PBProjectModel.h" static NSString *const TextCellID = @"TextCellID"; static NSString *const ImageCellID = @"ImageCellID"; @interface PBModelCategoryViewController () @property (nonatomic, strong) UIScrollView *phaseScrollView; @property (nonatomic, strong) NSArray *phaseArr; @property (nonatomic, strong) NSMutableArray *labelM; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, assign) NSInteger index; @property (nonatomic, strong) PBMoreFunctionView *moreFunctionView; @property (nonatomic, strong) PBPromptView *promptView; @property (nonatomic, strong) PBShareView *shareView; @end @implementation PBModelCategoryViewController - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) { self.edgesForExtendedLayout = UIRectEdgeNone; } [self setupNav]; [self setupUI]; [self loadData:0]; } /// 创建频道标签 - (void)createChannelLabel{ CGFloat labelW = self.view.width / 4; CGFloat labelH = self.phaseScrollView.bounds.size.height; for (int i = 0; i < self.phaseArr.count; i++) { PBPhaseLabel *label = [[PBPhaseLabel alloc] init]; [self.phaseScrollView addSubview:label]; CGFloat labelX = labelW * i; label.frame = CGRectMake(labelX, 0, labelW, labelH); self.phaseScrollView.contentSize = CGSizeMake(labelW * self.phaseArr.count, 0); label.text = self.phaseArr[i]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapClick:)]; [label addGestureRecognizer:tap]; label.tag = i; label.userInteractionEnabled = YES; [self.labelM addObject:label]; if (i == 0) { // label.scale = 1.0; label.isSelectLabel = YES; }else { label.isSelectLabel = NO; } } } #pragma mark - 频道标签的点击事件 - (void)labelTapClick:(UITapGestureRecognizer *)tap { PBPhaseLabel *selected_label = (PBPhaseLabel *)tap.view; CGFloat labelOffsetX = selected_label.center.x - (self.view.bounds.size.width * 0.5); CGFloat minOffsetX = 0; CGFloat maxOffsetX = self.phaseScrollView.contentSize.width - self.view.bounds.size.width; if (maxOffsetX > minOffsetX) { if (labelOffsetX < minOffsetX) { labelOffsetX = 0; } else if (labelOffsetX > maxOffsetX) { labelOffsetX = maxOffsetX; } [self.phaseScrollView setContentOffset:CGPointMake(labelOffsetX, 0) animated:YES]; } #pragma mark - 点击频道标签剧中时,更新模型 self.index = selected_label.tag; [self loadData:selected_label.tag]; #pragma mark - 点击label时选中的缩放比例最大,其余的保持 NSInteger index = selected_label.tag; for (int i = 0; i < self.labelM.count; ++i) { PBPhaseLabel *label = self.labelM[i]; if (index == i) { // label.scale = 1.0; label.isSelectLabel = YES; } else { // label.scale = 0; label.isSelectLabel = NO; } } } - (void)loadData:(NSInteger)index { self.dataList = nil; [self.tableView reloadData]; self.promptView.hidden = YES; // [YJProgressHUD showCustomAnimation:@"" inview:self.view]; [YJProgressHUD showProgress:@"" inView:self.view]; if (index == 0) { [[PBNetworkTools sharedTools] RequestGetAllViewsWithProjectID:_modelsModel.ProjectID andModelID:_modelsModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载失败" inView:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *arr = [NSArray yy_modelArrayWithClass:[PBViewsModel class] json:str]; self.dataList = arr.mutableCopy; if (self.dataList.count == 0) { self.promptView.hidden = NO; }else { [self.tableView reloadData]; } }]; }else if (index == 1) { [[PBNetworkTools sharedTools] RequestGetFIleWithProjectID:_modelsModel.ProjectID andModelID:_modelsModel.ID andVersionNO:@"" andFileType:@"PlanView" andFileName:@"sheets" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载失败" inView:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSDictionary *dict = [NSString convertTodictionaryOrArr:str]; NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (NSDictionary *obj in [dict valueForKey:@"sheets"]) { PBDrawingsModel *model = [PBDrawingsModel yy_modelWithDictionary:obj]; [arrM addObject:model]; } self.dataList = arrM; if (self.dataList.count == 0) { self.promptView.hidden = NO; }else { [self.tableView reloadData]; } }]; }else if (index == 2) { [[PBNetworkTools sharedTools] RequestGetAllViewpointWithProjectId:_modelsModel.ProjectID andModelId:_modelsModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载失败" inView:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *arr = [NSArray yy_modelArrayWithClass:[PBViewPointModel class] json:str]; NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (PBViewPointModel *viewPointModel in arr) { if ([viewPointModel.Type isEqualToString:@"0"]) { [arrM addObject:viewPointModel]; } } self.dataList =arrM; if (self.dataList.count == 0) { self.promptView.hidden = NO; }else { [self.tableView reloadData]; } }]; }else { [[PBNetworkTools sharedTools] RequestGetAllViewpointWithProjectId:_modelsModel.ProjectID andModelId:_modelsModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载失败" inView:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *arr = [NSArray yy_modelArrayWithClass:[PBViewPointModel class] json:str]; NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (PBViewPointModel *viewPointModel in arr) { if ([viewPointModel.Type isEqualToString:@"1"]) { [arrM addObject:viewPointModel]; } } self.dataList = arrM; if (self.dataList.count == 0) { self.promptView.hidden = NO; }else { [self.tableView reloadData]; } }]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id model = self.dataList[indexPath.row]; if (self.index == 0) { PBTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TextCellID forIndexPath:indexPath]; cell.viewsModel = (PBViewsModel *)model; return cell; }else { PBImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellID forIndexPath:indexPath]; if (self.index == 1) { cell.drawingsModel = (PBDrawingsModel *)model; }else { cell.viewPointModel = (PBViewPointModel *)model; } return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id model = self.dataList[indexPath.row]; if (self.index == 0) { PBLoadModelDisplayController *modelDisplayVC = [[PBLoadModelDisplayController alloc] init]; modelDisplayVC.projectModel = _projectModel; modelDisplayVC.modelID = _modelsModel.ID; PBViewsModel *viewsModel = (PBViewsModel *)model; modelDisplayVC.viewID = viewsModel.ID; [self.navigationController pushViewController:modelDisplayVC animated:YES]; }else if (self.index == 1) { PBDrawingsShowController *drawingsShowVC = [[PBDrawingsShowController alloc] init]; drawingsShowVC.modelsModel = _modelsModel; drawingsShowVC.drawingsModel = (PBDrawingsModel *)model; [self.navigationController pushViewController:drawingsShowVC animated:YES]; }else { PBViewPointDetailsController *viewPointDetailsVC = [[PBViewPointDetailsController alloc] init]; viewPointDetailsVC.updataDefaultViewPointBlock = ^{ [self loadData:self.index]; }; viewPointDetailsVC.projectModel = _projectModel; viewPointDetailsVC.modelsModel = _modelsModel; viewPointDetailsVC.viewPointModel = (PBViewPointModel *)model; [self.navigationController pushViewController:viewPointDetailsVC animated:YES]; } } - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { id model = self.dataList[indexPath.row]; UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"分享" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self shareWithModel:self.dataList[indexPath.row]]; }]; shareAction.backgroundColor = WarningColor; UITableViewRowAction *setDefaultAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"设为默认" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self creatAlertControllerWithIsDelete:NO andIsViews:YES andData:model andIndex:indexPath]; }]; setDefaultAction.backgroundColor = DisableColor; UITableViewRowAction *deleteAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self creatAlertControllerWithIsDelete:YES andIsViews:NO andData:model andIndex:indexPath]; }]; deleteAction.backgroundColor = ErrorColor; UITableViewRowAction *moreAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self moreWith:(PBViewPointModel *)model andIndexPath:indexPath]; }]; moreAction.backgroundColor = DisableColor; switch (self.index) { case 0:{ PBViewsModel *viewsModel = (PBViewsModel *)model; if (viewsModel.IsDefault) { return @[shareAction]; }else { return @[shareAction, setDefaultAction]; } break; } case 2:{ return @[shareAction, moreAction]; break; } default: return @[deleteAction, shareAction]; break; } } //分享 - (void)shareWithModel:(id)model { NSString *title; NSString *modelID; NSString *viewID; NSString *viewPointID; NSString *desc; UIImage *image; if (self.index == 0) { title = @"视图"; PBViewsModel *viewsModel = (PBViewsModel *)model; desc = viewsModel.Name; modelID = viewsModel.ModelID; viewID = viewsModel.ID; viewPointID = @""; image = nil; }else { if (self.index == 2) { title = @"视点"; }else { title = @"批注"; } PBViewPointModel *viewPointModel = (PBViewPointModel *)model; desc = viewPointModel.Name; modelID = viewPointModel.ModelID; viewID = @""; viewPointID = viewPointModel.ID; NSRange range = [viewPointModel.Snapshot rangeOfString:@"base64,"]; NSString *base64 = [viewPointModel.Snapshot substringFromIndex:range.location + range.length]; image = [NSString imageDecoding:base64]; } [PBKeyWindow addSubview:self.shareView]; self.shareView.modelID = modelID; self.shareView.desc = desc; self.shareView.viewID = viewID; self.shareView.viewPointID = viewPointID; self.shareView.title = title; self.shareView.image = image; [self.shareView show]; } - (PBMoreFunctionView *)moreFunctionView { if (_moreFunctionView == nil) { _moreFunctionView = [[PBMoreFunctionView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _moreFunctionView.visualViewHeight = 204; _moreFunctionView.dataArr = @[@"删除", @"设为默认"]; _moreFunctionView.title = @"模型名称"; } return _moreFunctionView; } - (void)moreWith:(PBViewPointModel *)viewPointModel andIndexPath:(NSIndexPath *)indexPath { [PBKeyWindow addSubview:self.moreFunctionView]; [self.moreFunctionView show]; __weak typeof(self) weakSelf = self; self.moreFunctionView.seletedIndexBlock = ^(NSInteger index) { if (index == 0) { [weakSelf creatAlertControllerWithIsDelete:YES andIsViews:NO andData:viewPointModel andIndex:indexPath]; }else { [weakSelf creatAlertControllerWithIsDelete:NO andIsViews:NO andData:viewPointModel andIndex:indexPath]; } }; } - (void)creatAlertControllerWithIsDelete:(BOOL)isDelete andIsViews:(BOOL)isViews andData:(id)model andIndex:(NSIndexPath *)indexPath { NSString *message; if (isDelete) { PBViewPointModel *viewPointModel = (PBViewPointModel *)model; NSString * str = [viewPointModel.Type isEqualToString:@"0"] ? @"视点" : @"批注"; message = [NSString stringWithFormat:@"您确定要删除此%@",str]; }else { NSString *str = isViews == YES ? @"视图" : @"视点"; message = [NSString stringWithFormat:@"您确定把此%@设为默认%@",str, str]; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { if (isDelete) { [self deleteViewPointWith:(PBViewPointModel *)model andIndexPath:indexPath]; }else { if (isViews) { [self setDefaultViewsWith:(PBViewsModel *)model]; }else { [self setDefaultViewPointWith:(PBViewPointModel *)model]; } } }]; [alertVC addAction:cancelAction]; [alertVC addAction:determineAction]; [self presentViewController:alertVC animated:YES completion:nil]; } - (void)deleteViewPointWith:(PBViewPointModel *)viewPointModel andIndexPath:(NSIndexPath *)indexPath { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] RequestDeleteViewpointWithProjectID:_modelsModel.ProjectID andViewpointID:viewPointModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showFailed:@"删除失败" inview:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); if ([str isEqualToString:@"true"]) { [YJProgressHUD hide]; [self.dataList removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; }else { [YJProgressHUD showFailed:@"删除失败" inview:self.view]; } }]; } - (void)setDefaultViewsWith:(PBViewsModel *)viewsModel { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] RequestSetDefaultViewsWithProjectID:_modelsModel.ProjectID andModelId:_modelsModel.ID andViewID:viewsModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showFailed:@"设置默认视图失败" inview:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); if ([str isEqualToString:@"\"\""]) { [self loadData:self.index]; }else { [YJProgressHUD showFailed:@"设置默认视图失败" inview:self.view]; } }]; } - (void)setDefaultViewPointWith:(PBViewPointModel *)viewPointModel { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] RequestSetDefaultViewpointWithProjectID:_modelsModel.ProjectID andModelId:_modelsModel.ID andViewpointID:viewPointModel.ID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showFailed:@"设置默认视点失败" inview:self.view]; return; } [YJProgressHUD hide]; NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); if ([str isEqualToString:@"\"\""]) { [self loadData:self.index]; }else { [YJProgressHUD showFailed:@"设置默认视点失败" inview:self.view]; } }]; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (self.index == 1) { return NO; }else { return YES; } } - (void)setupNav { self.title = _modelsModel.Name; PBBackNavItem *backNav = [PBBackNavItem backNacItem]; backNav.title = @"模型"; [backNav addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav]; self.navigationItem.leftBarButtonItem = backNavItem; } - (void)backItemAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)setupUI { self.view.backgroundColor = [UIColor z_colorWithR:239 G:239 B:239]; self.phaseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 43)]; self.phaseScrollView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.phaseScrollView]; self.tableView = [[UITableView alloc] init]; self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.rowHeight = 70; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [self.tableView registerClass:[PBTextTableViewCell class] forCellReuseIdentifier:TextCellID]; [self.tableView registerClass:[PBImageTableViewCell class] forCellReuseIdentifier:ImageCellID]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.phaseScrollView.mas_bottom).offset(1); make.left.right.bottom.equalTo(self.view); }]; [self.view addSubview:self.promptView]; [self.promptView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.tableView); make.bottom.left.right.equalTo(self.view); }]; self.promptView.hidden = YES; [self createChannelLabel]; } - (void)setModelsModel:(PBModelsModel *)modelsModel { _modelsModel = modelsModel; } - (NSMutableArray *)labelM { if (_labelM == nil) { _labelM = [NSMutableArray array]; } return _labelM; } - (NSArray *)phaseArr { if (_phaseArr == nil) { _phaseArr = @[@"视图", @"二维图纸",@"视点", @"批注"]; } return _phaseArr; } - (PBPromptView *)promptView { if (_promptView == nil) { _promptView = [[PBPromptView alloc] init]; _promptView.imageV.image = [UIImage imageNamed:@"Model_category_empty"]; _promptView.textL.text = @"暂无数据"; _promptView.hidden = YES; } return _promptView; } - (PBShareView *)shareView { if (_shareView == nil) { _shareView = [[PBShareView alloc] initWithFrame:[UIScreen mainScreen].bounds]; CGFloat height; if (IS_IPHONE_X) { height = 290.f + 34.f; }else { height = 290.f; } _shareView.visualViewHeight = height; _shareView.viewController = self; _shareView.projectID = self.projectModel.organizeid; } return _shareView; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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