TimePickerPopupView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // TimePickerPopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/24/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "ItemModel.h"
  10. #import "CustomLabel.h"
  11. #import "CustomButton.h"
  12. #import "CustomCheckBox.h"
  13. #import "CommonUtil.h"
  14. #import "CustomTextField.h"
  15. #import "TimePickerPopupView.h"
  16. @implementation CustomTimePicker
  17. - (void)awakeFromNib {
  18. self.layer.backgroundColor = [UIColor clearColor].CGColor;
  19. }
  20. - (void)addSubview:(UIView *)view {
  21. if (!_changed) {
  22. _changed = YES;
  23. [self setValue:kUITextColor02 forKey:@"textColor"];
  24. }
  25. [super addSubview:view];
  26. }
  27. @end
  28. @interface TimePickerPopupView () <CustomTextFieldDelegate>{
  29. }
  30. @end
  31. @implementation TimePickerPopupView
  32. - (id)initFromNib {
  33. for (UIView *view in [CommonUtil nibViews:@"TimePickerPopupView"]) {
  34. if ([view isKindOfClass:[TimePickerPopupView class]]) {
  35. self = (TimePickerPopupView *)view;
  36. ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON;
  37. ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE;
  38. ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED;
  39. ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU;
  40. ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI;
  41. ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT;
  42. ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN;
  43. [_chkDays[0] setDaySelectBgImage:1];
  44. [_chkDays[6] setDaySelectBgImage:3];
  45. for (int i = 1; i < 6; i++) {
  46. [_chkDays[i] setDaySelectBgImage:2];
  47. }
  48. _txtHour.delegate = self;
  49. _txtHour.keyboardType = UIKeyboardTypeNumberPad;
  50. _txtHour.returnKeyType = UIReturnKeyNext;
  51. _txtMinute.delegate = self;
  52. _txtMinute.keyboardType = UIKeyboardTypeNumberPad;
  53. _txtMinute.returnKeyType = UIReturnKeyDone;
  54. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  55. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  56. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  57. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  58. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  59. //Localization
  60. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  61. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  62. }
  63. }
  64. return self;
  65. }
  66. //- (void)alignTextAndImageOfButton:(UIButton *)button {
  67. // // the space between the image and text
  68. // CGFloat spacing = 6.0;
  69. // float textMargin = 0;
  70. //
  71. // // get the size of the elements here for readability
  72. // CGSize imageSize = button.imageView.image.size;
  73. // CGSize titleSize = button.titleLabel.frame.size;
  74. // CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  75. //
  76. // // lower the text and push it left to center it
  77. // button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  78. //
  79. // // the text width might have changed (in case it was shortened before due to
  80. // // lack of space and isn't anymore now), so we get the frame size again
  81. // titleSize = button.titleLabel.bounds.size;
  82. //
  83. // button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  84. //}
  85. - (void)setTimeTrigger:(ItemModel *)timeTrigger {
  86. _timeTrigger = timeTrigger;
  87. if (_timeTrigger.itemSubTypeCode && [_timeTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//기존 데이터가 있을 경우,
  88. ItemSubModel *subItem = _timeTrigger.timers[0];
  89. NSLog(@"subItem : %@", subItem);
  90. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  91. ds.hour = [subItem.hour integerValue];
  92. ds.minute = [subItem.minute integerValue];
  93. _btnMorning.selected = [subItem.hour integerValue] < 12;
  94. _btnAfternoon.selected = !_btnMorning.selected;
  95. _txtHour.text = subItem.hour;
  96. _txtMinute.text = subItem.minute;
  97. for (NSInteger i = 0 ; i < WEEKDAYS.count ; i++) {
  98. if ([subItem.dayofweek containsString: [WEEKDAYS objectAtIndex:i]]) {
  99. CustomCheckBox *chk = [_chkDays objectAtIndex:i];
  100. chk.checked = YES;
  101. }
  102. }
  103. // _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  104. } else {//is new
  105. _timeTrigger.itemName = @"이 시간마다";
  106. _timeTrigger.itemSubTypeCode = ksItemSubTypeCodeTimer;
  107. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  108. ds.hour = 9;
  109. ds.minute = 0;
  110. // _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  111. // for (CustomCheckBox *chk in _chkDays) {
  112. // chk.checked = YES;
  113. // }
  114. _btnMorning.selected = YES;
  115. }
  116. }
  117. - (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions {
  118. _refConditions = refConditions;
  119. [TimePickerPopupView setRefConditions:_refConditions chkDays:_chkDays];
  120. }
  121. + (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions chkDays:(NSArray *)chkDays {
  122. NSArray<ItemSubModel> *subConditions = (NSArray<ItemSubModel> *)[refConditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"];
  123. ItemSubModel *daysCondition = subConditions.firstObject;
  124. NSArray *days = [daysCondition.cmdclsValue componentsSeparatedByString:@"," ];
  125. for (NSString *day in days) {
  126. for (CustomCheckBox *chk in chkDays) {
  127. if ([chk.value isEqualToString:day]) {
  128. chk.checked = YES;
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. - (void)didMoveToSuperview {
  135. }
  136. #pragma mark - textfield delegate
  137. - (void)moveToPrevField:(id)sender {
  138. [_txtHour becomeFirstResponder];
  139. }
  140. - (void)moveToNextField:(id)sender {
  141. [_txtMinute becomeFirstResponder];
  142. }
  143. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  144. NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
  145. if ([textField isEqual:_txtHour] && [text integerValue] > 23) {
  146. textField.text = @"23";
  147. return NO;
  148. }
  149. else if ([textField isEqual:_txtMinute] && [text integerValue] > 60) {
  150. textField.text = @"60";
  151. return NO;
  152. }
  153. else if (text.length > 2) {
  154. return NO;
  155. }
  156. return YES;
  157. }
  158. #pragma mark - UI Events
  159. - (IBAction)btnMorningTouched:(id)sender {
  160. [self.btnMorning setSelected:YES];
  161. [self.btnAfternoon setSelected:NO];
  162. }
  163. - (IBAction)btnAfternoonTouched:(id)sender {
  164. [self.btnMorning setSelected:NO];
  165. [self.btnAfternoon setSelected:YES];
  166. }
  167. - (IBAction)btnConfirmTouched:(id)sender {
  168. //validate
  169. if (![TimePickerPopupView validateCondition:_chkDays]) {
  170. return;
  171. }
  172. if ([_txtHour.text isEmptyString] || [_txtMinute.text isEmptyString]) {
  173. return;
  174. }
  175. ItemSubModel *subItem = nil;
  176. if (_timeTrigger.subItems && _timeTrigger.subItems.count) {
  177. subItem = _timeTrigger.subItems[0];
  178. }
  179. else {
  180. subItem = [[ItemSubModel alloc] init];
  181. subItem.hour = _txtHour.text;
  182. subItem.minute = _txtMinute.text;
  183. subItem.dayofweek = [TimePickerPopupView daysOfWeek:_chkDays];
  184. _timeTrigger.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  185. [_timeTrigger.timers addObject:subItem];
  186. }
  187. //time
  188. // NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  189. //
  190. // subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  191. // subItem.minute = ds.minute < 10 ? [NSString stringWithFormat:@"0%zd", ds.minute] : [NSString stringWithFormat:@"%zd", ds.minute];
  192. //set Days condition
  193. // [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  194. [super btnConfirmTouched:sender];
  195. }
  196. #pragma mark - Class Methods
  197. //+ (NSString *)stringOfDay:(NSInteger)index {
  198. // NSString *day = nil;
  199. // switch (index) {
  200. // case 0:
  201. // day = NSLocalizedString(@"월", @"월");
  202. // break;
  203. // case 1:
  204. // day = NSLocalizedString(@"화", @"화");
  205. // break;
  206. // case 2:
  207. // day = NSLocalizedString(@"수", @"수");
  208. // break;
  209. // case 3:
  210. // day = NSLocalizedString(@"목", @"목");
  211. // break;
  212. // case 4:
  213. // day = NSLocalizedString(@"금", @"금");
  214. // break;
  215. // case 5:
  216. // day = NSLocalizedString(@"토", @"토");
  217. // break;
  218. // case 6:
  219. // day = NSLocalizedString(@"일", @"일");
  220. // break;
  221. // }
  222. // return day;
  223. //}
  224. + (NSString *)daysOfWeek:(NSArray *)chkDays {
  225. //dayOfWeek
  226. NSMutableString *days = [[NSMutableString alloc] init];
  227. NSInteger i = 0;
  228. for (CustomCheckBox *chk in chkDays) {
  229. if (chk.checked) {
  230. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  231. [days appendFormat:@"%@%@", prefix, WEEKDAY(i)];
  232. }
  233. i++;
  234. }
  235. return days;
  236. }
  237. + (NSString *)daysOfWeekValue:(NSArray *)chkDays {
  238. NSMutableString *days = [[NSMutableString alloc] init];
  239. for (CustomCheckBox *chk in chkDays) {
  240. if (chk.checked) {
  241. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  242. [days appendFormat:@"%@%@", prefix, chk.value];
  243. }
  244. }
  245. return days;
  246. }
  247. + (BOOL)validateCondition:(NSArray *)chkDays {
  248. //1.validate
  249. NSInteger count = 0;
  250. for (CustomCheckBox *chk in chkDays) {
  251. if (chk.checked) {
  252. count++;
  253. }
  254. }
  255. //1. validate
  256. if (count == 0) {
  257. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  258. }
  259. return count;
  260. }
  261. + (void)setDaysCondition:(NSMutableArray<ItemModel> *)conditions chkDays:(NSArray *)chkDays{
  262. [conditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
  263. if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  264. [conditions removeObject:pCondition];
  265. *stop = YES;
  266. }
  267. }];
  268. //set condition
  269. ItemModel *condition = [[ItemModel alloc] init];
  270. condition.itemName = @"해당 요일에만";
  271. condition.itemSubTypeCode = ksConditionSubTypeCodeDaysOfWeek;
  272. [conditions addObject:condition];
  273. //set subitems
  274. condition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  275. ItemSubModel *subCondition = [[ItemSubModel alloc] init];
  276. subCondition.conditionTypeCode = @"09";
  277. subCondition.cmdclsValue = [TimePickerPopupView daysOfWeekValue:chkDays];
  278. subCondition.dayofweek = [TimePickerPopupView daysOfWeek:chkDays];
  279. [condition.subItems addObject:subCondition];
  280. }
  281. @end