RegisterPushViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // RegisterPushViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/26/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "RuleModel.h"
  11. #import "CustomTextView.h"
  12. #import "CustomLabel.h"
  13. #import "CustomRadioGroup.h"
  14. #import "CustomCheckBox.h"
  15. #import "ScenesRegisterViewController.h"
  16. #import "RulesRegisterViewController.h"
  17. #import "PredefinedDeviceViewController.h"
  18. #import "RegisterPushViewController.h"
  19. #import "CustomButton.h"
  20. #import "ImageUtil.h"
  21. @implementation RegisterPushTableViewCell
  22. - (void)awakeFromNib {
  23. self.backgroundColor = [UIColor clearColor];
  24. self.selectionStyle = UITableViewCellSelectionStyleNone;
  25. _txvMessage.placeHolder = NSLocalizedString(@"여기에 푸시알림으로 받고 싶은 메시지를 입력하세요", @"여기에 푸시알림으로 받고 싶은 메시지를 입력하세요");
  26. }
  27. @end
  28. @implementation RegisterPushMemberTableViewCell
  29. - (void)awakeFromNib {
  30. self.backgroundColor = [UIColor clearColor];
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. }
  33. @end
  34. @implementation RegisterPushMemberTitleViewCell
  35. - (void)awakeFromNib {
  36. self.backgroundColor = [UIColor clearColor];
  37. self.selectionStyle = UITableViewCellSelectionStyleNone;
  38. [_btnPushMemberTitle setTitle:NSLocalizedString(@"푸시 받을 멤버", @"푸시 받을 멤버") forState:UIControlStateNormal];
  39. }
  40. @end
  41. @implementation RegisterPushMethodTableViewCell
  42. - (void)awakeFromNib {
  43. self.backgroundColor = [UIColor clearColor];
  44. self.selectionStyle = UITableViewCellSelectionStyleNone;
  45. [_btnMethodTitle setTitle:NSLocalizedString(@"푸시 방법", @"푸시 방법") forState:UIControlStateNormal];
  46. [_rdoNotificationCenter setTitle:NSLocalizedString(@"알림센터", @"알림센터") forState:UIControlStateNormal];
  47. [_rdoPopup setTitle:NSLocalizedString(@"팝업알림", @"팝업알림") forState:UIControlStateNormal];
  48. _lblMethodDesc.text = NSLocalizedString(@"아이폰을 사용하는 멤버는 휴대폰 설정의\n알림센터에서 지정한 방식에 따라 푸시를 받게됩니다", @"아이폰을 사용하는 멤버는 휴대폰 설정의\n알림센터에서 지정한 방식에 따라 푸시를 받게됩니다");
  49. }
  50. @end
  51. @interface RegisterPushViewController () <UITableViewDataSource, UITableViewDelegate, CustomCheckBoxDelegate> {
  52. CustomTextView *_txvTextView;
  53. NSArray<RulePushMemberModel> *_pushMemberList;
  54. CustomRadioGroup *_rgroup;
  55. UIImage *_bgCellImage1, *_bgCellImage2;
  56. BOOL _isNotFirstLoading;
  57. }
  58. @end
  59. #pragma mark - Class Definition
  60. @implementation RegisterPushViewController
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. // Do any additional setup after loading the view.
  64. [self initUI];
  65. [self prepareViewDidLoad];
  66. }
  67. - (void)initUI {
  68. _tableView.dataSource = self;
  69. _tableView.delegate = self;
  70. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  71. _tableView.backgroundColor = [UIColor clearColor];
  72. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  73. _rgroup = [[CustomRadioGroup alloc] init];
  74. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  75. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  76. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  77. //Localization
  78. [_btnSelect setTitle:NSLocalizedString(@"입력완료", @"입력완료") forState:UIControlStateNormal];
  79. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  80. }
  81. - (void)prepareViewDidLoad {
  82. [self performSelector:@selector(requestMembersForPush) withObject:nil afterDelay:0.0f];
  83. }
  84. #pragma mark - Main Logic
  85. - (void)requestMembersForPush {
  86. //parameters
  87. NSString *path = [NSString stringWithFormat:API_GET_RULE_MEMBERS];
  88. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[RulePushMemberListModel class] completion:^(id responseObject) {
  89. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  90. return;
  91. }
  92. RulePushMemberListModel *fetchedPushMemberList = (RulePushMemberListModel *) responseObject;
  93. if (fetchedPushMemberList && fetchedPushMemberList.list && fetchedPushMemberList.list.count) {//API 성공 ,
  94. _pushMemberList = fetchedPushMemberList.list;
  95. }
  96. [self matchPushMemberList];
  97. } failure:^(id errorObject) {
  98. JDErrorModel *error = (JDErrorModel *)errorObject;
  99. [[JDFacade facade] alert:error.errorMessage];
  100. }];
  101. }
  102. - (void)matchPushMemberList {
  103. for (ItemSubModel *subitem in _tmpItem.pushes) {//이전에 선택된 멤버를 다시 선택해줌.
  104. NSInteger idx = [_pushMemberList indexOfObjectPassingTest:^BOOL(RulePushMemberModel *pmember, NSUInteger idx, BOOL *stop) {
  105. return [subitem.sourceId isEqualToString:pmember.memberId];
  106. }];
  107. if (idx != NSNotFound) {
  108. RulePushMemberModel *pmember = _pushMemberList[idx];
  109. [[JDFacade facade] setCheckBoxStatus:@YES object:pmember]; //for checkbox
  110. }
  111. }
  112. [_tableView reloadData];
  113. }
  114. #pragma mark - UITableView DataSource & Delegate
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  116. return 2 + (_pushMemberList && _pushMemberList.count ? 3 : 0);
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. NSInteger count = 1;
  120. if (section == 4) {//push members
  121. count = _pushMemberList.count;
  122. }
  123. return count;
  124. }
  125. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. CGFloat height = 0;
  127. if (indexPath.section == 0) {//title
  128. height = 88;
  129. } else if (indexPath.section == 1) {//devices
  130. height = 185;
  131. } else if (indexPath.section == 2) {//push method
  132. height = 145;
  133. } else if (indexPath.section == 3) {//push title
  134. height = 30;
  135. } else if (indexPath.section == 4) {//push members
  136. height = 60;
  137. }
  138. return height;
  139. }
  140. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  141. UITableViewCell *cell = nil;
  142. if (indexPath.section == 0) {//title
  143. RulesDeviceTitleViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  144. if (tcell == nil) {
  145. tcell = [[RulesDeviceTitleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TitleCellIdentifier"];
  146. }
  147. [tcell.btnActionTitle setTitle:NSLocalizedString(@"푸시알림", @"푸시알림") forState:UIControlStateNormal];
  148. tcell.lblSelectCount.text = [NSString stringWithFormat:@"%zd / %zd", [self countForCheckedMembers], _pushMemberList.count];
  149. cell = tcell;
  150. } else if (indexPath.section == 1) {//predefined device list
  151. RegisterPushTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"MessageCellIdentifier"];
  152. if (tcell == nil) {
  153. tcell = [[RegisterPushTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageCellIdentifier"];
  154. }
  155. if (!_txvTextView) {
  156. _txvTextView = tcell.txvMessage;
  157. if (_tmpItem.pushes && _tmpItem.pushes.count) {
  158. ItemSubModel *push = _tmpItem.pushes[0];
  159. _txvTextView.text = push.cmdclsValue;
  160. }
  161. }
  162. cell = tcell;
  163. } else if (indexPath.section == 2) {//push method
  164. RegisterPushMethodTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"MethodCellIdentifier"];
  165. if (!_rgroup.rdoBtns.count) {
  166. tcell.rdoNotificationCenter.value = @"01";
  167. tcell.rdoPopup.value = @"02";
  168. [_rgroup addRadioButton:tcell.rdoNotificationCenter];
  169. [_rgroup addRadioButton:tcell.rdoPopup];
  170. if (_tmpItem.pushes && _tmpItem.pushes.count) {
  171. ItemSubModel *push = _tmpItem.pushes[0];
  172. if ([push.conditionTypeCode isEqualToString:@"02"]) {
  173. [_rgroup someRadioButtonTouched:tcell.rdoPopup];
  174. }
  175. }
  176. }
  177. cell = tcell;
  178. } else if (indexPath.section == 3) {//push title
  179. RegisterPushMemberTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"PushTitleCellIdentifier"];
  180. cell = tcell;
  181. } else if (indexPath.section == 4) {//push members
  182. RegisterPushMemberTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"PushMemberCellIdentifier"];
  183. RulePushMemberModel *pmember = _pushMemberList[indexPath.row];
  184. tcell.lblMemberName.text = pmember.nickname;
  185. tcell.chkSelect.value = pmember;
  186. tcell.chkSelect.delegate = self;
  187. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  188. cell = tcell;
  189. //set background image
  190. if (indexPath.row % 2 == 1) {
  191. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  192. } else {
  193. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  194. }
  195. }
  196. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  197. return cell;
  198. }
  199. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  200. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  201. if (indexPath.section == 4) {//액션일 경우,
  202. RegisterPushMemberTableViewCell *tcell = (RegisterPushMemberTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  203. [tcell.chkSelect checkBoxClicked];
  204. [_tableView reloadData];
  205. }
  206. }
  207. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  208. // Remove seperator inset
  209. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  210. [cell setSeparatorInset:UIEdgeInsetsZero];
  211. }
  212. // Prevent the cell from inheriting the Table View's margin settings
  213. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  214. [cell setPreservesSuperviewLayoutMargins:NO];
  215. }
  216. // Explictly set your cell's layout margins
  217. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  218. [cell setLayoutMargins:UIEdgeInsetsZero];
  219. }
  220. }
  221. #pragma mark - CustomCheckBox Delegate
  222. - (NSInteger)countForCheckedMembers {
  223. NSInteger count = 0;
  224. for (RulePushMemberModel *pmember in _pushMemberList) {
  225. if ([[[JDFacade facade] getCheckBoxStatus:pmember] boolValue]) {
  226. count++;
  227. }
  228. }
  229. return count;
  230. }
  231. - (void)didCheckBoxClicked:(id)sender {
  232. // CustomCheckBox *chk = (CustomCheckBox *)sender;
  233. // RulePushMemberModel *pmember = chk.value;
  234. [_tableView reloadData];
  235. }
  236. #pragma mark - UI Events
  237. - (void)btnSelectTouched:(id)sender {
  238. //1.validate
  239. if (!_txvTextView.text || [_txvTextView.text isEmptyString] || [_txvTextView.text isEqualToString:_txvTextView.placeHolder]) {
  240. [[JDFacade facade] alert:NSLocalizedString(@"푸시내용을 입력해주세요", @"푸시내용을 입력해주세요")];
  241. return;
  242. }
  243. _tmpItem.pushes = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  244. for (RulePushMemberModel *pmember in _pushMemberList) {
  245. if ([[[JDFacade facade] getCheckBoxStatus:pmember] boolValue]) {
  246. ItemSubModel *subItem = [[ItemSubModel alloc] init];
  247. subItem.cmdclsValue = _txvTextView.text;
  248. subItem.sourceId = pmember.memberId;
  249. subItem.sourceName = pmember.nickname;
  250. subItem.conditionTypeCode = _rgroup.valueForChecked;
  251. [_tmpItem.pushes addObject:subItem];
  252. }
  253. }
  254. if (!_tmpItem.pushes.count) {
  255. [[JDFacade facade] alert:NSLocalizedString(@"선택된 멤버가 없습니다", @"선택된 멤버가 없습니다")];
  256. return;
  257. }
  258. if (_isCustomCreation) {//자유 선택
  259. if (_tmpItem.predSceneId && ![_tmpItem.predSceneId isEmptyString]) {//씬일 경우,
  260. ScenesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ScenesRegisterViewController class]];
  261. [vc addItem:_tmpItem];
  262. } else if (_tmpItem.predRuleId && ![_tmpItem.predRuleId isEmptyString]) {//룰일 경우,
  263. RulesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[RulesRegisterViewController class]];
  264. [vc addItem:_tmpItem];
  265. }
  266. }
  267. // [[JDFacade facade] toast:@"추가되었습니다."];
  268. [self.navigationController popViewControllerAnimated:YES];
  269. }
  270. - (void)btnCancelTouched:(id)sender {
  271. [self.navigationController popViewControllerAnimated:YES];
  272. }
  273. #pragma mark - MemoryWarning
  274. - (void)didReceiveMemoryWarning
  275. {
  276. [super didReceiveMemoryWarning];
  277. // Dispose of any resources that can be recreated.
  278. }
  279. @end