GroupModifyViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // GroupModifyViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/30/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 "DeviceModel.h"
  13. #import "CustomLabel.h"
  14. #import "CustomButton.h"
  15. #import "CustomImageView.h"
  16. #import "UIImageView+WebCache.h"
  17. #import "GroupModifyViewController.h"
  18. #import "ThingsGroupAddViewController.h"
  19. #import "ThingsViewController.h"
  20. #import "CustomAlertView.h"
  21. #import "NewGroupPopupView.h"
  22. #import "CustomTextField.h"
  23. #define kfGroupTableViewCellHeight 100.0f
  24. @implementation GroupModifyTableViewCell
  25. - (void)awakeFromNib {
  26. self.backgroundColor = [UIColor clearColor];
  27. self.selectionStyle = UITableViewCellSelectionStyleNone;
  28. }
  29. @end
  30. @interface GroupModifyViewController () <UITableViewDataSource, UITableViewDelegate> {
  31. NSMutableArray<DeviceModel> *_deviceList;
  32. UIImage *_bgCellImage1, *_bgCellImage2;
  33. }
  34. @end
  35. #pragma mark - Class Definition
  36. @implementation GroupModifyViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. [self initUI];
  41. [self prepareViewDidLoad];
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. self.title = NSLocalizedString(@"그룹 편집",nil);
  46. }
  47. - (void)initUI {
  48. [self generateOptionButton];
  49. //set tableview option
  50. _tableView.delegate = self;
  51. _tableView.dataSource = self;
  52. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  53. _tableView.backgroundColor = [UIColor clearColor];
  54. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  55. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  56. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  57. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  58. [self setThingsPopoverOptions];
  59. }
  60. - (void)setThingsPopoverOptions {
  61. //set Popover Contents
  62. __weak typeof(self) weakSelf = self;
  63. _popooverOptionArray = [[NSMutableArray alloc] init];
  64. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"그룹에 장치 추가",nil),
  65. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  66. @"target": weakSelf,
  67. @"selector": [NSValue valueWithPointer:@selector(addDeviceToGroup)]}];
  68. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"그룹 삭제",nil),
  69. @"iconName": @"tp_01_img_bg_morepopup_icon_del",
  70. @"target": weakSelf,
  71. @"selector": [NSValue valueWithPointer:@selector(deleteGroup)]}];
  72. }
  73. - (void)prepareViewDidLoad {
  74. //FIXME : 뮤터블을 사용할 이유가 있나?
  75. _deviceList = (NSMutableArray<DeviceModel> *)[NSMutableArray arrayWithArray:_group.devices];
  76. [_btnGroupName setTitle:_group.dvcgrpName forState:UIControlStateNormal];
  77. [_tableView reloadData];
  78. }
  79. #pragma mark - Main Logic
  80. #pragma mark - UITableView DataSource & Delegate
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  82. return 1;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  85. return _deviceList.count;
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. GroupModifyTableViewCell *cell = (GroupModifyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  89. DeviceModel *device = _deviceList[indexPath.row];
  90. cell.lblTitle.text = device.deviceName;
  91. cell.btnDelete.value = device;
  92. [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] placeholderImage:nil];
  93. if (![cell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  94. [cell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  95. }
  96. //set background image
  97. if (indexPath.row % 2 == 1) {
  98. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  99. } else {
  100. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  101. }
  102. return cell;
  103. }
  104. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  105. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  106. }
  107. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  108. // Remove seperator inset
  109. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  110. [cell setSeparatorInset:UIEdgeInsetsZero];
  111. }
  112. // Prevent the cell from inheriting the Table View's margin settings
  113. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  114. [cell setPreservesSuperviewLayoutMargins:NO];
  115. }
  116. // Explictly set your cell's layout margins
  117. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  118. [cell setLayoutMargins:UIEdgeInsetsZero];
  119. }
  120. }
  121. #pragma mark - Main Logic
  122. //그룹 삭제
  123. - (void)requestDeleteGroup {
  124. //parameters
  125. NSString *path = [NSString stringWithFormat:API_DELETE_GROUP, _group.dvcgrpId];
  126. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  127. [[JDFacade facade] toast:NSLocalizedString(@"그룹이 삭제되었습니다", @"그룹이 삭제되었습니다")];
  128. //리스트를 다시 로드함.
  129. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  130. [vc prepareViewDidLoad];
  131. [self.navigationController popToRootViewControllerAnimated:YES];
  132. } failure:^(id errorObject) {
  133. JDErrorModel *error = (JDErrorModel *)errorObject;
  134. [[JDFacade facade] alert:error.errorMessage];
  135. }];
  136. }
  137. - (void)requestDeleteDeviceAtGroup:(DeviceModel *)device {
  138. //parameters
  139. NSString *path = [NSString stringWithFormat:API_POST_GROUP_DELETE, _group.dvcgrpId];
  140. NSDictionary *parameter = @{@"device_id": device.deviceId};
  141. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:parameter modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  142. [_deviceList removeObject:device];
  143. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  144. [vc prepareViewDidLoad];
  145. [[JDFacade facade] toast:NSLocalizedString(@"삭제 되었습니다", @"삭제 되었습니다")];
  146. [_tableView reloadData];
  147. } failure:^(id errorObject) {
  148. JDErrorModel *error = (JDErrorModel *)errorObject;
  149. [[JDFacade facade] alert:error.errorMessage];
  150. }];
  151. }
  152. #pragma mark - UI Events
  153. - (void)addDeviceToGroup {
  154. //그룹에 장치 추가로 이동.
  155. ThingsGroupAddViewController *vc = (ThingsGroupAddViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsGroupAddViewController" storyboardName:@"Things"];
  156. vc.addedGroup = _group;
  157. ThingsViewController *tvc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  158. vc.groupList = tvc.groupList;
  159. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  160. }
  161. - (void)deleteGroup {
  162. [self dismissOptionPopOver:^{
  163. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"그룹 삭제", @"그룹 삭제") message:NSLocalizedString(@"그룹을 삭제하시겠습니까?\n장치는 삭제되지 않고\n미분류 그룹으로 이동됩니다", @"그룹을 삭제하시겠습니까?\n장치는 삭제되지 않고\n미분류 그룹으로 이동됩니다") delegate:nil OKButtonTitle:NSLocalizedString(@"삭제", @"삭제") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  164. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  165. if (buttonIndex == 0) {//삭제
  166. [self requestDeleteGroup];
  167. }
  168. }];
  169. }];
  170. }
  171. - (IBAction)btnGroupNameTouched:(id)sender {
  172. NewGroupPopupView *popupView = [[NewGroupPopupView alloc] initFromNib];
  173. popupView.group = _group;
  174. [popupView showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  175. if (buttonIndex == 0) {//OK
  176. [_btnGroupName setTitle:popupView.txtGroupName.text forState:UIControlStateNormal];
  177. }
  178. }];
  179. }
  180. - (void)setGroup:(GroupsModel *)group {
  181. _group = group;
  182. }
  183. - (void)btnDeleteDeviceTouched:(id)sender {
  184. CustomButton *btn = (CustomButton *)sender;
  185. DeviceModel *device = (DeviceModel *)btn.value;
  186. [self requestDeleteDeviceAtGroup:device];
  187. }
  188. #pragma mark - MemoryWarning
  189. - (void)didReceiveMemoryWarning
  190. {
  191. [super didReceiveMemoryWarning];
  192. // Dispose of any resources that can be recreated.
  193. }
  194. @end