GroupModifyViewController.m 9.2 KB

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