ThingsGroupAddViewController.m 11 KB

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