ThingsViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // ThingsViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/10/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "DeviceModel.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "CustomLabel.h"
  14. #import "CustomImageView.h"
  15. #import "CustomTextField.h"
  16. #import "CustomButton.h"
  17. #import "JYRefreshController.h"
  18. #import "WYPopoverController.h"
  19. #import "ImageUtil.h"
  20. #import "CommandClassControlView.h"
  21. #import "ThingsDetailViewController.h"
  22. #import "ThingsViewController.h"
  23. #import "UIButton+WebCache.h"
  24. #define kfThingsTableViewCellHeight 100.0f
  25. @interface ThingsCollectionViewCell () {
  26. }
  27. @property (weak, nonatomic) NSIndexPath *indexPath;
  28. @end
  29. @implementation ThingsCollectionViewCell
  30. - (void)awakeFromNib {
  31. }
  32. @end
  33. @implementation ThingsAddCollectionViewCell
  34. @end
  35. @implementation ThingsCollectionFooterView
  36. @end
  37. @interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
  38. NSMutableArray<DeviceModel> *_deviceList;
  39. NSString *_pagingId, *_pagingType;
  40. BOOL _isNotFirstLoading, _isDeleteMode;
  41. }
  42. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  43. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  44. @end
  45. #pragma mark - Class Definition
  46. @implementation ThingsViewController
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. [self initUI];
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. [self prepareViewDidLoad];
  54. }
  55. - (void)initUI {
  56. //set tableview option
  57. _collectionView.delegate = self;
  58. _collectionView.dataSource = self;
  59. _collectionView.backgroundColor = kUIBgColor01;
  60. if ([JDFacade facade].loginUser.homehubDeviceId && ![[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  61. [self generateOptionButton];
  62. [self setThingsPopoverOptions];
  63. }
  64. //set refresh controls
  65. // __weak typeof(self) weakSelf = self;
  66. // self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  67. // self.refreshController.pullToRefreshHandleAction = ^{
  68. // [weakSelf requestPredefinedRulesRecently];
  69. // };
  70. //
  71. // self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  72. // self.loadMoreController.pullToLoadMoreHandleAction = ^{
  73. // [weakSelf requestPredefinedRulesOlder];
  74. // };
  75. }
  76. - (void)setThingsPopoverOptions {
  77. //set Popover Contents
  78. __weak typeof(self) weakSelf = self;
  79. _popooverOptionArray = [[NSMutableArray alloc] init];
  80. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  81. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  82. @"target": weakSelf,
  83. @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];
  84. if ([JDFacade facade].loginUser.level > 10) {//권한
  85. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"추가", @"추가"),
  86. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  87. @"target": weakSelf,
  88. @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
  89. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
  90. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  91. @"target": weakSelf,
  92. @"selector": [NSValue valueWithPointer:@selector(deleteDevice)]}];
  93. }
  94. }
  95. - (void)prepareViewDidLoad {
  96. //fetch devices from server
  97. if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  98. [_mainView bringSubviewToFront:_addHubContainerView];
  99. _collectionView.hidden = YES;
  100. } else {
  101. [_mainView bringSubviewToFront:_collectionView];
  102. _addHubContainerView.hidden = YES;
  103. _collectionView.hidden = NO;
  104. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  105. }
  106. }
  107. - (void)updateDevice:(DeviceModel *)device {
  108. // if ([device.deviceId isEqualToString:ldevice.deviceId]) {
  109. // ldevice.cmdclsValue = device.cmdclsValue;
  110. // ldevice.contentValue = device.contentValue;
  111. // break;
  112. // }
  113. }
  114. - (void)addNewDevice {
  115. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  116. [self presentViewController:vc animated:YES completion:nil];
  117. }
  118. - (void)deleteDevice {
  119. }
  120. - (void)refreshDeviceList {
  121. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  122. }
  123. #pragma mark - Main Logic
  124. - (void)requestDeviceListRecently {
  125. DeviceModel *firstDevice = [_deviceList firstObject];
  126. _pagingType = ksListPagingTypeUpward;
  127. _pagingId = firstDevice.createDatetime;
  128. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  129. }
  130. - (void)requestDeviceListOlder {
  131. DeviceModel *lastDevice = [_deviceList lastObject];
  132. _pagingType = ksListPagingTypeDownward;
  133. _pagingId = lastDevice.createDatetime;
  134. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  135. }
  136. - (void)requestDeviceList {
  137. //parameters
  138. NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
  139. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  140. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];
  141. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DeviceListModel class] completion:^(id responseObject) {
  142. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  143. return;
  144. }
  145. DeviceListModel *deviceList = (DeviceListModel *)responseObject;
  146. if (deviceList && deviceList.list && deviceList.list.count) {
  147. _deviceList = deviceList.list;
  148. _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
  149. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
  150. } else {
  151. if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  152. // NoContentView *noContentView = [NoContentView viewFromNib];
  153. // _tableView.tableFooterView = noContentView;
  154. }
  155. }
  156. [_collectionView reloadData];
  157. } failure:^(id errorObject) {
  158. JDErrorModel *error = (JDErrorModel *)errorObject;
  159. [[JDFacade facade] alert:error.errorMessage];
  160. }];
  161. }
  162. #pragma mark - UICollectionView Delegate
  163. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  164. NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
  165. NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
  166. return count;
  167. }
  168. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  169. UICollectionViewCell *rcell = nil;
  170. if (indexPath.row < _deviceList.count) {
  171. ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  172. DeviceModel *device =_deviceList[indexPath.row];
  173. cell.indexPath = indexPath;
  174. [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
  175. placeholderImage:nil
  176. options:SDWebImageRefreshCached
  177. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  178. [cell layoutIfNeeded];
  179. }];
  180. cell.lblDeviceName.text = device.deviceName;
  181. cell.lblDeviceStatus.text = device.contentValue;
  182. rcell = cell;
  183. } else {
  184. ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
  185. if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  186. [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  187. }
  188. rcell = cell;
  189. }
  190. return rcell;
  191. }
  192. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  193. viewForSupplementaryElementOfKind:(NSString *)kind
  194. atIndexPath:(NSIndexPath *)indexPath
  195. {
  196. ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
  197. withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
  198. if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  199. [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  200. }
  201. return footerView;
  202. }
  203. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  204. //FIXME : 권한 추가
  205. // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  206. if (_deviceList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  207. return CGSizeZero;
  208. }
  209. return CGSizeMake(IPHONE_WIDTH, 160.0f);
  210. }
  211. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  212. if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
  213. return CGSizeMake(212.0f, 160.0f);
  214. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
  215. return CGSizeMake(187.5, 160.0f);
  216. }
  217. return CGSizeMake(160.0f, 160.0f);
  218. }
  219. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  220. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  221. if (indexPath.row < _deviceList.count) {
  222. ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  223. DeviceModel *device = _deviceList[indexPath.row];
  224. }
  225. }
  226. #pragma mark - UI Events
  227. - (void)btnAddHubTouched:(id)sender {
  228. [[JDFacade facade] gotoHomeHubRegistration];
  229. }
  230. #pragma mark - MemoryWarning
  231. - (void)didReceiveMemoryWarning
  232. {
  233. [super didReceiveMemoryWarning];
  234. // Dispose of any resources that can be recreated.
  235. }
  236. @end