ThingsModifyViewController.m 5.6 KB

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