ThingsViewController.m 17 KB

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