HXAlbumListViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. //
  2. // HXDateAlbumViewController.m
  3. // HXPhotoPickerExample
  4. //
  5. // Created by Silence on 2017/10/14.
  6. // Copyright © 2017年 Silence. All rights reserved.
  7. //
  8. #import "HXAlbumListViewController.h"
  9. #import "HXPhotoViewController.h"
  10. #import "UIViewController+HXExtension.h"
  11. #import "HXAssetManager.h"
  12. @interface HXAlbumListViewController ()
  13. <
  14. HXPhotoViewControllerDelegate,
  15. UITableViewDataSource,
  16. UITableViewDelegate
  17. >
  18. @property (strong, nonatomic) UITableView *tableView;
  19. @property (strong, nonatomic) NSMutableArray *albumModelArray;
  20. @property (strong, nonatomic) UILabel *authorizationLb;
  21. @property (assign, nonatomic) BOOL orientationDidChange;
  22. @property (strong, nonatomic) NSIndexPath *beforeOrientationIndexPath;
  23. @end
  24. @implementation HXAlbumListViewController
  25. - (instancetype)initWithManager:(HXPhotoManager *)manager {
  26. self = [super init];
  27. if (self) {
  28. self.manager = manager;
  29. }
  30. return self;
  31. }
  32. - (void)requestData {
  33. // 获取当前应用对照片的访问授权状态
  34. HXWeakSelf
  35. self.hx_customNavigationController.reloadAsset = ^(BOOL initialAuthorization){
  36. if (initialAuthorization) {
  37. [weakSelf authorizationHandler];
  38. }
  39. };
  40. [self authorizationHandler];
  41. }
  42. - (void)authorizationHandler {
  43. PHAuthorizationStatus status = [HXPhotoTools authorizationStatus];
  44. if (status == PHAuthorizationStatusAuthorized) {
  45. [self getAlbumModelList:YES];
  46. }
  47. #ifdef __IPHONE_14_0
  48. else if (@available(iOS 14, *)) {
  49. if (status == PHAuthorizationStatusLimited) {
  50. [self getAlbumModelList:YES];
  51. return;
  52. }
  53. #endif
  54. else if (status == PHAuthorizationStatusDenied ||
  55. status == PHAuthorizationStatusRestricted){
  56. [self.hx_customNavigationController.view hx_handleLoading];
  57. [self.view addSubview:self.authorizationLb];
  58. [HXPhotoTools showNoAuthorizedAlertWithViewController:self status:status];
  59. }
  60. #ifdef __IPHONE_14_0
  61. }else if (status == PHAuthorizationStatusDenied ||
  62. status == PHAuthorizationStatusRestricted){
  63. [self.hx_customNavigationController.view hx_handleLoading];
  64. [self.view addSubview:self.authorizationLb];
  65. [HXPhotoTools showNoAuthorizedAlertWithViewController:self status:status];
  66. }
  67. #endif
  68. }
  69. - (UIStatusBarStyle)preferredStatusBarStyle {
  70. if ([HXPhotoCommon photoCommon].isDark) {
  71. return UIStatusBarStyleLightContent;
  72. }
  73. return self.manager.configuration.statusBarStyle;
  74. }
  75. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  76. [super traitCollectionDidChange:previousTraitCollection];
  77. #ifdef __IPHONE_13_0
  78. if (@available(iOS 13.0, *)) {
  79. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  80. [self changeColor];
  81. [self changeStatusBarStyle];
  82. [self setNeedsStatusBarAppearanceUpdate];
  83. UIColor *authorizationColor = self.manager.configuration.authorizationTipColor;
  84. _authorizationLb.textColor = [HXPhotoCommon photoCommon].isDark ? [UIColor whiteColor] : authorizationColor;
  85. }
  86. }
  87. #endif
  88. }
  89. - (void)viewDidLoad {
  90. [super viewDidLoad];
  91. self.extendedLayoutIncludesOpaqueBars = YES;
  92. self.edgesForExtendedLayout = UIRectEdgeAll;
  93. self.navigationController.popoverPresentationController.delegate = (id)self;
  94. [self requestData];
  95. [self setupUI];
  96. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  97. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customCameraViewControllerDidDoneClick) name:@"CustomCameraViewControllerDidDoneNotification" object:nil];
  98. }
  99. - (void)customCameraViewControllerDidDoneClick {
  100. NSInteger i = 0;
  101. for (HXAlbumModel *albumMd in self.albumModelArray) {
  102. albumMd.cameraCount = [self.manager cameraCount];
  103. if (i == 0 && !albumMd.localIdentifier) {
  104. albumMd.tempImage = [self.manager firstCameraModel].thumbPhoto;
  105. }
  106. i++;
  107. }
  108. [self.tableView reloadData];
  109. }
  110. - (void)viewDidLayoutSubviews {
  111. [super viewDidLayoutSubviews];
  112. if (self.orientationDidChange) {
  113. [self changeSubviewFrame];
  114. self.orientationDidChange = NO;
  115. }
  116. }
  117. - (void)deviceOrientationChanged:(NSNotification *)notify {
  118. self.orientationDidChange = YES;
  119. }
  120. - (void)changeSubviewFrame {
  121. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  122. CGFloat navBarHeight = hxNavigationBarHeight;
  123. #pragma clang diagnostic push
  124. #pragma clang diagnostic ignored"-Wdeprecated-declarations"
  125. if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown || HX_UI_IS_IPAD) {
  126. navBarHeight = hxNavigationBarHeight;
  127. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  128. }else if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
  129. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  130. if ([UIApplication sharedApplication].statusBarHidden) {
  131. navBarHeight = self.navigationController.navigationBar.hx_h;
  132. }else {
  133. navBarHeight = self.navigationController.navigationBar.hx_h + 20;
  134. }
  135. }
  136. #pragma clang diagnostic pop
  137. CGFloat leftMargin = 0;
  138. CGFloat rightMargin = 0;
  139. CGFloat bottomMargin = hxBottomMargin;
  140. if (HX_IS_IPhoneX_All && (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)) {
  141. leftMargin = 35;
  142. rightMargin = 35;
  143. bottomMargin = 0;
  144. }
  145. self.tableView.contentInset = UIEdgeInsetsMake(navBarHeight, leftMargin, bottomMargin, rightMargin);
  146. #ifdef __IPHONE_13_0
  147. if (@available(iOS 13.0, *)) {
  148. }else {
  149. self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(navBarHeight, leftMargin, bottomMargin, rightMargin);
  150. }
  151. #else
  152. self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(navBarHeight, leftMargin, bottomMargin, rightMargin);
  153. #endif
  154. self.tableView.frame = self.view.bounds;
  155. if (self.manager.configuration.albumListTableView) {
  156. self.manager.configuration.albumListTableView(self.tableView);
  157. }
  158. self.navigationController.navigationBar.translucent = self.manager.configuration.navBarTranslucent;
  159. if (self.manager.configuration.navigationBar) {
  160. self.manager.configuration.navigationBar(self.navigationController.navigationBar, self);
  161. }
  162. }
  163. - (void)viewWillAppear:(BOOL)animated {
  164. [super viewWillAppear:animated];
  165. [self changeStatusBarStyle];
  166. if (self.manager.viewWillAppear) {
  167. self.manager.viewWillAppear(self);
  168. }
  169. }
  170. - (void)viewWillDisappear:(BOOL)animated {
  171. [super viewWillDisappear:animated];
  172. if (self.manager.viewWillDisappear) {
  173. self.manager.viewWillDisappear(self);
  174. }
  175. }
  176. - (void)viewDidDisappear:(BOOL)animated {
  177. [super viewDidDisappear:animated];
  178. if (self.manager.viewDidDisappear) {
  179. self.manager.viewDidDisappear(self);
  180. }
  181. }
  182. #pragma clang diagnostic push
  183. #pragma clang diagnostic ignored"-Wdeprecated-declarations"
  184. - (void)changeStatusBarStyle {
  185. if ([HXPhotoCommon photoCommon].isDark) {
  186. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
  187. return;
  188. }
  189. [[UIApplication sharedApplication] setStatusBarStyle:self.manager.configuration.statusBarStyle animated:YES];
  190. }
  191. #pragma clang diagnostic pop
  192. - (void)viewDidAppear:(BOOL)animated {
  193. [super viewDidAppear:animated];
  194. if (!self.albumModelArray.count) {
  195. PHAuthorizationStatus status = [HXPhotoTools authorizationStatus];
  196. if (status == PHAuthorizationStatusAuthorized) {
  197. [self getAlbumModelList:NO];
  198. }
  199. #ifdef __IPHONE_14_0
  200. else if (@available(iOS 14, *)) {
  201. if (status == PHAuthorizationStatusLimited) {
  202. [self getAlbumModelList:NO];
  203. }
  204. }
  205. #endif
  206. }
  207. if (self.manager.viewDidAppear) {
  208. self.manager.viewDidAppear(self);
  209. }
  210. }
  211. - (void)setupUI {
  212. self.title = [NSBundle hx_localizedStringForKey:@"相册"];
  213. [self changeColor];
  214. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSBundle hx_localizedStringForKey:@"取消"] style:UIBarButtonItemStyleDone target:self action:@selector(cancelClick)];
  215. if (self.manager.configuration.navigationBar) {
  216. self.manager.configuration.navigationBar(self.navigationController.navigationBar, self);
  217. }
  218. }
  219. - (void)changeColor {
  220. UIColor *backgroudColor;
  221. UIColor *themeColor;
  222. UIColor *navBarBackgroudColor;
  223. UIColor *navigationTitleColor;
  224. if ([HXPhotoCommon photoCommon].isDark) {
  225. backgroudColor = [UIColor colorWithRed:0.075 green:0.075 blue:0.075 alpha:1];
  226. themeColor = [UIColor whiteColor];
  227. navBarBackgroudColor = [UIColor blackColor];
  228. navigationTitleColor = [UIColor whiteColor];
  229. }else {
  230. backgroudColor = self.manager.configuration.albumListViewBgColor;
  231. themeColor = self.manager.configuration.themeColor;
  232. navBarBackgroudColor = self.manager.configuration.navBarBackgroudColor;
  233. navigationTitleColor = self.manager.configuration.navigationTitleColor;
  234. }
  235. self.view.backgroundColor = backgroudColor;
  236. self.tableView.backgroundColor = backgroudColor;
  237. [self.navigationController.navigationBar setTintColor:themeColor];
  238. self.navigationController.navigationBar.barTintColor = navBarBackgroudColor;
  239. self.navigationController.navigationBar.barStyle = self.manager.configuration.navBarStyle;
  240. if (self.manager.configuration.navBarBackgroundImage) {
  241. [self.navigationController.navigationBar setBackgroundImage:self.manager.configuration.navBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
  242. }
  243. // if (navBarBackgroudColor) {
  244. // [self.navigationController.navigationBar setBackgroundColor:navBarBackgroudColor];
  245. // [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  246. // }
  247. if (self.manager.configuration.navigationTitleSynchColor) {
  248. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : themeColor};
  249. }else {
  250. if (navigationTitleColor) {
  251. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : navigationTitleColor};
  252. }else {
  253. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
  254. }
  255. }
  256. if (@available(iOS 15.0, *)) {
  257. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  258. appearance.titleTextAttributes = self.navigationController.navigationBar.titleTextAttributes;
  259. switch (self.manager.configuration.navBarStyle) {
  260. case UIBarStyleDefault:
  261. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  262. break;
  263. default:
  264. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  265. break;
  266. }
  267. self.navigationController.navigationBar.standardAppearance = appearance;
  268. self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  269. }
  270. }
  271. - (void)configTableView {
  272. [self.view addSubview:self.tableView];
  273. [self changeSubviewFrame];
  274. }
  275. - (void)cancelClick {
  276. [self.manager cancelBeforeSelectedList];
  277. if ([self.delegate respondsToSelector:@selector(albumListViewControllerDidCancel:)]) {
  278. [self.delegate albumListViewControllerDidCancel:self];
  279. }
  280. if (self.cancelBlock) {
  281. self.cancelBlock(self, self.manager);
  282. }
  283. self.manager.selectPhotoing = NO;
  284. BOOL selectPhotoCancelDismissAnimated = self.manager.selectPhotoCancelDismissAnimated;
  285. [self dismissViewControllerAnimated:selectPhotoCancelDismissAnimated completion:^{
  286. if ([self.delegate respondsToSelector:@selector(albumListViewControllerCancelDismissCompletion:)]) {
  287. [self.delegate albumListViewControllerCancelDismissCompletion:self];
  288. }
  289. }];
  290. }
  291. #pragma mark - < HXPhotoViewControllerDelegate >
  292. - (void)photoViewController:(HXPhotoViewController *)photoViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
  293. if ([self.delegate respondsToSelector:@selector(albumListViewController:didDoneAllList:photos:videos:original:)]) {
  294. [self.delegate albumListViewController:self didDoneAllList:allList photos:photoList videos:videoList original:original];
  295. }
  296. if (self.doneBlock) {
  297. self.doneBlock(allList, photoList, videoList, original, self, self.manager);
  298. }
  299. }
  300. - (void)photoViewController:(HXPhotoViewController *)photoViewController didDoneWithResult:(HXPickerResult *)result {
  301. if ([self.delegate respondsToSelector:@selector(albumListViewController:didDoneWithResult:)]) {
  302. [self.delegate albumListViewController:self
  303. didDoneWithResult:result];
  304. }
  305. }
  306. - (void)photoViewControllerDidCancel:(HXPhotoViewController *)photoViewController {
  307. [self cancelClick];
  308. }
  309. - (void)photoViewControllerDidChangeSelect:(HXPhotoModel *)model selected:(BOOL)selected {
  310. if (self.albumModelArray.count > 0) {
  311. // HXAlbumModel *albumModel = self.albumModelArray[model.currentAlbumIndex];
  312. // if (selected) {
  313. // albumModel.selectedCount++;
  314. // }else {
  315. // albumModel.selectedCount--;
  316. // }
  317. // [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:model.currentAlbumIndex inSection:0]]];
  318. }
  319. }
  320. - (void)pushPhotoListViewControllerWithAlbumModel:(HXAlbumModel *)albumModel animated:(BOOL) animated {
  321. if (self.navigationController.topViewController != self) {
  322. [self.navigationController popToViewController:self animated:NO];
  323. }
  324. HXPhotoViewController *vc = [[HXPhotoViewController alloc] init];
  325. vc.manager = self.manager;
  326. vc.title = albumModel.albumName;
  327. vc.albumModel = albumModel;
  328. vc.delegate = self;
  329. [self.navigationController pushViewController:vc animated:animated];
  330. }
  331. - (void)getAlbumModelList:(BOOL)isFirst {
  332. HXWeakSelf
  333. if (isFirst) {
  334. if (self.hx_customNavigationController.cameraRollAlbumModel) {
  335. [self.view hx_handleLoading];
  336. [self pushPhotoListViewControllerWithAlbumModel:self.hx_customNavigationController.cameraRollAlbumModel animated:NO];
  337. }else {
  338. self.hx_customNavigationController.requestCameraRollCompletion = ^{
  339. [weakSelf.view hx_handleLoading];
  340. [weakSelf pushPhotoListViewControllerWithAlbumModel:weakSelf.hx_customNavigationController.cameraRollAlbumModel animated:NO];
  341. };
  342. }
  343. }else {
  344. [self configTableView];
  345. [self.view hx_showLoadingHUDText:nil];
  346. if (self.hx_customNavigationController.albums) {
  347. self.albumModelArray = self.hx_customNavigationController.albums;
  348. [self.tableView reloadData];
  349. [self.view hx_handleLoading:YES];
  350. }else {
  351. self.hx_customNavigationController.requestAllAlbumCompletion = ^{
  352. weakSelf.albumModelArray = weakSelf.hx_customNavigationController.albums;
  353. [weakSelf.tableView reloadData];
  354. [weakSelf.view hx_handleLoading:YES];
  355. };
  356. }
  357. }
  358. }
  359. #pragma mark - < UITableViewDataSource >
  360. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  361. return self.albumModelArray.count;
  362. }
  363. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  364. HXAlbumListSingleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCellId"];
  365. cell.bgColor = self.manager.configuration.albumListViewCellBgColor;
  366. cell.textColor = self.manager.configuration.albumListViewCellTextColor;
  367. cell.selectedBgColor = self.manager.configuration.albumListViewCellSelectBgColor;
  368. cell.lineViewColor = self.manager.configuration.albumListViewCellLineColor;
  369. cell.model = self.albumModelArray[indexPath.row];
  370. HXWeakSelf
  371. cell.getResultCompleteBlock = ^(NSInteger count, HXAlbumListSingleViewCell *myCell) {
  372. if (count <= 0) {
  373. if ([weakSelf.albumModelArray containsObject:myCell.model]) {
  374. NSIndexPath *myIndexPath = [weakSelf.tableView indexPathForCell:myCell];
  375. if (myIndexPath) {
  376. [weakSelf.albumModelArray removeObject:myCell.model];
  377. [weakSelf.tableView deleteRowsAtIndexPaths:@[myIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  378. }
  379. }
  380. }
  381. };
  382. return cell;
  383. }
  384. #pragma mark - < UITableViewDelegate >
  385. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  386. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  387. if (self.navigationController.topViewController != self) {
  388. return;
  389. }
  390. [self.hx_customNavigationController clearAssetCache];
  391. HXAlbumModel *model = self.albumModelArray[indexPath.row];
  392. [self pushPhotoListViewControllerWithAlbumModel:model animated:YES];
  393. }
  394. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  395. return 100;
  396. }
  397. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  398. [(HXAlbumListSingleViewCell *)cell cancelRequest];
  399. }
  400. #pragma mark - < 懒加载 >
  401. - (UITableView *)tableView {
  402. if (!_tableView) {
  403. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.hx_w, self.view.hx_h) style:UITableViewStylePlain];
  404. _tableView.dataSource = self;
  405. _tableView.delegate = self;
  406. _tableView.estimatedRowHeight = 0;
  407. _tableView.estimatedSectionFooterHeight = 0;
  408. _tableView.estimatedSectionHeaderHeight = 0;
  409. _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  410. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  411. [_tableView registerClass:[HXAlbumListSingleViewCell class] forCellReuseIdentifier:@"tableViewCellId"];
  412. #ifdef __IPHONE_11_0
  413. if (@available(iOS 11.0, *)) {
  414. // if ([self hx_navigationBarWhetherSetupBackground]) {
  415. // self.navigationController.navigationBar.translucent = NO;
  416. // }else {
  417. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  418. // }
  419. #else
  420. if ((NO)) {
  421. #endif
  422. } else {
  423. // if ([self hx_navigationBarWhetherSetupBackground]) {
  424. // self.navigationController.navigationBar.translucent = NO;
  425. // }else {
  426. self.automaticallyAdjustsScrollViewInsets = NO;
  427. // }
  428. }
  429. }
  430. return _tableView;
  431. }
  432. - (UILabel *)authorizationLb {
  433. if (!_authorizationLb) {
  434. _authorizationLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 100)];
  435. _authorizationLb.text = [NSBundle hx_localizedStringForKey:@"无法访问照片\n请点击这里前往设置中允许访问照片"];
  436. _authorizationLb.textAlignment = NSTextAlignmentCenter;
  437. _authorizationLb.numberOfLines = 0;
  438. UIColor *authorizationColor = self.manager.configuration.authorizationTipColor;
  439. _authorizationLb.textColor = [HXPhotoCommon photoCommon].isDark ? [UIColor whiteColor] : authorizationColor;
  440. _authorizationLb.font = [UIFont systemFontOfSize:15];
  441. _authorizationLb.userInteractionEnabled = YES;
  442. [_authorizationLb addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goSetup)]];
  443. }
  444. return _authorizationLb;
  445. }
  446. - (void)dealloc {
  447. self.manager.selectPhotoing = NO;
  448. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CustomCameraViewControllerDidDoneNotification" object:nil];
  449. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  450. }
  451. - (void)goSetup {
  452. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  453. if (@available(iOS 10.0, *)) {
  454. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  455. }else {
  456. [[UIApplication sharedApplication] openURL:url];
  457. }
  458. }
  459. @end
  460. @interface HXAlbumListSingleViewCell ()
  461. @property (strong, nonatomic) UIImageView *coverView1;
  462. @property (strong, nonatomic) UILabel *albumNameLb;
  463. @property (strong, nonatomic) UILabel *photoNumberLb;
  464. @property (assign, nonatomic) PHImageRequestID requestId1;
  465. @property (assign, nonatomic) PHImageRequestID requestId2;
  466. @property (assign, nonatomic) PHImageRequestID requestId3;
  467. @property (strong, nonatomic) UIView *lineView;
  468. @property (strong, nonatomic) UIView *selectBgView;
  469. @end
  470. @implementation HXAlbumListSingleViewCell
  471. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  472. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  473. if (self) {
  474. self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  475. [self setupUI];
  476. }
  477. return self;
  478. }
  479. - (void)setupUI {
  480. [self.contentView addSubview:self.coverView1];
  481. [self.contentView addSubview:self.albumNameLb];
  482. [self.contentView addSubview:self.photoNumberLb];
  483. [self.contentView addSubview:self.lineView];
  484. }
  485. - (void)cancelRequest {
  486. if (self.requestId1) {
  487. [[PHImageManager defaultManager] cancelImageRequest:self.requestId1];
  488. self.requestId1 = -1;
  489. }
  490. }
  491. - (void)setModel:(HXAlbumModel *)model {
  492. _model = model;
  493. [self changeColor];
  494. self.albumNameLb.text = self.model.albumName;
  495. if (!model.assetResult && model.localIdentifier) {
  496. HXWeakSelf
  497. [model getResultWithCompletion:^(HXAlbumModel *albumModel) {
  498. if (albumModel == weakSelf.model) {
  499. [weakSelf setAlbumImage];
  500. }
  501. }];
  502. }else {
  503. [self setAlbumImage];
  504. }
  505. if (!model.assetResult || !model.count) {
  506. self.coverView1.image = model.tempImage ?: [UIImage hx_imageNamed:@"hx_yundian_tupian"];
  507. }
  508. }
  509. - (void)setAlbumImage {
  510. NSInteger photoCount = self.model.count;
  511. HXWeakSelf
  512. PHAsset *asset = self.model.assetResult.lastObject;
  513. if (asset) {
  514. self.requestId1 = [HXAssetManager requestThumbnailImageForAsset:asset targetWidth:300 completion:^(UIImage * _Nonnull result, NSDictionary<NSString *,id> * _Nonnull info) {
  515. if (weakSelf.model.assetResult.lastObject == asset && result) {
  516. weakSelf.coverView1.image = result;
  517. }
  518. }];
  519. }
  520. self.photoNumberLb.text = [@(photoCount + self.model.cameraCount).stringValue hx_countStrBecomeComma];
  521. if (self.getResultCompleteBlock) {
  522. self.getResultCompleteBlock(photoCount + self.model.cameraCount, self);
  523. }
  524. }
  525. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  526. [super setHighlighted:highlighted animated:animated];
  527. }
  528. - (void)layoutSubviews {
  529. [super layoutSubviews];
  530. self.coverView1.frame = CGRectMake(10, 5, self.hx_h - 10, self.hx_h - 10);
  531. CGFloat albumNameLbX = CGRectGetMaxX(self.coverView1.frame) + 12;
  532. CGFloat albumNameLbY = self.hx_h / 2 - 16;
  533. self.albumNameLb.frame = CGRectMake(albumNameLbX, albumNameLbY, self.hx_w - albumNameLbX - 40, 14);
  534. self.photoNumberLb.frame = CGRectMake(albumNameLbX, self.hx_h / 2 + 4, self.hx_w, 13);
  535. self.lineView.frame = CGRectMake(10, self.hx_h - 0.5f, self.hx_w - 22, 0.5f);
  536. self.selectBgView.frame = self.bounds;
  537. }
  538. - (void)dealloc {
  539. // [self cancelRequest];
  540. }
  541. #pragma mark - < cell懒加载 >
  542. - (UIView *)lineView {
  543. if (!_lineView) {
  544. _lineView = [[UIView alloc] init];
  545. }
  546. return _lineView;
  547. }
  548. - (UIImageView *)coverView1 {
  549. if (!_coverView1) {
  550. _coverView1 = [[UIImageView alloc] init];
  551. _coverView1.contentMode = UIViewContentModeScaleAspectFill;
  552. _coverView1.clipsToBounds = YES;
  553. _coverView1.layer.borderColor = [UIColor whiteColor].CGColor;
  554. _coverView1.layer.borderWidth = 0.5f;
  555. }
  556. return _coverView1;
  557. }
  558. - (UILabel *)albumNameLb {
  559. if (!_albumNameLb) {
  560. _albumNameLb = [[UILabel alloc] init];
  561. _albumNameLb.font = [UIFont hx_mediumSFUITextOfSize:13];
  562. }
  563. return _albumNameLb;
  564. }
  565. - (UILabel *)photoNumberLb {
  566. if (!_photoNumberLb) {
  567. _photoNumberLb = [[UILabel alloc] init];
  568. _photoNumberLb.textColor = [UIColor lightGrayColor];
  569. _photoNumberLb.font = [UIFont systemFontOfSize:12];
  570. }
  571. return _photoNumberLb;
  572. }
  573. - (UIView *)selectBgView {
  574. if (!_selectBgView) {
  575. _selectBgView = [[UIView alloc] init];
  576. _selectBgView.backgroundColor = [UIColor colorWithRed:0.125 green:0.125 blue:0.125 alpha:1];
  577. }
  578. return _selectBgView;
  579. }
  580. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  581. [super traitCollectionDidChange:previousTraitCollection];
  582. #ifdef __IPHONE_13_0
  583. if (@available(iOS 13.0, *)) {
  584. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  585. [self changeColor];
  586. }
  587. }
  588. #endif
  589. }
  590. - (void)changeAlbumNameTextColor {
  591. if ([HXPhotoCommon photoCommon].isDark) {
  592. self.albumNameLb.textColor = [UIColor whiteColor];
  593. }else {
  594. self.albumNameLb.textColor = self.textColor;
  595. }
  596. }
  597. - (void)changeColor {
  598. self.backgroundColor = [HXPhotoCommon photoCommon].isDark ? [UIColor colorWithRed:0.075 green:0.075 blue:0.075 alpha:1] : self.bgColor;
  599. if (self.selectedBgColor) {
  600. self.selectBgView.backgroundColor = self.selectedBgColor;
  601. self.selectedBackgroundView = self.selectBgView;
  602. }else {
  603. self.selectedBackgroundView = [HXPhotoCommon photoCommon].isDark ? self.selectBgView : nil;
  604. }
  605. self.lineView.backgroundColor = [HXPhotoCommon photoCommon].isDark ? [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1] colorWithAlphaComponent:1] : self.lineViewColor;
  606. [self changeAlbumNameTextColor];
  607. }
  608. @end