// // SPSBAlbumViewController.m // 我的社保 // // Created by shanp on 2020/9/15. // #import "SPSBPhotoAlbumViewController.h" #import "JXHPhotoAlbumManager.h" #import "SPSBUIGeneralHeader.h" #import "SPSBAlbumItemCollectionViewController.h" #import "SPSBChooseAlbumTableViewController.h" #import "UIViewController+SPSBInitialization.h" #import "UIViewController+SPSBNavigationBar.h" #import "SPSBVerificationProfile.h" #import "SPSBBusinessManager.h" #import "SPSBPhotoAlbumPreviewViewController.h" #import "SPSBNotificationDelegateManager.h" #define spsb_bottomViewHeight 56 @interface SPSBPhotoAlbumViewController () { JXHPhotoAlbumManager *_photoAlbumManager; SPSBAlbumItemCollectionViewController *_photoVC; UIView *_albumCoverView; SPSBChooseAlbumTableViewController *_albumVC; CGFloat _chooseAlbumViewHeight; UIButton *_confirmButton; JXHCenterButton *_navigationButton; UIButton *_previewButton; } @end @implementation SPSBPhotoAlbumViewController - (void)viewDidLoad { [super viewDidLoad]; self.spsb_isHiddenNavigationBar = true; [self setupUI]; [self getData]; spsb_setNotificationKey(SPSBChoosePublishPhotosNotificationKey); } - (void)dealloc { spsb_removeNotificationKey(SPSBChoosePublishPhotosNotificationKey); } #pragma mark - Action - (void)previewAction { if (spsb_currentChosePublishPhotos().count == 0) return; SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new; vc.spsb_dataArray = spsb_currentChosePublishPhotos(); vc.spsb_startIndex = 0; vc.spsb_finishChose = _spsb_finishChose; vc.spsb_photoAlbum = _photoVC.spsb_photoAlbum; [self.navigationController pushViewController:vc animated:true]; } - (void)confirmAction { if (spsb_currentChosePublishPhotos().count == 0) return; if (_spsb_finishActionFirst) { if (self->_spsb_finishChose) { self->_spsb_finishChose(nil); } [self.navigationController dismissViewControllerAnimated:true completion:nil]; return; } [self.navigationController dismissViewControllerAnimated:false completion:^{ if (self->_spsb_finishChose) { self->_spsb_finishChose(nil); } }]; } - (void)changeAlbum { if (_albumCoverView.hidden) { [self showChooseAlbumView]; } else { [self hiddenChooseAlbumView]; } } - (void)publishPhotoArrayChange:(NSArray *)array { } - (void)finishChosePhotoAlbum:(JXHPhotoAlbum *)photoAlbum { [self hiddenChooseAlbumView]; if ([photoAlbum isEqual:_photoVC.spsb_photoAlbum]) { return; } [self showPhotoWihtPhotoAlbum:photoAlbum]; } - (void)getData { _photoAlbumManager = JXHPhotoAlbumManager.new; _photoAlbumManager.jxh_canChooseVideo = spsb_publishCanChooseVideo(); self.authorizationStatus = [_photoAlbumManager getPhotosAuthorization]; } - (void)setAuthorizationStatus:(JXHPhotoAlbumStatus)authorizetionStatus { switch (authorizetionStatus) { case JXHPhotoAlbumStatusNormal: [self getAssetCollection]; break; case JXHPhotoAlbumStatusNoResource: { [self createTipLabelWithTitle:@"无照片" needSettingButton:false]; } break; case JXHPhotoAlbumStatusNoAuthorization: { NSString *tipTextWhenNoPhotosAuthorization; NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleName"]; tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"%@%@%@", @"请在设备的\"设置-", appName, @"-照片\"选项中,开启使用照片的权限"]; [self createTipLabelWithTitle:tipTextWhenNoPhotosAuthorization needSettingButton:true]; } break; case JXHPhotoAlbumStatusNoStatus: { [self authorize]; } break; default: break; } } - (void)authorize { [_photoAlbumManager requestAuthorization:^(JXHPhotoAlbumStatus status) { dispatch_main_async_safe(^ { self.authorizationStatus = status; }); }]; } - (void)toSetting { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; jxh_openUrl(url, nil); } - (void)getAssetCollection { @weakify(self) [_photoAlbumManager getLibraryWithCompletion:^(NSArray *albumArray, JXHPhotoAlbumResult photoAlbumResult) { @strongify(self) if (photoAlbumResult == JXHPhotoAlbumResultSuccess) { [self showPhotoWihtPhotoAlbum:albumArray[0]]; [self->_albumVC reloadData:albumArray]; } else if (photoAlbumResult == JXHPhotoAlbumResultNoResource) { self.authorizationStatus = JXHPhotoAlbumStatusNoResource; } }]; } - (void)showPhotoWihtPhotoAlbum:(JXHPhotoAlbum *)PhotoAlbum { _photoVC.spsb_photoAlbum = PhotoAlbum; _photoVC.view.hidden = false; [_navigationButton setTitle:_photoVC.spsb_photoAlbum.jxh_albumName state:JXHButtonControlStateNormal]; _navigationButton.hidden = false; } - (void)showChooseAlbumView { [_albumVC.tableView reloadData]; _albumCoverView.hidden = false; [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom).update(-10); }]; [UIView animateWithDuration:0.2 animations:^{ self->_navigationButton.jxh_icon.transform = CGAffineTransformMakeRotation(M_PI); [self.view layoutSubviews]; }]; } - (void)hiddenChooseAlbumView { [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom).update(-self->_chooseAlbumViewHeight); }]; [UIView animateWithDuration:0.2 animations:^{ self->_navigationButton.jxh_icon.transform = CGAffineTransformMakeRotation(0); [self.view layoutSubviews]; } completion:^(BOOL finished) { self->_albumCoverView.hidden = true; }]; } #pragma mark - SPSBNotificationDelegate - (void)spsb_currenChosePublishPhotosChanged:(NSArray *)currentArray chose:(JXHPhotoManager *)chose { if (currentArray.count > 0) { _previewButton.enabled = _confirmButton.enabled = true; [_confirmButton setTitle:[NSString stringWithFormat:@"%@(%ld/%ld)", @"确定", (long)currentArray.count + spsb_alreadyChosePublishPhoto(), (long)spsb_publishMaxCount()] state:JXHButtonControlStateNormal]; } else { _previewButton.enabled = _confirmButton.enabled = false; } } #pragma mark - UI - (void)setupUI { [self createNavigationBar]; [self.view layoutSubviews];//获取navigationbar高度 [self setControllers]; [self.view bringSubviewToFront:self.spsb_navigationBar]; [self setNavigationBar]; } - (void)setNavigationBar { _navigationButton = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisHorizontal interval:0 font:spsb_mediumFont(16) target:self action:@selector(changeAlbum)]; [_navigationButton setImage:jxh_getImage(photo_album_arrow_down) state:JXHButtonControlStateNormal]; _navigationButton.hidden = true; [self.spsb_navigationBar addSubview:_navigationButton]; [_navigationButton makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(72); make.trailing.equalTo(-72); make.height.equalTo(jxh_navigationViewHeight()); make.bottom.equalTo(0); }]; } - (void)setControllers { _photoVC = SPSBAlbumItemCollectionViewController.new; _photoVC.spsb_finishChose = _spsb_finishChose; _photoVC.spsb_finishActionFirst = _spsb_finishActionFirst; _photoVC.view.hidden = true; [self addChildViewController:_photoVC]; [self.view addSubview:_photoVC.view]; [_photoVC.view makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom); make.bottom.equalTo(self.view.safebottom).offset(-spsb_bottomViewHeight); make.leading.and.trailing.equalTo(0); }]; [self createBottomView]; _albumCoverView = UIView.new; _albumCoverView.hidden = true; _albumCoverView.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha); [_albumCoverView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenChooseAlbumView)]]; [self.view addSubview:_albumCoverView]; [_albumCoverView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom); make.leading.and.trailing.and.bottom.equalTo(0); }]; _chooseAlbumViewHeight = jxh_screenHeight() * 0.7; _albumVC = SPSBChooseAlbumTableViewController.new; @weakify(self) _albumVC.spsb_finishChose = ^(JXHPhotoAlbum * _Nonnull result) { @strongify(self) [self finishChosePhotoAlbum:result]; }; [_albumVC.tableView setLayerCornerRadius:8 clipToBounds:true]; [self addChildViewController:_albumVC]; [self.view addSubview:_albumVC.tableView]; [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom).offset(-self->_chooseAlbumViewHeight); make.leading.and.trailing.equalTo(0); make.height.equalTo(self->_chooseAlbumViewHeight); }]; } - (void)createBottomView { UIView *bottomView = UIView.new; bottomView.backgroundColor = spsb_FFFFFF_color(1.f); [self.view addSubview:bottomView]; [bottomView makeConstraints:^(JXHConstraintMaker *make) { make.leading.and.trailing.and.bottom.equalTo(0); make.top.equalTo(self.view.safebottom).offset(-spsb_bottomViewHeight); }]; _previewButton = [UIButton convenienceWithFont:spsb_mediumFont(16) target:self action:@selector(previewAction)]; _previewButton.enabled = false; [_previewButton setTitleColor:spsb_000000_color(0.28) state:JXHButtonControlStateDisabled]; [_previewButton setTitle:@"预览" titleColor:spsb_000000_color(0.8) state:JXHButtonControlStateNormal]; [bottomView addSubview:_previewButton]; [_previewButton makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(0); make.bottom.equalTo(bottomView.safebottom); make.size.equalTo((CGSize){64, spsb_bottomViewHeight}); }]; _confirmButton = [UIButton convenienceWithFont:spsb_mediumFont(14) target:self action:@selector(confirmAction)]; CGSize size = (CGSize){96, 32}; [_confirmButton setTitleColor:spsb_FFFFFF_color(1.f) state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted]; [_confirmButton setBackgroundImage:jxh_createCornerImage(spsb_000000_color(0.8), size.height / 2, size, JXHCornerPositionAll, 0, nil) state:JXHButtonControlStateNormal]; [_confirmButton setTitleColor:spsb_000000_color(0.28) state:JXHButtonControlStateDisabled]; [_confirmButton setBackgroundImage:jxh_createCornerImage(spsb_F5F5F5_color(), size.height / 2, size, JXHCornerPositionAll, 0, nil) state:JXHButtonControlStateDisabled]; _confirmButton.enabled = false; [_confirmButton setTitle:@"确定" state:JXHButtonControlStateNormal]; [bottomView addSubview:_confirmButton]; [_confirmButton makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-16); make.bottom.equalTo(bottomView.safebottom).offset(-12); make.size.equalTo(size); }]; } - (void)createTipLabelWithTitle:(NSString *)title needSettingButton:(bool)flag { UILabel *tipsLabel = [UILabel convenienceWithFont:spsb_font(14) textColor:spsb_000000_color(0.8) limitWidth:jxh_screenWidth()]; [tipsLabel setAttributedStringWithText:title textAlignment:NSTextAlignmentCenter lineSpacing:10 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:0]; [self.view addSubview:tipsLabel]; [tipsLabel makeConstraints:^(JXHConstraintMaker *make) { make.left.greaterThanOrEqualTo(16); make.right.lessThanOrEqualTo(-16); make.centerX.equalTo(self.view).priorityRequired(); make.top.equalTo(jxh_viewHeight(self.spsb_navigationBar) + 60); }]; if (flag) { UIButton *settingButton = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(toSetting)]; [settingButton setTitle:@"去设置" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal]; [self.view addSubview:settingButton]; [settingButton makeConstraints:^(JXHConstraintMaker *make) { make.centerX.equalTo(self.view); make.top.equalTo(tipsLabel.bottom).offset(20); make.size.equalTo(CGSizeMake(60, 44)); }]; } } @end