ThingsViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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)updateHomeHubStatusToDevices {
  108. for (DeviceModel *device in _deviceList) {
  109. device.onlineState = [JDFacade facade].loginUser.homehubOnlineState;
  110. }
  111. [_collectionView reloadData];
  112. }
  113. - (void)updateDevice:(DeviceModel *)device {
  114. // if ([device.deviceId isEqualToString:ldevice.deviceId]) {
  115. // ldevice.cmdclsValue = device.cmdclsValue;
  116. // ldevice.contentValue = device.contentValue;
  117. // break;
  118. // }
  119. }
  120. - (void)addNewDevice {
  121. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  122. [self presentViewController:vc animated:YES completion:nil];
  123. }
  124. - (void)deleteDevice {
  125. }
  126. - (void)refreshDeviceList {
  127. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  128. }
  129. #pragma mark - Main Logic
  130. - (void)requestDeviceListRecently {
  131. DeviceModel *firstDevice = [_deviceList firstObject];
  132. _pagingType = ksListPagingTypeUpward;
  133. _pagingId = firstDevice.createDatetime;
  134. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  135. }
  136. - (void)requestDeviceListOlder {
  137. DeviceModel *lastDevice = [_deviceList lastObject];
  138. _pagingType = ksListPagingTypeDownward;
  139. _pagingId = lastDevice.createDatetime;
  140. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  141. }
  142. - (void)requestDeviceList {
  143. //parameters
  144. NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
  145. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  146. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];
  147. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DeviceListModel class] completion:^(id responseObject) {
  148. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  149. return;
  150. }
  151. DeviceListModel *deviceList = (DeviceListModel *)responseObject;
  152. if (deviceList && deviceList.list && deviceList.list.count) {
  153. _deviceList = deviceList.list;
  154. _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
  155. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
  156. } else {
  157. if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  158. // NoContentView *noContentView = [NoContentView viewFromNib];
  159. // _tableView.tableFooterView = noContentView;
  160. }
  161. }
  162. [_collectionView reloadData];
  163. } failure:^(id errorObject) {
  164. JDErrorModel *error = (JDErrorModel *)errorObject;
  165. [[JDFacade facade] alert:error.errorMessage];
  166. }];
  167. }
  168. #pragma mark - UICollectionView Delegate
  169. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  170. NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
  171. NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
  172. return count;
  173. }
  174. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  175. UICollectionViewCell *rcell = nil;
  176. if (indexPath.row < _deviceList.count) {
  177. ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  178. DeviceModel *device =_deviceList[indexPath.row];
  179. cell.indexPath = indexPath;
  180. [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
  181. placeholderImage:nil
  182. options:SDWebImageRefreshCached
  183. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  184. [cell layoutIfNeeded];
  185. }];
  186. cell.lblDeviceName.text = device.deviceName;
  187. NSString *deviceStatus = [[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"] ? @"OFFLINE" : device.contentValue;
  188. cell.lblDeviceStatus.text = deviceStatus;
  189. rcell = cell;
  190. } else {
  191. ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
  192. if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  193. [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  194. }
  195. rcell = cell;
  196. }
  197. return rcell;
  198. }
  199. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  200. viewForSupplementaryElementOfKind:(NSString *)kind
  201. atIndexPath:(NSIndexPath *)indexPath
  202. {
  203. ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
  204. withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
  205. if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  206. [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  207. }
  208. return footerView;
  209. }
  210. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  211. //FIXME : 권한 추가
  212. // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  213. if (_deviceList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  214. return CGSizeZero;
  215. }
  216. return CGSizeMake(IPHONE_WIDTH, 160.0f);
  217. }
  218. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  219. if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
  220. return CGSizeMake(212.0f, 160.0f);
  221. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
  222. return CGSizeMake(187.5, 160.0f);
  223. }
  224. return CGSizeMake(160.0f, 160.0f);
  225. }
  226. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  227. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  228. if (indexPath.row < _deviceList.count) {
  229. ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  230. DeviceModel *device = _deviceList[indexPath.row];
  231. }
  232. }
  233. #pragma mark - UI Events
  234. - (void)btnAddHubTouched:(id)sender {
  235. [[JDFacade facade] gotoHomeHubRegistration];
  236. }
  237. #pragma mark - MemoryWarning
  238. - (void)didReceiveMemoryWarning
  239. {
  240. [super didReceiveMemoryWarning];
  241. // Dispose of any resources that can be recreated.
  242. }
  243. @end