ChangeIconViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // ChangeIconViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 6/16/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "LoginModel.h"
  11. #import "UIButton+WebCache.h"
  12. #import "CustomButton.h"
  13. #import "ChangeIconViewController.h"
  14. @interface MemberIconCell () {
  15. }
  16. @property (weak, nonatomic) NSIndexPath *indexPath;
  17. @end
  18. @implementation MemberIconCell
  19. @end
  20. @implementation MemberIconeHeaderView
  21. - (void)awakeFromNib {
  22. _lblHeaderTitle.text = NSLocalizedString(@"아이콘 변경", @"아이콘 변경");
  23. }
  24. @end
  25. @interface ChangeIconViewController () <UICollectionViewDelegate, UICollectionViewDataSource> {
  26. NSArray<MemberIconModel> *_iconList;
  27. CustomButton *_selectedCoverIcon;
  28. BOOL _isNotFirstLoading;
  29. LoginModel *_loginUser;
  30. }
  31. @end
  32. #pragma mark - Class Definition
  33. @implementation ChangeIconViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. [self initUI];
  38. [self prepareViewDidLoad];
  39. }
  40. - (void)initUI {
  41. _collectionView.delegate = self;
  42. _collectionView.dataSource = self;
  43. _collectionView.scrollEnabled = NO;
  44. _collectionView.backgroundColor = kUIBgColor01;
  45. //Localization
  46. [_btnSave setTitle:NSLocalizedString(@"저장", @"저장") forState:UIControlStateNormal];
  47. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  48. }
  49. - (void)prepareViewDidLoad {
  50. _loginUser = [JDFacade facade].loginUser;
  51. [self performSelector:@selector(requestMemberIcons) withObject:nil afterDelay:0.0f];
  52. }
  53. - (MemberIconModel *)selectedIcon {
  54. CustomButton *btnMembeIcon = _selectedCoverIcon ? _selectedCoverIcon.value : nil;
  55. return btnMembeIcon ? btnMembeIcon.value : nil;
  56. }
  57. #pragma mark - Main Logic
  58. - (void)requestMemberIcons {
  59. //parameters
  60. NSString *path = [NSString stringWithFormat:API_GET_MEMBER_ICONS];
  61. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[MemberIconListModel class] completion:^(id responseObject) {
  62. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  63. return;
  64. }
  65. MemberIconListModel *fetchedIconList = (MemberIconListModel *) responseObject;
  66. if (fetchedIconList && fetchedIconList.iconList && fetchedIconList.iconList.count) {//API 성공 ,
  67. _iconList = fetchedIconList.iconList;
  68. NSInteger index = [_iconList indexOfObjectPassingTest:^BOOL(MemberIconModel *obj, NSUInteger idx, BOOL *stop) {
  69. return [_loginUser.imageFileName isEqualToString:obj.imageFileName];
  70. }];
  71. if (index != NSNotFound) {
  72. _selectedIcon = _iconList[index];
  73. }
  74. [_collectionView reloadData];
  75. }
  76. } failure:^(id errorObject) {
  77. JDErrorModel *error = (JDErrorModel *)errorObject;
  78. [[JDFacade facade] alert:error.errorMessage];
  79. }];
  80. }
  81. #pragma mark - UICollectionView Delegate
  82. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  83. return _iconList.count;
  84. }
  85. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  86. static NSString *kCellID = @"CollectionCellIdentifier";
  87. MemberIconCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
  88. MemberIconModel *memberIcon = _iconList[indexPath.row];
  89. cell.indexPath = indexPath;
  90. cell.btnMemberIcon.value = memberIcon;
  91. cell.btnCover.value = cell.btnMemberIcon;
  92. [cell.btnMemberIcon.imageView setContentMode:UIViewContentModeScaleAspectFit];
  93. [cell.btnMemberIcon sd_setImageWithURL:[NSURL URLWithString:memberIcon.imageFileName] forState:UIControlStateNormal];
  94. // [cell.btnMemberIcon addTarget:self action:@selector(btnMemberIconTouched:) forControlEvents:UIControlEventTouchUpInside];
  95. [cell.btnCover setImage:[CommonUtil imageWithColor:[UIColor clearColor] height:1.0f] forState:UIControlStateNormal];
  96. [cell.btnCover addTarget:self action:@selector(btnCoverIconTouched:) forControlEvents:UIControlEventTouchUpInside];
  97. if (!_isNotFirstLoading) {
  98. if ([_selectedIcon isEqual:memberIcon]) {
  99. [self btnCoverIconTouched:cell.btnCover];
  100. _isNotFirstLoading = YES;
  101. }
  102. }
  103. return cell;
  104. }
  105. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  106. viewForSupplementaryElementOfKind:(NSString *)kind
  107. atIndexPath:(NSIndexPath *)indexPath
  108. {
  109. MemberIconeHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
  110. UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
  111. return headerView;
  112. }
  113. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  114. if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
  115. return CGSizeMake(84.0f, 64.0f);
  116. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
  117. return CGSizeMake(75.0f, 64.0f);
  118. }
  119. return CGSizeMake(64.0f, 64.0f);
  120. }
  121. #pragma mark - UI Events
  122. - (IBAction)btnCoverIconTouched:(id)sender {
  123. if (_selectedCoverIcon) {
  124. [_selectedCoverIcon faceOffImage];
  125. }
  126. CustomButton *btn = (CustomButton *)sender;
  127. _selectedCoverIcon = btn;
  128. [_selectedCoverIcon faceOffImage];
  129. }
  130. #pragma mark - MemoryWarning
  131. - (void)didReceiveMemoryWarning
  132. {
  133. [super didReceiveMemoryWarning];
  134. // Dispose of any resources that can be recreated.
  135. }
  136. @end