// // ChangeIconViewController.m // kneet // // Created by Jason Lee on 6/16/15. // Copyright (c) 2015 ntels. All rights reserved. // #import "JDObject.h" #import "RequestHandler.h" #import "LoginModel.h" #import "UIButton+WebCache.h" #import "CustomButton.h" #import "ChangeIconViewController.h" #import "ImageUtil.h" @interface MemberIconCell () { } @property (weak, nonatomic) NSIndexPath *indexPath; @end @implementation MemberIconCell @end @implementation MemberIconeHeaderView - (void)awakeFromNib { _lblHeaderTitle.text = NSLocalizedString(@"아이콘 변경", @"아이콘 변경"); } @end @interface ChangeIconViewController () { NSArray *_iconList; CustomButton *_selectedCoverIcon; BOOL _isNotFirstLoading; LoginModel *_loginUser; } @end #pragma mark - Class Definition @implementation ChangeIconViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; [self prepareViewDidLoad]; } - (void)initUI { _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.scrollEnabled = NO; _collectionView.backgroundColor = kUIBgColor01; //Localization [_btnSave setTitle:NSLocalizedString(@"저장", @"저장") forState:UIControlStateNormal]; [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal]; } - (void)prepareViewDidLoad { _loginUser = [JDFacade facade].loginUser; [self performSelector:@selector(requestMemberIcons) withObject:nil afterDelay:0.0f]; } - (MemberIconModel *)selectedIcon { CustomButton *btnMembeIcon = _selectedCoverIcon ? _selectedCoverIcon.value : nil; return btnMembeIcon ? btnMembeIcon.value : nil; } #pragma mark - Main Logic - (void)requestMemberIcons { //parameters NSString *path = [NSString stringWithFormat:API_GET_MEMBER_ICONS]; [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[MemberIconListModel class] completion:^(id responseObject) { if (!responseObject) {//응답결과가 잘못되었거나 없을 경우, return; } MemberIconListModel *fetchedIconList = (MemberIconListModel *) responseObject; if (fetchedIconList && fetchedIconList.iconList && fetchedIconList.iconList.count) {//API 성공 , _iconList = fetchedIconList.iconList; NSInteger index = [_iconList indexOfObjectPassingTest:^BOOL(MemberIconModel *obj, NSUInteger idx, BOOL *stop) { return [_loginUser.imageFileName isEqualToString:obj.imageFileName]; }]; if (index != NSNotFound) { _selectedIcon = _iconList[index]; } [_collectionView reloadData]; } } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } #pragma mark - UICollectionView Delegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _iconList.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCellID = @"CollectionCellIdentifier"; MemberIconCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath]; MemberIconModel *memberIcon = _iconList[indexPath.row]; cell.indexPath = indexPath; cell.btnMemberIcon.value = memberIcon; cell.btnCover.value = cell.btnMemberIcon; [cell.btnMemberIcon.imageView setContentMode:UIViewContentModeScaleAspectFit]; [cell.btnMemberIcon sd_setImageWithURL:[NSURL URLWithString:memberIcon.imageFileName] forState:UIControlStateNormal]; // [cell.btnMemberIcon addTarget:self action:@selector(btnMemberIconTouched:) forControlEvents:UIControlEventTouchUpInside]; [cell.btnCover setImage:[ImageUtil imageWithColor:[UIColor clearColor] height:1.0f] forState:UIControlStateNormal]; [cell.btnCover addTarget:self action:@selector(btnCoverIconTouched:) forControlEvents:UIControlEventTouchUpInside]; if (!_isNotFirstLoading) { if ([_selectedIcon isEqual:memberIcon]) { [self btnCoverIconTouched:cell.btnCover]; _isNotFirstLoading = YES; } } return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { MemberIconeHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; return headerView; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우, return CGSizeMake(84.0f, 64.0f); } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우 return CGSizeMake(75.0f, 64.0f); } return CGSizeMake(64.0f, 64.0f); } #pragma mark - UI Events - (IBAction)btnCoverIconTouched:(id)sender { if (_selectedCoverIcon) { [_selectedCoverIcon faceOffImage]; } CustomButton *btn = (CustomButton *)sender; _selectedCoverIcon = btn; [_selectedCoverIcon faceOffImage]; } #pragma mark - MemoryWarning - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end