RegisterModeViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // RulesRegisterModeViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/23/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import ObjectiveC.runtime;
  9. #import "RequestHandler.h"
  10. #import "ModeModel.h"
  11. #import "RuleModel.h"
  12. #import "CustomLabel.h"
  13. #import "CustomRadioGroup.h"
  14. #import "CustomImageView.h"
  15. #import "CustomCheckBox.h"
  16. #import "UIImageView+WebCache.h"
  17. #import "CustomButton.h"
  18. #import "PredefinedDeviceViewController.h"
  19. #import "ScenesRegisterViewController.h"
  20. #import "RegisterModeViewController.h"
  21. #import "RulesTriggerOptionViewController.h"
  22. #import "RulesRegisterViewController.h"
  23. @implementation RulesStaticTitleTableViewCell
  24. @end
  25. @implementation RulesModeTableViewCell
  26. - (void)awakeFromNib {
  27. self.selectionStyle = UITableViewCellSelectionStyleNone;
  28. self.backgroundColor = [UIColor clearColor];
  29. }
  30. @end
  31. @interface RegisterModeViewController () <UITableViewDataSource, UITableViewDelegate, CustomRadioButtonDelegate> {
  32. NSMutableArray<ModeModel> *_modeList;
  33. CustomRadioReusableGroup *_rgroup;
  34. BOOL _isNotFirstLoading;
  35. NSInteger _sectionCount;
  36. RulesTriggerOptionViewController *_triggervc;
  37. }
  38. @end
  39. #pragma mark - Class Definition
  40. @implementation RegisterModeViewController
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // initialize
  44. _rgroup = [[CustomRadioReusableGroup alloc] init];
  45. _rgroup.tableView = _tableView;
  46. [self initUI];
  47. [self prepareViewDidLoad];
  48. }
  49. - (void)initUI {
  50. _tableView.dataSource = self;
  51. _tableView.delegate = self;
  52. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  53. _tableView.separatorColor = kUILineColor2;
  54. _tableView.backgroundColor = [UIColor clearColor];
  55. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  56. //Localization
  57. [_btnSelect setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  58. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  59. }
  60. - (void)prepareViewDidLoad {
  61. if (_tmpItem.itemTypeCode) {
  62. ItemSubModel *subItem = _tmpItem.modes[0];
  63. _sectionCount = subItem.conditions && subItem.conditions.count ? 4 : 3;
  64. } else {
  65. _sectionCount = 2;
  66. }
  67. //itemSubTypeCode가 02-모드일 경우,
  68. [self requestModes];
  69. }
  70. #pragma mark - Main Logic
  71. - (void)requestModes {
  72. NSString *path = API_GET_DASHBOARD_MODE_LIST;
  73. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ModeListModel class] completion:^(id responseObject) {
  74. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  75. return;
  76. }
  77. ModeListModel *fetchedModeList = (ModeListModel *)responseObject;
  78. if (fetchedModeList && fetchedModeList.list && fetchedModeList.list.count) {//API 성공 ,
  79. _modeList = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:fetchedModeList.list];
  80. // for (ModeModel *mode in _tmpItem.modes) {
  81. // [_modeList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  82. //
  83. // ModeModel *cmode = (ModeModel *)obj;
  84. // if ([mode.modeId isEqualToString:cmode.modeId]) {//동일한 모드일 경우, 저장된 모드로 교체함
  85. // [_modeList replaceObjectAtIndex:idx withObject:mode];
  86. // *stop = YES;
  87. // }
  88. // }];
  89. // }
  90. for (ModeModel *mode in _tmpItem.modes) {
  91. NSInteger indx = [_modeList indexOfObjectPassingTest:^BOOL(ModeModel *obj, NSUInteger idx, BOOL *stop) {
  92. return [mode.modeId isEqualToString:obj.modeId];
  93. }];
  94. if (indx != NSNotFound) {
  95. ModeModel *tmodel = _modeList[indx];
  96. [[JDFacade facade] setRadioButtonStatus:@YES object:tmodel];
  97. // id radioStatus = [[JDFacade facade] getRadioButtonStatus:mode];
  98. // if (radioStatus) {
  99. // }
  100. }
  101. }
  102. [self setModeContents];
  103. }
  104. } failure:^(id errorObject) {
  105. JDErrorModel *error = (JDErrorModel *)errorObject;
  106. [[JDFacade facade] alert:error.errorMessage];
  107. }];
  108. }
  109. - (void)setModeContents {
  110. [JDFacade facade].modeList = (NSArray<ModeModel> *)[NSArray arrayWithArray:_modeList];
  111. [_tableView reloadData];
  112. }
  113. #pragma mark - UITableView DataSource & Delegate
  114. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  115. return _sectionCount;
  116. }
  117. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  118. NSInteger count = 0;
  119. if (section == 0) {//title
  120. count = 1;
  121. } else if (section == 1) {
  122. count = _modeList && _modeList.count ? _modeList.count : 0;
  123. } else if (section == 2) {//activation cell
  124. count = 1;
  125. } else if (section == 3) {//condition cell
  126. count = 1;
  127. }
  128. return count;
  129. }
  130. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  131. CGFloat height = 0;
  132. if (indexPath.section == 0) {//title
  133. height = _isTrigger ? 65 : 88;
  134. } else if (indexPath.section == 1) {//modes
  135. height = 100;
  136. } else if (indexPath.section == 2) {//activation
  137. height = 80.0f;
  138. } else if (indexPath.section == 3) {//container
  139. height = 240;
  140. }
  141. return height;
  142. }
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  144. UITableViewCell *cell = nil;
  145. if (indexPath.section == 0) {//title
  146. if (_isTrigger) {
  147. RulesStaticTitleTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"StaticTitleCellIdentifier"];
  148. if (tcell == nil) {
  149. tcell = [[RulesStaticTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StaticTitleCellIdentifier"];
  150. }
  151. tcell.lblItemName.text = NSLocalizedString(@"홈모드가 바뀔 때", @"홈모드가 바뀔 때");
  152. cell = tcell;
  153. } else {
  154. RulesDeviceTitleViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  155. if (tcell == nil) {
  156. tcell = [[RulesDeviceTitleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TitleCellIdentifier"];
  157. }
  158. [tcell.btnActionTitle setTitle:_tmpItem.itemName forState:UIControlStateNormal];
  159. tcell.lblSelectCount.text = [NSString stringWithFormat:@"%zd / %zd", 1, _modeList.count]; //모드는 무조건 1임.
  160. cell = tcell;
  161. }
  162. } else if (indexPath.section == 1) {
  163. RulesModeTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  164. if (tcell == nil) {
  165. tcell = [[RulesModeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  166. }
  167. ModeModel *mode = _modeList[indexPath.row];
  168. [tcell.imgvItem sd_setImageWithURL:[NSURL URLWithString:mode.imageFileName] placeholderImage:nil];
  169. tcell.lblItemName.text = mode.modeName;
  170. tcell.rdoSelect.value = mode;
  171. if (indexPath.row == 0 && !_isNotFirstLoading && !_tmpItem.modes) {
  172. _isNotFirstLoading = YES;
  173. tcell.rdoSelect.checked = YES;
  174. } else {
  175. tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
  176. }
  177. [_rgroup addRadioButton:tcell.rdoSelect];
  178. cell = tcell;
  179. } else if (indexPath.section == 2) {//activation
  180. // RegisterTriggerActivationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier" forIndexPath:indexPath];
  181. // if (!tcell) {
  182. // tcell = (RegisterTriggerActivationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier"];
  183. // }
  184. //
  185. // if (![tcell.btnTriggerOption actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  186. // [tcell.btnTriggerOption addTarget:self action:@selector(btnAddTriggerOptionTouched:) forControlEvents:UIControlEventTouchUpInside];
  187. // }
  188. //
  189. // //이미지 전환
  190. // [tcell setTriggerOptionEnable:_sectionCount == 4];
  191. // cell = tcell;
  192. } else if (indexPath.section == 3) {
  193. // RegisterContainerTableViewCell *tcell = (RegisterContainerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ContainerCellIdentifier"];
  194. //
  195. // if (!_triggervc) {//트리거뷰를 로드함.
  196. // _triggervc = (RulesTriggerOptionViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesTriggerOptionViewController" storyboardName:@"Rules"];
  197. // _triggervc.tmpItem = _tmpItem;
  198. //
  199. // [self addChildViewController:_triggervc];
  200. // [_triggervc didMoveToParentViewController:self];
  201. //
  202. // if ([self isViewLoaded] && tcell.triggerOptionContainer) {
  203. // [_triggervc beginAppearanceTransition:YES animated:NO];
  204. // [tcell.triggerOptionContainer addSubview:_triggervc.view];
  205. // [_triggervc endAppearanceTransition];
  206. // [_triggervc.view mas_makeConstraints:^(MASConstraintMaker *make) {
  207. // make.size.mas_equalTo(tcell.triggerOptionContainer);
  208. // make.center.equalTo(tcell.triggerOptionContainer);
  209. // }];
  210. // }
  211. // }
  212. //
  213. // cell = tcell;
  214. }
  215. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  216. return cell;
  217. }
  218. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  219. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  220. if (indexPath.section == 1) {//modes
  221. RulesDeviceTableViewCell *tcell = (RulesDeviceTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  222. [_rgroup someRadioButtonTouched:tcell.rdoSelect];
  223. } else if (indexPath.section == 2) {
  224. // RegisterTriggerActivationTableViewCell *tcell = (RegisterTriggerActivationTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  225. // [self btnAddTriggerOptionTouched:tcell.btnTriggerOption];
  226. }
  227. [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  228. }
  229. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  230. // Remove seperator inset
  231. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  232. [cell setSeparatorInset:UIEdgeInsetsZero];
  233. }
  234. // Prevent the cell from inheriting the Table View's margin settings
  235. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  236. [cell setPreservesSuperviewLayoutMargins:NO];
  237. }
  238. // Explictly set your cell's layout margins
  239. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  240. [cell setLayoutMargins:UIEdgeInsetsZero];
  241. }
  242. }
  243. #pragma mark - RulesItemActionTableViewCellDelegate
  244. - (IBAction)btnAddTriggerOptionTouched:(id)sender {
  245. _sectionCount = _sectionCount == 3 ? 4 : 3;
  246. [self.tableView reloadData];
  247. if (_sectionCount == 4) {
  248. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
  249. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  250. }
  251. }
  252. //- (NSMutableArray<ItemModel> *)conditions {
  253. // //컨디션을 설정함 - 이전 선택된 컨디션을 유지하기 위해 먼저 확인 - 테이블 뷰의 특성상.
  254. // NSMutableArray<ItemModel> *conditions = nil;
  255. // if (_triggervc && [_triggervc hasCondition]) {
  256. // if (!_triggervc.conditions) {
  257. // return nil;
  258. // }
  259. //
  260. // conditions = _triggervc.conditions;
  261. // } else {
  262. // if (_tmpItem.timers && _tmpItem.timers.count && !_triggervc) {
  263. // ModeModel *mode = _tmpItem.modes[0];
  264. // conditions = mode.conditions;
  265. // }
  266. // }
  267. // return conditions;
  268. //}
  269. - (void)btnSelectTouched:(id)sender {
  270. ModeModel *mode = _rgroup.valueForChecked;
  271. if (_isTrigger) {//트리거일 경우,
  272. //컨디션을 설정함 - 이전 선택된 컨디션을 유지하기 위해 먼저 확인 - 테이블 뷰의 특성상.
  273. NSMutableArray<ItemModel> *conditions = nil;
  274. if (_triggervc && [_triggervc hasCondition]) {
  275. if (!_triggervc.conditions) {
  276. return;
  277. }
  278. conditions = _triggervc.conditions;
  279. } else {
  280. if (_tmpItem.modes && _tmpItem.modes.count && !_triggervc) {//이전에 선택된 컨디션 정보
  281. ModeModel *mode = _tmpItem.modes[0];
  282. conditions = mode.conditions;
  283. }
  284. }
  285. if (conditions) {
  286. mode.conditions = conditions;
  287. }
  288. //trigger
  289. _tmpItem.modes = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:@[mode]];
  290. } else {//액션일 경우,
  291. //actions
  292. _tmpItem.modes = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:@[mode]];
  293. if (_isCustomCreation) {//자유 선택
  294. if (_tmpItem.predSceneId && ![_tmpItem.predSceneId isEmptyString]) {//씬일 경우,
  295. ScenesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ScenesRegisterViewController class]];
  296. [vc addItem:_tmpItem];
  297. } else if (_tmpItem.predRuleId && ![_tmpItem.predRuleId isEmptyString]) {//룰일 경우,
  298. RulesRegisterViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[RulesRegisterViewController class]];
  299. [vc addItem:_tmpItem];
  300. }
  301. }
  302. }
  303. [self.navigationController popViewControllerAnimated:YES];
  304. }
  305. - (void)btnCancelTouched:(id)sender {
  306. [self.navigationController popViewControllerAnimated:YES];
  307. }
  308. #pragma mark - UI Events
  309. #pragma mark - MemoryWarning
  310. - (void)didReceiveMemoryWarning
  311. {
  312. [super didReceiveMemoryWarning];
  313. // Dispose of any resources that can be recreated.
  314. }
  315. @end