SPSBPhotoAlbumViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // SPSBAlbumViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2020/9/15.
  6. //
  7. #import "SPSBPhotoAlbumViewController.h"
  8. #import "JXHPhotoAlbumManager.h"
  9. #import "SPSBUIGeneralHeader.h"
  10. #import "SPSBAlbumItemCollectionViewController.h"
  11. #import "SPSBChooseAlbumTableViewController.h"
  12. #import "UIViewController+SPSBInitialization.h"
  13. #import "UIViewController+SPSBNavigationBar.h"
  14. #import "SPSBVerificationProfile.h"
  15. #import "SPSBBusinessManager.h"
  16. #import "SPSBPhotoAlbumPreviewViewController.h"
  17. #import "SPSBNotificationDelegateManager.h"
  18. #define spsb_bottomViewHeight 56
  19. @interface SPSBPhotoAlbumViewController ()<SPSBNotificationDelegate> {
  20. JXHPhotoAlbumManager *_photoAlbumManager;
  21. SPSBAlbumItemCollectionViewController *_photoVC;
  22. UIView *_albumCoverView;
  23. SPSBChooseAlbumTableViewController *_albumVC;
  24. CGFloat _chooseAlbumViewHeight;
  25. UIButton *_confirmButton;
  26. JXHCenterButton *_navigationButton;
  27. UIButton *_previewButton;
  28. }
  29. @end
  30. @implementation SPSBPhotoAlbumViewController
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.spsb_isHiddenNavigationBar = true;
  34. [self setupUI];
  35. [self getData];
  36. spsb_setNotificationKey(SPSBChoosePublishPhotosNotificationKey);
  37. }
  38. - (void)dealloc {
  39. spsb_removeNotificationKey(SPSBChoosePublishPhotosNotificationKey);
  40. }
  41. #pragma mark - Action
  42. - (void)previewAction {
  43. if (spsb_currentChosePublishPhotos().count == 0) return;
  44. SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new;
  45. vc.spsb_dataArray = spsb_currentChosePublishPhotos();
  46. vc.spsb_startIndex = 0;
  47. vc.spsb_finishChose = _spsb_finishChose;
  48. vc.spsb_photoAlbum = _photoVC.spsb_photoAlbum;
  49. [self.navigationController pushViewController:vc animated:true];
  50. }
  51. - (void)confirmAction {
  52. if (spsb_currentChosePublishPhotos().count == 0) return;
  53. if (_spsb_finishActionFirst) {
  54. if (self->_spsb_finishChose) {
  55. self->_spsb_finishChose(nil);
  56. }
  57. [self.navigationController dismissViewControllerAnimated:true completion:nil];
  58. return;
  59. }
  60. [self.navigationController dismissViewControllerAnimated:false completion:^{
  61. if (self->_spsb_finishChose) {
  62. self->_spsb_finishChose(nil);
  63. }
  64. }];
  65. }
  66. - (void)changeAlbum {
  67. if (_albumCoverView.hidden) {
  68. [self showChooseAlbumView];
  69. } else {
  70. [self hiddenChooseAlbumView];
  71. }
  72. }
  73. - (void)publishPhotoArrayChange:(NSArray<JXHPhotoManager *> *)array {
  74. }
  75. - (void)finishChosePhotoAlbum:(JXHPhotoAlbum *)photoAlbum {
  76. [self hiddenChooseAlbumView];
  77. if ([photoAlbum isEqual:_photoVC.spsb_photoAlbum]) {
  78. return;
  79. }
  80. [self showPhotoWihtPhotoAlbum:photoAlbum];
  81. }
  82. - (void)getData {
  83. _photoAlbumManager = JXHPhotoAlbumManager.new;
  84. _photoAlbumManager.jxh_canChooseVideo = spsb_publishCanChooseVideo();
  85. self.authorizationStatus = [_photoAlbumManager getPhotosAuthorization];
  86. }
  87. - (void)setAuthorizationStatus:(JXHPhotoAlbumStatus)authorizetionStatus {
  88. switch (authorizetionStatus) {
  89. case JXHPhotoAlbumStatusNormal: [self getAssetCollection];
  90. break;
  91. case JXHPhotoAlbumStatusNoResource: {
  92. [self createTipLabelWithTitle:@"无照片" needSettingButton:false];
  93. }
  94. break;
  95. case JXHPhotoAlbumStatusNoAuthorization: {
  96. NSString *tipTextWhenNoPhotosAuthorization;
  97. NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary];
  98. NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleName"];
  99. tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"%@%@%@", @"请在设备的\"设置-", appName, @"-照片\"选项中,开启使用照片的权限"];
  100. [self createTipLabelWithTitle:tipTextWhenNoPhotosAuthorization needSettingButton:true];
  101. }
  102. break;
  103. case JXHPhotoAlbumStatusNoStatus: {
  104. [self authorize];
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. - (void)authorize {
  112. [_photoAlbumManager requestAuthorization:^(JXHPhotoAlbumStatus status) {
  113. dispatch_main_async_safe(^ {
  114. self.authorizationStatus = status;
  115. });
  116. }];
  117. }
  118. - (void)toSetting {
  119. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  120. jxh_openUrl(url, nil);
  121. }
  122. - (void)getAssetCollection {
  123. @weakify(self)
  124. [_photoAlbumManager getLibraryWithCompletion:^(NSArray<JXHPhotoAlbum *> *albumArray, JXHPhotoAlbumResult photoAlbumResult) {
  125. @strongify(self)
  126. if (photoAlbumResult == JXHPhotoAlbumResultSuccess) {
  127. [self showPhotoWihtPhotoAlbum:albumArray[0]];
  128. [self->_albumVC reloadData:albumArray];
  129. } else if (photoAlbumResult == JXHPhotoAlbumResultNoResource) {
  130. self.authorizationStatus = JXHPhotoAlbumStatusNoResource;
  131. }
  132. }];
  133. }
  134. - (void)showPhotoWihtPhotoAlbum:(JXHPhotoAlbum *)PhotoAlbum {
  135. _photoVC.spsb_photoAlbum = PhotoAlbum;
  136. _photoVC.view.hidden = false;
  137. [_navigationButton setTitle:_photoVC.spsb_photoAlbum.jxh_albumName state:JXHButtonControlStateNormal];
  138. _navigationButton.hidden = false;
  139. }
  140. - (void)showChooseAlbumView {
  141. [_albumVC.tableView reloadData];
  142. _albumCoverView.hidden = false;
  143. [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) {
  144. make.top.equalTo(self.spsb_navigationBar.bottom).update(-10);
  145. }];
  146. [UIView animateWithDuration:0.2 animations:^{
  147. self->_navigationButton.jxh_icon.transform = CGAffineTransformMakeRotation(M_PI);
  148. [self.view layoutSubviews];
  149. }];
  150. }
  151. - (void)hiddenChooseAlbumView {
  152. [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) {
  153. make.top.equalTo(self.spsb_navigationBar.bottom).update(-self->_chooseAlbumViewHeight);
  154. }];
  155. [UIView animateWithDuration:0.2 animations:^{
  156. self->_navigationButton.jxh_icon.transform = CGAffineTransformMakeRotation(0);
  157. [self.view layoutSubviews];
  158. } completion:^(BOOL finished) {
  159. self->_albumCoverView.hidden = true;
  160. }];
  161. }
  162. #pragma mark - SPSBNotificationDelegate
  163. - (void)spsb_currenChosePublishPhotosChanged:(NSArray<JXHPhotoManager *> *)currentArray chose:(JXHPhotoManager *)chose {
  164. if (currentArray.count > 0) {
  165. _previewButton.enabled = _confirmButton.enabled = true;
  166. [_confirmButton setTitle:[NSString stringWithFormat:@"%@(%ld/%ld)", @"确定", (long)currentArray.count + spsb_alreadyChosePublishPhoto(), (long)spsb_publishMaxCount()] state:JXHButtonControlStateNormal];
  167. } else {
  168. _previewButton.enabled = _confirmButton.enabled = false;
  169. }
  170. }
  171. #pragma mark - UI
  172. - (void)setupUI {
  173. [self createNavigationBar];
  174. [self.view layoutSubviews];//获取navigationbar高度
  175. [self setControllers];
  176. [self.view bringSubviewToFront:self.spsb_navigationBar];
  177. [self setNavigationBar];
  178. }
  179. - (void)setNavigationBar {
  180. _navigationButton = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisHorizontal interval:0 font:spsb_mediumFont(16) target:self action:@selector(changeAlbum)];
  181. [_navigationButton setImage:jxh_getImage(photo_album_arrow_down) state:JXHButtonControlStateNormal];
  182. _navigationButton.hidden = true;
  183. [self.spsb_navigationBar addSubview:_navigationButton];
  184. [_navigationButton makeConstraints:^(JXHConstraintMaker *make) {
  185. make.leading.equalTo(72);
  186. make.trailing.equalTo(-72);
  187. make.height.equalTo(jxh_navigationViewHeight());
  188. make.bottom.equalTo(0);
  189. }];
  190. }
  191. - (void)setControllers {
  192. _photoVC = SPSBAlbumItemCollectionViewController.new;
  193. _photoVC.spsb_finishChose = _spsb_finishChose;
  194. _photoVC.spsb_finishActionFirst = _spsb_finishActionFirst;
  195. _photoVC.view.hidden = true;
  196. [self addChildViewController:_photoVC];
  197. [self.view addSubview:_photoVC.view];
  198. [_photoVC.view makeConstraints:^(JXHConstraintMaker *make) {
  199. make.top.equalTo(self.spsb_navigationBar.bottom);
  200. make.bottom.equalTo(self.view.safebottom).offset(-spsb_bottomViewHeight);
  201. make.leading.and.trailing.equalTo(0);
  202. }];
  203. [self createBottomView];
  204. _albumCoverView = UIView.new;
  205. _albumCoverView.hidden = true;
  206. _albumCoverView.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha);
  207. [_albumCoverView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenChooseAlbumView)]];
  208. [self.view addSubview:_albumCoverView];
  209. [_albumCoverView makeConstraints:^(JXHConstraintMaker *make) {
  210. make.top.equalTo(self.spsb_navigationBar.bottom);
  211. make.leading.and.trailing.and.bottom.equalTo(0);
  212. }];
  213. _chooseAlbumViewHeight = jxh_screenHeight() * 0.7;
  214. _albumVC = SPSBChooseAlbumTableViewController.new;
  215. @weakify(self)
  216. _albumVC.spsb_finishChose = ^(JXHPhotoAlbum * _Nonnull result) {
  217. @strongify(self)
  218. [self finishChosePhotoAlbum:result];
  219. };
  220. [_albumVC.tableView setLayerCornerRadius:8 clipToBounds:true];
  221. [self addChildViewController:_albumVC];
  222. [self.view addSubview:_albumVC.tableView];
  223. [_albumVC.view makeConstraints:^(JXHConstraintMaker *make) {
  224. make.top.equalTo(self.spsb_navigationBar.bottom).offset(-self->_chooseAlbumViewHeight);
  225. make.leading.and.trailing.equalTo(0);
  226. make.height.equalTo(self->_chooseAlbumViewHeight);
  227. }];
  228. }
  229. - (void)createBottomView {
  230. UIView *bottomView = UIView.new;
  231. bottomView.backgroundColor = spsb_FFFFFF_color(1.f);
  232. [self.view addSubview:bottomView];
  233. [bottomView makeConstraints:^(JXHConstraintMaker *make) {
  234. make.leading.and.trailing.and.bottom.equalTo(0);
  235. make.top.equalTo(self.view.safebottom).offset(-spsb_bottomViewHeight);
  236. }];
  237. _previewButton = [UIButton convenienceWithFont:spsb_mediumFont(16) target:self action:@selector(previewAction)];
  238. _previewButton.enabled = false;
  239. [_previewButton setTitleColor:spsb_000000_color(0.28) state:JXHButtonControlStateDisabled];
  240. [_previewButton setTitle:@"预览" titleColor:spsb_000000_color(0.8) state:JXHButtonControlStateNormal];
  241. [bottomView addSubview:_previewButton];
  242. [_previewButton makeConstraints:^(JXHConstraintMaker *make) {
  243. make.leading.equalTo(0);
  244. make.bottom.equalTo(bottomView.safebottom);
  245. make.size.equalTo((CGSize){64, spsb_bottomViewHeight});
  246. }];
  247. _confirmButton = [UIButton convenienceWithFont:spsb_mediumFont(14) target:self action:@selector(confirmAction)];
  248. CGSize size = (CGSize){96, 32};
  249. [_confirmButton setTitleColor:spsb_FFFFFF_color(1.f)
  250. state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  251. [_confirmButton setBackgroundImage:jxh_createCornerImage(spsb_000000_color(0.8), size.height / 2, size, JXHCornerPositionAll, 0, nil)
  252. state:JXHButtonControlStateNormal];
  253. [_confirmButton setTitleColor:spsb_000000_color(0.28)
  254. state:JXHButtonControlStateDisabled];
  255. [_confirmButton setBackgroundImage:jxh_createCornerImage(spsb_F5F5F5_color(), size.height / 2, size, JXHCornerPositionAll, 0, nil)
  256. state:JXHButtonControlStateDisabled];
  257. _confirmButton.enabled = false;
  258. [_confirmButton setTitle:@"确定" state:JXHButtonControlStateNormal];
  259. [bottomView addSubview:_confirmButton];
  260. [_confirmButton makeConstraints:^(JXHConstraintMaker *make) {
  261. make.trailing.equalTo(-16);
  262. make.bottom.equalTo(bottomView.safebottom).offset(-12);
  263. make.size.equalTo(size);
  264. }];
  265. }
  266. - (void)createTipLabelWithTitle:(NSString *)title needSettingButton:(bool)flag {
  267. UILabel *tipsLabel = [UILabel convenienceWithFont:spsb_font(14) textColor:spsb_000000_color(0.8) limitWidth:jxh_screenWidth()];
  268. [tipsLabel setAttributedStringWithText:title textAlignment:NSTextAlignmentCenter lineSpacing:10 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:0];
  269. [self.view addSubview:tipsLabel];
  270. [tipsLabel makeConstraints:^(JXHConstraintMaker *make) {
  271. make.left.greaterThanOrEqualTo(16);
  272. make.right.lessThanOrEqualTo(-16);
  273. make.centerX.equalTo(self.view).priorityRequired();
  274. make.top.equalTo(jxh_viewHeight(self.spsb_navigationBar) + 60);
  275. }];
  276. if (flag) {
  277. UIButton *settingButton = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(toSetting)];
  278. [settingButton setTitle:@"去设置" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  279. [self.view addSubview:settingButton];
  280. [settingButton makeConstraints:^(JXHConstraintMaker *make) {
  281. make.centerX.equalTo(self.view);
  282. make.top.equalTo(tipsLabel.bottom).offset(20);
  283. make.size.equalTo(CGSizeMake(60, 44));
  284. }];
  285. }
  286. }
  287. @end