ThingsGroupAddViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // ThingsGroupAddViewController.h
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/14/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import ObjectiveC.runtime;
  9. #import "JDObject.h"
  10. #import "RequestHandler.h"
  11. #import "JDJSONModel.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "CustomLabel.h"
  14. #import "CustomImageView.h"
  15. #import "CustomButton.h"
  16. #import "CustomCheckBox.h"
  17. #import "ThingsGroupAddViewController.h"
  18. #import "ThingsViewController.h"
  19. #import "GroupModifyViewController.h"
  20. #import "ImageUtil.h"
  21. #define kfThingsTableViewCellHeight 100.0f
  22. @protocol ThingsGroupHeaderTableViewCellDelegate <NSObject>
  23. @optional
  24. - (void)didThingsHeaderTableViewCellBtnHeaderTouched:(id)sender;
  25. @end
  26. @interface ThingsGroupHeaderTableViewCell () {
  27. }
  28. @property (assign, nonatomic) id <ThingsGroupHeaderTableViewCellDelegate> delegate;
  29. @end
  30. @implementation ThingsGroupHeaderTableViewCell
  31. - (IBAction)btnHeaderTouched:(id)sender {
  32. [_btnHeader faceOffImage];
  33. _group.unfold = !_group.unfold;
  34. // _group[@"expanded"] = [NSNumber numberWithBool:![_group[@"expanded"] boolValue]];
  35. if ([self.delegate respondsToSelector:@selector(didThingsHeaderTableViewCellBtnHeaderTouched:)]) {
  36. [self.delegate didThingsHeaderTableViewCellBtnHeaderTouched:self];
  37. }
  38. }
  39. @end
  40. @implementation ThingsGroupTableViewCell
  41. - (void)awakeFromNib {
  42. self.backgroundColor = [UIColor clearColor];
  43. self.selectionStyle = UITableViewCellSelectionStyleNone;
  44. self.opaque = NO;
  45. }
  46. @end
  47. @implementation ThingsGroupTitleTableViewCell
  48. - (void)awakeFromNib {
  49. _lblGroupTitle.text = [NSString stringWithFormat:NSLocalizedString(@"추가할 그룹 : %@", @"추가할 그룹 : %@"), ksEmptyString];
  50. _lblGroupInfo.text = NSLocalizedString(@"다른 그룹에 속해있는 장치를 이 그룹에\n추가하면 해당 그룹에서 제외됩니다", @"다른 그룹에 속해있는 장치를 이 그룹에\n추가하면 해당 그룹에서 제외됩니다");
  51. }
  52. @end
  53. @interface ThingsGroupAddViewController () <UITableViewDelegate, UITableViewDataSource, ThingsGroupHeaderTableViewCellDelegate> {
  54. UIImage *_bgCellImage1, *_bgCellImage2;
  55. NSMutableArray *_arrayForHeader;
  56. NSString *_deviceGroupId;
  57. }
  58. @end
  59. #pragma mark - Class Definition
  60. @implementation ThingsGroupAddViewController
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. _deviceGroupId = ksEmptyString;
  64. [self initUI];
  65. [self prepareViewDidLoad];
  66. }
  67. - (void)viewWillAppear:(BOOL)animated {
  68. [super viewWillAppear:animated];
  69. self.title = NSLocalizedString(@"그룹에 장치 추가",nil);
  70. }
  71. - (void)initUI {
  72. //set tableview option
  73. _tableView.delegate = self;
  74. _tableView.dataSource = self;
  75. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  76. _tableView.backgroundColor = [UIColor clearColor];
  77. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  78. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  79. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  80. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  81. //Localization
  82. [_btnSave setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  83. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  84. }
  85. - (void)prepareViewDidLoad {
  86. }
  87. #pragma mark - UITableView DataSource & Delegate
  88. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  89. return _groupList.count + 1;
  90. }
  91. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  92. if (section == 0) {
  93. return nil;
  94. }
  95. if (!_groupList.count) {
  96. return nil;
  97. }
  98. GroupsModel *group = _groupList[section-1];
  99. UIView *view = _arrayForHeader.count > section-1 ? _arrayForHeader[section-1] : nil;
  100. if (!view) {
  101. ThingsGroupHeaderTableViewCell *hcell = (ThingsGroupHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  102. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  103. [view addSubview:hcell];
  104. hcell.delegate = self;
  105. hcell.section = section;
  106. hcell.group = group;
  107. [hcell.btnHeader setTitle:group.dvcgrpName forState:UIControlStateNormal];
  108. [_arrayForHeader insertObject:view atIndex:section-1];
  109. }
  110. return view;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  113. if (section == 0) {
  114. return 0.01f;
  115. }
  116. return 40.0f;
  117. }
  118. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  119. return 0.01f;
  120. }
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  122. if (section == 0) {//첫 섹션에 타이틀셀을 추가로 넣어줌
  123. return 1;
  124. }
  125. if (!_groupList.count) {
  126. return 0;
  127. }
  128. GroupsModel *group = _groupList[section-1];
  129. if (group.unfold) {
  130. return 0;
  131. }
  132. return group.devices.count;
  133. }
  134. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  135. CGFloat height = kfThingsTableViewCellHeight;
  136. if (indexPath.section == 0) {
  137. return 97.0f;
  138. }
  139. _tableView.scrollEnabled = _groupList.count;
  140. return height;
  141. }
  142. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. UITableViewCell *cell = nil;
  144. if (indexPath.section == 0) {//title cell
  145. ThingsGroupTitleTableViewCell *tcell = (ThingsGroupTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  146. tcell.lblGroupTitle.text = [NSString stringWithFormat:NSLocalizedString(@"추가할 그룹 : %@", @"추가할 그룹 : %@"), _addedGroup.dvcgrpName];
  147. cell = tcell;
  148. } else {
  149. ThingsGroupTableViewCell *tcell = (ThingsGroupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  150. if (tcell == nil) {
  151. tcell = [[ThingsGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  152. }
  153. GroupsModel *group = _groupList[indexPath.section-1];
  154. NSArray<DeviceModel> *deviceList = group.devices;
  155. DeviceModel *device = deviceList[indexPath.row];
  156. //set contents
  157. tcell.lblTitle.text = device.deviceName;
  158. [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] placeholderImage:nil];
  159. tcell.chkSelect.value = device;
  160. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  161. cell = tcell;
  162. //set background image
  163. if (indexPath.row % 2 == 0) {
  164. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  165. } else {
  166. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  167. }
  168. }
  169. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  170. return cell;
  171. }
  172. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  173. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  174. if (indexPath.section == 0) {
  175. return;
  176. }
  177. GroupsModel *group = _groupList[indexPath.section-1];
  178. NSArray<DeviceModel> *deviceList = group.devices;
  179. ThingsGroupTableViewCell *tcell = (ThingsGroupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  180. tcell.chkSelect.value = deviceList[indexPath.row];
  181. tcell.chkSelect.checked = !tcell.chkSelect.checked;
  182. [_tableView reloadData];
  183. }
  184. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  185. // Remove seperator inset
  186. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  187. [cell setSeparatorInset:UIEdgeInsetsZero];
  188. }
  189. // Prevent the cell from inheriting the Table View's margin settings
  190. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  191. [cell setPreservesSuperviewLayoutMargins:NO];
  192. }
  193. // Explictly set your cell's layout margins
  194. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  195. [cell setLayoutMargins:UIEdgeInsetsZero];
  196. }
  197. }
  198. #pragma mark - ThingsHeaderTableViewCellDelegate
  199. - (void)didThingsHeaderTableViewCellBtnHeaderTouched:(id)sender {
  200. ThingsGroupHeaderTableViewCell *hcell = (ThingsGroupHeaderTableViewCell *)sender;
  201. BOOL isExpanded = !hcell.group.unfold;
  202. [_tableView enumarateTableViewCellsSection:hcell.section UsingBlock:^(UITableViewCell *cell) {
  203. ThingsGroupHeaderTableViewCell *tmpCell = (ThingsGroupHeaderTableViewCell *)cell;
  204. tmpCell.contentView.hidden = isExpanded;
  205. }];
  206. [_tableView reloadData];
  207. }
  208. #pragma mark - Main Logic
  209. - (void)requestAddDeviceToAddedGroup:(NSMutableArray *)devices {
  210. //parameters
  211. NSDictionary *parameter = @{@"devices": devices};
  212. NSString *path = [NSString stringWithFormat:API_POST_GROUP_ADD, _addedGroup.dvcgrpId];
  213. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  214. [[JDFacade facade] toast:NSLocalizedString(@"장치가 추가되었습니다", @"장치가 추가되었습니다")];
  215. //리스트를 다시 로드함.
  216. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  217. [vc prepareViewDidLoad];
  218. [self.navigationController popViewControllerAnimated:YES];
  219. } failure:^(id errorObject) {
  220. JDErrorModel *error = (JDErrorModel *)errorObject;
  221. [[JDFacade facade] alert:error.errorMessage];
  222. }];
  223. }
  224. #pragma mark - UI Events
  225. - (void)btnBackTouched:(id)sender {
  226. //그룹 리스트로 리턴
  227. [self.navigationController popToRootViewControllerAnimated:YES];
  228. }
  229. - (IBAction)btnSaveTouched:(id)sender
  230. {
  231. NSMutableArray *selectedDevices = [[NSMutableArray alloc] init];
  232. for (GroupsModel *group in _groupList) {
  233. for (DeviceModel *device in group.devices) {
  234. if ([[[JDFacade facade] getCheckBoxStatus:device] boolValue]) {
  235. [selectedDevices addObject:@{@"device_id" : device.deviceId}];
  236. }
  237. }
  238. }
  239. if (!selectedDevices.count) {
  240. [[JDFacade facade] alert:NSLocalizedString(@"선택된 장치가 없습니다\n장치를 선택해주세요", @"선택된 장치가 없습니다\n장치를 선택해주세요")];
  241. return;
  242. }
  243. [self requestAddDeviceToAddedGroup:selectedDevices];
  244. }
  245. - (IBAction)btnCancelTouched:(id)sender
  246. {
  247. //리스트를 다시 로드함.
  248. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  249. [vc prepareViewDidLoad];
  250. [self.navigationController popViewControllerAnimated:YES];
  251. [self btnBackTouched:nil];
  252. }
  253. #pragma mark - MemoryWarning
  254. - (void)didReceiveMemoryWarning
  255. {
  256. [super didReceiveMemoryWarning];
  257. // Dispose of any resources that can be recreated.
  258. }
  259. @end