// // RemoteCameraViewController.m // watch // // Created by xTT on 2017/7/17. // Copyright © 2017年 xTT. All rights reserved. // #import "RemoteCameraViewController.h" #import "MyMQTT.h" //#import "XLPhotoBrowser.h" #import "Photo.h" #import "PhotoHeaderCollectionReusableView.h" #import "PhotosViewController.h" #import "WHToast.h" @interface RemoteCameraViewController () @property (assign, nonatomic) BOOL isDelete; @property (nonatomic,strong) UIView * vbottom; @property (nonatomic,strong) UIButton *leftBtn; @property (nonatomic,strong) UIButton *rightBtn; @property (nonatomic,strong) UIBarButtonItem *rightBarButton; @property (nonatomic,copy) id delayBlock; /** 总的数组(未分 日期) */ @property (strong, nonatomic) NSMutableArray *dayDataSource; @end @implementation RemoteCameraViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] init]; } return _myDataSource; } -(NSMutableArray *)dayDataSource{ if(!_dayDataSource){ _dayDataSource = [NSMutableArray array]; } return _dayDataSource; } -(void)setRightBarBtnTitle:(NSString *)title RoImage:(UIImage *)image{ if(title){ self.rightBarButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClick:)]; } if (image) { self.rightBarButton = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed:@"icon_delete"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClick:)]; } self.navigationItem.rightBarButtonItem = self.rightBarButton; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"远程拍照"; [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; //注册头部 [self.myCollectionView registerNib:[UINib nibWithNibName:@"PhotoHeaderCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PhotoHeaderCollectionReusableView"]; [[NSNotificationCenter defaultCenter] removeObserver:self name:PUSH_type_deviceAccountMessage object:nil]; WEAKSELF [[NSNotificationCenter defaultCenter] addObserverForName:PUSH_type_deviceAccountMessage object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { if ([note.object[@"message"][@"imei"] isEqualToString:cUser.cDevice.imei]) { if([note.object[@"message"][@"type"] isEqualToString:@"photograph"]){ [weakSelf loadMoreDataWithPhoto:nil]; } // Photo *obj = [Photo mj_objectWithKeyValues:note.object[@"message"]]; // obj.id = note.object[@"message"][@"photographID"]; // [self.myDataSource insertObject:obj atIndex:0]; // [self.myCollectionView reloadData]; } }]; self.myCollectionView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{ [weakSelf loadMoreDataWithPhoto:[[self.myDataSource lastObject] lastObject]]; }]; // self.myCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ // [weakSelf loadMoreDataWithPhoto:nil]; // }]; [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view.mas_centerX); make.bottom.equalTo(self.view.mas_bottom).offset(-22); make.height.offset(44); make.width.offset(SWIDTH-28); }]; [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.view); make.bottom.equalTo(self.bottomBtn.mas_top).offset(-6); }]; self.bottomBtn.layer.cornerRadius = 3; self.bottomBtn.layer.masksToBounds = YES; self.bottomBtn.backgroundColor = mainColor; [self setUI]; } -(void)setUI{ if(_vbottom){ [_vbottom removeFromSuperview]; _vbottom = nil; } _vbottom = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-getRectNavAndStatusHight-45-11, ScreenWidth, 45+11)]; _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_leftBtn setTitle:@"全选" forState:UIControlStateNormal]; _leftBtn.layer.cornerRadius = 3; _leftBtn.layer.masksToBounds = YES; [_leftBtn setBackgroundColor:mainColor]; [_leftBtn setTitleColor:[UIColor whiteColor] forState:0]; [_leftBtn.titleLabel setFont:[UIFont systemFontOfSize:15]]; _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightBtn setTitle:@"删除" forState:UIControlStateNormal]; _rightBtn.layer.cornerRadius = 3; _rightBtn.layer.masksToBounds = YES; [_rightBtn setBackgroundColor:mainColor]; [_rightBtn setTitleColor:[UIColor whiteColor] forState:0]; [_rightBtn.titleLabel setFont:[UIFont systemFontOfSize:15]]; // [_vbottom.layer setBorderColor:[UIColor lightGrayColor].CGColor]; // _vbottom.backgroundColor = RGBA(241, 241, 241, 1); _leftBtn.frame = CGRectMake(14, 1, (ScreenWidth-28-25)/2, 44); _rightBtn.frame = CGRectMake(14+(ScreenWidth-28-25)/2+25, 1, (ScreenWidth-28-25)/2, 44); [_vbottom addSubview:_leftBtn]; [_vbottom addSubview:_rightBtn]; [self.view addSubview:_vbottom]; [self.view bringSubviewToFront:_vbottom]; //置顶 [_rightBtn addTarget:self action:@selector(rightBtnActoin:) forControlEvents:UIControlEventTouchUpInside]; [_leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside]; //默认是隐藏的 [self.vbottom setHidden:YES]; WEAKSELF self.delayBlock = dispatch_block_create(0, ^{ weakSelf.bottomBtn.backgroundColor = mainColor; weakSelf.bottomBtn.enabled = YES; [weakSelf.bottomBtn setTitle:@"远程拍照" forState:0]; }); } -(void)rightBtnActoin:(UIButton *)sender{ // 删除 NSMutableArray *photos = [NSMutableArray array]; for (NSArray * secArr in self.myDataSource) { for (Photo *photo in secArr) { if(photo.isSelect){ [photos addObject: photo]; } } } if(photos.count == 0){ LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"注意" message:@"请选择要删除的照片" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; return; } NSString *content = [NSString stringWithFormat:@"确认要删除%d张照片?",photos.count]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"注意" message:content style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { WEAKSELF [Photo deletePhotos:[photos copy] success:^{ //删除成功 刷新数据 [weakSelf loadMoreDataWithPhoto:nil]; [UICommon MessageSuccessText:@"删除照片成功" isImg:YES]; // 取消删除模式 [weakSelf.vbottom setHidden:YES]; [weakSelf.bottomBtn setHidden:NO]; weakSelf.isDelete = NO; } failure:^{ xLog(@"删除失败,请稍后再试..."); [UICommon MessageErrorText:@"删除失败,请稍后再试..."]; }]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; } -(void)leftBtnAction:(UIButton *)sender{ //全选 if ([sender.titleLabel.text isEqualToString:@"全选"]) { for (NSArray* secArr in self.myDataSource){ for (Photo *photo in secArr) { photo.isSelect = YES; } } [self.myCollectionView reloadData]; [sender setTitle:@"全不选" forState:0]; }else{ //取消全选 for (NSArray* secArr in self.myDataSource){ for (Photo *photo in secArr) { photo.isSelect = NO; } } [self.myCollectionView reloadData]; [sender setTitle:@"全选" forState:0]; } } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // [self.myCollectionView.mj_header beginRefreshing]; [self loadMoreDataWithPhoto:nil]; } - (void)loadMoreDataWithPhoto:(Photo *)photo{ WEAKSELF if(!photo){ [self.dayDataSource removeAllObjects]; } [Photo getObjsWithID:photo.id success:^(NSMutableArray *arr) { [weakSelf.dayDataSource addObjectsFromArray:arr]; [weakSelf.myDataSource removeAllObjects]; weakSelf.myDataSource = [weakSelf partitionByDayWithArr:weakSelf.dayDataSource]; [weakSelf.myCollectionView reloadData]; // weakSelf.isDelete = NO; // [weakSelf.myCollectionView.mj_header endRefreshing]; [weakSelf.myCollectionView.mj_footer endRefreshing]; if (weakSelf.myDataSource.count == 0){ weakSelf.navigationItem.rightBarButtonItem = nil; [weakSelf.vbottom setHidden:YES]; [weakSelf.bottomBtn setHidden:NO]; }else{ //右侧导航栏添加个按钮 删除模式 if(weakSelf.isDelete){ [weakSelf setRightBarBtnTitle:@"取消" RoImage:nil]; }else{ [weakSelf setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; } } BOOL temp = NO; for (NSArray* secArr in self.myDataSource){ for (Photo *photo in secArr) { if (photo.isSelect == NO){ temp = YES; } } } if(temp){ //有个一个没选到 [self.leftBtn setTitle:@"全选" forState:0]; }else{ [self.leftBtn setTitle:@"全不选" forState:0]; } } failure:^(NSError *error){ if (!error) { } // [weakSelf.myCollectionView.mj_header endRefreshing]; [weakSelf.myCollectionView.mj_footer endRefreshing]; }]; } -(NSMutableArray*)partitionByDayWithArr:(NSArray*)arr{ //按日期分区 NSMutableArray *mArr = [NSMutableArray array]; NSMutableArray *singleDayArr = [NSMutableArray array]; NSDateFormatter *formater = [[NSDateFormatter alloc] init]; formater.dateFormat = @"yyyy年MM月dd日"; if(arr.count>0){ Photo *fristPhoto = arr[0]; NSDate *fristTimeDate = [NSDate dateWithTimeIntervalSince1970:fristPhoto.time.intValue]; NSString *lastDay = [formater stringFromDate:fristTimeDate]; //第一个日期 for (int i = 0 ;i < arr.count ; i++) { Photo *photo = arr[i]; NSNumber *time = photo.time; NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:time.intValue]; NSString *day = [formater stringFromDate:timeDate]; if([lastDay isEqualToString:day]){ //同一天 [singleDayArr addObject:photo]; if(i == arr.count-1){ [mArr addObject:[singleDayArr mutableCopy]]; } }else{ lastDay = day; [mArr addObject:[singleDayArr mutableCopy]]; [singleDayArr removeAllObjects]; [singleDayArr addObject:photo]; if(i == arr.count-1){ [mArr addObject:[singleDayArr mutableCopy]]; } } } } return mArr; } - (void)rightBarItemClick:(UIBarButtonItem *)sender{ self.isDelete = !self.isDelete; if (self.isDelete) { [self setRightBarBtnTitle:@"取消" RoImage:nil]; }else{ [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; } if(self.isDelete){ [self.vbottom setHidden:NO]; [self.bottomBtn setHidden:YES]; }else{ [self.vbottom setHidden:YES]; [self.bottomBtn setHidden:NO]; } [self.myCollectionView reloadData]; } - (IBAction)bottomClick:(UIButton *)sender{ WEAKSELF [cUser.cDevice editOperationDeviceInfoWithParameters:@{@"opPhoto":@(YES)} success:^(id responseObject) { weakSelf.bottomBtn.backgroundColor = disableColor; weakSelf.bottomBtn.enabled = NO; [weakSelf.bottomBtn setTitle:@"请稍后再点击" forState:0]; [WHToast showMessage:@"请求拍照成功,请等待设备上传照片" duration:3 finishHandler:^{ }]; /*LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"请求拍照成功,请等待设备上传照片" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; */ } failure:^(NSError *error) { }]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 15* NSEC_PER_SEC), dispatch_get_main_queue(), self.delayBlock); } #pragma collectionView -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.myDataSource.count; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSArray *arr = self.myDataSource[section]; return arr.count; } -(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if([kind isEqualToString:UICollectionElementKindSectionHeader]){ PhotoHeaderCollectionReusableView *headView = (PhotoHeaderCollectionReusableView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PhotoHeaderCollectionReusableView" forIndexPath:indexPath]; Photo *model = self.myDataSource[indexPath.section][0]; NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:model.time.intValue]; NSDateFormatter *formater = [[NSDateFormatter alloc] init]; formater.dateFormat = @"yyyy年MM月dd日"; headView.timeTitleLabel.text = [formater stringFromDate:timeDate]; // headView.backgroundColor = [mainColor colorWithAlphaComponent:0.5]; return headView; } return nil; } //这个方法是返回 Header的大小 size -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(SWIDTH, 30); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; UIImageView *imageView = [cell viewWithTag:100]; UIImageView *selImageView = [cell viewWithTag:101]; if (!imageView) { imageView = [[UIImageView alloc] init]; imageView.tag = 100; [cell.contentView addSubview:imageView]; [imageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(cell.contentView).insets(UIEdgeInsetsMake(2, 2, 2, 2)); }]; selImageView = [[UIImageView alloc] init]; selImageView.tag = 101; [cell.contentView addSubview:selImageView]; [selImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(imageView.mas_top).offset(4); make.right.equalTo(imageView.mas_right).offset(-4); }]; selImageView.hidden = YES; } Photo *photo = self.myDataSource[indexPath.section][indexPath.item]; [imageView sd_setImageWithURL:[NSURL URLWithString:photo.thumbURL] placeholderImage:[myHelper getImageWithName:@""] options:SDWebImageRetryFailed]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.masksToBounds = YES; if (self.isDelete) { selImageView.hidden = NO; }else{ selImageView.hidden = YES; } if (photo.isSelect) { selImageView.image = [myHelper getImageWithName:@"选中"]; }else{ selImageView.image = [myHelper getImageWithName:@"未选中"]; } return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return (CGSize){SWIDTH / 4, SWIDTH / 4}; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.isDelete) { Photo *photo = self.myDataSource[indexPath.section][indexPath.item]; photo.isSelect = !photo.isSelect; [collectionView reloadItemsAtIndexPaths:@[indexPath]]; BOOL temp = NO; for (NSArray* secArr in self.myDataSource){ for (Photo *photo in secArr) { if (photo.isSelect == NO){ temp = YES; } } } if(temp){ //有个一个没选到 [self.leftBtn setTitle:@"全选" forState:0]; }else{ [self.leftBtn setTitle:@"全不选" forState:0]; } }else{ // NSMutableArray *photoArr = [NSMutableArray array]; // NSArray *sectionArray = self.myDataSource[indexPath.section]; // [sectionArray enumerateObjectsUsingBlock:^(Photo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // [photoArr addObject:obj.url]; // }]; // [XLPhotoBrowser showPhotoBrowserWithImages:photoArr currentImageIndex:indexPath.item]; PhotosViewController *vc = [PhotosViewController new]; vc.dataSouce = [self.myDataSource[indexPath.section] copy]; vc.currentIndex = indexPath.item; [self.navigationController pushViewController:vc animated:YES]; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; dispatch_block_cancel(self.delayBlock); } - (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