ThingsViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "CommandClassControlView.h"
  20. #import "ThingsDetailViewController.h"
  21. #import "NewGroupPopupView.h"
  22. #import "GroupModifyViewController.h"
  23. #import "ThingsViewController.h"
  24. #define kfThingsTableViewCellHeight 100.0f
  25. @protocol ThingsHeaderTableViewCellDelegate <NSObject>
  26. @optional
  27. - (void)didThingsHeaderTableViewCellBtnHeaderTouched:(id)sender;
  28. @end
  29. @interface ThingsHeaderTableViewCell () {
  30. }
  31. @property (assign, nonatomic) id <ThingsHeaderTableViewCellDelegate> delegate;
  32. @end
  33. @implementation ThingsHeaderTableViewCell
  34. - (IBAction)btnHeaderTouched:(id)sender {
  35. [_btnHeader faceOffImage];
  36. _group.unfold = !_group.unfold;
  37. // _group[@"expanded"] = [NSNumber numberWithBool:![_group[@"expanded"] boolValue]];
  38. if ([self.delegate respondsToSelector:@selector(didThingsHeaderTableViewCellBtnHeaderTouched:)]) {
  39. [self.delegate didThingsHeaderTableViewCellBtnHeaderTouched:self];
  40. }
  41. }
  42. - (void)btnEditTouched:(id)sender {
  43. GroupModifyViewController *vc = (GroupModifyViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"GroupModifyViewController" storyboardName:@"Things"];
  44. vc.group = _group;
  45. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  46. }
  47. @end
  48. @implementation ThingsTableViewCell
  49. - (void)awakeFromNib {
  50. self.backgroundColor = [UIColor clearColor];
  51. self.selectionStyle = UITableViewCellSelectionStyleNone;
  52. self.opaque = NO;
  53. }
  54. @end
  55. @implementation ThingsAddTableViewCell
  56. - (void)awakeFromNib {
  57. _lblAdd.text = NSLocalizedString(@"내 장치를 등록해보세요", @"내 장치를 등록해보세요");
  58. self.backgroundColor = [UIColor clearColor];
  59. self.selectionStyle = UITableViewCellSelectionStyleNone;
  60. }
  61. - (void)btnAddTouched:(id)sender {
  62. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  63. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  64. }
  65. @end
  66. @interface ThingsViewController () <UITableViewDelegate, UITableViewDataSource, ThingsHeaderTableViewCellDelegate> {
  67. UIImage *_bgCellImage1, *_bgCellImage2;
  68. NSMutableArray *_arrayForHeader;
  69. NSString *_deviceGroupId;
  70. UIImage *_offlineImage;
  71. }
  72. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  73. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  74. @end
  75. #pragma mark - Class Definition
  76. @implementation ThingsViewController
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. _groupList = (NSMutableArray<GroupsModel> *)[[NSMutableArray alloc] init];
  80. [self initUI];
  81. [self prepareViewDidLoad];
  82. }
  83. - (void)viewWillAppear:(BOOL)animated {
  84. [super viewWillAppear:animated];
  85. self.title = NSLocalizedString(@"내 장치", nil);
  86. [_tableView reloadData];
  87. }
  88. - (void)initUI {
  89. [self generateOptionButton];
  90. //set tableview option
  91. _tableView.delegate = self;
  92. _tableView.dataSource = self;
  93. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  94. _tableView.backgroundColor = [UIColor clearColor];
  95. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  96. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  97. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  98. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  99. [self setThingsPopoverOptions];
  100. _offlineImage = [UIImage imageNamed:@"tp_01_img_things_offline_cover"];
  101. }
  102. - (void)setThingsPopoverOptions {
  103. //set Popover Contents
  104. __weak typeof(self) weakSelf = self;
  105. _popooverOptionArray = [[NSMutableArray alloc] init];
  106. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  107. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  108. @"target": weakSelf,
  109. @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];
  110. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 장치 추가",nil),
  111. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  112. @"target": weakSelf,
  113. @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
  114. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 그룹 추가",nil),
  115. @"iconName": @"tp_01_img_bg_morepopup_icon_groupadd",
  116. @"target": weakSelf,
  117. @"selector": [NSValue valueWithPointer:@selector(addNewGroup)]}];
  118. }
  119. - (void)prepareViewDidLoad {
  120. //fetch devices from server
  121. if (_groupList && _groupList.count) {
  122. [_groupList removeAllObjects];
  123. [_arrayForHeader removeAllObjects];
  124. }
  125. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  126. }
  127. - (void)updateDevice:(DeviceModel *)device {
  128. for (GroupsModel *group in _groupList) {
  129. for (DeviceModel *ldevice in group.devices) {
  130. if ([device.deviceId isEqualToString:ldevice.deviceId]) {
  131. ldevice.cmdclsValue = device.cmdclsValue;
  132. ldevice.contentValue = device.contentValue;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. - (void)addNewDevice {
  139. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  140. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  141. }
  142. - (void)addNewGroup {
  143. [self dismissOptionPopOver:^{
  144. NewGroupPopupView *popupView = [[NewGroupPopupView alloc] initFromNib];
  145. [popupView showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  146. if (buttonIndex == 0) {//OK
  147. NSLog(@"%@", popupView.txtGroupName.text);
  148. }
  149. }];
  150. }];
  151. }
  152. - (void)refreshDeviceList {
  153. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  154. }
  155. #pragma mark - Main Logic
  156. - (void)requestDeviceList {
  157. //parameters
  158. NSDictionary *parameter = _deviceGroupId ? @{@"dvcgrp_id": _deviceGroupId} : nil;
  159. NSString *path = [NSString stringWithFormat:API_GET_GROUPS_DEVICES];
  160. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[GroupsListModel class] completion:^(id responseObject) {
  161. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  162. return;
  163. }
  164. GroupsListModel *groupList = (GroupsListModel *)responseObject;
  165. if (groupList && groupList.groups && groupList.groups.count) {
  166. _groupList = (NSMutableArray<GroupsModel> *)[NSMutableArray arrayWithArray:groupList.groups];
  167. _arrayForHeader = [[NSMutableArray alloc] init]; //헤더 저장용으로 사용
  168. GroupModifyViewController *gvc = [[JDFacade facade] viewControllerOnNaviationController:[GroupModifyViewController class]];
  169. if (gvc) {
  170. for (GroupsModel *group in _groupList) {
  171. if ([gvc.group.dvcgrpId isEqualToString:group.dvcgrpId]) {//동일 그룹이면 업데이트 해줌
  172. gvc.group = group;
  173. [gvc prepareViewDidLoad];
  174. break;
  175. }
  176. }
  177. }
  178. } else {
  179. if (!_groupList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  180. // NoContentView *noContentView = [NoContentView viewFromNib];
  181. // _tableView.tableFooterView = noContentView;
  182. }
  183. }
  184. [_tableView reloadData];
  185. } failure:^(id errorObject) {
  186. // [self.tableView.pullToRefreshView stopAnimating];
  187. JDErrorModel *error = (JDErrorModel *)errorObject;
  188. [[JDFacade facade] alert:error.errorMessage];
  189. }];
  190. }
  191. #pragma mark - UITableView DataSource & Delegate
  192. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  193. return _groupList.count + 1;
  194. }
  195. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  196. if (section == _groupList.count) {
  197. return nil;
  198. }
  199. if (!_groupList.count) {
  200. return nil;
  201. }
  202. GroupsModel *group = _groupList[section];
  203. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  204. if (!view) {
  205. ThingsHeaderTableViewCell *hcell = (ThingsHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  206. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  207. [view addSubview:hcell];
  208. hcell.delegate = self;
  209. hcell.section = section;
  210. hcell.group = group;
  211. hcell.btnEdit.hidden = [group.defaultYn boolValue];
  212. NSString *headerTitle = [NSString stringWithFormat:@"%@ (%@)", group.dvcgrpName, group.deviceCount];
  213. [hcell.btnHeader setTitle:headerTitle forState:UIControlStateNormal];
  214. if (_arrayForHeader.count == section) {
  215. [_arrayForHeader insertObject:view atIndex:section];
  216. }
  217. } else {
  218. ThingsHeaderTableViewCell *hcell = (ThingsHeaderTableViewCell *)view.subviews[0];
  219. NSString *headerTitle = [NSString stringWithFormat:@"%@ (%@)", group.dvcgrpName, group.deviceCount];
  220. [hcell.btnHeader setTitle:headerTitle forState:UIControlStateNormal];
  221. }
  222. return view;
  223. }
  224. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  225. if (section == _groupList.count) {
  226. return 0.01f;
  227. }
  228. return 40.0f;
  229. }
  230. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  231. return 0.01f;
  232. }
  233. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  234. if (section == _groupList.count) {//마지막 섹션에 장치 추가를 넣어줌
  235. return 1;
  236. }
  237. if (!_groupList.count) {
  238. return 0;
  239. }
  240. GroupsModel *group = _groupList[section];
  241. if (group.unfold) {
  242. return 0;
  243. }
  244. return group.devices.count;
  245. }
  246. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  247. CGFloat height = kfThingsTableViewCellHeight;
  248. if (indexPath.section == _groupList.count) {
  249. height = _groupList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  250. _tableView.scrollEnabled = _groupList.count;
  251. }
  252. return height;
  253. }
  254. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  255. UITableViewCell *cell = nil;
  256. if (indexPath.section < _groupList.count) {
  257. GroupsModel *group = _groupList[indexPath.section];
  258. NSArray<DeviceModel> *deviceList = group.devices;
  259. ThingsTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  260. if (tcell == nil) {
  261. tcell = [[ThingsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  262. }
  263. tcell.lblTitle.textColor = [UIColor whiteColor];
  264. //뷰를 초기화함.
  265. [[tcell.controlContainerView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  266. UIView *subview = (UIView *)obj;
  267. [subview removeFromSuperview];
  268. }];
  269. DeviceModel *device = deviceList[indexPath.row];
  270. tcell.lblTitle.text = device.deviceName;
  271. //On-offline 처리
  272. [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] placeholderImage:nil];
  273. if (device.networkYn && ![device.networkYn isEqualToString:@"none"] && ![device.networkYn boolValue]) {
  274. if (!tcell.imgvIcon.subviews.count) {
  275. UIImageView *imgvOffline = [[UIImageView alloc] initWithImage:_offlineImage];
  276. CGRect ir = tcell.imgvIcon.frame;
  277. ir.origin = (CGPoint){.x=0, .y=0};
  278. imgvOffline.frame = ir;
  279. [tcell.imgvIcon addSubview:imgvOffline];
  280. } else {
  281. [tcell.imgvIcon bringSubviewToFront:tcell.imgvIcon.subviews.firstObject];
  282. }
  283. } else {
  284. if (tcell.imgvIcon.subviews.count) {
  285. [tcell.imgvIcon.subviews.firstObject removeFromSuperview];
  286. }
  287. }
  288. CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:device.cmdclsType];
  289. controlView.device = device;
  290. tcell.controlContainerView.hidden = !controlView;
  291. if (!tcell.controlContainerView.hidden) {
  292. UIView *superview = tcell.controlContainerView;
  293. [superview addSubview:controlView];
  294. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  295. make.top.equalTo(superview.mas_top);
  296. make.left.equalTo(superview.mas_left);
  297. make.center.equalTo(superview);
  298. }];
  299. }
  300. cell = tcell;
  301. //set background image
  302. if (indexPath.row % 2 == 1) {
  303. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  304. } else {
  305. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  306. }
  307. } else {
  308. ThingsAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  309. if (acell == nil) {
  310. acell = [[ThingsAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
  311. }
  312. cell = acell;
  313. }
  314. return cell;
  315. }
  316. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  317. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  318. if (indexPath.section == _groupList.count) {
  319. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  320. [self.navigationController pushViewController:vc animated:YES];
  321. return;
  322. }
  323. GroupsModel *group = _groupList[indexPath.section];
  324. NSArray<DeviceModel> *deviceList = group.devices;
  325. DeviceModel *device = deviceList[indexPath.row];
  326. ThingsDetailViewController *vc = (ThingsDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsDetailViewController" storyboardName:@"Things"];
  327. vc.title = device.deviceName;
  328. vc.device = device;
  329. [self presentViewController:vc animated:YES completion:nil];
  330. }
  331. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  332. // Remove seperator inset
  333. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  334. [cell setSeparatorInset:UIEdgeInsetsZero];
  335. }
  336. // Prevent the cell from inheriting the Table View's margin settings
  337. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  338. [cell setPreservesSuperviewLayoutMargins:NO];
  339. }
  340. // Explictly set your cell's layout margins
  341. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  342. [cell setLayoutMargins:UIEdgeInsetsZero];
  343. }
  344. }
  345. #pragma mark - ThingsHeaderTableViewCellDelegate
  346. - (void)didThingsHeaderTableViewCellBtnHeaderTouched:(id)sender {
  347. ThingsHeaderTableViewCell *hcell = (ThingsHeaderTableViewCell *)sender;
  348. BOOL isExpanded = !hcell.group.unfold;
  349. [_tableView enumarateTableViewCellsSection:hcell.section UsingBlock:^(UITableViewCell *cell) {
  350. ThingsHeaderTableViewCell *tmpCell = (ThingsHeaderTableViewCell *)cell;
  351. tmpCell.contentView.hidden = isExpanded;
  352. }];
  353. // if (isExpanded) {
  354. // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:hcell.section] withRowAnimation:UITableViewRowAnimationFade];
  355. // } else {
  356. // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:hcell.section] withRowAnimation:UITableViewRowAnimationFade];
  357. // }
  358. [_tableView reloadData];
  359. }
  360. #pragma mark - UI Events
  361. #pragma mark - MemoryWarning
  362. - (void)didReceiveMemoryWarning
  363. {
  364. [super didReceiveMemoryWarning];
  365. // Dispose of any resources that can be recreated.
  366. }
  367. @end