RegisterTimerViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // RulesRegisterTimerViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/24/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import QuartzCore;
  9. #import "JDObject.h"
  10. #import "RuleModel.h"
  11. #import "ItemModel.h"
  12. #import "CustomLabel.h"
  13. #import "CustomCheckBox.h"
  14. #import "CommonUtil.h"
  15. #import "CustomButton.h"
  16. #import "RulesTriggerOptionViewController.h"
  17. #import "RegisterTimerViewController.h"
  18. @implementation CustomDatePicker
  19. - (void)awakeFromNib {
  20. self.layer.backgroundColor = [UIColor clearColor].CGColor;
  21. }
  22. - (void)addSubview:(UIView *)view {
  23. if (!changed) {
  24. changed = YES;
  25. [self setValue:[UIColor whiteColor] forKey:@"textColor"];
  26. }
  27. [super addSubview:view];
  28. }
  29. @end
  30. @implementation RegisterTimerTitleTableViewCell
  31. @end
  32. @implementation RegisterPickerTableViewCell
  33. @end
  34. @interface RegisterTriggerActivationTableViewCell () {
  35. }
  36. @end
  37. @implementation RegisterTriggerActivationTableViewCell
  38. - (void)awakeFromNib {
  39. _btnAddTriggerImageNormal = [UIImage imageNamed:@"common_list_btn_s_add"];
  40. _btnAddTriggerImageHighlight = [UIImage imageNamed:@"common_list_btn_s_add_press"];
  41. _btnDeleteTriggerImageNormal = [UIImage imageNamed:@"common_list_btn_del"];
  42. _btnDeleteTriggerImageHighlight = [UIImage imageNamed:@"common_list_btn_del_press"];
  43. _lblDesc.text = NSLocalizedString(@"특정 날짜나 요일에만 실행하려면\n추가 조건을 걸어보세요", @"특정 날짜나 요일에만 실행하려면\n추가 조건을 걸어보세요");
  44. }
  45. - (void)setTriggerOptionEnable:(BOOL)enable {
  46. if (!enable) {
  47. [self.btnTriggerOption setImage:_btnAddTriggerImageNormal forState:UIControlStateNormal];
  48. [self.btnTriggerOption setImage:_btnAddTriggerImageHighlight forState:UIControlStateHighlighted];
  49. } else {
  50. [self.btnTriggerOption setImage:_btnDeleteTriggerImageNormal forState:UIControlStateNormal];
  51. [self.btnTriggerOption setImage:_btnDeleteTriggerImageHighlight forState:UIControlStateHighlighted];
  52. }
  53. }
  54. @end
  55. @implementation RegisterContainerTableViewCell
  56. @end
  57. @interface RegisterTimerViewController () <UIPickerViewDelegate, UITableViewDataSource, UITableViewDelegate> {
  58. NSInteger _sectionCount;
  59. CustomDatePicker *_timePicker;
  60. RulesTriggerOptionViewController *_triggervc;
  61. BOOL _isNotFirstLoading;
  62. }
  63. @end
  64. #pragma mark - Class Definition
  65. @implementation RegisterTimerViewController
  66. - (void)viewDidLoad {
  67. [super viewDidLoad];
  68. // Do any additional setup after loading the view.
  69. [self initUI];
  70. [self prepareViewDidLoad];
  71. }
  72. - (void)initUI {
  73. self.tableView.delegate = self;
  74. self.tableView.dataSource = self;
  75. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  76. self.tableView.backgroundColor = [UIColor clearColor];
  77. self.tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  78. [_timePicker setTimeZone:[NSTimeZone systemTimeZone]];
  79. //Localization
  80. [_btnSelect setTitle:NSLocalizedString(@"선택완료", @"선택완료") forState:UIControlStateNormal];
  81. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  82. }
  83. - (void)prepareViewDidLoad {
  84. ItemSubModel *subItem = _tmpItem.timers[0];
  85. _sectionCount = subItem.conditions && subItem.conditions.count ? 4 : 3;
  86. if (!_tmpItem.timers || !_tmpItem.timers.count)
  87. return;
  88. }
  89. #pragma mark - Main Logic
  90. #pragma mark - UITableView Delegate
  91. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  92. return _sectionCount;
  93. }
  94. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  95. return 1;
  96. }
  97. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  98. CGFloat height = 0.0f;
  99. if (indexPath.section == 0) {
  100. height = 75.0f;
  101. } else if (indexPath.section == 1) {
  102. height = 210.0f;
  103. } else if (indexPath.section == 2) {//activation cell
  104. height = 80.0f;
  105. } else if (indexPath.section == 3) {//container
  106. height = 240;
  107. }
  108. return height;
  109. }
  110. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  111. UITableViewCell *cell = nil;
  112. if (indexPath.section == 0) {
  113. RegisterTimerTitleTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier" forIndexPath:indexPath];
  114. if (!tcell) {
  115. tcell = (RegisterTimerTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  116. }
  117. tcell.lblItemTitle.text = _tmpItem.itemName;
  118. cell = tcell;
  119. } else if (indexPath.section == 1) {
  120. RegisterPickerTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"PickerCellIdentifier" forIndexPath:indexPath];
  121. if (!tcell) {
  122. tcell = (RegisterPickerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"PickerCellIdentifier"];
  123. }
  124. if (!_isNotFirstLoading) {//처음 로딩 시,
  125. _isNotFirstLoading = YES;
  126. ItemSubModel *subItem = _tmpItem.timers[0];
  127. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  128. ds.hour = [subItem.hour integerValue];
  129. ds.minute = [subItem.minute integerValue];
  130. tcell.timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  131. }
  132. _timePicker = tcell.timePicker;
  133. cell = tcell;
  134. } else if (indexPath.section == 2) {//activation
  135. RegisterTriggerActivationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier" forIndexPath:indexPath];
  136. if (!tcell) {
  137. tcell = (RegisterTriggerActivationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier"];
  138. }
  139. if (![tcell.btnTriggerOption actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  140. [tcell.btnTriggerOption addTarget:self action:@selector(btnAddTriggerOptionTouched:) forControlEvents:UIControlEventTouchUpInside];
  141. }
  142. //이미지 전환
  143. [tcell setTriggerOptionEnable:_sectionCount == 4];
  144. cell = tcell;
  145. } else if (indexPath.section == 3) {//container
  146. RegisterContainerTableViewCell *tcell = (RegisterContainerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ContainerCellIdentifier"];
  147. if (!_triggervc) {//트리거뷰를 로드함.
  148. _triggervc = (RulesTriggerOptionViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesTriggerOptionViewController" storyboardName:@"Rules"];
  149. _triggervc.tmpItem = _tmpItem;
  150. [self addChildViewController:_triggervc];
  151. [_triggervc didMoveToParentViewController:self];
  152. if ([self isViewLoaded] && tcell.triggerOptionContainer) {
  153. [_triggervc beginAppearanceTransition:YES animated:NO];
  154. [tcell.triggerOptionContainer addSubview:_triggervc.view];
  155. [_triggervc endAppearanceTransition];
  156. [_triggervc.view mas_makeConstraints:^(MASConstraintMaker *make) {
  157. make.size.mas_equalTo(tcell.triggerOptionContainer);
  158. make.center.equalTo(tcell.triggerOptionContainer);
  159. }];
  160. }
  161. }
  162. cell = tcell;
  163. }
  164. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  165. return cell;
  166. }
  167. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  168. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  169. if (indexPath.section == 2) {//activationCell
  170. RegisterTriggerActivationTableViewCell *tcell = (RegisterTriggerActivationTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  171. [self btnAddTriggerOptionTouched:tcell.btnTriggerOption];
  172. }
  173. }
  174. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  175. // Remove seperator inset
  176. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  177. [cell setSeparatorInset:UIEdgeInsetsZero];
  178. }
  179. // Prevent the cell from inheriting the Table View's margin settings
  180. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  181. [cell setPreservesSuperviewLayoutMargins:NO];
  182. }
  183. // Explictly set your cell's layout margins
  184. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  185. [cell setLayoutMargins:UIEdgeInsetsZero];
  186. }
  187. }
  188. #pragma mark - UI Events
  189. - (IBAction)btnAddTriggerOptionTouched:(id)sender {
  190. _sectionCount = _sectionCount == 3 ? 4 : 3;
  191. [self.tableView reloadData];
  192. if (_sectionCount == 4) {
  193. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
  194. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  195. }
  196. }
  197. - (void)btnSelectTouched:(id)sender {
  198. //컨디션을 설정함 - 이전 선택된 컨디션을 유지하기 위해 먼저 확인 - 테이블 뷰의 특성상.
  199. NSMutableArray<ItemModel> *conditions = nil;
  200. if (_triggervc && [_triggervc hasCondition]) {//트리거를 선택했을 경우,
  201. if (!_triggervc.conditions) {
  202. return;
  203. }
  204. conditions = _triggervc.conditions;
  205. } else {
  206. if (_tmpItem.timers && _tmpItem.timers.count && !_triggervc) {
  207. ItemSubModel *tmpSubItem = _tmpItem.timers[0];
  208. conditions = tmpSubItem.conditions;
  209. }
  210. }
  211. //time
  212. NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  213. ItemSubModel *subItem = [[ItemSubModel alloc] init];
  214. subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  215. subItem.minute = [NSString stringWithFormat:@"%zd", ds.minute];
  216. if (conditions) {
  217. subItem.conditions = conditions;
  218. }
  219. _tmpItem.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:@[subItem]];
  220. [self.navigationController popViewControllerAnimated:YES];
  221. }
  222. - (void)btnCancelTouched:(id)sender {
  223. [self.navigationController popViewControllerAnimated:YES];
  224. }
  225. - (IBAction)btnHourUpTouched:(id)sender {
  226. // NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date];
  227. // ds.hour += 1;
  228. // _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  229. }
  230. #pragma mark - MemoryWarning
  231. - (void)didReceiveMemoryWarning
  232. {
  233. [super didReceiveMemoryWarning];
  234. // Dispose of any resources that can be recreated.
  235. }
  236. @end